Skip to content

Latest commit

 

History

History
1935 lines (1718 loc) · 74.8 KB

mojoshader_effects.c

File metadata and controls

1935 lines (1718 loc) · 74.8 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
19
void MOJOSHADER_runPreshader(const MOJOSHADER_preshader *preshader,
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
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);
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++)
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)
113
114
115
116
else
{
int cpy;
for (cpy = 0; cpy < elems; 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.");
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?
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);
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];
212
213
214
215
static MOJOSHADER_effect MOJOSHADER_out_of_mem_effect = {
1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
static MOJOSHADER_error MOJOSHADER_need_a_backend_error = {
"Need a MOJOSHADER_effectShaderContext", NULL, MOJOSHADER_POSITION_NONE
};
static MOJOSHADER_effect MOJOSHADER_need_a_backend_effect = {
1, &MOJOSHADER_need_a_backend_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static MOJOSHADER_error MOJOSHADER_unexpected_eof_error = {
"Unexpected EOF", NULL, MOJOSHADER_POSITION_NONE
};
static MOJOSHADER_effect MOJOSHADER_unexpected_eof_effect = {
1, &MOJOSHADER_unexpected_eof_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static MOJOSHADER_error MOJOSHADER_not_an_effect_error = {
"Not an Effects Framework binary", NULL, MOJOSHADER_POSITION_NONE
};
static MOJOSHADER_effect MOJOSHADER_not_an_effect_effect = {
1, &MOJOSHADER_not_an_effect_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static void push_errors(ErrorList *list, MOJOSHADER_error *errors, int len)
{
int i;
for (i = 0; i < len; i += 1)
errorlist_add(list, errors[i].filename, errors[i].error_position, errors[i].error);
} // push_errors
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
257
258
259
260
static char *readstring(const uint8 *base,
const uint32 offset,
MOJOSHADER_malloc m,
void *d)
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// !!! 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!");
283
284
285
286
287
288
289
290
291
292
}
static void readvalue(const uint8 *base,
const uint32 typeoffset,
const uint32 valoffset,
MOJOSHADER_effectValue *value,
MOJOSHADER_effectObject *objects,
MOJOSHADER_malloc m,
void *d)
{
294
295
296
297
298
299
300
301
302
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);
303
304
value->type.parameter_type = (MOJOSHADER_symbolType) type;
value->type.parameter_class = (MOJOSHADER_symbolClass) valclass;
305
306
value->name = readstring(base, name, m, d);
value->semantic = readstring(base, semantic, m, d);
307
value->type.elements = numelements;
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* 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);
323
324
value->type.columns = columncount;
value->type.rows = rowcount;
327
328
329
330
331
if (numelements > 0)
siz *= numelements;
value->value_count = siz;
siz *= 4;
value->values = m(siz, d);
332
333
334
335
memset(value->values, '\0', siz);
siz /= 16;
for (i = 0; i < siz; i++)
memcpy(value->valuesF + (i << 2), valptr + ((columncount << 2) * i), columncount << 2);
336
337
338
339
340
341
342
343
344
345
346
347
348
349
} // 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);
353
354
355
const uint32 siz = sizeof(MOJOSHADER_effectSamplerState) * numstates;
value->values = m(siz, d);
memset(value->values, '\0', siz);
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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;
379
value->value_count = numobjects;
381
382
383
const uint32 siz = 4 * numobjects;
value->values = m(siz, d);
memcpy(value->values, valptr, siz);
385
386
387
388
389
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)
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
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
415
&& mem->info.parameter_class <= MOJOSHADER_SYMCLASS_MATRIX_COLUMNS);
416
417
418
419
420
assert(mem->info.parameter_type >= MOJOSHADER_SYMTYPE_BOOL
&& mem->info.parameter_type <= MOJOSHADER_SYMTYPE_FLOAT);
mem->info.member_count = 0;
mem->info.members = NULL;
422
423
424
425
426
427
428
429
430
431
432
433
434
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);
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
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);
453
454
455
456
457
458
459
460
461
462
463
464
465
466
} // 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;
468
469
470
const uint32 siz = sizeof(MOJOSHADER_effectAnnotation) * numannos;
*annotations = (MOJOSHADER_effectAnnotation *) m(siz, d);
memset(*annotations, '\0', siz);
472
473
474
for (i = 0; i < numannos; i++)
{
MOJOSHADER_effectAnnotation *anno = &(*annotations)[i];
476
477
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
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;
497
498
499
500
501
uint32 siz = sizeof(MOJOSHADER_effectParam) * numparams;
*params = (MOJOSHADER_effectParam *) m(siz, d);
memset(*params, '\0', siz);
for (i = 0; i < numparams; i++)
503
MOJOSHADER_effectParam *param = &(*params)[i];
505
506
507
508
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
/*const uint32 flags =*/ readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
510
511
512
513
param->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
&param->annotations, objects,
m, d);
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
readvalue(base, typeoffset, valoffset,
&param->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;
533
534
535
536
537
538
539
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];
541
542
543
544
const uint32 type = readui32(ptr, len);
/*const uint32 FIXME =*/ readui32(ptr, len);
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
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++)
571
MOJOSHADER_effectPass *pass = &(*passes)[i];
573
574
575
const uint32 passnameoffset = readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
const uint32 numstates = readui32(ptr, len);
577
pass->name = readstring(base, passnameoffset, m, d);
579
580
581
582
pass->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
&pass->annotations, objects,
m, d);
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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;
603
604
605
const uint32 siz = sizeof (MOJOSHADER_effectTechnique) * numtechniques;
*techniques = (MOJOSHADER_effectTechnique *) m(siz, d);
memset(*techniques, '\0', siz);
607
608
609
for (i = 0; i < numtechniques; i++)
{
MOJOSHADER_effectTechnique *technique = &(*techniques)[i];
611
612
613
const uint32 nameoffset = readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
const uint32 numpasses = readui32(ptr, len);
615
technique->name = readstring(base, nameoffset, m, d);
617
618
619
620
technique->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
&technique->annotations, objects,
m, d);
622
623
624
625
626
627
628
629
630
631
632
633
634
635
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,
636
637
const unsigned int smapcount,
ErrorList *errors)
640
641
642
643
MOJOSHADER_parseData *pd;
MOJOSHADER_malloc m = effect->ctx.m;
void *d = effect->ctx.malloc_data;
644
if (numsmallobjects == 0) return;
646
647
648
649
for (i = 1; i < numsmallobjects + 1; i++)
{
const uint32 index = readui32(ptr, len);
const uint32 length = readui32(ptr, len);
651
652
653
654
655
656
657
658
659
660
661
662
663
664
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
665
666
|| object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
|| 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)
{
683
snprintf(mainfn, sizeof(mainfn), "ShaderFunction%u", (unsigned int) index);
684
685
object->shader.technique = -1;
object->shader.pass = -1;
686
687
688
object->shader.shader = effect->ctx.compileShader(mainfn, *ptr, length,
swiz, swizcount,
smap, smapcount);
689
690
691
692
693
694
if (object->shader.shader == NULL)
{
// Bail ASAP, so we can get the error to the application
errorlist_add(errors, NULL, 0, effect->ctx.getError());
return;
} // if
695
pd = effect->ctx.getParseData(object->shader.shader);
696
697
698
699
700
701
if (pd->error_count > 0)
{
// Bail ASAP, so we can get the error to the application
push_errors(errors, pd->errors, pd->error_count);
return;
} // if
702
703
704
for (j = 0; j < pd->symbol_count; j++)
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
706
object->shader.param_count = pd->symbol_count;
707
708
709
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;
710
for (j = 0; j < pd->symbol_count; j++)
711
712
713
{
int par = findparameter(effect->params,
effect->param_count,
714
pd->symbols[j].name);
716
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
718
object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name;
719
object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index;
720
721
722
723
724
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
727
object->shader.preshader_param_count = pd->preshader->symbol_count;
728
object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
729
for (j = 0; j < pd->preshader->symbol_count; j++)
730
731
732
{
object->shader.preshader_params[j] = findparameter(effect->params,
effect->param_count,
733
pd->preshader->symbols[j].name);
734
735
736
737
738
739
740
} // for
} // if
} // else if
else
{
assert(0 && "Small object type unknown!");
} // else
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* 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,
757
758
const unsigned int smapcount,
ErrorList *errors)
761
762
763
764
765
MOJOSHADER_parseData *pd;
MOJOSHADER_malloc m = effect->ctx.m;
MOJOSHADER_free f = effect->ctx.f;
void *d = effect->ctx.malloc_data;
766
if (numlargeobjects == 0) return;
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
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];
784
785
786
787
788
789
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;
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
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);
806
object->shader.preshader = MOJOSHADER_parsePreshader(*ptr + start, length,
807
808
809
810
811
812
813
814
815
816
817
818
819
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
{
820
821
char mainfn[32];
snprintf(mainfn, sizeof (mainfn), "ShaderFunction%u", (unsigned int) objectIndex);
822
823
824
object->shader.shader = effect->ctx.compileShader(mainfn, *ptr, length,
swiz, swizcount,
smap, smapcount);
825
826
827
828
829
830
if (object->shader.shader == NULL)
{
// Bail ASAP, so we can get the error to the application
errorlist_add(errors, NULL, 0, effect->ctx.getError());
return;
} // if
831
pd = effect->ctx.getParseData(object->shader.shader);
832
833
834
835
836
837
if (pd->error_count > 0)
{
// Bail ASAP, so we can get the error to the application
push_errors(errors, pd->errors, pd->error_count);
return;
} // if
838
839
840
for (j = 0; j < pd->symbol_count; j++)
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
842
object->shader.param_count = pd->symbol_count;
843
844
845
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;
846
for (j = 0; j < pd->symbol_count; j++)
847
848
849
{
int par = findparameter(effect->params,
effect->param_count,
850
pd->symbols[j].name);
852
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
854
object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name;
855
object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index;
856
857
858
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++;
863
object->shader.preshader_param_count = pd->preshader->symbol_count;
864
object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
865
for (j = 0; j < pd->preshader->symbol_count; j++)
866
867
868
{
object->shader.preshader_params[j] = findparameter(effect->params,
effect->param_count,
869
pd->preshader->symbols[j].name);
870
871
872
873
} // for
} // if
}
} // if
874
875
876
877
878
879
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
880
881
882
883
884
885
886
887
888
889
|| 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;
891
892
893
894
895
} // else if
else if (object->type != MOJOSHADER_SYMTYPE_VOID) // FIXME: Why? -flibit
{
assert(0 && "Large object type unknown!");
} // else
897
898
899
900
901
902
903
/* Object block is always a multiple of four */
const uint32 blocklen = (length + 3) - ((length - 1) % 4);
*ptr += blocklen;
*len -= blocklen;
} // for
} // readobjects
904
905
906
907
908
909
910
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)
911
912
913
{
const uint8 *ptr = (const uint8 *) buf;
uint32 len = (uint32) _len;
915
916
917
918
919
920
MOJOSHADER_malloc m;
MOJOSHADER_free f;
void *d;
/* Need a backend! */
if (ctx == NULL)
921
return &MOJOSHADER_need_a_backend_effect;
922
923
/* Supply both m and f, or neither */
924
925
if ( ((ctx->m == NULL) && (ctx->f != NULL))
|| ((ctx->m != NULL) && (ctx->f == NULL)) )
926
927
928
return &MOJOSHADER_out_of_mem_effect;
/* Use default malloc/free if m/f were not passed */
929
930
931
932
933
934
935
936
937
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;
938
939
/* malloc base effect structure */
940
941
MOJOSHADER_effect *retval = (MOJOSHADER_effect *) m(sizeof (MOJOSHADER_effect),
ctx->malloc_data);
942
943
944
945
if (retval == NULL)
return &MOJOSHADER_out_of_mem_effect;
memset(retval, '\0', sizeof (*retval));
946
947
948
949
/* Store ctx in effect structure */
memcpy(&retval->ctx, ctx, sizeof(MOJOSHADER_effectShaderContext));
retval->ctx.m = m;
retval->ctx.f = f;
950
951
952
953
if (len < 8)
goto parseEffect_unexpectedEOF;
954
955
956
957
/* Read in header magic, seek to initial offset */
const uint8 *base = NULL;
uint32 header = readui32(&ptr, &len);
if (header == 0xBCF00BCF)
959
960
961
962
963
964
965
966
/* 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);
969
970
971
972
{
MOJOSHADER_deleteEffect(retval);
return &MOJOSHADER_not_an_effect_effect;
} // if
973
974
975
976
977
978
979
980
981
982
983
984
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;
986
987
988
989
990
991
992
993
994
995
996
997
998
/* 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);