/
mojoshader_effects.c
1872 lines (1657 loc) · 72.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
13
14
#ifdef MOJOSHADER_EFFECT_SUPPORT
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
23
const float *inregs = preshader->registers;
24
25
26
27
// this is fairly straightforward, as there aren't any branching
// opcodes in the preshader instruction set (at the moment, at least).
const int scalarstart = (int) MOJOSHADER_PRESHADEROP_SCALAR_OPS;
28
29
30
31
32
33
double *temps = NULL;
if (preshader->temp_count > 0)
{
temps = (double *) alloca(sizeof (double) * preshader->temp_count);
memset(temps, '\0', sizeof (double) * preshader->temp_count);
} // if
34
35
36
37
38
39
double dst[4] = { 0, 0, 0, 0 };
double src[3][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
const double *src0 = &src[0][0];
const double *src1 = &src[1][0];
const double *src2 = &src[2][0];
40
41
42
43
MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
int instit;
44
45
46
47
48
49
50
51
52
53
54
#if 0 // FIXME: Do we need to do this or is the compiler smart enough?
// Clear preshader output registers first!
for (instit = 0; instit < preshader->instruction_count; instit++, inst++)
{
const MOJOSHADER_preshaderOperand *operand = &inst->operands[inst->operand_count - 1];
if (operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT)
memset(&outregs[operand->index], '\0', sizeof(float) * 4);
} // for
inst = preshader->instructions;
#endif
55
56
for (instit = 0; instit < preshader->instruction_count; instit++, inst++)
{
57
const MOJOSHADER_preshaderOperand *operand = inst->operands;
58
59
const int elems = inst->element_count;
const int elemsbytes = sizeof (double) * elems;
60
const int isscalarop = (inst->opcode >= scalarstart);
61
62
63
64
assert(elems >= 0);
assert(elems <= 4);
65
66
// load up our operands...
int opiter, elemiter;
67
for (opiter = 0; opiter < inst->operand_count-1; opiter++, operand++)
68
{
69
const int isscalar = ((isscalarop) && (opiter == 0));
70
71
72
73
74
75
const unsigned int index = operand->index;
switch (operand->type)
{
case MOJOSHADER_PRESHADEROPERAND_LITERAL:
{
if (!isscalar)
76
77
78
79
{
assert((index + elems) <= preshader->literal_count);
memcpy(&src[opiter][0], &preshader->literals[index], elemsbytes);
} // if
80
81
82
else
{
for (elemiter = 0; elemiter < elems; elemiter++)
83
src[opiter][elemiter] = preshader->literals[index];
84
85
86
87
88
} // else
break;
} // case
case MOJOSHADER_PRESHADEROPERAND_INPUT:
89
90
91
92
93
94
95
96
97
98
99
100
if (operand->array_register_count > 0)
{
int i;
const int *regsi = (const int *) inregs;
int arrIndex = regsi[((index >> 4) * 4) + ((index >> 2) & 3)];
for (i = 0; i < operand->array_register_count; i++)
{
arrIndex = regsi[operand->array_registers[i] + arrIndex];
}
src[opiter][0] = arrIndex;
} // if
else if (isscalar)
101
102
103
104
105
106
107
108
109
src[opiter][0] = inregs[index];
else
{
int cpy;
for (cpy = 0; cpy < elems; cpy++)
src[opiter][cpy] = inregs[index+cpy];
} // else
break;
110
111
case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
if (isscalar)
112
src[opiter][0] = outregs[index];
113
114
115
116
else
{
int cpy;
for (cpy = 0; cpy < elems; cpy++)
117
src[opiter][cpy] = outregs[index+cpy];
118
119
120
121
} // else
break;
case MOJOSHADER_PRESHADEROPERAND_TEMP:
122
123
124
125
126
127
128
if (temps != NULL)
{
if (isscalar)
src[opiter][0] = temps[index];
else
memcpy(src[opiter], temps + index, elemsbytes);
} // if
129
130
131
132
break;
default:
assert(0 && "unexpected preshader operand type.");
133
return;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
} // switch
} // for
// run the actual instruction, store result to dst.
int i;
switch (inst->opcode)
{
#define OPCODE_CASE(op, val) \
case MOJOSHADER_PRESHADEROP_##op: \
for (i = 0; i < elems; i++) { dst[i] = val; } \
break;
//OPCODE_CASE(NOP, 0.0) // not a real instruction.
OPCODE_CASE(MOV, src0[i])
OPCODE_CASE(NEG, -src0[i])
OPCODE_CASE(RCP, 1.0 / src0[i])
OPCODE_CASE(FRC, src0[i] - floor(src0[i]))
OPCODE_CASE(EXP, exp(src0[i]))
OPCODE_CASE(LOG, log(src0[i]))
OPCODE_CASE(RSQ, 1.0 / sqrt(src0[i]))
OPCODE_CASE(SIN, sin(src0[i]))
OPCODE_CASE(COS, cos(src0[i]))
OPCODE_CASE(ASIN, asin(src0[i]))
OPCODE_CASE(ACOS, acos(src0[i]))
OPCODE_CASE(ATAN, atan(src0[i]))
OPCODE_CASE(MIN, (src0[i] < src1[i]) ? src0[i] : src1[i])
OPCODE_CASE(MAX, (src0[i] > src1[i]) ? src0[i] : src1[i])
OPCODE_CASE(LT, (src0[i] < src1[i]) ? 1.0 : 0.0)
OPCODE_CASE(GE, (src0[i] >= src1[i]) ? 1.0 : 0.0)
OPCODE_CASE(ADD, src0[i] + src1[i])
OPCODE_CASE(MUL, src0[i] * src1[i])
OPCODE_CASE(ATAN2, atan2(src0[i], src1[i]))
OPCODE_CASE(DIV, src0[i] / src1[i])
OPCODE_CASE(CMP, (src0[i] >= 0.0) ? src1[i] : src2[i])
//OPCODE_CASE(NOISE, ???) // !!! FIXME: don't know what this does
//OPCODE_CASE(MOVC, ???) // !!! FIXME: don't know what this does
170
171
172
173
174
175
176
177
178
179
OPCODE_CASE(MIN_SCALAR, (src0[0] < src1[i]) ? src0[0] : src1[i])
OPCODE_CASE(MAX_SCALAR, (src0[0] > src1[i]) ? src0[0] : src1[i])
OPCODE_CASE(LT_SCALAR, (src0[0] < src1[i]) ? 1.0 : 0.0)
OPCODE_CASE(GE_SCALAR, (src0[0] >= src1[i]) ? 1.0 : 0.0)
OPCODE_CASE(ADD_SCALAR, src0[0] + src1[i])
OPCODE_CASE(MUL_SCALAR, src0[0] * src1[i])
OPCODE_CASE(ATAN2_SCALAR, atan2(src0[0], src1[i]))
OPCODE_CASE(DIV_SCALAR, src0[0] / src1[i])
//OPCODE_CASE(DOT_SCALAR) // !!! FIXME: isn't this just a MUL?
//OPCODE_CASE(NOISE_SCALAR, ???) // !!! FIXME: ?
180
181
182
183
184
185
186
187
188
#undef OPCODE_CASE
case MOJOSHADER_PRESHADEROP_DOT:
{
double final = 0.0;
for (i = 0; i < elems; i++)
final += src0[i] * src1[i];
for (i = 0; i < elems; i++)
dst[i] = final; // !!! FIXME: is this right?
189
break;
190
191
192
193
194
195
196
197
198
} // case
default:
assert(0 && "Unhandled preshader opcode!");
break;
} // switch
// Figure out where dst wants to be stored.
if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
199
200
201
{
assert(preshader->temp_count >=
operand->index + (elemsbytes / sizeof (double)));
202
memcpy(temps + operand->index, dst, elemsbytes);
203
} // if
204
205
206
207
else
{
assert(operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT);
for (i = 0; i < elems; i++)
208
outregs[operand->index + i] = (float) dst[i];
209
210
} // else
} // for
211
} // MOJOSHADER_runPreshader
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
static MOJOSHADER_effect MOJOSHADER_out_of_mem_effect = {
1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static uint32 readui32(const uint8 **_ptr, uint32 *_len)
{
uint32 retval = 0;
if (*_len < sizeof (retval))
*_len = 0;
else
{
const uint32 *ptr = (const uint32 *) *_ptr;
retval = SWAP32(*ptr);
*_ptr += sizeof (retval);
*_len -= sizeof (retval);
} // else
return retval;
} // readui32
232
233
234
235
static char *readstring(const uint8 *base,
const uint32 offset,
MOJOSHADER_malloc m,
void *d)
236
{
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// !!! FIXME: sanity checks!
// !!! FIXME: verify this doesn't go past EOF looking for a null.
const char *str = ((const char *) base) + offset;
const uint32 len = *((const uint32 *) str);
char *strptr = NULL;
if (len == 0) return NULL; /* No length? No string. */
strptr = (char *) m(len, d);
memcpy(strptr, str + 4, len);
return strptr;
} // readstring
static int findparameter(const MOJOSHADER_effectParam *params,
const uint32 param_count,
const char *name)
{
int i;
for (i = 0; i < param_count; i++)
if (strcmp(name, params[i].value.name) == 0)
return i;
assert(0 && "Parameter not found!");
257
return -1;
258
259
260
261
262
263
264
265
266
267
}
static void readvalue(const uint8 *base,
const uint32 typeoffset,
const uint32 valoffset,
MOJOSHADER_effectValue *value,
MOJOSHADER_effectObject *objects,
MOJOSHADER_malloc m,
void *d)
{
268
int i, j, k;
269
270
271
272
273
274
275
276
277
const uint8 *typeptr = base + typeoffset;
const uint8 *valptr = base + valoffset;
unsigned int typelen = 9999999; // !!! FIXME
const uint32 type = readui32(&typeptr, &typelen);
const uint32 valclass = readui32(&typeptr, &typelen);
const uint32 name = readui32(&typeptr, &typelen);
const uint32 semantic = readui32(&typeptr, &typelen);
const uint32 numelements = readui32(&typeptr, &typelen);
278
279
value->type.parameter_type = (MOJOSHADER_symbolType) type;
value->type.parameter_class = (MOJOSHADER_symbolClass) valclass;
280
281
value->name = readstring(base, name, m, d);
value->semantic = readstring(base, semantic, m, d);
282
value->type.elements = numelements;
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* Class sanity check */
assert(valclass >= MOJOSHADER_SYMCLASS_SCALAR && valclass <= MOJOSHADER_SYMCLASS_STRUCT);
if (valclass == MOJOSHADER_SYMCLASS_SCALAR
|| valclass == MOJOSHADER_SYMCLASS_VECTOR
|| valclass == MOJOSHADER_SYMCLASS_MATRIX_ROWS
|| valclass == MOJOSHADER_SYMCLASS_MATRIX_COLUMNS)
{
/* These classes only ever contain scalar values */
assert(type >= MOJOSHADER_SYMTYPE_BOOL && type <= MOJOSHADER_SYMTYPE_FLOAT);
const uint32 columncount = readui32(&typeptr, &typelen);
const uint32 rowcount = readui32(&typeptr, &typelen);
298
299
value->type.columns = columncount;
value->type.rows = rowcount;
300
301
uint32 siz = 4 * rowcount;
302
303
304
305
306
if (numelements > 0)
siz *= numelements;
value->value_count = siz;
siz *= 4;
value->values = m(siz, d);
307
308
309
310
memset(value->values, '\0', siz);
siz /= 16;
for (i = 0; i < siz; i++)
memcpy(value->valuesF + (i << 2), valptr + ((columncount << 2) * i), columncount << 2);
311
312
313
314
315
316
317
318
319
320
321
322
323
324
} // if
else if (valclass == MOJOSHADER_SYMCLASS_OBJECT)
{
/* This class contains either samplers or "objects" */
assert(type >= MOJOSHADER_SYMTYPE_STRING && type <= MOJOSHADER_SYMTYPE_VERTEXSHADER);
if (type == MOJOSHADER_SYMTYPE_SAMPLER
|| type == MOJOSHADER_SYMTYPE_SAMPLER1D
|| type == MOJOSHADER_SYMTYPE_SAMPLER2D
|| type == MOJOSHADER_SYMTYPE_SAMPLER3D
|| type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
{
unsigned int vallen = 9999999; // !!! FIXME
const uint32 numstates = readui32(&valptr, &vallen);
325
326
value->value_count = numstates;
327
328
329
330
const uint32 siz = sizeof(MOJOSHADER_effectSamplerState) * numstates;
value->values = m(siz, d);
memset(value->values, '\0', siz);
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
for (i = 0; i < numstates; i++)
{
MOJOSHADER_effectSamplerState *state = &value->valuesSS[i];
const uint32 stype = readui32(&valptr, &vallen) & ~0xA0;
/*const uint32 FIXME =*/ readui32(&valptr, &vallen);
const uint32 statetypeoffset = readui32(&valptr, &vallen);
const uint32 statevaloffset = readui32(&valptr, &vallen);
state->type = (MOJOSHADER_samplerStateType) stype;
readvalue(base, statetypeoffset, statevaloffset,
&state->value, objects,
m, d);
if (stype == MOJOSHADER_SAMP_TEXTURE)
objects[state->value.valuesI[0]].type = (MOJOSHADER_symbolType) type;
} // for
} // if
else
{
uint32 numobjects = 1;
if (numelements > 0)
numobjects = numelements;
353
354
value->value_count = numobjects;
355
356
357
358
const uint32 siz = 4 * numobjects;
value->values = m(siz, d);
memcpy(value->values, valptr, siz);
359
360
361
362
363
364
for (i = 0; i < value->value_count; i++)
objects[value->valuesI[i]].type = (MOJOSHADER_symbolType) type;
} // else
} // else if
else if (valclass == MOJOSHADER_SYMCLASS_STRUCT)
365
{
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
uint32 siz;
value->type.member_count = readui32(&typeptr, &typelen);
siz = value->type.member_count * sizeof (MOJOSHADER_symbolStructMember);
value->type.members = (MOJOSHADER_symbolStructMember *) m(siz, d);
uint32 structsize = 0;
for (i = 0; i < value->type.member_count; i++)
{
MOJOSHADER_symbolStructMember *mem = &value->type.members[i];
mem->info.parameter_type = (MOJOSHADER_symbolType) readui32(&typeptr, &typelen);
mem->info.parameter_class = (MOJOSHADER_symbolClass) readui32(&typeptr, &typelen);
const uint32 memname = readui32(&typeptr, &typelen);
/*const uint32 memsemantic =*/ readui32(&typeptr, &typelen);
mem->name = readstring(base, memname, m, d);
mem->info.elements = readui32(&typeptr, &typelen);
mem->info.columns = readui32(&typeptr, &typelen);
mem->info.rows = readui32(&typeptr, &typelen);
// !!! FIXME: Nested structs! -flibit
assert(mem->info.parameter_class >= MOJOSHADER_SYMCLASS_SCALAR
390
&& mem->info.parameter_class <= MOJOSHADER_SYMCLASS_MATRIX_COLUMNS);
391
392
393
394
395
assert(mem->info.parameter_type >= MOJOSHADER_SYMTYPE_BOOL
&& mem->info.parameter_type <= MOJOSHADER_SYMTYPE_FLOAT);
mem->info.member_count = 0;
mem->info.members = NULL;
396
uint32 memsize = 4 * mem->info.rows;
397
398
399
400
401
402
403
404
405
406
407
408
409
if (mem->info.elements > 0)
memsize *= mem->info.elements;
structsize += memsize;
} // for
value->type.columns = structsize;
value->type.rows = 1;
value->value_count = structsize;
if (numelements > 0)
value->value_count *= numelements;
siz = value->value_count * 4;
value->values = m(siz, d);
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
memset(value->values, '\0', siz);
int dst_offset = 0, src_offset = 0;
i = 0;
do
{
for (j = 0; j < value->type.member_count; j++)
{
siz = value->type.members[j].info.rows * value->type.members[j].info.elements;
for (k = 0; k < siz; k++)
{
memcpy(value->valuesF + dst_offset,
typeptr + src_offset, /* Yes, typeptr. -flibit */
value->type.members[j].info.columns << 2);
dst_offset += 4;
src_offset += value->type.members[j].info.columns << 2;
} // for
}
} while (++i < numelements);
428
429
430
431
432
433
434
435
436
437
438
439
440
441
} // else if
} // readvalue
static void readannotations(const uint32 numannos,
const uint8 *base,
const uint8 **ptr,
uint32 *len,
MOJOSHADER_effectAnnotation **annotations,
MOJOSHADER_effectObject *objects,
MOJOSHADER_malloc m,
void *d)
{
int i;
if (numannos == 0) return;
442
443
444
445
const uint32 siz = sizeof(MOJOSHADER_effectAnnotation) * numannos;
*annotations = (MOJOSHADER_effectAnnotation *) m(siz, d);
memset(*annotations, '\0', siz);
446
447
448
449
for (i = 0; i < numannos; i++)
{
MOJOSHADER_effectAnnotation *anno = &(*annotations)[i];
450
451
452
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
readvalue(base, typeoffset, valoffset,
anno, objects,
m, d);
} // for
} // readannotation
static void readparameters(const uint32 numparams,
const uint8 *base,
const uint8 **ptr,
uint32 *len,
MOJOSHADER_effectParam **params,
MOJOSHADER_effectObject *objects,
MOJOSHADER_malloc m,
void *d)
{
int i;
if (numparams == 0) return;
471
472
473
474
475
476
uint32 siz = sizeof(MOJOSHADER_effectParam) * numparams;
*params = (MOJOSHADER_effectParam *) m(siz, d);
memset(*params, '\0', siz);
for (i = 0; i < numparams; i++)
477
{
478
MOJOSHADER_effectParam *param = &(*params)[i];
479
480
481
482
483
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
/*const uint32 flags =*/ readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
484
485
486
487
488
param->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
¶m->annotations, objects,
m, d);
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
readvalue(base, typeoffset, valoffset,
¶m->value, objects,
m, d);
} // for
} // readparameters
static void readstates(const uint32 numstates,
const uint8 *base,
const uint8 **ptr,
uint32 *len,
MOJOSHADER_effectState **states,
MOJOSHADER_effectObject *objects,
MOJOSHADER_malloc m,
void *d)
{
int i;
if (numstates == 0) return;
507
508
509
510
511
512
513
514
const uint32 siz = sizeof (MOJOSHADER_effectState) * numstates;
*states = (MOJOSHADER_effectState *) m(siz, d);
memset(*states, '\0', siz);
for (i = 0; i < numstates; i++)
{
MOJOSHADER_effectState *state = &(*states)[i];
515
516
517
518
519
const uint32 type = readui32(ptr, len);
/*const uint32 FIXME =*/ readui32(ptr, len);
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
state->type = (MOJOSHADER_renderStateType) type;
readvalue(base, typeoffset, valoffset,
&state->value, objects,
m, d);
} // for
} // readstates
static void readpasses(const uint32 numpasses,
const uint8 *base,
const uint8 **ptr,
uint32 *len,
MOJOSHADER_effectPass **passes,
MOJOSHADER_effectObject *objects,
MOJOSHADER_malloc m,
void *d)
{
int i;
if (numpasses == 0) return;
const uint32 siz = sizeof (MOJOSHADER_effectPass) * numpasses;
*passes = (MOJOSHADER_effectPass *) m(siz, d);
memset(*passes, '\0', siz);
for (i = 0; i < numpasses; i++)
545
{
546
MOJOSHADER_effectPass *pass = &(*passes)[i];
547
548
549
550
const uint32 passnameoffset = readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
const uint32 numstates = readui32(ptr, len);
551
552
pass->name = readstring(base, passnameoffset, m, d);
553
554
555
556
557
pass->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
&pass->annotations, objects,
m, d);
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
pass->state_count = numstates;
readstates(numstates, base, ptr, len,
&pass->states, objects,
m, d);
} // for
} // readpasses
static void readtechniques(const uint32 numtechniques,
const uint8 *base,
const uint8 **ptr,
uint32 *len,
MOJOSHADER_effectTechnique **techniques,
MOJOSHADER_effectObject *objects,
MOJOSHADER_malloc m,
void *d)
{
int i;
if (numtechniques == 0) return;
577
578
579
580
const uint32 siz = sizeof (MOJOSHADER_effectTechnique) * numtechniques;
*techniques = (MOJOSHADER_effectTechnique *) m(siz, d);
memset(*techniques, '\0', siz);
581
582
583
584
for (i = 0; i < numtechniques; i++)
{
MOJOSHADER_effectTechnique *technique = &(*techniques)[i];
585
586
587
588
const uint32 nameoffset = readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
const uint32 numpasses = readui32(ptr, len);
589
590
technique->name = readstring(base, nameoffset, m, d);
591
592
593
594
595
technique->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
&technique->annotations, objects,
m, d);
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
technique->pass_count = numpasses;
readpasses(numpasses, base, ptr, len,
&technique->passes, objects,
m, d);
} // for
} // readtechniques
static void readsmallobjects(const uint32 numsmallobjects,
const uint8 **ptr,
uint32 *len,
MOJOSHADER_effect *effect,
const MOJOSHADER_swizzle *swiz,
const unsigned int swizcount,
const MOJOSHADER_samplerMap *smap,
611
const unsigned int smapcount)
612
613
{
int i, j;
614
615
616
617
MOJOSHADER_parseData *pd;
MOJOSHADER_malloc m = effect->ctx.m;
void *d = effect->ctx.malloc_data;
618
if (numsmallobjects == 0) return;
619
620
621
622
623
for (i = 1; i < numsmallobjects + 1; i++)
{
const uint32 index = readui32(ptr, len);
const uint32 length = readui32(ptr, len);
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
MOJOSHADER_effectObject *object = &effect->objects[index];
if (object->type == MOJOSHADER_SYMTYPE_STRING)
{
if (length > 0)
{
char *str = (char *) m(length, d);
memcpy(str, *ptr, length);
object->string.string = str;
} // if
} // if
else if (object->type == MOJOSHADER_SYMTYPE_TEXTURE
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE1D
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE2D
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE3D
639
640
|| object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
{
if (length > 0)
{
char *str = (char *) m(length, d);
memcpy(str, *ptr, length);
object->mapping.name = str;
} // if
} // else if
else if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
{
656
char mainfn[32];
657
snprintf(mainfn, sizeof(mainfn), "ShaderFunction%u", (unsigned int) index);
658
659
660
object->shader.technique = -1;
object->shader.pass = -1;
// !!! FIXME: check for errors.
661
662
663
664
665
666
667
object->shader.shader = effect->ctx.compileShader(mainfn, *ptr, length,
swiz, swizcount,
smap, smapcount);
pd = effect->ctx.getParseData(object->shader.shader);
for (j = 0; j < pd->symbol_count; j++)
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
668
object->shader.sampler_count++;
669
object->shader.param_count = pd->symbol_count;
670
671
672
object->shader.params = (uint32 *) m(object->shader.param_count * sizeof (uint32), d);
object->shader.samplers = (MOJOSHADER_samplerStateRegister *) m(object->shader.sampler_count * sizeof (MOJOSHADER_samplerStateRegister), d);
uint32 curSampler = 0;
673
for (j = 0; j < pd->symbol_count; j++)
674
675
676
{
int par = findparameter(effect->params,
effect->param_count,
677
pd->symbols[j].name);
678
object->shader.params[j] = par;
679
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
680
{
681
object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name;
682
object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index;
683
684
685
686
687
object->shader.samplers[curSampler].sampler_state_count = effect->params[par].value.value_count;
object->shader.samplers[curSampler].sampler_states = effect->params[par].value.valuesSS;
curSampler++;
} // if
} // for
688
if (pd->preshader)
689
{
690
object->shader.preshader_param_count = pd->preshader->symbol_count;
691
object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
692
for (j = 0; j < pd->preshader->symbol_count; j++)
693
694
695
{
object->shader.preshader_params[j] = findparameter(effect->params,
effect->param_count,
696
pd->preshader->symbols[j].name);
697
698
699
700
701
702
703
} // for
} // if
} // else if
else
{
assert(0 && "Small object type unknown!");
} // else
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/* Object block is always a multiple of four */
const uint32 blocklen = (length + 3) - ((length - 1) % 4);
*ptr += blocklen;
*len -= blocklen;
} // for
} // readstrings
static void readlargeobjects(const uint32 numlargeobjects,
const uint32 numsmallobjects,
const uint8 **ptr,
uint32 *len,
MOJOSHADER_effect *effect,
const MOJOSHADER_swizzle *swiz,
const unsigned int swizcount,
const MOJOSHADER_samplerMap *smap,
720
const unsigned int smapcount)
721
722
{
int i, j;
723
724
725
726
727
MOJOSHADER_parseData *pd;
MOJOSHADER_malloc m = effect->ctx.m;
MOJOSHADER_free f = effect->ctx.f;
void *d = effect->ctx.malloc_data;
728
if (numlargeobjects == 0) return;
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
int numobjects = numsmallobjects + numlargeobjects + 1;
for (i = numsmallobjects + 1; i < numobjects; i++)
{
const uint32 technique = readui32(ptr, len);
const uint32 index = readui32(ptr, len);
/*const uint32 FIXME =*/ readui32(ptr, len);
const uint32 state = readui32(ptr, len);
const uint32 type = readui32(ptr, len);
const uint32 length = readui32(ptr, len);
uint32 objectIndex;
if (technique == -1)
objectIndex = effect->params[index].value.valuesSS[state].value.valuesI[0];
else
objectIndex = effect->techniques[technique].passes[index].states[state].value.valuesI[0];
745
746
747
748
749
750
751
MOJOSHADER_effectObject *object = &effect->objects[objectIndex];
if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
{
object->shader.technique = technique;
object->shader.pass = index;
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
if (type == 2)
{
/* This is a standalone preshader!
* It exists solely for effect passes that do not use a single
* vertex/fragment shader.
*/
object->shader.is_preshader = 1;
const uint32 start = *((uint32 *) *ptr) + 4;
const char *array = readstring(*ptr, 0, m, d);
object->shader.param_count = 1;
object->shader.params = (uint32 *) m(sizeof (uint32), d);
object->shader.params[0] = findparameter(effect->params,
effect->param_count,
array);
f((void *) array, d);
768
object->shader.preshader = MOJOSHADER_parsePreshader(*ptr + start, length,
769
770
771
772
773
774
775
776
777
778
779
780
781
m, f, d);
// !!! FIXME: check for errors.
object->shader.preshader_param_count = object->shader.preshader->symbol_count;
object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
for (j = 0; j < object->shader.preshader->symbol_count; j++)
{
object->shader.preshader_params[j] = findparameter(effect->params,
effect->param_count,
object->shader.preshader->symbols[j].name);
} // for
} // if
else
{
782
783
char mainfn[32];
snprintf(mainfn, sizeof (mainfn), "ShaderFunction%u", (unsigned int) objectIndex);
784
// !!! FIXME: check for errors.
785
786
787
788
789
790
791
object->shader.shader = effect->ctx.compileShader(mainfn, *ptr, length,
swiz, swizcount,
smap, smapcount);
pd = effect->ctx.getParseData(object->shader.shader);
for (j = 0; j < pd->symbol_count; j++)
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
792
object->shader.sampler_count++;
793
object->shader.param_count = pd->symbol_count;
794
795
796
object->shader.params = (uint32 *) m(object->shader.param_count * sizeof (uint32), d);
object->shader.samplers = (MOJOSHADER_samplerStateRegister *) m(object->shader.sampler_count * sizeof (MOJOSHADER_samplerStateRegister), d);
uint32 curSampler = 0;
797
for (j = 0; j < pd->symbol_count; j++)
798
799
800
{
int par = findparameter(effect->params,
effect->param_count,
801
pd->symbols[j].name);
802
object->shader.params[j] = par;
803
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
804
{
805
object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name;
806
object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index;
807
808
809
object->shader.samplers[curSampler].sampler_state_count = effect->params[par].value.value_count;
object->shader.samplers[curSampler].sampler_states = effect->params[par].value.valuesSS;
curSampler++;
810
811
} // if
} // for
812
if (pd->preshader)
813
{
814
object->shader.preshader_param_count = pd->preshader->symbol_count;
815
object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
816
for (j = 0; j < pd->preshader->symbol_count; j++)
817
818
819
{
object->shader.preshader_params[j] = findparameter(effect->params,
effect->param_count,
820
pd->preshader->symbols[j].name);
821
822
823
824
} // for
} // if
}
} // if
825
826
827
828
829
830
else if (object->type == MOJOSHADER_SYMTYPE_TEXTURE
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE1D
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE2D
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE3D
|| object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER
831
832
833
834
835
836
837
838
839
840
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
{
if (length > 0)
{
char *str = (char *) m(length, d);
memcpy(str, *ptr, length);
object->mapping.name = str;
841
} // if
842
843
844
845
846
} // else if
else if (object->type != MOJOSHADER_SYMTYPE_VOID) // FIXME: Why? -flibit
{
assert(0 && "Large object type unknown!");
} // else
847
848
849
850
851
852
853
854
/* Object block is always a multiple of four */
const uint32 blocklen = (length + 3) - ((length - 1) % 4);
*ptr += blocklen;
*len -= blocklen;
} // for
} // readobjects
855
856
857
858
859
860
861
MOJOSHADER_effect *MOJOSHADER_compileEffect(const unsigned char *buf,
const unsigned int _len,
const MOJOSHADER_swizzle *swiz,
const unsigned int swizcount,
const MOJOSHADER_samplerMap *smap,
const unsigned int smapcount,
const MOJOSHADER_effectShaderContext *ctx)
862
863
864
{
const uint8 *ptr = (const uint8 *) buf;
uint32 len = (uint32) _len;
865
866
867
868
869
870
871
MOJOSHADER_malloc m;
MOJOSHADER_free f;
void *d;
/* Need a backend! */
if (ctx == NULL)
return &MOJOSHADER_out_of_mem_effect;
872
873
/* Supply both m and f, or neither */
874
875
if ( ((ctx->m == NULL) && (ctx->f != NULL))
|| ((ctx->m != NULL) && (ctx->f == NULL)) )
876
877
878
return &MOJOSHADER_out_of_mem_effect;
/* Use default malloc/free if m/f were not passed */
879
880
881
882
883
884
885
886
887
if (ctx->m == NULL)
m = MOJOSHADER_internal_malloc;
else
m = ctx->m;
if (ctx->f == NULL)
f = MOJOSHADER_internal_free;
else
f = ctx->f;
d = ctx->malloc_data;
888
889
/* malloc base effect structure */
890
891
MOJOSHADER_effect *retval = (MOJOSHADER_effect *) m(sizeof (MOJOSHADER_effect),
ctx->malloc_data);
892
893
894
895
if (retval == NULL)
return &MOJOSHADER_out_of_mem_effect;
memset(retval, '\0', sizeof (*retval));
896
897
898
899
/* Store ctx in effect structure */
memcpy(&retval->ctx, ctx, sizeof(MOJOSHADER_effectShaderContext));
retval->ctx.m = m;
retval->ctx.f = f;
900
901
902
903
if (len < 8)
goto parseEffect_unexpectedEOF;
904
905
906
907
/* Read in header magic, seek to initial offset */
const uint8 *base = NULL;
uint32 header = readui32(&ptr, &len);
if (header == 0xBCF00BCF)
908
{
909
910
911
912
913
914
915
916
/* The Effect compiler provided with XNA4 adds some extra mess at the
* beginning of the file. It's useless though, so just skip it.
* -flibit
*/
const uint32 skip = readui32(&ptr, &len) - 8;
ptr += skip;
len += skip;
header = readui32(&ptr, &len);
917
} // if
918
919
920
921
922
923
924
925
926
927
928
929
930
931
if (header != 0xFEFF0901)
goto parseEffect_notAnEffectsFile;
else
{
const uint32 offset = readui32(&ptr, &len);
base = ptr;
if (offset > len)
goto parseEffect_unexpectedEOF;
ptr += offset;
len -= offset;
} // else
if (len < 16)
goto parseEffect_unexpectedEOF;
932
933
934
935
936
937
938
939
940
941
942
943
944
945
/* Parse structure counts */
const uint32 numparams = readui32(&ptr, &len);
const uint32 numtechniques = readui32(&ptr, &len);
/*const uint32 FIXME =*/ readui32(&ptr, &len);
const uint32 numobjects = readui32(&ptr, &len);
/* Alloc structures now, so object types can be stored */
retval->object_count = numobjects;
const uint32 siz = sizeof (MOJOSHADER_effectObject) * numobjects;
retval->objects = (MOJOSHADER_effectObject *) m(siz, d);
if (retval->objects == NULL)
goto parseEffect_outOfMemory;
memset(retval->objects, '\0', siz);
946
947
948
949
950
951
/* Parse effect parameters */
retval->param_count = numparams;
readparameters(numparams, base, &ptr, &len,
&retval->params, retval->objects,
m, d);
952
953
954
955
956
957
/* Parse effect techniques */
retval->technique_count = numtechniques;
readtechniques(numtechniques, base, &ptr, &len,
&retval->techniques, retval->objects,
m, d);
958
959
960
961
/* Initial effect technique/pass */
retval->current_technique = &retval->techniques[0];
retval->current_pass = -1;
962
963
964
if (len < 8)
goto parseEffect_unexpectedEOF;
965
966
967
968
/* Parse object counts */
const int numsmallobjects = readui32(&ptr, &len);
const int numlargeobjects = readui32(&ptr, &len);
969
970
/* Parse "small" object table */
971
972
readsmallobjects(numsmallobjects, &ptr, &len, retval,
swiz, swizcount, smap, smapcount);
973
974
/* Parse "large" object table. */
975
976
readlargeobjects(numlargeobjects, numsmallobjects, &ptr, &len, retval,
swiz, swizcount, smap, smapcount);
977
978
979
980
981
982
983
return retval;
// !!! FIXME: do something with this.
parseEffect_notAnEffectsFile:
parseEffect_unexpectedEOF:
parseEffect_outOfMemory:
984
MOJOSHADER_deleteEffect(retval);
985
986
987
988
return &MOJOSHADER_out_of_mem_effect;
} // MOJOSHADER_parseEffect
989
990
991
992
993
994
995
996
997
998
999
1000
void freetypeinfo(MOJOSHADER_symbolTypeInfo *typeinfo,
MOJOSHADER_free f, void *d)
{
int i;
for (i = 0; i < typeinfo->member_count; i++)
{
f((void *) typeinfo->members[i].name, d);
freetypeinfo(&typeinfo->members[i].info, f, d);
} // for
f((void *) typeinfo->members, d);
} // freetypeinfo