| 1 | /** |
| 2 | * MojoShader; generate shader programs from bytecode of compiled |
| 3 | * Direct3D shaders. |
| 4 | * |
| 5 | * Please see the file LICENSE.txt in the source's root directory. |
| 6 | * |
| 7 | * This file written by Ryan C. Gordon. |
| 8 | */ |
| 9 | |
| 10 | #define __MOJOSHADER_INTERNAL__ 1 |
| 11 | #include "mojoshader_internal.h" |
| 12 | |
| 13 | #ifdef MOJOSHADER_EFFECT_SUPPORT |
| 14 | |
| 15 | #ifndef MOJOSHADER_USE_SDL_STDLIB |
| 16 | #include <math.h> |
| 17 | #endif /* MOJOSHADER_USE_SDL_STDLIB */ |
| 18 | |
| 19 | void MOJOSHADER_runPreshader(const MOJOSHADER_preshader *preshader, |
| 20 | float *outregs) |
| 21 | { |
| 22 | const float *inregs = preshader->registers; |
| 23 | |
| 24 | // this is fairly straightforward, as there aren't any branching |
| 25 | // opcodes in the preshader instruction set (at the moment, at least). |
| 26 | const int scalarstart = (int) MOJOSHADER_PRESHADEROP_SCALAR_OPS; |
| 27 | |
| 28 | double *temps = NULL; |
| 29 | if (preshader->temp_count > 0) |
| 30 | { |
| 31 | temps = (double *) alloca(sizeof (double) * preshader->temp_count); |
| 32 | memset(temps, '\0', sizeof (double) * preshader->temp_count); |
| 33 | } // if |
| 34 | |
| 35 | double dst[4] = { 0, 0, 0, 0 }; |
| 36 | double src[3][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }; |
| 37 | const double *src0 = &src[0][0]; |
| 38 | const double *src1 = &src[1][0]; |
| 39 | const double *src2 = &src[2][0]; |
| 40 | |
| 41 | MOJOSHADER_preshaderInstruction *inst = preshader->instructions; |
| 42 | int instit; |
| 43 | |
| 44 | #if 0 // FIXME: Do we need to do this or is the compiler smart enough? |
| 45 | // Clear preshader output registers first! |
| 46 | for (instit = 0; instit < preshader->instruction_count; instit++, inst++) |
| 47 | { |
| 48 | const MOJOSHADER_preshaderOperand *operand = &inst->operands[inst->operand_count - 1]; |
| 49 | if (operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT) |
| 50 | memset(&outregs[operand->index], '\0', sizeof(float) * 4); |
| 51 | } // for |
| 52 | inst = preshader->instructions; |
| 53 | #endif |
| 54 | |
| 55 | for (instit = 0; instit < preshader->instruction_count; instit++, inst++) |
| 56 | { |
| 57 | const MOJOSHADER_preshaderOperand *operand = inst->operands; |
| 58 | const int elems = inst->element_count; |
| 59 | const int elemsbytes = sizeof (double) * elems; |
| 60 | const int isscalarop = (inst->opcode >= scalarstart); |
| 61 | |
| 62 | assert(elems >= 0); |
| 63 | assert(elems <= 4); |
| 64 | |
| 65 | // load up our operands... |
| 66 | int opiter, elemiter; |
| 67 | for (opiter = 0; opiter < inst->operand_count-1; opiter++, operand++) |
| 68 | { |
| 69 | const int isscalar = ((isscalarop) && (opiter == 0)); |
| 70 | const unsigned int index = operand->index; |
| 71 | switch (operand->type) |
| 72 | { |
| 73 | case MOJOSHADER_PRESHADEROPERAND_LITERAL: |
| 74 | { |
| 75 | if (!isscalar) |
| 76 | { |
| 77 | assert((index + elems) <= preshader->literal_count); |
| 78 | memcpy(&src[opiter][0], &preshader->literals[index], elemsbytes); |
| 79 | } // if |
| 80 | else |
| 81 | { |
| 82 | for (elemiter = 0; elemiter < elems; elemiter++) |
| 83 | src[opiter][elemiter] = preshader->literals[index]; |
| 84 | } // else |
| 85 | break; |
| 86 | } // case |
| 87 | |
| 88 | case MOJOSHADER_PRESHADEROPERAND_INPUT: |
| 89 | if (operand->array_register_count > 0) |
| 90 | { |
| 91 | int i; |
| 92 | const int *regsi = (const int *) inregs; |
| 93 | int arrIndex = regsi[((index >> 4) * 4) + ((index >> 2) & 3)]; |
| 94 | for (i = 0; i < operand->array_register_count; i++) |
| 95 | { |
| 96 | arrIndex = regsi[operand->array_registers[i] + arrIndex]; |
| 97 | } |
| 98 | src[opiter][0] = arrIndex; |
| 99 | } // if |
| 100 | else if (isscalar) |
| 101 | src[opiter][0] = inregs[index]; |
| 102 | else |
| 103 | { |
| 104 | int cpy; |
| 105 | for (cpy = 0; cpy < elems; cpy++) |
| 106 | src[opiter][cpy] = inregs[index+cpy]; |
| 107 | } // else |
| 108 | break; |
| 109 | |
| 110 | case MOJOSHADER_PRESHADEROPERAND_OUTPUT: |
| 111 | if (isscalar) |
| 112 | src[opiter][0] = outregs[index]; |
| 113 | else |
| 114 | { |
| 115 | int cpy; |
| 116 | for (cpy = 0; cpy < elems; cpy++) |
| 117 | src[opiter][cpy] = outregs[index+cpy]; |
| 118 | } // else |
| 119 | break; |
| 120 | |
| 121 | case MOJOSHADER_PRESHADEROPERAND_TEMP: |
| 122 | if (temps != NULL) |
| 123 | { |
| 124 | if (isscalar) |
| 125 | src[opiter][0] = temps[index]; |
| 126 | else |
| 127 | memcpy(src[opiter], temps + index, elemsbytes); |
| 128 | } // if |
| 129 | break; |
| 130 | |
| 131 | default: |
| 132 | assert(0 && "unexpected preshader operand type."); |
| 133 | return; |
| 134 | } // switch |
| 135 | } // for |
| 136 | |
| 137 | // run the actual instruction, store result to dst. |
| 138 | int i; |
| 139 | switch (inst->opcode) |
| 140 | { |
| 141 | #define OPCODE_CASE(op, val) \ |
| 142 | case MOJOSHADER_PRESHADEROP_##op: \ |
| 143 | for (i = 0; i < elems; i++) { dst[i] = val; } \ |
| 144 | break; |
| 145 | |
| 146 | //OPCODE_CASE(NOP, 0.0) // not a real instruction. |
| 147 | OPCODE_CASE(MOV, src0[i]) |
| 148 | OPCODE_CASE(NEG, -src0[i]) |
| 149 | OPCODE_CASE(RCP, 1.0 / src0[i]) |
| 150 | OPCODE_CASE(FRC, src0[i] - floor(src0[i])) |
| 151 | OPCODE_CASE(EXP, exp(src0[i])) |
| 152 | OPCODE_CASE(LOG, log(src0[i])) |
| 153 | OPCODE_CASE(RSQ, 1.0 / sqrt(src0[i])) |
| 154 | OPCODE_CASE(SIN, sin(src0[i])) |
| 155 | OPCODE_CASE(COS, cos(src0[i])) |
| 156 | OPCODE_CASE(ASIN, asin(src0[i])) |
| 157 | OPCODE_CASE(ACOS, acos(src0[i])) |
| 158 | OPCODE_CASE(ATAN, atan(src0[i])) |
| 159 | OPCODE_CASE(MIN, (src0[i] < src1[i]) ? src0[i] : src1[i]) |
| 160 | OPCODE_CASE(MAX, (src0[i] > src1[i]) ? src0[i] : src1[i]) |
| 161 | OPCODE_CASE(LT, (src0[i] < src1[i]) ? 1.0 : 0.0) |
| 162 | OPCODE_CASE(GE, (src0[i] >= src1[i]) ? 1.0 : 0.0) |
| 163 | OPCODE_CASE(ADD, src0[i] + src1[i]) |
| 164 | OPCODE_CASE(MUL, src0[i] * src1[i]) |
| 165 | OPCODE_CASE(ATAN2, atan2(src0[i], src1[i])) |
| 166 | OPCODE_CASE(DIV, src0[i] / src1[i]) |
| 167 | OPCODE_CASE(CMP, (src0[i] >= 0.0) ? src1[i] : src2[i]) |
| 168 | //OPCODE_CASE(NOISE, ???) // !!! FIXME: don't know what this does |
| 169 | //OPCODE_CASE(MOVC, ???) // !!! FIXME: don't know what this does |
| 170 | OPCODE_CASE(MIN_SCALAR, (src0[0] < src1[i]) ? src0[0] : src1[i]) |
| 171 | OPCODE_CASE(MAX_SCALAR, (src0[0] > src1[i]) ? src0[0] : src1[i]) |
| 172 | OPCODE_CASE(LT_SCALAR, (src0[0] < src1[i]) ? 1.0 : 0.0) |
| 173 | OPCODE_CASE(GE_SCALAR, (src0[0] >= src1[i]) ? 1.0 : 0.0) |
| 174 | OPCODE_CASE(ADD_SCALAR, src0[0] + src1[i]) |
| 175 | OPCODE_CASE(MUL_SCALAR, src0[0] * src1[i]) |
| 176 | OPCODE_CASE(ATAN2_SCALAR, atan2(src0[0], src1[i])) |
| 177 | OPCODE_CASE(DIV_SCALAR, src0[0] / src1[i]) |
| 178 | //OPCODE_CASE(DOT_SCALAR) // !!! FIXME: isn't this just a MUL? |
| 179 | //OPCODE_CASE(NOISE_SCALAR, ???) // !!! FIXME: ? |
| 180 | #undef OPCODE_CASE |
| 181 | |
| 182 | case MOJOSHADER_PRESHADEROP_DOT: |
| 183 | { |
| 184 | double final = 0.0; |
| 185 | for (i = 0; i < elems; i++) |
| 186 | final += src0[i] * src1[i]; |
| 187 | for (i = 0; i < elems; i++) |
| 188 | dst[i] = final; // !!! FIXME: is this right? |
| 189 | break; |
| 190 | } // case |
| 191 | |
| 192 | default: |
| 193 | assert(0 && "Unhandled preshader opcode!"); |
| 194 | break; |
| 195 | } // switch |
| 196 | |
| 197 | // Figure out where dst wants to be stored. |
| 198 | if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP) |
| 199 | { |
| 200 | assert(preshader->temp_count >= |
| 201 | operand->index + (elemsbytes / sizeof (double))); |
| 202 | memcpy(temps + operand->index, dst, elemsbytes); |
| 203 | } // if |
| 204 | else |
| 205 | { |
| 206 | assert(operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT); |
| 207 | for (i = 0; i < elems; i++) |
| 208 | outregs[operand->index + i] = (float) dst[i]; |
| 209 | } // else |
| 210 | } // for |
| 211 | } // MOJOSHADER_runPreshader |
| 212 | |
| 213 | static MOJOSHADER_effect MOJOSHADER_out_of_mem_effect = { |
| 214 | 1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 215 | }; |
| 216 | static MOJOSHADER_error MOJOSHADER_need_a_backend_error = { |
| 217 | "Need a MOJOSHADER_effectShaderContext", NULL, MOJOSHADER_POSITION_NONE |
| 218 | }; |
| 219 | static MOJOSHADER_effect MOJOSHADER_need_a_backend_effect = { |
| 220 | 1, &MOJOSHADER_need_a_backend_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 221 | }; |
| 222 | static MOJOSHADER_error MOJOSHADER_unexpected_eof_error = { |
| 223 | "Unexpected EOF", NULL, MOJOSHADER_POSITION_NONE |
| 224 | }; |
| 225 | static MOJOSHADER_effect MOJOSHADER_unexpected_eof_effect = { |
| 226 | 1, &MOJOSHADER_unexpected_eof_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 227 | }; |
| 228 | static MOJOSHADER_error MOJOSHADER_not_an_effect_error = { |
| 229 | "Not an Effects Framework binary", NULL, MOJOSHADER_POSITION_NONE |
| 230 | }; |
| 231 | static MOJOSHADER_effect MOJOSHADER_not_an_effect_effect = { |
| 232 | 1, &MOJOSHADER_not_an_effect_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 233 | }; |
| 234 | |
| 235 | static void push_errors(ErrorList *list, MOJOSHADER_error *errors, int len) |
| 236 | { |
| 237 | int i; |
| 238 | for (i = 0; i < len; i += 1) |
| 239 | errorlist_add(list, errors[i].filename, errors[i].error_position, errors[i].error); |
| 240 | } // push_errors |
| 241 | |
| 242 | static void read_version_token(const uint8 **_ptr, uint32 *_len, uint16 *magic, uint8 *version_major, uint8 *version_minor) |
| 243 | { |
| 244 | const uint32 *ptr = (const uint32 *) *_ptr; |
| 245 | const uint32 token = SWAP32(*ptr); |
| 246 | *_ptr += sizeof (token); |
| 247 | *_len -= sizeof (token); |
| 248 | |
| 249 | *magic = ((token >> 16) & 0xFFFF); |
| 250 | *version_major = (uint8) ((token >> 8) & 0xFF); |
| 251 | *version_minor = (uint8) (token & 0xFF); |
| 252 | } // read_version_token |
| 253 | |
| 254 | static uint32 readui32(const uint8 **_ptr, uint32 *_len) |
| 255 | { |
| 256 | uint32 retval = 0; |
| 257 | if (*_len < sizeof (retval)) |
| 258 | *_len = 0; |
| 259 | else |
| 260 | { |
| 261 | const uint32 *ptr = (const uint32 *) *_ptr; |
| 262 | retval = SWAP32(*ptr); |
| 263 | *_ptr += sizeof (retval); |
| 264 | *_len -= sizeof (retval); |
| 265 | } // else |
| 266 | return retval; |
| 267 | } // readui32 |
| 268 | |
| 269 | static char *readstring(const uint8 *base, |
| 270 | const uint32 offset, |
| 271 | MOJOSHADER_malloc m, |
| 272 | void *d) |
| 273 | { |
| 274 | // !!! FIXME: sanity checks! |
| 275 | // !!! FIXME: verify this doesn't go past EOF looking for a null. |
| 276 | uint8 *pair = (uint8 *) (base + offset); |
| 277 | const uint32 *lenptr = (const uint32 *) pair; |
| 278 | char *strptr = (char *) (pair + sizeof(uint32)); |
| 279 | |
| 280 | const uint32 len = SWAP32(*lenptr); |
| 281 | char *result = NULL; |
| 282 | |
| 283 | if (len == 0) return NULL; /* No length? No string. */ |
| 284 | |
| 285 | result = (char *) m(len, d); |
| 286 | memcpy(result, strptr, len); |
| 287 | return result; |
| 288 | } // readstring |
| 289 | |
| 290 | static int findparameter(const MOJOSHADER_effectParam *params, |
| 291 | const uint32 param_count, |
| 292 | const char *name) |
| 293 | { |
| 294 | int i; |
| 295 | for (i = 0; i < param_count; i++) |
| 296 | if (strcmp(name, params[i].value.name) == 0) |
| 297 | return i; |
| 298 | assert(0 && "Parameter not found!"); |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | static void readvalue(const uint8 *base, |
| 303 | const uint32 typeoffset, |
| 304 | const uint32 valoffset, |
| 305 | MOJOSHADER_effectValue *value, |
| 306 | MOJOSHADER_effectObject *objects, |
| 307 | MOJOSHADER_malloc m, |
| 308 | void *d) |
| 309 | { |
| 310 | int i, j, k; |
| 311 | const uint8 *typeptr = base + typeoffset; |
| 312 | const uint8 *valptr = base + valoffset; |
| 313 | unsigned int typelen = 9999999; // !!! FIXME |
| 314 | const uint32 type = readui32(&typeptr, &typelen); |
| 315 | const uint32 valclass = readui32(&typeptr, &typelen); |
| 316 | const uint32 name = readui32(&typeptr, &typelen); |
| 317 | const uint32 semantic = readui32(&typeptr, &typelen); |
| 318 | const uint32 numelements = readui32(&typeptr, &typelen); |
| 319 | |
| 320 | value->type.parameter_type = (MOJOSHADER_symbolType) type; |
| 321 | value->type.parameter_class = (MOJOSHADER_symbolClass) valclass; |
| 322 | value->name = readstring(base, name, m, d); |
| 323 | value->semantic = readstring(base, semantic, m, d); |
| 324 | value->type.elements = numelements; |
| 325 | |
| 326 | /* Class sanity check */ |
| 327 | assert(valclass >= MOJOSHADER_SYMCLASS_SCALAR && valclass <= MOJOSHADER_SYMCLASS_STRUCT); |
| 328 | |
| 329 | if (valclass == MOJOSHADER_SYMCLASS_SCALAR |
| 330 | || valclass == MOJOSHADER_SYMCLASS_VECTOR |
| 331 | || valclass == MOJOSHADER_SYMCLASS_MATRIX_ROWS |
| 332 | || valclass == MOJOSHADER_SYMCLASS_MATRIX_COLUMNS) |
| 333 | { |
| 334 | /* These classes only ever contain scalar values */ |
| 335 | assert(type >= MOJOSHADER_SYMTYPE_BOOL && type <= MOJOSHADER_SYMTYPE_FLOAT); |
| 336 | |
| 337 | const uint32 columncount = readui32(&typeptr, &typelen); |
| 338 | const uint32 rowcount = readui32(&typeptr, &typelen); |
| 339 | |
| 340 | value->type.columns = columncount; |
| 341 | value->type.rows = rowcount; |
| 342 | |
| 343 | uint32 siz = 4 * rowcount; |
| 344 | if (numelements > 0) |
| 345 | siz *= numelements; |
| 346 | value->value_count = siz; |
| 347 | siz *= 4; |
| 348 | value->values = m(siz, d); |
| 349 | memset(value->values, '\0', siz); |
| 350 | siz /= 16; |
| 351 | for (i = 0; i < siz; i++) |
| 352 | memcpy(value->valuesF + (i << 2), valptr + ((columncount << 2) * i), columncount << 2); |
| 353 | } // if |
| 354 | else if (valclass == MOJOSHADER_SYMCLASS_OBJECT) |
| 355 | { |
| 356 | /* This class contains either samplers or "objects" */ |
| 357 | assert(type >= MOJOSHADER_SYMTYPE_STRING && type <= MOJOSHADER_SYMTYPE_VERTEXSHADER); |
| 358 | |
| 359 | if (type == MOJOSHADER_SYMTYPE_SAMPLER |
| 360 | || type == MOJOSHADER_SYMTYPE_SAMPLER1D |
| 361 | || type == MOJOSHADER_SYMTYPE_SAMPLER2D |
| 362 | || type == MOJOSHADER_SYMTYPE_SAMPLER3D |
| 363 | || type == MOJOSHADER_SYMTYPE_SAMPLERCUBE) |
| 364 | { |
| 365 | unsigned int vallen = 9999999; // !!! FIXME |
| 366 | const uint32 numstates = readui32(&valptr, &vallen); |
| 367 | |
| 368 | value->value_count = numstates; |
| 369 | |
| 370 | const uint32 siz = sizeof(MOJOSHADER_effectSamplerState) * numstates; |
| 371 | value->values = m(siz, d); |
| 372 | memset(value->values, '\0', siz); |
| 373 | |
| 374 | for (i = 0; i < numstates; i++) |
| 375 | { |
| 376 | MOJOSHADER_effectSamplerState *state = &value->valuesSS[i]; |
| 377 | const uint32 stype = readui32(&valptr, &vallen) & ~0xA0; |
| 378 | /*const uint32 FIXME =*/ readui32(&valptr, &vallen); |
| 379 | const uint32 statetypeoffset = readui32(&valptr, &vallen); |
| 380 | const uint32 statevaloffset = readui32(&valptr, &vallen); |
| 381 | |
| 382 | state->type = (MOJOSHADER_samplerStateType) stype; |
| 383 | readvalue(base, statetypeoffset, statevaloffset, |
| 384 | &state->value, objects, |
| 385 | m, d); |
| 386 | if (stype == MOJOSHADER_SAMP_TEXTURE) |
| 387 | objects[state->value.valuesI[0]].type = (MOJOSHADER_symbolType) type; |
| 388 | } // for |
| 389 | } // if |
| 390 | else |
| 391 | { |
| 392 | uint32 numobjects = 1; |
| 393 | if (numelements > 0) |
| 394 | numobjects = numelements; |
| 395 | |
| 396 | value->value_count = numobjects; |
| 397 | |
| 398 | const uint32 siz = 4 * numobjects; |
| 399 | value->values = m(siz, d); |
| 400 | memcpy(value->values, valptr, siz); |
| 401 | #if MOJOSHADER_BIG_ENDIAN |
| 402 | int valI; |
| 403 | for (valI=0;valI < (value->value_count);valI++) { |
| 404 | value->valuesI[valI] = SWAP32(value->valuesI[valI]); |
| 405 | } |
| 406 | #endif |
| 407 | for (i = 0; i < value->value_count; i++) |
| 408 | objects[value->valuesI[i]].type = (MOJOSHADER_symbolType) type; |
| 409 | } // else |
| 410 | } // else if |
| 411 | else if (valclass == MOJOSHADER_SYMCLASS_STRUCT) |
| 412 | { |
| 413 | uint32 siz; |
| 414 | |
| 415 | value->type.member_count = readui32(&typeptr, &typelen); |
| 416 | siz = value->type.member_count * sizeof (MOJOSHADER_symbolStructMember); |
| 417 | value->type.members = (MOJOSHADER_symbolStructMember *) m(siz, d); |
| 418 | |
| 419 | uint32 structsize = 0; |
| 420 | for (i = 0; i < value->type.member_count; i++) |
| 421 | { |
| 422 | MOJOSHADER_symbolStructMember *mem = &value->type.members[i]; |
| 423 | |
| 424 | mem->info.parameter_type = (MOJOSHADER_symbolType) readui32(&typeptr, &typelen); |
| 425 | mem->info.parameter_class = (MOJOSHADER_symbolClass) readui32(&typeptr, &typelen); |
| 426 | |
| 427 | const uint32 memname = readui32(&typeptr, &typelen); |
| 428 | /*const uint32 memsemantic =*/ readui32(&typeptr, &typelen); |
| 429 | mem->name = readstring(base, memname, m, d); |
| 430 | |
| 431 | mem->info.elements = readui32(&typeptr, &typelen); |
| 432 | mem->info.columns = readui32(&typeptr, &typelen); |
| 433 | mem->info.rows = readui32(&typeptr, &typelen); |
| 434 | |
| 435 | // !!! FIXME: Nested structs! -flibit |
| 436 | assert(mem->info.parameter_class >= MOJOSHADER_SYMCLASS_SCALAR |
| 437 | && mem->info.parameter_class <= MOJOSHADER_SYMCLASS_MATRIX_COLUMNS); |
| 438 | assert(mem->info.parameter_type >= MOJOSHADER_SYMTYPE_BOOL |
| 439 | && mem->info.parameter_type <= MOJOSHADER_SYMTYPE_FLOAT); |
| 440 | mem->info.member_count = 0; |
| 441 | mem->info.members = NULL; |
| 442 | |
| 443 | uint32 memsize = 4 * mem->info.rows; |
| 444 | if (mem->info.elements > 0) |
| 445 | memsize *= mem->info.elements; |
| 446 | structsize += memsize; |
| 447 | } // for |
| 448 | |
| 449 | value->type.columns = structsize; |
| 450 | value->type.rows = 1; |
| 451 | value->value_count = structsize; |
| 452 | if (numelements > 0) |
| 453 | value->value_count *= numelements; |
| 454 | |
| 455 | siz = value->value_count * 4; |
| 456 | value->values = m(siz, d); |
| 457 | memset(value->values, '\0', siz); |
| 458 | int dst_offset = 0, src_offset = 0; |
| 459 | i = 0; |
| 460 | do |
| 461 | { |
| 462 | for (j = 0; j < value->type.member_count; j++) |
| 463 | { |
| 464 | siz = value->type.members[j].info.rows * value->type.members[j].info.elements; |
| 465 | for (k = 0; k < siz; k++) |
| 466 | { |
| 467 | memcpy(value->valuesF + dst_offset, |
| 468 | typeptr + src_offset, /* Yes, typeptr. -flibit */ |
| 469 | value->type.members[j].info.columns << 2); |
| 470 | dst_offset += 4; |
| 471 | src_offset += value->type.members[j].info.columns << 2; |
| 472 | } // for |
| 473 | } |
| 474 | } while (++i < numelements); |
| 475 | } // else if |
| 476 | } // readvalue |
| 477 | |
| 478 | static void readannotations(const uint32 numannos, |
| 479 | const uint8 *base, |
| 480 | const uint8 **ptr, |
| 481 | uint32 *len, |
| 482 | MOJOSHADER_effectAnnotation **annotations, |
| 483 | MOJOSHADER_effectObject *objects, |
| 484 | MOJOSHADER_malloc m, |
| 485 | void *d) |
| 486 | { |
| 487 | int i; |
| 488 | if (numannos == 0) return; |
| 489 | |
| 490 | const uint32 siz = sizeof(MOJOSHADER_effectAnnotation) * numannos; |
| 491 | *annotations = (MOJOSHADER_effectAnnotation *) m(siz, d); |
| 492 | memset(*annotations, '\0', siz); |
| 493 | |
| 494 | for (i = 0; i < numannos; i++) |
| 495 | { |
| 496 | MOJOSHADER_effectAnnotation *anno = &(*annotations)[i]; |
| 497 | |
| 498 | const uint32 typeoffset = readui32(ptr, len); |
| 499 | const uint32 valoffset = readui32(ptr, len); |
| 500 | |
| 501 | readvalue(base, typeoffset, valoffset, |
| 502 | anno, objects, |
| 503 | m, d); |
| 504 | } // for |
| 505 | } // readannotation |
| 506 | |
| 507 | static void readparameters(const uint32 numparams, |
| 508 | const uint8 *base, |
| 509 | const uint8 **ptr, |
| 510 | uint32 *len, |
| 511 | MOJOSHADER_effectParam **params, |
| 512 | MOJOSHADER_effectObject *objects, |
| 513 | MOJOSHADER_malloc m, |
| 514 | void *d) |
| 515 | { |
| 516 | int i; |
| 517 | if (numparams == 0) return; |
| 518 | |
| 519 | uint32 siz = sizeof(MOJOSHADER_effectParam) * numparams; |
| 520 | *params = (MOJOSHADER_effectParam *) m(siz, d); |
| 521 | memset(*params, '\0', siz); |
| 522 | |
| 523 | for (i = 0; i < numparams; i++) |
| 524 | { |
| 525 | MOJOSHADER_effectParam *param = &(*params)[i]; |
| 526 | |
| 527 | const uint32 typeoffset = readui32(ptr, len); |
| 528 | const uint32 valoffset = readui32(ptr, len); |
| 529 | /*const uint32 flags =*/ readui32(ptr, len); |
| 530 | const uint32 numannos = readui32(ptr, len); |
| 531 | |
| 532 | param->annotation_count = numannos; |
| 533 | readannotations(numannos, base, ptr, len, |
| 534 | ¶m->annotations, objects, |
| 535 | m, d); |
| 536 | |
| 537 | readvalue(base, typeoffset, valoffset, |
| 538 | ¶m->value, objects, |
| 539 | m, d); |
| 540 | } // for |
| 541 | } // readparameters |
| 542 | |
| 543 | static void readstates(const uint32 numstates, |
| 544 | const uint8 *base, |
| 545 | const uint8 **ptr, |
| 546 | uint32 *len, |
| 547 | MOJOSHADER_effectState **states, |
| 548 | MOJOSHADER_effectObject *objects, |
| 549 | MOJOSHADER_malloc m, |
| 550 | void *d) |
| 551 | { |
| 552 | int i; |
| 553 | if (numstates == 0) return; |
| 554 | |
| 555 | const uint32 siz = sizeof (MOJOSHADER_effectState) * numstates; |
| 556 | *states = (MOJOSHADER_effectState *) m(siz, d); |
| 557 | memset(*states, '\0', siz); |
| 558 | |
| 559 | for (i = 0; i < numstates; i++) |
| 560 | { |
| 561 | MOJOSHADER_effectState *state = &(*states)[i]; |
| 562 | |
| 563 | const uint32 type = readui32(ptr, len); |
| 564 | /*const uint32 FIXME =*/ readui32(ptr, len); |
| 565 | const uint32 typeoffset = readui32(ptr, len); |
| 566 | const uint32 valoffset = readui32(ptr, len); |
| 567 | |
| 568 | state->type = (MOJOSHADER_renderStateType) type; |
| 569 | readvalue(base, typeoffset, valoffset, |
| 570 | &state->value, objects, |
| 571 | m, d); |
| 572 | } // for |
| 573 | } // readstates |
| 574 | |
| 575 | static void readpasses(const uint32 numpasses, |
| 576 | const uint8 *base, |
| 577 | const uint8 **ptr, |
| 578 | uint32 *len, |
| 579 | MOJOSHADER_effectPass **passes, |
| 580 | MOJOSHADER_effectObject *objects, |
| 581 | MOJOSHADER_malloc m, |
| 582 | void *d) |
| 583 | { |
| 584 | int i; |
| 585 | if (numpasses == 0) return; |
| 586 | |
| 587 | const uint32 siz = sizeof (MOJOSHADER_effectPass) * numpasses; |
| 588 | *passes = (MOJOSHADER_effectPass *) m(siz, d); |
| 589 | memset(*passes, '\0', siz); |
| 590 | |
| 591 | for (i = 0; i < numpasses; i++) |
| 592 | { |
| 593 | MOJOSHADER_effectPass *pass = &(*passes)[i]; |
| 594 | |
| 595 | const uint32 passnameoffset = readui32(ptr, len); |
| 596 | const uint32 numannos = readui32(ptr, len); |
| 597 | const uint32 numstates = readui32(ptr, len); |
| 598 | |
| 599 | pass->name = readstring(base, passnameoffset, m, d); |
| 600 | |
| 601 | pass->annotation_count = numannos; |
| 602 | readannotations(numannos, base, ptr, len, |
| 603 | &pass->annotations, objects, |
| 604 | m, d); |
| 605 | |
| 606 | pass->state_count = numstates; |
| 607 | readstates(numstates, base, ptr, len, |
| 608 | &pass->states, objects, |
| 609 | m, d); |
| 610 | } // for |
| 611 | } // readpasses |
| 612 | |
| 613 | static void readtechniques(const uint32 numtechniques, |
| 614 | const uint8 *base, |
| 615 | const uint8 **ptr, |
| 616 | uint32 *len, |
| 617 | MOJOSHADER_effectTechnique **techniques, |
| 618 | MOJOSHADER_effectObject *objects, |
| 619 | MOJOSHADER_malloc m, |
| 620 | void *d) |
| 621 | { |
| 622 | int i; |
| 623 | if (numtechniques == 0) return; |
| 624 | |
| 625 | const uint32 siz = sizeof (MOJOSHADER_effectTechnique) * numtechniques; |
| 626 | *techniques = (MOJOSHADER_effectTechnique *) m(siz, d); |
| 627 | memset(*techniques, '\0', siz); |
| 628 | |
| 629 | for (i = 0; i < numtechniques; i++) |
| 630 | { |
| 631 | MOJOSHADER_effectTechnique *technique = &(*techniques)[i]; |
| 632 | |
| 633 | const uint32 nameoffset = readui32(ptr, len); |
| 634 | const uint32 numannos = readui32(ptr, len); |
| 635 | const uint32 numpasses = readui32(ptr, len); |
| 636 | |
| 637 | technique->name = readstring(base, nameoffset, m, d); |
| 638 | |
| 639 | technique->annotation_count = numannos; |
| 640 | readannotations(numannos, base, ptr, len, |
| 641 | &technique->annotations, objects, |
| 642 | m, d); |
| 643 | |
| 644 | technique->pass_count = numpasses; |
| 645 | readpasses(numpasses, base, ptr, len, |
| 646 | &technique->passes, objects, |
| 647 | m, d); |
| 648 | } // for |
| 649 | } // readtechniques |
| 650 | |
| 651 | static void readsmallobjects(const uint32 numsmallobjects, |
| 652 | const uint8 **ptr, |
| 653 | uint32 *len, |
| 654 | MOJOSHADER_effect *effect, |
| 655 | const MOJOSHADER_swizzle *swiz, |
| 656 | const unsigned int swizcount, |
| 657 | const MOJOSHADER_samplerMap *smap, |
| 658 | const unsigned int smapcount, |
| 659 | ErrorList *errors) |
| 660 | { |
| 661 | int i, j; |
| 662 | MOJOSHADER_parseData *pd; |
| 663 | MOJOSHADER_malloc m = effect->ctx.m; |
| 664 | void *d = effect->ctx.malloc_data; |
| 665 | |
| 666 | if (numsmallobjects == 0) return; |
| 667 | |
| 668 | for (i = 1; i < numsmallobjects + 1; i++) |
| 669 | { |
| 670 | const uint32 index = readui32(ptr, len); |
| 671 | const uint32 length = readui32(ptr, len); |
| 672 | |
| 673 | MOJOSHADER_effectObject *object = &effect->objects[index]; |
| 674 | if (object->type == MOJOSHADER_SYMTYPE_STRING) |
| 675 | { |
| 676 | if (length > 0) |
| 677 | { |
| 678 | char *str = (char *) m(length, d); |
| 679 | memcpy(str, *ptr, length); |
| 680 | object->string.string = str; |
| 681 | } // if |
| 682 | } // if |
| 683 | else if (object->type == MOJOSHADER_SYMTYPE_TEXTURE |
| 684 | || object->type == MOJOSHADER_SYMTYPE_TEXTURE1D |
| 685 | || object->type == MOJOSHADER_SYMTYPE_TEXTURE2D |
| 686 | || object->type == MOJOSHADER_SYMTYPE_TEXTURE3D |
| 687 | || object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE |
| 688 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER |
| 689 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D |
| 690 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D |
| 691 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D |
| 692 | || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE) |
| 693 | { |
| 694 | if (length > 0) |
| 695 | { |
| 696 | char *str = (char *) m(length, d); |
| 697 | memcpy(str, *ptr, length); |
| 698 | object->mapping.name = str; |
| 699 | } // if |
| 700 | } // else if |
| 701 | else if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER |
| 702 | || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER) |
| 703 | { |
| 704 | char mainfn[32]; |
| 705 | snprintf(mainfn, sizeof(mainfn), "ShaderFunction%u", (unsigned int) index); |
| 706 | object->shader.technique = -1; |
| 707 | object->shader.pass = -1; |
| 708 | object->shader.shader = effect->ctx.compileShader(effect->ctx.shaderContext, |
| 709 | mainfn, *ptr, length, |
| 710 | swiz, swizcount, |
| 711 | smap, smapcount); |
| 712 | if (object->shader.shader == NULL) |
| 713 | { |
| 714 | // Bail ASAP, so we can get the error to the application |
| 715 | errorlist_add(errors, NULL, 0, effect->ctx.getError(effect->ctx.shaderContext)); |
| 716 | return; |
| 717 | } // if |
| 718 | pd = effect->ctx.getParseData(object->shader.shader); |
| 719 | if (pd->error_count > 0) |
| 720 | { |
| 721 | // Bail ASAP, so we can get the error to the application |
| 722 | push_errors(errors, pd->errors, pd->error_count); |
| 723 | return; |
| 724 | } // if |
| 725 | |
| 726 | for (j = 0; j < pd->symbol_count; j++) |
| 727 | if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER) |
| 728 | object->shader.sampler_count++; |
| 729 | object->shader.param_count = pd->symbol_count; |
| 730 | object->shader.params = (uint32 *) m(object->shader.param_count * sizeof (uint32), d); |
| 731 | object->shader.samplers = (MOJOSHADER_samplerStateRegister *) m(object->shader.sampler_count * sizeof (MOJOSHADER_samplerStateRegister), d); |
| 732 | uint32 curSampler = 0; |
| 733 | for (j = 0; j < pd->symbol_count; j++) |
| 734 | { |
| 735 | int par = findparameter(effect->params, |
| 736 | effect->param_count, |
| 737 | pd->symbols[j].name); |
| 738 | object->shader.params[j] = par; |
| 739 | if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER) |
| 740 | { |
| 741 | object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name; |
| 742 | object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index; |
| 743 | object->shader.samplers[curSampler].sampler_state_count = effect->params[par].value.value_count; |
| 744 | object->shader.samplers[curSampler].sampler_states = effect->params[par].value.valuesSS; |
| 745 | curSampler++; |
| 746 | } // if |
| 747 | } // for |
| 748 | if (pd->preshader) |
| 749 | { |
| 750 | object->shader.preshader_param_count = pd->preshader->symbol_count; |
| 751 | object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d); |
| 752 | for (j = 0; j < pd->preshader->symbol_count; j++) |
| 753 | { |
| 754 | object->shader.preshader_params[j] = findparameter(effect->params, |
| 755 | effect->param_count, |
| 756 | pd->preshader->symbols[j].name); |
| 757 | } // for |
| 758 | } // if |
| 759 | } // else if |
| 760 | else |
| 761 | { |
| 762 | assert(0 && "Small object type unknown!"); |
| 763 | } // else |
| 764 | |
| 765 | /* Object block is always a multiple of four */ |
| 766 | const uint32 blocklen = (length + 3) - ((length - 1) % 4); |
| 767 | *ptr += blocklen; |
| 768 | *len -= blocklen; |
| 769 | } // for |
| 770 | } // readstrings |
| 771 | |
| 772 | static void readlargeobjects(const uint32 numlargeobjects, |
| 773 | const uint32 numsmallobjects, |
| 774 | const uint8 **ptr, |
| 775 | uint32 *len, |
| 776 | MOJOSHADER_effect *effect, |
| 777 | const MOJOSHADER_swizzle *swiz, |
| 778 | const unsigned int swizcount, |
| 779 | const MOJOSHADER_samplerMap *smap, |
| 780 | const unsigned int smapcount, |
| 781 | ErrorList *errors) |
| 782 | { |
| 783 | int i, j; |
| 784 | MOJOSHADER_parseData *pd; |
| 785 | MOJOSHADER_malloc m = effect->ctx.m; |
| 786 | MOJOSHADER_free f = effect->ctx.f; |
| 787 | void *d = effect->ctx.malloc_data; |
| 788 | |
| 789 | if (numlargeobjects == 0) return; |
| 790 | |
| 791 | int numobjects = numsmallobjects + numlargeobjects + 1; |
| 792 | for (i = numsmallobjects + 1; i < numobjects; i++) |
| 793 | { |
| 794 | const uint32 technique = readui32(ptr, len); |
| 795 | const uint32 index = readui32(ptr, len); |
| 796 | /*const uint32 FIXME =*/ readui32(ptr, len); |
| 797 | const uint32 state = readui32(ptr, len); |
| 798 | const uint32 type = readui32(ptr, len); |
| 799 | const uint32 length = readui32(ptr, len); |
| 800 | |
| 801 | uint32 objectIndex; |
| 802 | if (technique == -1) |
| 803 | objectIndex = effect->params[index].value.valuesSS[state].value.valuesI[0]; |
| 804 | else |
| 805 | objectIndex = effect->techniques[technique].passes[index].states[state].value.valuesI[0]; |
| 806 | |
| 807 | MOJOSHADER_effectObject *object = &effect->objects[objectIndex]; |
| 808 | if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER |
| 809 | || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER) |
| 810 | { |
| 811 | object->shader.technique = technique; |
| 812 | object->shader.pass = index; |
| 813 | |
| 814 | if (type == 2) |
| 815 | { |
| 816 | /* This is a standalone preshader! |
| 817 | * It exists solely for effect passes that do not use a single |
| 818 | * vertex/fragment shader. |
| 819 | */ |
| 820 | object->shader.is_preshader = 1; |
| 821 | const uint32 start = *((uint32 *) *ptr) + 4; |
| 822 | const char *array = readstring(*ptr, 0, m, d); |
| 823 | object->shader.param_count = 1; |
| 824 | object->shader.params = (uint32 *) m(sizeof (uint32), d); |
| 825 | object->shader.params[0] = findparameter(effect->params, |
| 826 | effect->param_count, |
| 827 | array); |
| 828 | f((void *) array, d); |
| 829 | object->shader.preshader = MOJOSHADER_parsePreshader(*ptr + start, length, |
| 830 | m, f, d); |
| 831 | // !!! FIXME: check for errors. |
| 832 | object->shader.preshader_param_count = object->shader.preshader->symbol_count; |
| 833 | object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d); |
| 834 | for (j = 0; j < object->shader.preshader->symbol_count; j++) |
| 835 | { |
| 836 | object->shader.preshader_params[j] = findparameter(effect->params, |
| 837 | effect->param_count, |
| 838 | object->shader.preshader->symbols[j].name); |
| 839 | } // for |
| 840 | } // if |
| 841 | else |
| 842 | { |
| 843 | char mainfn[32]; |
| 844 | snprintf(mainfn, sizeof (mainfn), "ShaderFunction%u", (unsigned int) objectIndex); |
| 845 | object->shader.shader = effect->ctx.compileShader(effect->ctx.shaderContext, |
| 846 | mainfn, *ptr, length, |
| 847 | swiz, swizcount, |
| 848 | smap, smapcount); |
| 849 | if (object->shader.shader == NULL) |
| 850 | { |
| 851 | // Bail ASAP, so we can get the error to the application |
| 852 | errorlist_add(errors, NULL, 0, effect->ctx.getError(effect->ctx.shaderContext)); |
| 853 | return; |
| 854 | } // if |
| 855 | pd = effect->ctx.getParseData(object->shader.shader); |
| 856 | if (pd->error_count > 0) |
| 857 | { |
| 858 | // Bail ASAP, so we can get the error to the application |
| 859 | push_errors(errors, pd->errors, pd->error_count); |
| 860 | return; |
| 861 | } // if |
| 862 | |
| 863 | for (j = 0; j < pd->symbol_count; j++) |
| 864 | if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER) |
| 865 | object->shader.sampler_count++; |
| 866 | object->shader.param_count = pd->symbol_count; |
| 867 | object->shader.params = (uint32 *) m(object->shader.param_count * sizeof (uint32), d); |
| 868 | object->shader.samplers = (MOJOSHADER_samplerStateRegister *) m(object->shader.sampler_count * sizeof (MOJOSHADER_samplerStateRegister), d); |
| 869 | uint32 curSampler = 0; |
| 870 | for (j = 0; j < pd->symbol_count; j++) |
| 871 | { |
| 872 | int par = findparameter(effect->params, |
| 873 | effect->param_count, |
| 874 | pd->symbols[j].name); |
| 875 | object->shader.params[j] = par; |
| 876 | if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER) |
| 877 | { |
| 878 | object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name; |
| 879 | object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index; |
| 880 | object->shader.samplers[curSampler].sampler_state_count = effect->params[par].value.value_count; |
| 881 | object->shader.samplers[curSampler].sampler_states = effect->params[par].value.valuesSS; |
| 882 | curSampler++; |
| 883 | } // if |
| 884 | } // for |
| 885 | if (pd->preshader) |
| 886 | { |
| 887 | object->shader.preshader_param_count = pd->preshader->symbol_count; |
| 888 | object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d); |
| 889 | for (j = 0; j < pd->preshader->symbol_count; j++) |
| 890 | { |
| 891 | object->shader.preshader_params[j] = findparameter(effect->params, |
| 892 | effect->param_count, |
| 893 | pd->preshader->symbols[j].name); |
| 894 | } // for |
| 895 | } // if |
| 896 | } |
| 897 | } // if |
| 898 | else if (object->type == MOJOSHADER_SYMTYPE_TEXTURE |
| 899 | || object->type == MOJOSHADER_SYMTYPE_TEXTURE1D |
| 900 | || object->type == MOJOSHADER_SYMTYPE_TEXTURE2D |
| 901 | || object->type == MOJOSHADER_SYMTYPE_TEXTURE3D |
| 902 | || object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE |
| 903 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER |
| 904 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D |
| 905 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D |
| 906 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D |
| 907 | || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE) |
| 908 | { |
| 909 | if (length > 0) |
| 910 | { |
| 911 | char *str = (char *) m(length, d); |
| 912 | memcpy(str, *ptr, length); |
| 913 | object->mapping.name = str; |
| 914 | } // if |
| 915 | } // else if |
| 916 | else if (object->type != MOJOSHADER_SYMTYPE_VOID) // FIXME: Why? -flibit |
| 917 | { |
| 918 | assert(0 && "Large object type unknown!"); |
| 919 | } // else |
| 920 | |
| 921 | /* Object block is always a multiple of four */ |
| 922 | const uint32 blocklen = (length + 3) - ((length - 1) % 4); |
| 923 | *ptr += blocklen; |
| 924 | *len -= blocklen; |
| 925 | } // for |
| 926 | } // readobjects |
| 927 | |
| 928 | MOJOSHADER_effect *MOJOSHADER_compileEffect(const unsigned char *buf, |
| 929 | const unsigned int _len, |
| 930 | const MOJOSHADER_swizzle *swiz, |
| 931 | const unsigned int swizcount, |
| 932 | const MOJOSHADER_samplerMap *smap, |
| 933 | const unsigned int smapcount, |
| 934 | const MOJOSHADER_effectShaderContext *ctx) |
| 935 | { |
| 936 | const uint8 *ptr = (const uint8 *) buf; |
| 937 | uint32 len = (uint32) _len; |
| 938 | ErrorList *errors; |
| 939 | MOJOSHADER_malloc m; |
| 940 | MOJOSHADER_free f; |
| 941 | void *d; |
| 942 | |
| 943 | /* Need a backend! */ |
| 944 | if (ctx == NULL) |
| 945 | return &MOJOSHADER_need_a_backend_effect; |
| 946 | |
| 947 | /* Supply both m and f, or neither */ |
| 948 | if ( ((ctx->m == NULL) && (ctx->f != NULL)) |
| 949 | || ((ctx->m != NULL) && (ctx->f == NULL)) ) |
| 950 | return &MOJOSHADER_out_of_mem_effect; |
| 951 | |
| 952 | /* Use default malloc/free if m/f were not passed */ |
| 953 | if (ctx->m == NULL) |
| 954 | m = MOJOSHADER_internal_malloc; |
| 955 | else |
| 956 | m = ctx->m; |
| 957 | if (ctx->f == NULL) |
| 958 | f = MOJOSHADER_internal_free; |
| 959 | else |
| 960 | f = ctx->f; |
| 961 | d = ctx->malloc_data; |
| 962 | |
| 963 | /* malloc base effect structure */ |
| 964 | MOJOSHADER_effect *retval = (MOJOSHADER_effect *) m(sizeof (MOJOSHADER_effect), |
| 965 | ctx->malloc_data); |
| 966 | if (retval == NULL) |
| 967 | return &MOJOSHADER_out_of_mem_effect; |
| 968 | memset(retval, '\0', sizeof (*retval)); |
| 969 | |
| 970 | /* Store ctx in effect structure */ |
| 971 | memcpy(&retval->ctx, ctx, sizeof(MOJOSHADER_effectShaderContext)); |
| 972 | retval->ctx.m = m; |
| 973 | retval->ctx.f = f; |
| 974 | |
| 975 | if (len < 8) |
| 976 | goto parseEffect_unexpectedEOF; |
| 977 | |
| 978 | /* Read in header magic, seek to initial offset */ |
| 979 | const uint8 *base = NULL; |
| 980 | uint16 magic; |
| 981 | uint8 version_major; |
| 982 | uint8 version_minor; |
| 983 | read_version_token(&ptr, &len, &magic, &version_major, &version_minor); |
| 984 | if ((magic == 0xBCF0) && (version_major == 0x0B) && (version_minor == 0xCF)) |
| 985 | { |
| 986 | /* The Effect compiler provided with XNA4 adds some extra mess at the |
| 987 | * beginning of the file. It's useless though, so just skip it. |
| 988 | * -flibit |
| 989 | */ |
| 990 | const uint32 skip = readui32(&ptr, &len) - 8; |
| 991 | ptr += skip; |
| 992 | len += skip; |
| 993 | read_version_token(&ptr, &len, &magic, &version_major, &version_minor); |
| 994 | } // if |
| 995 | if (!((magic == 0xFEFF) && (version_major == 0x09) && (version_minor == 0x01))) |
| 996 | { |
| 997 | MOJOSHADER_deleteEffect(retval); |
| 998 | return &MOJOSHADER_not_an_effect_effect; |
| 999 | } // if |
| 1000 | else |
| 1001 | { |
| 1002 | const uint32 offset = readui32(&ptr, &len); |
| 1003 | base = ptr; |
| 1004 | if (offset > len) |
| 1005 | goto parseEffect_unexpectedEOF; |
| 1006 | ptr += offset; |
| 1007 | len -= offset; |
| 1008 | } // else |
| 1009 | |
| 1010 | if (len < 16) |
| 1011 | goto parseEffect_unexpectedEOF; |
| 1012 | |
| 1013 | /* Parse structure counts */ |
| 1014 | const uint32 numparams = readui32(&ptr, &len); |
| 1015 | const uint32 numtechniques = readui32(&ptr, &len); |
| 1016 | /*const uint32 FIXME =*/ readui32(&ptr, &len); |
| 1017 | const uint32 numobjects = readui32(&ptr, &len); |
| 1018 | |
| 1019 | /* Alloc structures now, so object types can be stored */ |
| 1020 | retval->object_count = numobjects; |
| 1021 | const uint32 siz = sizeof (MOJOSHADER_effectObject) * numobjects; |
| 1022 | retval->objects = (MOJOSHADER_effectObject *) m(siz, d); |
| 1023 | if (retval->objects == NULL) |
| 1024 | goto parseEffect_outOfMemory; |
| 1025 | memset(retval->objects, '\0', siz); |
| 1026 | |
| 1027 | /* Parse effect parameters */ |
| 1028 | retval->param_count = numparams; |
| 1029 | readparameters(numparams, base, &ptr, &len, |
| 1030 | &retval->params, retval->objects, |
| 1031 | m, d); |
| 1032 | |
| 1033 | /* Parse effect techniques */ |
| 1034 | retval->technique_count = numtechniques; |
| 1035 | readtechniques(numtechniques, base, &ptr, &len, |
| 1036 | &retval->techniques, retval->objects, |
| 1037 | m, d); |
| 1038 | |
| 1039 | /* Initial effect technique/pass */ |
| 1040 | retval->current_technique = &retval->techniques[0]; |
| 1041 | retval->current_pass = -1; |
| 1042 | |
| 1043 | if (len < 8) |
| 1044 | goto parseEffect_unexpectedEOF; |
| 1045 | |
| 1046 | /* Parse object counts */ |
| 1047 | const int numsmallobjects = readui32(&ptr, &len); |
| 1048 | const int numlargeobjects = readui32(&ptr, &len); |
| 1049 | |
| 1050 | errors = errorlist_create(m, f, d); |
| 1051 | if (errors == NULL) |
| 1052 | goto parseEffect_outOfMemory; |
| 1053 | |
| 1054 | /* Parse "small" object table */ |
| 1055 | readsmallobjects(numsmallobjects, &ptr, &len, retval, |
| 1056 | swiz, swizcount, smap, smapcount, errors); |
| 1057 | if (errorlist_count(errors) == 0) |
| 1058 | { |
| 1059 | /* Parse "large" object table. */ |
| 1060 | readlargeobjects(numlargeobjects, numsmallobjects, &ptr, &len, retval, |
| 1061 | swiz, swizcount, smap, smapcount, errors); |
| 1062 | } // if |
| 1063 | |
| 1064 | retval->error_count = errorlist_count(errors); |
| 1065 | retval->errors = errorlist_flatten(errors); |
| 1066 | errorlist_destroy(errors); |
| 1067 | |
| 1068 | return retval; |
| 1069 | |
| 1070 | parseEffect_unexpectedEOF: |
| 1071 | MOJOSHADER_deleteEffect(retval); |
| 1072 | return &MOJOSHADER_unexpected_eof_effect; |
| 1073 | parseEffect_outOfMemory: |
| 1074 | MOJOSHADER_deleteEffect(retval); |
| 1075 | return &MOJOSHADER_out_of_mem_effect; |
| 1076 | } // MOJOSHADER_parseEffect |
| 1077 | |
| 1078 | |
| 1079 | void freetypeinfo(MOJOSHADER_symbolTypeInfo *typeinfo, |
| 1080 | MOJOSHADER_free f, void *d) |
| 1081 | { |
| 1082 | int i; |
| 1083 | for (i = 0; i < typeinfo->member_count; i++) |
| 1084 | { |
| 1085 | f((void *) typeinfo->members[i].name, d); |
| 1086 | freetypeinfo(&typeinfo->members[i].info, f, d); |
| 1087 | } // for |
| 1088 | f((void *) typeinfo->members, d); |
| 1089 | } // freetypeinfo |
| 1090 | |
| 1091 | |
| 1092 | void freevalue(MOJOSHADER_effectValue *value, MOJOSHADER_free f, void *d) |
| 1093 | { |
| 1094 | int i; |
| 1095 | f((void *) value->name, d); |
| 1096 | f((void *) value->semantic, d); |
| 1097 | freetypeinfo(&value->type, f, d); |
| 1098 | if (value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER |
| 1099 | || value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER1D |
| 1100 | || value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER2D |
| 1101 | || value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER3D |
| 1102 | || value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLERCUBE) |
| 1103 | for (i = 0; i < value->value_count; i++) |
| 1104 | freevalue(&value->valuesSS[i].value, f, d); |
| 1105 | f(value->values, d); |
| 1106 | } // freevalue |
| 1107 | |
| 1108 | |
| 1109 | void MOJOSHADER_deleteEffect(const MOJOSHADER_effect *_effect) |
| 1110 | { |
| 1111 | MOJOSHADER_effect *effect = (MOJOSHADER_effect *) _effect; |
| 1112 | if ((effect == NULL) || (effect == &MOJOSHADER_out_of_mem_effect)) |
| 1113 | return; // no-op. |
| 1114 | |
| 1115 | MOJOSHADER_free f = effect->ctx.f; |
| 1116 | void *d = effect->ctx.malloc_data; |
| 1117 | int i, j, k; |
| 1118 | |
| 1119 | /* Free errors */ |
| 1120 | for (i = 0; i < effect->error_count; i++) |
| 1121 | { |
| 1122 | f((void *) effect->errors[i].error, d); |
| 1123 | f((void *) effect->errors[i].filename, d); |
| 1124 | } // for |
| 1125 | f((void *) effect->errors, d); |
| 1126 | |
| 1127 | /* Free parameters, including annotations */ |
| 1128 | for (i = 0; i < effect->param_count; i++) |
| 1129 | { |
| 1130 | MOJOSHADER_effectParam *param = &effect->params[i]; |
| 1131 | freevalue(¶m->value, f, d); |
| 1132 | for (j = 0; j < param->annotation_count; j++) |
| 1133 | { |
| 1134 | freevalue(¶m->annotations[j], f, d); |
| 1135 | } // for |
| 1136 | f((void *) param->annotations, d); |
| 1137 | } // for |
| 1138 | f((void *) effect->params, d); |
| 1139 | |
| 1140 | /* Free techniques, including passes and all annotations */ |
| 1141 | for (i = 0; i < effect->technique_count; i++) |
| 1142 | { |
| 1143 | MOJOSHADER_effectTechnique *technique = &effect->techniques[i]; |
| 1144 | f((void *) technique->name, d); |
| 1145 | for (j = 0; j < technique->pass_count; j++) |
| 1146 | { |
| 1147 | MOJOSHADER_effectPass *pass = &technique->passes[j]; |
| 1148 | f((void *) pass->name, d); |
| 1149 | for (k = 0; k < pass->state_count; k++) |
| 1150 | { |
| 1151 | freevalue(&pass->states[k].value, f, d); |
| 1152 | } // for |
| 1153 | f((void *) pass->states, d); |
| 1154 | for (k = 0; k < pass->annotation_count; k++) |
| 1155 | { |
| 1156 | freevalue(&pass->annotations[k], f, d); |
| 1157 | } // for |
| 1158 | f((void *) pass->annotations, d); |
| 1159 | } // for |
| 1160 | f((void *) technique->passes, d); |
| 1161 | for (j = 0; j < technique->annotation_count; j++) |
| 1162 | { |
| 1163 | freevalue(&technique->annotations[j], f, d); |
| 1164 | } // for |
| 1165 | f((void *) technique->annotations, d); |
| 1166 | } // for |
| 1167 | f((void *) effect->techniques, d); |
| 1168 | |
| 1169 | /* Free object table */ |
| 1170 | for (i = 0; i < effect->object_count; i++) |
| 1171 | { |
| 1172 | MOJOSHADER_effectObject *object = &effect->objects[i]; |
| 1173 | if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER |
| 1174 | || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER) |
| 1175 | { |
| 1176 | if (object->shader.is_preshader) |
| 1177 | MOJOSHADER_freePreshader(object->shader.preshader); |
| 1178 | else |
| 1179 | effect->ctx.deleteShader(effect->ctx.shaderContext, object->shader.shader); |
| 1180 | f((void *) object->shader.params, d); |
| 1181 | f((void *) object->shader.samplers, d); |
| 1182 | f((void *) object->shader.preshader_params, d); |
| 1183 | } // if |
| 1184 | else if (object->type == MOJOSHADER_SYMTYPE_SAMPLER |
| 1185 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D |
| 1186 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D |
| 1187 | || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D |
| 1188 | || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE) |
| 1189 | f((void *) object->mapping.name, d); |
| 1190 | else if (object->type == MOJOSHADER_SYMTYPE_STRING) |
| 1191 | f((void *) object->string.string, d); |
| 1192 | } // for |
| 1193 | f((void *) effect->objects, d); |
| 1194 | |
| 1195 | /* Free base effect structure */ |
| 1196 | f((void *) effect, d); |
| 1197 | } // MOJOSHADER_freeEffect |
| 1198 | |
| 1199 | |
| 1200 | // !!! FIXME: Out of memory check! |
| 1201 | #define COPY_STRING(location) \ |
| 1202 | if (src->location != NULL) \ |
| 1203 | { \ |
| 1204 | siz = strlen(src->location) + 1; \ |
| 1205 | stringcopy = (char *) m(siz, d); \ |
| 1206 | strcpy(stringcopy, src->location); \ |
| 1207 | dst->location = stringcopy; \ |
| 1208 | } // if |
| 1209 | |
| 1210 | |
| 1211 | void copysymboltypeinfo(MOJOSHADER_symbolTypeInfo *dst, |
| 1212 | MOJOSHADER_symbolTypeInfo *src, |
| 1213 | MOJOSHADER_malloc m, |
| 1214 | void *d) |
| 1215 | { |
| 1216 | int i; |
| 1217 | uint32 siz = 0; |
| 1218 | char *stringcopy = NULL; |
| 1219 | memcpy(dst, src, sizeof (MOJOSHADER_symbolTypeInfo)); |
| 1220 | if (dst->member_count > 0) |
| 1221 | { |
| 1222 | siz = dst->member_count * sizeof (MOJOSHADER_symbolStructMember); |
| 1223 | dst->members = (MOJOSHADER_symbolStructMember *) m(siz, d); |
| 1224 | for (i = 0; i < dst->member_count; i++) |
| 1225 | { |
| 1226 | COPY_STRING(members[i].name) |
| 1227 | copysymboltypeinfo(&dst->members[i].info, &src->members[i].info, m, d); |
| 1228 | } // for |
| 1229 | } // if |
| 1230 | } // copysymboltypeinfo |
| 1231 | |
| 1232 | |
| 1233 | void copyvalue(MOJOSHADER_effectValue *dst, |
| 1234 | MOJOSHADER_effectValue *src, |
| 1235 | MOJOSHADER_malloc m, |
| 1236 | void *d) |
| 1237 | { |
| 1238 | int i; |
| 1239 | uint32 siz = 0; |
| 1240 | char *stringcopy = NULL; |
| 1241 | |
| 1242 | COPY_STRING(name) |
| 1243 | COPY_STRING(semantic) |
| 1244 | copysymboltypeinfo(&dst->type, &src->type, m, d); |
| 1245 | dst->value_count = src->value_count; |
| 1246 | |
| 1247 | if (dst->type.parameter_class == MOJOSHADER_SYMCLASS_SCALAR |
| 1248 | || dst->type.parameter_class == MOJOSHADER_SYMCLASS_VECTOR |
| 1249 | || dst->type.parameter_class == MOJOSHADER_SYMCLASS_MATRIX_ROWS |
| 1250 | || dst->type.parameter_class == MOJOSHADER_SYMCLASS_MATRIX_COLUMNS |
| 1251 | || dst->type.parameter_class == MOJOSHADER_SYMCLASS_STRUCT) |
| 1252 | { |
| 1253 | siz = dst->value_count * 4; |
| 1254 | dst->values = m(siz, d); |
| 1255 | // !!! FIXME: Out of memory check! |
| 1256 | memcpy(dst->values, src->values, siz); |
| 1257 | } // if |
| 1258 | else if (dst->type.parameter_class == MOJOSHADER_SYMCLASS_OBJECT) |
| 1259 | { |
| 1260 | if (dst->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER |
| 1261 | || dst->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER1D |
| 1262 | || dst->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER2D |
| 1263 | || dst->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER3D |
| 1264 | || dst->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLERCUBE) |
| 1265 | { |
| 1266 | siz = dst->value_count * sizeof (MOJOSHADER_effectSamplerState); |
| 1267 | dst->values = m(siz, d); |
| 1268 | // !!! FIXME: Out of memory check! |
| 1269 | memset(dst->values, '\0', siz); |
| 1270 | for (i = 0; i < dst->value_count; i++) |
| 1271 | { |
| 1272 | dst->valuesSS[i].type = src->valuesSS[i].type; |
| 1273 | copyvalue(&dst->valuesSS[i].value, |
| 1274 | &src->valuesSS[i].value, |
| 1275 | m, d); |
| 1276 | } // for |
| 1277 | } // if |
| 1278 | else |
| 1279 | { |
| 1280 | siz = dst->value_count * 4; |
| 1281 | dst->values = m(siz, d); |
| 1282 | // !!! FIXME: Out of memory check! |
| 1283 | memcpy(dst->values, src->values, siz); |
| 1284 | } // else |
| 1285 | } // else if |
| 1286 | |
| 1287 | } // copyvalue |
| 1288 | |
| 1289 | |
| 1290 | #undef COPY_STRING |
| 1291 | |
| 1292 | |
| 1293 | void copysymbolinfo(MOJOSHADER_symbolTypeInfo *dst, |
| 1294 | MOJOSHADER_symbolTypeInfo *src, |
| 1295 | MOJOSHADER_malloc m, |
| 1296 | void *d) |
| 1297 | { |
| 1298 | int i; |
| 1299 | uint32 siz; |
| 1300 | char *stringcopy; |
| 1301 | |
| 1302 | dst->parameter_class = src->parameter_class; |
| 1303 | dst->parameter_type = src->parameter_type; |
| 1304 | dst->rows = src->rows; |
| 1305 | dst->columns = src->columns; |
| 1306 | dst->elements = src->elements; |
| 1307 | dst->member_count = src->member_count; |
| 1308 | |
| 1309 | if (dst->member_count > 0) |
| 1310 | { |
| 1311 | siz = sizeof (MOJOSHADER_symbolStructMember) * dst->member_count; |
| 1312 | dst->members = (MOJOSHADER_symbolStructMember *) m(siz, d); |
| 1313 | // !!! FIXME: Out of memory check! |
| 1314 | for (i = 0; i < dst->member_count; i++) |
| 1315 | { |
| 1316 | if (src->members[i].name != NULL) |
| 1317 | { |
| 1318 | siz = strlen(src->members[i].name) + 1; |
| 1319 | stringcopy = (char *) m(siz, d); |
| 1320 | strcpy(stringcopy, src->members[i].name); |
| 1321 | dst->members[i].name = stringcopy; |
| 1322 | } // if |
| 1323 | copysymbolinfo(&dst->members[i].info, &src->members[i].info, m, d); |
| 1324 | } // for |
| 1325 | } // if |
| 1326 | } // copysymbolinfo |
| 1327 | |
| 1328 | |
| 1329 | void copysymbol(MOJOSHADER_symbol *dst, |
| 1330 | MOJOSHADER_symbol *src, |
| 1331 | MOJOSHADER_malloc m, |
| 1332 | void *d) |
| 1333 | { |
| 1334 | uint32 siz = strlen(src->name) + 1; |
| 1335 | char *stringcopy = (char *) m(siz, d); |
| 1336 | // !!! FIXME: Out of memory check! |
| 1337 | strcpy(stringcopy, src->name); |
| 1338 | dst->name = stringcopy; |
| 1339 | dst->register_set = src->register_set; |
| 1340 | dst->register_index = src->register_index; |
| 1341 | dst->register_count = src->register_count; |
| 1342 | copysymbolinfo(&dst->info, &src->info, m, d); |
| 1343 | } // copysymbol |
| 1344 | |
| 1345 | |
| 1346 | MOJOSHADER_preshader *copypreshader(const MOJOSHADER_preshader *src, |
| 1347 | MOJOSHADER_malloc m, |
| 1348 | void *d) |
| 1349 | { |
| 1350 | int i, j; |
| 1351 | uint32 siz; |
| 1352 | MOJOSHADER_preshader *retval; |
| 1353 | |
| 1354 | retval = (MOJOSHADER_preshader *) m(sizeof (MOJOSHADER_preshader), d); |
| 1355 | // !!! FIXME: Out of memory check! |
| 1356 | memset(retval, '\0', sizeof (MOJOSHADER_preshader)); |
| 1357 | |
| 1358 | siz = sizeof (double) * src->literal_count; |
| 1359 | retval->literal_count = src->literal_count; |
| 1360 | retval->literals = (double *) m(siz, d); |
| 1361 | // !!! FIXME: Out of memory check! |
| 1362 | memcpy(retval->literals, src->literals, siz); |
| 1363 | |
| 1364 | retval->temp_count = src->temp_count; |
| 1365 | |
| 1366 | siz = sizeof (MOJOSHADER_symbol) * src->symbol_count; |
| 1367 | retval->symbol_count = src->symbol_count; |
| 1368 | retval->symbols = (MOJOSHADER_symbol *) m(siz, d); |
| 1369 | // !!! FIXME: Out of memory check! |
| 1370 | memset(retval->symbols, '\0', siz); |
| 1371 | |
| 1372 | for (i = 0; i < retval->symbol_count; i++) |
| 1373 | copysymbol(&retval->symbols[i], &src->symbols[i], m, d); |
| 1374 | |
| 1375 | siz = sizeof (MOJOSHADER_preshaderInstruction) * src->instruction_count; |
| 1376 | retval->instruction_count = src->instruction_count; |
| 1377 | retval->instructions = (MOJOSHADER_preshaderInstruction *) m(siz, d); |
| 1378 | // !!! FIXME: Out of memory check! |
| 1379 | memcpy(retval->instructions, src->instructions, siz); |
| 1380 | for (i = 0; i < retval->instruction_count; i++) |
| 1381 | for (j = 0; j < retval->instructions[i].operand_count; j++) |
| 1382 | { |
| 1383 | siz = sizeof (unsigned int) * retval->instructions[i].operands[j].array_register_count; |
| 1384 | retval->instructions[i].operands[j].array_registers = (unsigned int *) m(siz, d); |
| 1385 | // !!! FIXME: Out of memory check! |
| 1386 | memcpy(retval->instructions[i].operands[j].array_registers, |
| 1387 | src->instructions[i].operands[j].array_registers, |
| 1388 | siz); |
| 1389 | } // for |
| 1390 | |
| 1391 | siz = sizeof (float) * 4 * src->register_count; |
| 1392 | retval->register_count = src->register_count; |
| 1393 | retval->registers = (float *) m(siz, d); |
| 1394 | // !!! FIXME: Out of memory check! |
| 1395 | memcpy(retval->registers, src->registers, siz); |
| 1396 | |
| 1397 | return retval; |
| 1398 | } // copypreshader |
| 1399 | |
| 1400 | |
| 1401 | MOJOSHADER_effect *MOJOSHADER_cloneEffect(const MOJOSHADER_effect *effect) |
| 1402 | { |
| 1403 | int i, j, k; |
| 1404 | MOJOSHADER_parseData *pd; |
| 1405 | MOJOSHADER_effect *clone; |
| 1406 | MOJOSHADER_malloc m = effect->ctx.m; |
| 1407 | void *d = effect->ctx.malloc_data; |
| 1408 | uint32 siz = 0; |
| 1409 | char *stringcopy = NULL; |
| 1410 | uint32 curSampler; |
| 1411 | |
| 1412 | if ((effect == NULL) || (effect == &MOJOSHADER_out_of_mem_effect)) |
| 1413 | return NULL; // no-op. |
| 1414 | |
| 1415 | clone = (MOJOSHADER_effect *) m(sizeof (MOJOSHADER_effect), d); |
| 1416 | if (clone == NULL) |
| 1417 | return NULL; // Maybe out_of_mem_effect instead? |
| 1418 | memset(clone, '\0', sizeof (MOJOSHADER_effect)); |
| 1419 | |
| 1420 | /* Copy ctx */ |
| 1421 | memcpy(&clone->ctx, &effect->ctx, sizeof(MOJOSHADER_effectShaderContext)); |
| 1422 | |
| 1423 | #define COPY_STRING(location) \ |
| 1424 | siz = strlen(effect->location) + 1; \ |
| 1425 | stringcopy = (char *) m(siz, d); \ |
| 1426 | if (stringcopy == NULL) \ |
| 1427 | goto cloneEffect_outOfMemory; \ |
| 1428 | strcpy(stringcopy, effect->location); \ |
| 1429 | clone->location = stringcopy; \ |
| 1430 | |
| 1431 | /* Copy errors */ |
| 1432 | siz = sizeof (MOJOSHADER_error) * effect->error_count; |
| 1433 | clone->error_count = effect->error_count; |
| 1434 | clone->errors = (MOJOSHADER_error *) m(siz, d); |
| 1435 | if (clone->errors == NULL) |
| 1436 | goto cloneEffect_outOfMemory; |
| 1437 | memset(clone->errors, '\0', siz); |
| 1438 | for (i = 0; i < clone->error_count; i++) |
| 1439 | { |
| 1440 | COPY_STRING(errors[i].error) |
| 1441 | COPY_STRING(errors[i].filename) |
| 1442 | clone->errors[i].error_position = effect->errors[i].error_position; |
| 1443 | } // for |
| 1444 | |
| 1445 | /* Copy parameters */ |
| 1446 | siz = sizeof (MOJOSHADER_effectParam) * effect->param_count; |
| 1447 | clone->param_count = effect->param_count; |
| 1448 | clone->params = (MOJOSHADER_effectParam *) m(siz, d); |
| 1449 | if (clone->params == NULL) |
| 1450 | goto cloneEffect_outOfMemory; |
| 1451 | memset(clone->params, '\0', siz); |
| 1452 | for (i = 0; i < clone->param_count; i++) |
| 1453 | { |
| 1454 | copyvalue(&clone->params[i].value, &effect->params[i].value, m, d); |
| 1455 | |
| 1456 | /* Copy parameter annotations */ |
| 1457 | siz = sizeof (MOJOSHADER_effectAnnotation) * effect->params[i].annotation_count; |
| 1458 | clone->params[i].annotation_count = effect->params[i].annotation_count; |
| 1459 | clone->params[i].annotations = (MOJOSHADER_effectAnnotation *) m(siz, d); |
| 1460 | if (clone->params[i].annotations == NULL) |
| 1461 | goto cloneEffect_outOfMemory; |
| 1462 | memset(clone->params[i].annotations, '\0', siz); |
| 1463 | for (j = 0; j < clone->params[i].annotation_count; j++) |
| 1464 | copyvalue(&clone->params[i].annotations[j], |
| 1465 | &effect->params[i].annotations[j], |
| 1466 | m, d); |
| 1467 | } // for |
| 1468 | |
| 1469 | /* Copy techniques */ |
| 1470 | siz = sizeof (MOJOSHADER_effectTechnique) * effect->technique_count; |
| 1471 | clone->technique_count = effect->technique_count; |
| 1472 | clone->techniques = (MOJOSHADER_effectTechnique *) m(siz, d); |
| 1473 | if (clone->techniques == NULL) |
| 1474 | goto cloneEffect_outOfMemory; |
| 1475 | memset(clone->techniques, '\0', siz); |
| 1476 | for (i = 0; i < clone->technique_count; i++) |
| 1477 | { |
| 1478 | COPY_STRING(techniques[i].name) |
| 1479 | |
| 1480 | /* Copy passes */ |
| 1481 | siz = sizeof (MOJOSHADER_effectPass) * effect->techniques[i].pass_count; |
| 1482 | clone->techniques[i].pass_count = effect->techniques[i].pass_count; |
| 1483 | clone->techniques[i].passes = (MOJOSHADER_effectPass *) m(siz, d); |
| 1484 | if (clone->techniques[i].passes == NULL) |
| 1485 | goto cloneEffect_outOfMemory; |
| 1486 | memset(clone->techniques[i].passes, '\0', siz); |
| 1487 | for (j = 0; j < clone->techniques[i].pass_count; j++) |
| 1488 | { |
| 1489 | COPY_STRING(techniques[i].passes[j].name) |
| 1490 | |
| 1491 | /* Copy pass states */ |
| 1492 | siz = sizeof (MOJOSHADER_effectState) * effect->techniques[i].passes[j].state_count; |
| 1493 | clone->techniques[i].passes[j].state_count = effect->techniques[i].passes[j].state_count; |
| 1494 | clone->techniques[i].passes[j].states = (MOJOSHADER_effectState *) m(siz, d); |
| 1495 | if (clone->techniques[i].passes[j].states == NULL) |
| 1496 | goto cloneEffect_outOfMemory; |
| 1497 | memset(clone->techniques[i].passes[j].states, '\0', siz); |
| 1498 | for (k = 0; k < clone->techniques[i].passes[j].state_count; k++) |
| 1499 | { |
| 1500 | clone->techniques[i].passes[j].states[k].type = effect->techniques[i].passes[j].states[k].type; |
| 1501 | copyvalue(&clone->techniques[i].passes[j].states[k].value, |
| 1502 | &effect->techniques[i].passes[j].states[k].value, |
| 1503 | m, d); |
| 1504 | } // for |
| 1505 | |
| 1506 | /* Copy pass annotations */ |
| 1507 | siz = sizeof (MOJOSHADER_effectAnnotation) * effect->techniques[i].passes[j].annotation_count; |
| 1508 | clone->techniques[i].passes[j].annotation_count = effect->techniques[i].passes[j].annotation_count; |
| 1509 | clone->techniques[i].passes[j].annotations = (MOJOSHADER_effectAnnotation *) m(siz, d); |
| 1510 | if (clone->techniques[i].passes[j].annotations == NULL) |
| 1511 | goto cloneEffect_outOfMemory; |
| 1512 | memset(clone->techniques[i].passes[j].annotations, '\0', siz); |
| 1513 | for (k = 0; k < clone->techniques[i].passes[j].annotation_count; k++) |
| 1514 | copyvalue(&clone->techniques[i].passes[j].annotations[k], |
| 1515 | &effect->techniques[i].passes[j].annotations[k], |
| 1516 | m, d); |
| 1517 | } // for |
| 1518 | |
| 1519 | /* Copy technique annotations */ |
| 1520 | siz = sizeof (MOJOSHADER_effectAnnotation) * effect->techniques[i].annotation_count; |
| 1521 | clone->techniques[i].annotation_count = effect->techniques[i].annotation_count; |
| 1522 | clone->techniques[i].annotations = (MOJOSHADER_effectAnnotation *) m(siz, d); |
| 1523 | if (clone->techniques[i].annotations == NULL) |
| 1524 | goto cloneEffect_outOfMemory; |
| 1525 | memset(clone->techniques[i].annotations, '\0', siz); |
| 1526 | for (j = 0; j < clone->techniques[i].annotation_count; j++) |
| 1527 | copyvalue(&clone->techniques[i].annotations[j], |
| 1528 | &effect->techniques[i].annotations[j], |
| 1529 | m, d); |
| 1530 | } // for |
| 1531 | |
| 1532 | /* Copy the current technique, but do NOT copy the pass, pass >= 0 just |
| 1533 | * means that the effect we're cloning is currently active |
| 1534 | */ |
| 1535 | for (i = 0; i < effect->technique_count; i++) |
| 1536 | if (&effect->techniques[i] == effect->current_technique) |
| 1537 | { |
| 1538 | clone->current_technique = &clone->techniques[i]; |
| 1539 | break; |
| 1540 | } // if |
| 1541 | assert(clone->current_technique != NULL); |
| 1542 | clone->current_pass = -1; |
| 1543 | |
| 1544 | /* Copy object table */ |
| 1545 | siz = sizeof (MOJOSHADER_effectObject) * effect->object_count; |
| 1546 | clone->object_count = effect->object_count; |
| 1547 | clone->objects = (MOJOSHADER_effectObject *) m(siz, d); |
| 1548 | if (clone->objects == NULL) |
| 1549 | goto cloneEffect_outOfMemory; |
| 1550 | memset(clone->objects, '\0', siz); |
| 1551 | for (i = 0; i < clone->object_count; i++) |
| 1552 | { |
| 1553 | clone->objects[i].type = effect->objects[i].type; |
| 1554 | if (clone->objects[i].type == MOJOSHADER_SYMTYPE_PIXELSHADER |
| 1555 | || clone->objects[i].type == MOJOSHADER_SYMTYPE_VERTEXSHADER) |
| 1556 | { |
| 1557 | clone->objects[i].shader.technique = effect->objects[i].shader.technique; |
| 1558 | clone->objects[i].shader.pass = effect->objects[i].shader.pass; |
| 1559 | clone->objects[i].shader.is_preshader = effect->objects[i].shader.is_preshader; |
| 1560 | siz = sizeof (uint32) * effect->objects[i].shader.preshader_param_count; |
| 1561 | clone->objects[i].shader.preshader_param_count = effect->objects[i].shader.preshader_param_count; |
| 1562 | clone->objects[i].shader.preshader_params = (uint32 *) m(siz, d); |
| 1563 | memcpy(clone->objects[i].shader.preshader_params, |
| 1564 | effect->objects[i].shader.preshader_params, |
| 1565 | siz); |
| 1566 | siz = sizeof (uint32) * effect->objects[i].shader.param_count; |
| 1567 | clone->objects[i].shader.param_count = effect->objects[i].shader.param_count; |
| 1568 | clone->objects[i].shader.params = (uint32 *) m(siz, d); |
| 1569 | memcpy(clone->objects[i].shader.params, |
| 1570 | effect->objects[i].shader.params, |
| 1571 | siz); |
| 1572 | |
| 1573 | if (clone->objects[i].shader.is_preshader) |
| 1574 | { |
| 1575 | clone->objects[i].shader.preshader = copypreshader(effect->objects[i].shader.preshader, |
| 1576 | m, d); |
| 1577 | continue; |
| 1578 | } // if |
| 1579 | |
| 1580 | effect->ctx.shaderAddRef(effect->objects[i].shader.shader); |
| 1581 | clone->objects[i].shader.shader = effect->objects[i].shader.shader; |
| 1582 | pd = clone->ctx.getParseData(clone->objects[i].shader.shader); |
| 1583 | |
| 1584 | siz = sizeof (MOJOSHADER_samplerStateRegister) * effect->objects[i].shader.sampler_count; |
| 1585 | clone->objects[i].shader.sampler_count = effect->objects[i].shader.sampler_count; |
| 1586 | clone->objects[i].shader.samplers = (MOJOSHADER_samplerStateRegister *) m(siz, d); |
| 1587 | if (clone->objects[i].shader.samplers == NULL) |
| 1588 | goto cloneEffect_outOfMemory; |
| 1589 | curSampler = 0; |
| 1590 | for (j = 0; j < pd->symbol_count; j++) |
| 1591 | if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER) |
| 1592 | { |
| 1593 | clone->objects[i].shader.samplers[curSampler].sampler_name = clone->params[clone->objects[i].shader.params[j]].value.name; |
| 1594 | clone->objects[i].shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index; |
| 1595 | clone->objects[i].shader.samplers[curSampler].sampler_state_count = clone->params[clone->objects[i].shader.params[j]].value.value_count; |
| 1596 | clone->objects[i].shader.samplers[curSampler].sampler_states = clone->params[clone->objects[i].shader.params[j]].value.valuesSS; |
| 1597 | curSampler++; |
| 1598 | } // if |
| 1599 | } // if |
| 1600 | else if (clone->objects[i].type == MOJOSHADER_SYMTYPE_SAMPLER |
| 1601 | || clone->objects[i].type == MOJOSHADER_SYMTYPE_SAMPLER1D |
| 1602 | || clone->objects[i].type == MOJOSHADER_SYMTYPE_SAMPLER2D |
| 1603 | || clone->objects[i].type == MOJOSHADER_SYMTYPE_SAMPLER3D |
| 1604 | || clone->objects[i].type == MOJOSHADER_SYMTYPE_SAMPLERCUBE) |
| 1605 | { |
| 1606 | COPY_STRING(objects[i].mapping.name) |
| 1607 | } // else if |
| 1608 | else if (clone->objects[i].type == MOJOSHADER_SYMTYPE_STRING) |
| 1609 | { |
| 1610 | COPY_STRING(objects[i].string.string) |
| 1611 | } // else if |
| 1612 | } // for |
| 1613 | |
| 1614 | #undef COPY_STRING |
| 1615 | |
| 1616 | return clone; |
| 1617 | |
| 1618 | cloneEffect_outOfMemory: |
| 1619 | MOJOSHADER_deleteEffect(clone); |
| 1620 | return NULL; |
| 1621 | } // MOJOSHADER_cloneEffect |
| 1622 | |
| 1623 | |
| 1624 | void MOJOSHADER_effectSetRawValueHandle(const MOJOSHADER_effectParam *parameter, |
| 1625 | const void *data, |
| 1626 | const unsigned int offset, |
| 1627 | const unsigned int len) |
| 1628 | { |
| 1629 | // !!! FIXME: char* case is arbitary, for Win32 -flibit |
| 1630 | memcpy((char *) parameter->value.values + offset, data, len); |
| 1631 | } // MOJOSHADER_effectSetRawValueHandle |
| 1632 | |
| 1633 | |
| 1634 | void MOJOSHADER_effectSetRawValueName(const MOJOSHADER_effect *effect, |
| 1635 | const char *name, |
| 1636 | const void *data, |
| 1637 | const unsigned int offset, |
| 1638 | const unsigned int len) |
| 1639 | { |
| 1640 | int i; |
| 1641 | for (i = 0; i < effect->param_count; i++) |
| 1642 | { |
| 1643 | if (strcmp(name, effect->params[i].value.name) == 0) |
| 1644 | { |
| 1645 | // !!! FIXME: char* case is arbitary, for Win32 -flibit |
| 1646 | memcpy((char *) effect->params[i].value.values + offset, data, len); |
| 1647 | return; |
| 1648 | } // if |
| 1649 | } // for |
| 1650 | assert(0 && "Effect parameter not found!"); |
| 1651 | } // MOJOSHADER_effectSetRawValueName |
| 1652 | |
| 1653 | |
| 1654 | const MOJOSHADER_effectTechnique *MOJOSHADER_effectGetCurrentTechnique(const MOJOSHADER_effect *effect) |
| 1655 | { |
| 1656 | return effect->current_technique; |
| 1657 | } // MOJOSHADER_effectGetCurrentTechnique |
| 1658 | |
| 1659 | |
| 1660 | void MOJOSHADER_effectSetTechnique(MOJOSHADER_effect *effect, |
| 1661 | const MOJOSHADER_effectTechnique *technique) |
| 1662 | { |
| 1663 | int i; |
| 1664 | for (i = 0; i < effect->technique_count; i++) |
| 1665 | { |
| 1666 | if (technique == &effect->techniques[i]) |
| 1667 | { |
| 1668 | effect->current_technique = technique; |
| 1669 | return; |
| 1670 | } // if |
| 1671 | } // for |
| 1672 | assert(0 && "Technique is not part of this effect!"); |
| 1673 | } // MOJOSHADER_effectSetTechnique |
| 1674 | |
| 1675 | |
| 1676 | const MOJOSHADER_effectTechnique *MOJOSHADER_effectFindNextValidTechnique(const MOJOSHADER_effect *effect, |
| 1677 | const MOJOSHADER_effectTechnique *technique |
| 1678 | ) |
| 1679 | { |
| 1680 | int i; |
| 1681 | if (technique == NULL) |
| 1682 | return &effect->techniques[0]; |
| 1683 | for (i = 0; i < effect->technique_count; i++) |
| 1684 | { |
| 1685 | if (technique == &effect->techniques[i]) |
| 1686 | { |
| 1687 | if (i == effect->technique_count - 1) |
| 1688 | return NULL; /* We were passed the last technique! */ |
| 1689 | return &effect->techniques[i + 1]; |
| 1690 | } // if |
| 1691 | } // for |
| 1692 | assert(0 && "Technique is not part of this effect!"); |
| 1693 | return NULL; |
| 1694 | } // MOJOSHADER_effectFindNextValidTechnique |
| 1695 | |
| 1696 | |
| 1697 | void MOJOSHADER_effectBegin(MOJOSHADER_effect *effect, |
| 1698 | unsigned int *numPasses, |
| 1699 | int saveShaderState, |
| 1700 | MOJOSHADER_effectStateChanges *stateChanges) |
| 1701 | { |
| 1702 | *numPasses = effect->current_technique->pass_count; |
| 1703 | effect->restore_shader_state = saveShaderState; |
| 1704 | effect->state_changes = stateChanges; |
| 1705 | |
| 1706 | if (effect->restore_shader_state) |
| 1707 | { |
| 1708 | effect->ctx.getBoundShaders(effect->ctx.shaderContext, |
| 1709 | &effect->prev_vertex_shader, |
| 1710 | &effect->prev_pixel_shader); |
| 1711 | } // if |
| 1712 | } // MOJOSHADER_effectBegin |
| 1713 | |
| 1714 | |
| 1715 | void MOJOSHADER_effectBeginPass(MOJOSHADER_effect *effect, |
| 1716 | unsigned int pass) |
| 1717 | { |
| 1718 | int i; |
| 1719 | MOJOSHADER_effectPass *curPass; |
| 1720 | MOJOSHADER_effectState *state; |
| 1721 | MOJOSHADER_effectShader *rawVert = effect->current_vert_raw; |
| 1722 | MOJOSHADER_effectShader *rawPixl = effect->current_pixl_raw; |
| 1723 | int has_preshader = 0; |
| 1724 | |
| 1725 | effect->ctx.getBoundShaders(effect->ctx.shaderContext, |
| 1726 | &effect->current_vert, |
| 1727 | &effect->current_pixl); |
| 1728 | |
| 1729 | assert(effect->current_pass == -1); |
| 1730 | effect->current_pass = pass; |
| 1731 | curPass = &effect->current_technique->passes[pass]; |
| 1732 | |
| 1733 | // !!! FIXME: I bet this could be stored at parse/compile time. -flibit |
| 1734 | for (i = 0; i < curPass->state_count; i++) |
| 1735 | { |
| 1736 | state = &curPass->states[i]; |
| 1737 | if (state->type == MOJOSHADER_RS_VERTEXSHADER) |
| 1738 | { |
| 1739 | rawVert = &effect->objects[*state->value.valuesI].shader; |
| 1740 | if (rawVert->is_preshader) |
| 1741 | has_preshader = 1; |
| 1742 | else |
| 1743 | effect->current_vert = rawVert->shader; |
| 1744 | } // if |
| 1745 | else if (state->type == MOJOSHADER_RS_PIXELSHADER) |
| 1746 | { |
| 1747 | rawPixl = &effect->objects[*state->value.valuesI].shader; |
| 1748 | if (rawPixl->is_preshader) |
| 1749 | has_preshader = 1; |
| 1750 | else |
| 1751 | effect->current_pixl = rawPixl->shader; |
| 1752 | } |
| 1753 | } // for |
| 1754 | |
| 1755 | effect->state_changes->render_state_changes = curPass->states; |
| 1756 | effect->state_changes->render_state_change_count = curPass->state_count; |
| 1757 | |
| 1758 | effect->current_vert_raw = rawVert; |
| 1759 | effect->current_pixl_raw = rawPixl; |
| 1760 | |
| 1761 | /* If this effect pass has an array of shaders, we get to wait until |
| 1762 | * CommitChanges to actually bind the final shaders. |
| 1763 | * -flibit |
| 1764 | */ |
| 1765 | if (!has_preshader) |
| 1766 | { |
| 1767 | effect->ctx.bindShaders(effect->ctx.shaderContext, |
| 1768 | effect->current_vert, |
| 1769 | effect->current_pixl); |
| 1770 | if (effect->current_vert_raw != NULL) |
| 1771 | { |
| 1772 | effect->state_changes->vertex_sampler_state_changes = rawVert->samplers; |
| 1773 | effect->state_changes->vertex_sampler_state_change_count = rawVert->sampler_count; |
| 1774 | } // if |
| 1775 | if (effect->current_pixl_raw != NULL) |
| 1776 | { |
| 1777 | effect->state_changes->sampler_state_changes = rawPixl->samplers; |
| 1778 | effect->state_changes->sampler_state_change_count = rawPixl->sampler_count; |
| 1779 | } // if |
| 1780 | } // if |
| 1781 | |
| 1782 | MOJOSHADER_effectCommitChanges(effect); |
| 1783 | } // MOJOSHADER_effectBeginPass |
| 1784 | |
| 1785 | |
| 1786 | static inline void copy_parameter_data(MOJOSHADER_effectParam *params, |
| 1787 | unsigned int *param_loc, |
| 1788 | MOJOSHADER_symbol *symbols, |
| 1789 | unsigned int symbol_count, |
| 1790 | float *regf, int *regi, uint8 *regb) |
| 1791 | { |
| 1792 | int i, j, r, c; |
| 1793 | |
| 1794 | i = 0; |
| 1795 | for (i = 0; i < symbol_count; i++) |
| 1796 | { |
| 1797 | const MOJOSHADER_symbol *sym = &symbols[i]; |
| 1798 | const MOJOSHADER_effectValue *param = ¶ms[param_loc[i]].value; |
| 1799 | |
| 1800 | // float/int registers are vec4, so they have 4 elements each |
| 1801 | const uint32 start = sym->register_index << 2; |
| 1802 | |
| 1803 | if (param->type.parameter_type == MOJOSHADER_SYMTYPE_FLOAT) |
| 1804 | memcpy(regf + start, param->valuesF, sym->register_count << 4); |
| 1805 | else if (sym->register_set == MOJOSHADER_SYMREGSET_FLOAT4) |
| 1806 | { |
| 1807 | // Structs are a whole different world... |
| 1808 | if (param->type.parameter_class == MOJOSHADER_SYMCLASS_STRUCT) |
| 1809 | memcpy(regf + start, param->valuesF, sym->register_count << 4); |
| 1810 | else |
| 1811 | { |
| 1812 | // Sometimes int/bool parameters get thrown into float registers... |
| 1813 | j = 0; |
| 1814 | do |
| 1815 | { |
| 1816 | c = 0; |
| 1817 | do |
| 1818 | { |
| 1819 | regf[start + (j << 2) + c] = (float) param->valuesI[(j << 2) + c]; |
| 1820 | } while (++c < param->type.columns); |
| 1821 | } while (++j < sym->register_count); |
| 1822 | } // else |
| 1823 | } // else if |
| 1824 | else if (sym->register_set == MOJOSHADER_SYMREGSET_INT4) |
| 1825 | memcpy(regi + start, param->valuesI, sym->register_count << 4); |
| 1826 | else if (sym->register_set == MOJOSHADER_SYMREGSET_BOOL) |
| 1827 | { |
| 1828 | j = 0; |
| 1829 | r = 0; |
| 1830 | do |
| 1831 | { |
| 1832 | c = 0; |
| 1833 | do |
| 1834 | { |
| 1835 | // regb is not a vec4, enjoy that 'start' bitshift! -flibit |
| 1836 | regb[(start >> 2) + r + c] = param->valuesI[(j << 2) + c]; |
| 1837 | c++; |
| 1838 | } while (c < param->type.columns && ((r + c) < sym->register_count)); |
| 1839 | r += c; |
| 1840 | j++; |
| 1841 | } while (r < sym->register_count); |
| 1842 | } // else if |
| 1843 | } // for |
| 1844 | } // copy_parameter_data |
| 1845 | |
| 1846 | |
| 1847 | void MOJOSHADER_effectCommitChanges(MOJOSHADER_effect *effect) |
| 1848 | { |
| 1849 | MOJOSHADER_effectShader *rawVert = effect->current_vert_raw; |
| 1850 | MOJOSHADER_effectShader *rawPixl = effect->current_pixl_raw; |
| 1851 | |
| 1852 | /* Used for shader selection from preshaders */ |
| 1853 | int i, j; |
| 1854 | MOJOSHADER_effectValue *param; |
| 1855 | MOJOSHADER_parseData *pd; |
| 1856 | float selector; |
| 1857 | int shader_object; |
| 1858 | int selector_ran = 0; |
| 1859 | |
| 1860 | float *vs_reg_file_f, *ps_reg_file_f; |
| 1861 | int *vs_reg_file_i, *ps_reg_file_i; |
| 1862 | uint8 *vs_reg_file_b, *ps_reg_file_b; |
| 1863 | |
| 1864 | /* For effect passes with arrays of shaders, we have to run a preshader |
| 1865 | * that determines which shader to use, based on a parameter's value. |
| 1866 | * -flibit |
| 1867 | */ |
| 1868 | // !!! FIXME: We're just running the preshaders every time. Blech. -flibit |
| 1869 | #define SELECT_SHADER_FROM_PRESHADER(raw, gls) \ |
| 1870 | if (raw != NULL && raw->is_preshader) \ |
| 1871 | { \ |
| 1872 | i = 0; \ |
| 1873 | do \ |
| 1874 | { \ |
| 1875 | param = &effect->params[raw->preshader_params[i]].value; \ |
| 1876 | for (j = 0; j < (param->value_count >> 2); j++) \ |
| 1877 | memcpy(raw->preshader->registers + raw->preshader->symbols[i].register_index + j, \ |
| 1878 | param->valuesI + (j << 2), \ |
| 1879 | param->type.columns << 2); \ |
| 1880 | } while (++i < raw->preshader->symbol_count); \ |
| 1881 | MOJOSHADER_runPreshader(raw->preshader, &selector); \ |
| 1882 | shader_object = effect->params[raw->params[0]].value.valuesI[(int) selector]; \ |
| 1883 | raw = &effect->objects[shader_object].shader; \ |
| 1884 | gls = raw->shader; \ |
| 1885 | selector_ran = 1; \ |
| 1886 | } |
| 1887 | SELECT_SHADER_FROM_PRESHADER(rawVert, effect->current_vert) |
| 1888 | SELECT_SHADER_FROM_PRESHADER(rawPixl, effect->current_pixl) |
| 1889 | #undef SELECT_SHADER_FROM_PRESHADER |
| 1890 | if (selector_ran) |
| 1891 | { |
| 1892 | effect->ctx.bindShaders(effect->ctx.shaderContext, |
| 1893 | effect->current_vert, |
| 1894 | effect->current_pixl); |
| 1895 | if (effect->current_vert_raw != NULL) |
| 1896 | { |
| 1897 | effect->state_changes->vertex_sampler_state_changes = rawVert->samplers; |
| 1898 | effect->state_changes->vertex_sampler_state_change_count = rawVert->sampler_count; |
| 1899 | } // if |
| 1900 | if (effect->current_pixl_raw != NULL) |
| 1901 | { |
| 1902 | effect->state_changes->sampler_state_changes = rawPixl->samplers; |
| 1903 | effect->state_changes->sampler_state_change_count = rawPixl->sampler_count; |
| 1904 | } // if |
| 1905 | } // if |
| 1906 | |
| 1907 | /* This is where parameters are copied into the constant buffers. |
| 1908 | * If you're looking for where things slow down immensely, look at |
| 1909 | * the copy_parameter_data() and MOJOSHADER_runPreshader() functions. |
| 1910 | * -flibit |
| 1911 | */ |
| 1912 | // !!! FIXME: We're just copying everything every time. Blech. -flibit |
| 1913 | // !!! FIXME: We're just running the preshaders every time. Blech. -flibit |
| 1914 | // !!! FIXME: Will the preshader ever want int/bool registers? -flibit |
| 1915 | #define COPY_PARAMETER_DATA(raw, stage) \ |
| 1916 | if (raw != NULL && raw->shader != NULL) \ |
| 1917 | { \ |
| 1918 | pd = effect->ctx.getParseData(raw->shader); \ |
| 1919 | copy_parameter_data(effect->params, raw->params, \ |
| 1920 | pd->symbols, \ |
| 1921 | pd->symbol_count, \ |
| 1922 | stage##_reg_file_f, \ |
| 1923 | stage##_reg_file_i, \ |
| 1924 | stage##_reg_file_b); \ |
| 1925 | if (pd->preshader) \ |
| 1926 | { \ |
| 1927 | copy_parameter_data(effect->params, raw->preshader_params, \ |
| 1928 | pd->preshader->symbols, \ |
| 1929 | pd->preshader->symbol_count, \ |
| 1930 | pd->preshader->registers, \ |
| 1931 | NULL, \ |
| 1932 | NULL); \ |
| 1933 | MOJOSHADER_runPreshader(pd->preshader, stage##_reg_file_f); \ |
| 1934 | } \ |
| 1935 | } |
| 1936 | effect->ctx.mapUniformBufferMemory(effect->ctx.shaderContext, |
| 1937 | &vs_reg_file_f, &vs_reg_file_i, &vs_reg_file_b, |
| 1938 | &ps_reg_file_f, &ps_reg_file_i, &ps_reg_file_b); |
| 1939 | COPY_PARAMETER_DATA(rawVert, vs) |
| 1940 | COPY_PARAMETER_DATA(rawPixl, ps) |
| 1941 | effect->ctx.unmapUniformBufferMemory(effect->ctx.shaderContext); |
| 1942 | #undef COPY_PARAMETER_DATA |
| 1943 | } // MOJOSHADER_effectCommitChanges |
| 1944 | |
| 1945 | |
| 1946 | void MOJOSHADER_effectEndPass(MOJOSHADER_effect *effect) |
| 1947 | { |
| 1948 | assert(effect->current_pass != -1); |
| 1949 | effect->current_pass = -1; |
| 1950 | } // MOJOSHADER_effectEndPass |
| 1951 | |
| 1952 | |
| 1953 | void MOJOSHADER_effectEnd(MOJOSHADER_effect *effect) |
| 1954 | { |
| 1955 | if (effect->restore_shader_state) |
| 1956 | { |
| 1957 | effect->restore_shader_state = 0; |
| 1958 | effect->ctx.bindShaders(effect->ctx.shaderContext, |
| 1959 | effect->prev_vertex_shader, |
| 1960 | effect->prev_pixel_shader); |
| 1961 | } // if |
| 1962 | |
| 1963 | effect->state_changes = NULL; |
| 1964 | } // MOJOSHADER_effectEnd |
| 1965 | |
| 1966 | #endif // MOJOSHADER_EFFECT_SUPPORT |
| 1967 | |
| 1968 | // end of mojoshader_effects.c ... |
| 1969 |