/
mojoshader_effects.c
622 lines (526 loc) · 22 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
#include <math.h>
15
16
#if SUPPORT_PRESHADERS
void MOJOSHADER_runPreshader(const MOJOSHADER_preshader *preshader,
17
const float *inregs, float *outregs)
18
19
20
21
22
{
// 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;
23
24
25
26
27
28
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
29
30
31
32
33
34
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];
35
36
37
38
39
40
MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
int instit;
for (instit = 0; instit < preshader->instruction_count; instit++, inst++)
{
41
const MOJOSHADER_preshaderOperand *operand = inst->operands;
42
43
const int elems = inst->element_count;
const int elemsbytes = sizeof (double) * elems;
44
const int isscalarop = (inst->opcode >= scalarstart);
45
46
47
48
assert(elems >= 0);
assert(elems <= 4);
49
50
// load up our operands...
int opiter, elemiter;
51
for (opiter = 0; opiter < inst->operand_count-1; opiter++, operand++)
52
{
53
const int isscalar = ((isscalarop) && (opiter == 0));
54
55
56
57
58
59
const unsigned int index = operand->index;
switch (operand->type)
{
case MOJOSHADER_PRESHADEROPERAND_LITERAL:
{
const double *lit = &preshader->literals[index];
60
assert((index + elems) <= preshader->literal_count);
61
if (!isscalar)
62
memcpy(&src[opiter][0], lit, elemsbytes);
63
64
65
66
67
68
69
70
71
72
else
{
const double val = *lit;
for (elemiter = 0; elemiter < elems; elemiter++)
src[opiter][elemiter] = val;
} // else
break;
} // case
case MOJOSHADER_PRESHADEROPERAND_INPUT:
73
74
75
76
77
78
79
80
81
82
if (isscalar)
src[opiter][0] = inregs[index];
else
{
int cpy;
for (cpy = 0; cpy < elems; cpy++)
src[opiter][cpy] = inregs[index+cpy];
} // else
break;
83
84
case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
if (isscalar)
85
src[opiter][0] = outregs[index];
86
87
88
89
else
{
int cpy;
for (cpy = 0; cpy < elems; cpy++)
90
src[opiter][cpy] = outregs[index+cpy];
91
92
93
94
} // else
break;
case MOJOSHADER_PRESHADEROPERAND_TEMP:
95
96
97
98
99
100
101
if (temps != NULL)
{
if (isscalar)
src[opiter][0] = temps[index];
else
memcpy(src[opiter], temps + index, elemsbytes);
} // if
102
103
104
105
break;
default:
assert(0 && "unexpected preshader operand type.");
106
return;
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
} // 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
143
144
145
146
147
148
149
150
151
152
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: ?
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#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?
} // case
default:
assert(0 && "Unhandled preshader opcode!");
break;
} // switch
// Figure out where dst wants to be stored.
if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
171
172
173
{
assert(preshader->temp_count >=
operand->index + (elemsbytes / sizeof (double)));
174
memcpy(temps + operand->index, dst, elemsbytes);
175
} // if
176
177
178
179
else
{
assert(operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT);
for (i = 0; i < elems; i++)
180
outregs[operand->index + i] = (float) dst[i];
181
182
} // else
} // for
183
184
} // MOJOSHADER_runPreshader
#endif
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
// !!! FIXME: this is sort of a big, ugly function.
const MOJOSHADER_effect *MOJOSHADER_parseEffect(const char *profile,
const unsigned char *buf,
const unsigned int _len,
const MOJOSHADER_swizzle *swiz,
const unsigned int swizcount,
MOJOSHADER_malloc m,
MOJOSHADER_free f,
void *d)
{
if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) )
return &MOJOSHADER_out_of_mem_effect; // supply both or neither.
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;
MOJOSHADER_effect *retval = m(sizeof (MOJOSHADER_effect), d);
if (retval == NULL)
return &MOJOSHADER_out_of_mem_effect; // supply both or neither.
memset(retval, '\0', sizeof (*retval));
retval->malloc = m;
retval->free = f;
retval->malloc_data = d;
const uint8 *ptr = (const uint8 *) buf;
uint32 len = (uint32) _len;
size_t siz = 0;
int i, j, k;
if (len < 8)
goto parseEffect_unexpectedEOF;
238
const uint8 *base = NULL;
239
240
241
242
243
if (readui32(&ptr, &len) != 0xFEFF0901) // !!! FIXME: is this always magic?
goto parseEffect_notAnEffectsFile;
else
{
const uint32 offset = readui32(&ptr, &len);
244
245
base = ptr;
//printf("base offset == %u\n", offset);
246
247
248
249
250
251
252
253
254
255
256
257
258
259
if (offset > len)
goto parseEffect_unexpectedEOF;
ptr += offset;
len -= offset;
} // else
// params...
if (len < 16)
goto parseEffect_unexpectedEOF;
const uint32 numparams = readui32(&ptr, &len);
const uint32 numtechniques = readui32(&ptr, &len);
260
261
readui32(&ptr, &len); // !!! FIXME: there are 8 unknown bytes here. Annotations?
/*const uint32 numobjects = */ readui32(&ptr, &len);
262
263
if (numparams > 0)
264
{
265
266
267
268
269
siz = sizeof (MOJOSHADER_effectParam) * numparams;
retval->params = (MOJOSHADER_effectParam *) m(siz, d);
if (retval->params == NULL)
goto parseEffect_outOfMemory;
memset(retval->params, '\0', siz);
270
271
272
273
retval->param_count = numparams;
for (i = 0; i < numparams; i++)
274
{
275
if (len < 16)
276
goto parseEffect_unexpectedEOF;
277
278
const uint32 typeoffset = readui32(&ptr, &len);
279
280
/*const uint32 valoffset =*/ readui32(&ptr, &len);
/*const uint32 flags =*/ readui32(&ptr, &len);
281
282
283
284
285
286
287
288
289
290
291
292
const uint32 numannos = readui32(&ptr, &len);
for (j = 0; j < numannos; j++)
{
if (len < 8)
goto parseEffect_unexpectedEOF;
// !!! FIXME: parse annotations.
readui32(&ptr, &len);
readui32(&ptr, &len);
} // for
const uint8 *typeptr = base + typeoffset;
unsigned int typelen = 9999999; // !!! FIXME
293
294
/*const uint32 paramtype =*/ readui32(&typeptr, &typelen);
/*const uint32 paramclass =*/ readui32(&typeptr, &typelen);
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const uint32 paramname = readui32(&typeptr, &typelen);
const uint32 paramsemantic = readui32(&typeptr, &typelen);
// !!! FIXME: sanity checks!
const char *namestr = ((const char *) base) + paramname;
const char *semstr = ((const char *) base) + paramsemantic;
uint32 len;
char *strptr;
len = *((const uint32 *) namestr);
strptr = (char *) m(len + 1, d);
memcpy(strptr, namestr + 4, len);
strptr[len] = '\0';
retval->params[i].name = strptr;
len = *((const uint32 *) semstr);
strptr = (char *) m(len + 1, d);
memcpy(strptr, semstr + 4, len);
strptr[len] = '\0';
retval->params[i].semantic = strptr;
313
} // for
314
} // if
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
uint32 numshaders = 0; // we'll calculate this later.
// techniques...
if (numtechniques > 0)
{
siz = sizeof (MOJOSHADER_effectTechnique) * numtechniques;
retval->techniques = (MOJOSHADER_effectTechnique *) m(siz, d);
if (retval->techniques == NULL)
goto parseEffect_outOfMemory;
memset(retval->techniques, '\0', siz);
retval->technique_count = numtechniques;
for (i = 0; i < numtechniques; i++)
{
if (len < 12)
goto parseEffect_unexpectedEOF;
MOJOSHADER_effectTechnique *technique = &retval->techniques[i];
337
338
const uint32 nameoffset = readui32(&ptr, &len);
const uint32 numannos = readui32(&ptr, &len);
339
340
341
342
343
const uint32 numpasses = readui32(&ptr, &len);
if (nameoffset >= _len)
goto parseEffect_unexpectedEOF;
344
if (numannos > 0)
345
{
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// !!! FIXME: expose these to the caller?
for (j = 0; j < numannos; j++)
{
if (len < 8)
goto parseEffect_unexpectedEOF;
readui32(&ptr, &len); // typedef offset
readui32(&ptr, &len); // value offset
} // for
} // if
// !!! FIXME: verify this doesn't go past EOF looking for a null.
{
const char *namestr = ((char *) base) + nameoffset;
uint32 len = *((const uint32 *) namestr);
char *strptr = (char *) m(len + 1, d);
memcpy(strptr, namestr + 4, len);
strptr[len] = '\0';
technique->name = strptr;
}
365
366
367
if (numpasses > 0)
{
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
technique->pass_count = numpasses;
siz = sizeof (MOJOSHADER_effectPass) * numpasses;
technique->passes = (MOJOSHADER_effectPass *) m(siz, d);
if (technique->passes == NULL)
goto parseEffect_outOfMemory;
memset(technique->passes, '\0', siz);
for (j = 0; j < numpasses; j++)
{
if (len < 12)
goto parseEffect_unexpectedEOF;
MOJOSHADER_effectPass *pass = &technique->passes[j];
383
384
const uint32 passnameoffset = readui32(&ptr, &len);
const uint32 numannos = readui32(&ptr, &len);
385
386
387
388
389
390
const uint32 numstates = readui32(&ptr, &len);
if (passnameoffset >= _len)
goto parseEffect_unexpectedEOF;
// !!! FIXME: verify this doesn't go past EOF looking for a null.
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
{
const char *namestr = ((char *) base) + passnameoffset;
uint32 len = *((const uint32 *) namestr);
char *strptr = (char *) m(len + 1, d);
memcpy(strptr, namestr + 4, len);
strptr[len] = '\0';
pass->name = strptr;
}
if (numannos > 0)
{
for (k = 0; k < numannos; k++)
{
if (len < 8)
goto parseEffect_unexpectedEOF;
// !!! FIXME: do something with this.
readui32(&ptr, &len);
readui32(&ptr, &len);
} // for
} // if
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
if (numstates > 0)
{
pass->state_count = numstates;
siz = sizeof (MOJOSHADER_effectState) * numstates;
pass->states = (MOJOSHADER_effectState *) m(siz, d);
if (pass->states == NULL)
goto parseEffect_outOfMemory;
memset(pass->states, '\0', siz);
for (k = 0; k < numstates; k++)
{
if (len < 16)
goto parseEffect_unexpectedEOF;
MOJOSHADER_effectState *state = &pass->states[k];
const uint32 type = readui32(&ptr, &len);
readui32(&ptr, &len); // !!! FIXME: don't know what this field does.
/*const uint32 offsetend = */ readui32(&ptr, &len);
/*const uint32 offsetstart = */ readui32(&ptr, &len);
state->type = type;
if ((type == 0x92) || (type == 0x93))
numshaders++;
} // for
} // if
} // for
} // if
} // for
} // if
// textures...
if (len < 8)
goto parseEffect_unexpectedEOF;
const int numtextures = readui32(&ptr, &len);
const int numobjects = readui32(&ptr, &len); // !!! FIXME: "objects" for lack of a better word.
if (numtextures > 0)
{
siz = sizeof (MOJOSHADER_effectTexture) * numtextures;
retval->textures = m(siz, d);
if (retval->textures == NULL)
goto parseEffect_outOfMemory;
memset(retval->textures, '\0', siz);
for (i = 0; i < numtextures; i++)
{
if (len < 8)
goto parseEffect_unexpectedEOF;
MOJOSHADER_effectTexture *texture = &retval->textures[i];
const uint32 texparam = readui32(&ptr, &len);
const uint32 texsize = readui32(&ptr, &len);
// apparently texsize will pad out to 32 bits.
const uint32 readsize = (((texsize + 3) / 4) * 4);
if (len < readsize)
goto parseEffect_unexpectedEOF;
texture->param = texparam;
char *str = m(texsize + 1, d);
if (str == NULL)
goto parseEffect_outOfMemory;
memcpy(str, ptr, texsize);
str[texsize] = '\0';
texture->name = str;
ptr += readsize;
len -= readsize;
} // for
} // if
// shaders...
if (numshaders > 0)
{
siz = sizeof (MOJOSHADER_effectShader) * numshaders;
retval->shaders = (MOJOSHADER_effectShader *) m(siz, d);
if (retval->shaders == NULL)
goto parseEffect_outOfMemory;
memset(retval->shaders, '\0', siz);
retval->shader_count = numshaders;
// !!! FIXME: I wonder if we should pull these from offsets and not
// !!! FIXME: count on them all being in a line like this.
for (i = 0; i < numshaders; i++)
{
if (len < 24)
goto parseEffect_unexpectedEOF;
MOJOSHADER_effectShader *shader = &retval->shaders[i];
const uint32 technique = readui32(&ptr, &len);
const uint32 pass = readui32(&ptr, &len);
readui32(&ptr, &len); // !!! FIXME: don't know what this does.
readui32(&ptr, &len); // !!! FIXME: don't know what this does (vertex/pixel/geometry?)
readui32(&ptr, &len); // !!! FIXME: don't know what this does.
const uint32 shadersize = readui32(&ptr, &len);
if (len < shadersize)
goto parseEffect_unexpectedEOF;
shader->technique = technique;
shader->pass = pass;
shader->shader = MOJOSHADER_parse(profile, ptr, shadersize,
swiz, swizcount, m, f, d);
// !!! FIXME: check for errors.
ptr += shadersize;
len -= shadersize;
} // for
} // if
// !!! FIXME: we parse this, but don't expose the data, yet.
// mappings ...
assert(numshaders <= numobjects);
const uint32 nummappings = numobjects - numshaders;
if (nummappings > 0)
{
for (i = 0; i < nummappings; i++)
{
if (len < 24)
goto parseEffect_unexpectedEOF;
/*const uint32 magic = */ readui32(&ptr, &len);
/*const uint32 index = */ readui32(&ptr, &len);
readui32(&ptr, &len); // !!! FIXME: what is this field?
readui32(&ptr, &len); // !!! FIXME: what is this field?
/*const uint32 type = */ readui32(&ptr, &len);
const uint32 mapsize = readui32(&ptr, &len);
if (mapsize > 0)
{
const uint32 readsize = (((mapsize + 3) / 4) * 4);
if (len < readsize)
goto parseEffect_unexpectedEOF;
} // if
} // for
} // if
retval->profile = (char *) m(strlen(profile) + 1, d);
if (retval->profile == NULL)
goto parseEffect_outOfMemory;
strcpy((char *) retval->profile, profile);
return retval;
// !!! FIXME: do something with this.
parseEffect_notAnEffectsFile:
parseEffect_unexpectedEOF:
parseEffect_outOfMemory:
MOJOSHADER_freeEffect(retval);
return &MOJOSHADER_out_of_mem_effect;
} // MOJOSHADER_parseEffect
void MOJOSHADER_freeEffect(const MOJOSHADER_effect *_effect)
{
MOJOSHADER_effect *effect = (MOJOSHADER_effect *) _effect;
if ((effect == NULL) || (effect == &MOJOSHADER_out_of_mem_effect))
return; // no-op.
MOJOSHADER_free f = effect->free;
void *d = effect->malloc_data;
int i, j;
for (i = 0; i < effect->error_count; i++)
{
f((void *) effect->errors[i].error, d);
f((void *) effect->errors[i].filename, d);
} // for
f((void *) effect->errors, d);
f((void *) effect->profile, d);
589
590
591
592
593
594
595
for (i = 0; i < effect->param_count; i++)
{
f((void *) effect->params[i].name, d);
f((void *) effect->params[i].semantic, d);
} // for
f(effect->params, d);
596
597
598
for (i = 0; i < effect->technique_count; i++)
{
MOJOSHADER_effectTechnique *technique = &effect->techniques[i];
599
f((void *) technique->name, d);
600
for (j = 0; j < technique->pass_count; j++)
601
602
{
f((void *) technique->passes[j].name, d);
603
f(technique->passes[j].states, d);
604
} // for
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
f(technique->passes, d);
} // for
f(effect->techniques, d);
for (i = 0; i < effect->texture_count; i++)
f((void *) effect->textures[i].name, d);
f(effect->textures, d);
for (i = 0; i < effect->shader_count; i++)
MOJOSHADER_freeParseData(effect->shaders[i].shader);
f(effect->shaders, d);
f(effect, d);
} // MOJOSHADER_freeEffect
// end of mojoshader_effects.c ...