-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmojoshader_effects.c
1969 lines (1748 loc) · 76.4 KB
/
mojoshader_effects.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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"
#ifdef MOJOSHADER_EFFECT_SUPPORT
#ifndef MOJOSHADER_USE_SDL_STDLIB
#include <math.h>
#endif /* MOJOSHADER_USE_SDL_STDLIB */
void MOJOSHADER_runPreshader(const MOJOSHADER_preshader *preshader,
float *outregs)
{
const float *inregs = preshader->registers;
// 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;
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
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];
MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
int instit;
#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
for (instit = 0; instit < preshader->instruction_count; instit++, inst++)
{
const MOJOSHADER_preshaderOperand *operand = inst->operands;
const int elems = inst->element_count;
const int elemsbytes = sizeof (double) * elems;
const int isscalarop = (inst->opcode >= scalarstart);
assert(elems >= 0);
assert(elems <= 4);
// load up our operands...
int opiter, elemiter;
for (opiter = 0; opiter < inst->operand_count-1; opiter++, operand++)
{
const int isscalar = ((isscalarop) && (opiter == 0));
const unsigned int index = operand->index;
switch (operand->type)
{
case MOJOSHADER_PRESHADEROPERAND_LITERAL:
{
if (!isscalar)
{
assert((index + elems) <= preshader->literal_count);
memcpy(&src[opiter][0], &preshader->literals[index], elemsbytes);
} // if
else
{
for (elemiter = 0; elemiter < elems; elemiter++)
src[opiter][elemiter] = preshader->literals[index];
} // else
break;
} // case
case MOJOSHADER_PRESHADEROPERAND_INPUT:
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)
src[opiter][0] = inregs[index];
else
{
int cpy;
for (cpy = 0; cpy < elems; cpy++)
src[opiter][cpy] = inregs[index+cpy];
} // else
break;
case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
if (isscalar)
src[opiter][0] = outregs[index];
else
{
int cpy;
for (cpy = 0; cpy < elems; cpy++)
src[opiter][cpy] = outregs[index+cpy];
} // else
break;
case MOJOSHADER_PRESHADEROPERAND_TEMP:
if (temps != NULL)
{
if (isscalar)
src[opiter][0] = temps[index];
else
memcpy(src[opiter], temps + index, elemsbytes);
} // if
break;
default:
assert(0 && "unexpected preshader operand type.");
return;
} // 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
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: ?
#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?
break;
} // case
default:
assert(0 && "Unhandled preshader opcode!");
break;
} // switch
// Figure out where dst wants to be stored.
if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
{
assert(preshader->temp_count >=
operand->index + (elemsbytes / sizeof (double)));
memcpy(temps + operand->index, dst, elemsbytes);
} // if
else
{
assert(operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT);
for (i = 0; i < elems; i++)
outregs[operand->index + i] = (float) dst[i];
} // else
} // for
} // MOJOSHADER_runPreshader
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 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
static void read_version_token(const uint8 **_ptr, uint32 *_len, uint16 *magic, uint8 *version_major, uint8 *version_minor)
{
const uint32 *ptr = (const uint32 *) *_ptr;
const uint32 token = SWAP32(*ptr);
*_ptr += sizeof (token);
*_len -= sizeof (token);
*magic = ((token >> 16) & 0xFFFF);
*version_major = (uint8) ((token >> 8) & 0xFF);
*version_minor = (uint8) (token & 0xFF);
} // read_version_token
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
static char *readstring(const uint8 *base,
const uint32 offset,
MOJOSHADER_malloc m,
void *d)
{
// !!! FIXME: sanity checks!
// !!! FIXME: verify this doesn't go past EOF looking for a null.
uint8 *pair = (uint8 *) (base + offset);
const uint32 *lenptr = (const uint32 *) pair;
char *strptr = (char *) (pair + sizeof(uint32));
const uint32 len = SWAP32(*lenptr);
char *result = NULL;
if (len == 0) return NULL; /* No length? No string. */
result = (char *) m(len, d);
memcpy(result, strptr, len);
return result;
} // 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!");
return -1;
}
static void readvalue(const uint8 *base,
const uint32 typeoffset,
const uint32 valoffset,
MOJOSHADER_effectValue *value,
MOJOSHADER_effectObject *objects,
MOJOSHADER_malloc m,
void *d)
{
int i, j, k;
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);
value->type.parameter_type = (MOJOSHADER_symbolType) type;
value->type.parameter_class = (MOJOSHADER_symbolClass) valclass;
value->name = readstring(base, name, m, d);
value->semantic = readstring(base, semantic, m, d);
value->type.elements = numelements;
/* 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);
value->type.columns = columncount;
value->type.rows = rowcount;
uint32 siz = 4 * rowcount;
if (numelements > 0)
siz *= numelements;
value->value_count = siz;
siz *= 4;
value->values = m(siz, d);
memset(value->values, '\0', siz);
siz /= 16;
for (i = 0; i < siz; i++)
memcpy(value->valuesF + (i << 2), valptr + ((columncount << 2) * i), columncount << 2);
} // 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);
value->value_count = numstates;
const uint32 siz = sizeof(MOJOSHADER_effectSamplerState) * numstates;
value->values = m(siz, d);
memset(value->values, '\0', siz);
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;
value->value_count = numobjects;
const uint32 siz = 4 * numobjects;
value->values = m(siz, d);
memcpy(value->values, valptr, siz);
#if MOJOSHADER_BIG_ENDIAN
int valI;
for (valI=0;valI < (value->value_count);valI++) {
value->valuesI[valI] = SWAP32(value->valuesI[valI]);
}
#endif
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)
{
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
&& mem->info.parameter_class <= MOJOSHADER_SYMCLASS_MATRIX_COLUMNS);
assert(mem->info.parameter_type >= MOJOSHADER_SYMTYPE_BOOL
&& mem->info.parameter_type <= MOJOSHADER_SYMTYPE_FLOAT);
mem->info.member_count = 0;
mem->info.members = NULL;
uint32 memsize = 4 * mem->info.rows;
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);
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);
} // 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;
const uint32 siz = sizeof(MOJOSHADER_effectAnnotation) * numannos;
*annotations = (MOJOSHADER_effectAnnotation *) m(siz, d);
memset(*annotations, '\0', siz);
for (i = 0; i < numannos; i++)
{
MOJOSHADER_effectAnnotation *anno = &(*annotations)[i];
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
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;
uint32 siz = sizeof(MOJOSHADER_effectParam) * numparams;
*params = (MOJOSHADER_effectParam *) m(siz, d);
memset(*params, '\0', siz);
for (i = 0; i < numparams; i++)
{
MOJOSHADER_effectParam *param = &(*params)[i];
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
/*const uint32 flags =*/ readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
param->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
¶m->annotations, objects,
m, d);
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;
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];
const uint32 type = readui32(ptr, len);
/*const uint32 FIXME =*/ readui32(ptr, len);
const uint32 typeoffset = readui32(ptr, len);
const uint32 valoffset = readui32(ptr, len);
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++)
{
MOJOSHADER_effectPass *pass = &(*passes)[i];
const uint32 passnameoffset = readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
const uint32 numstates = readui32(ptr, len);
pass->name = readstring(base, passnameoffset, m, d);
pass->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
&pass->annotations, objects,
m, d);
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;
const uint32 siz = sizeof (MOJOSHADER_effectTechnique) * numtechniques;
*techniques = (MOJOSHADER_effectTechnique *) m(siz, d);
memset(*techniques, '\0', siz);
for (i = 0; i < numtechniques; i++)
{
MOJOSHADER_effectTechnique *technique = &(*techniques)[i];
const uint32 nameoffset = readui32(ptr, len);
const uint32 numannos = readui32(ptr, len);
const uint32 numpasses = readui32(ptr, len);
technique->name = readstring(base, nameoffset, m, d);
technique->annotation_count = numannos;
readannotations(numannos, base, ptr, len,
&technique->annotations, objects,
m, d);
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,
const unsigned int smapcount,
ErrorList *errors)
{
int i, j;
MOJOSHADER_parseData *pd;
MOJOSHADER_malloc m = effect->ctx.m;
void *d = effect->ctx.malloc_data;
if (numsmallobjects == 0) return;
for (i = 1; i < numsmallobjects + 1; i++)
{
const uint32 index = readui32(ptr, len);
const uint32 length = readui32(ptr, len);
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
|| object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER
|| 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)
{
char mainfn[32];
snprintf(mainfn, sizeof(mainfn), "ShaderFunction%u", (unsigned int) index);
object->shader.technique = -1;
object->shader.pass = -1;
object->shader.shader = effect->ctx.compileShader(effect->ctx.shaderContext,
mainfn, *ptr, length,
swiz, swizcount,
smap, smapcount);
if (object->shader.shader == NULL)
{
// Bail ASAP, so we can get the error to the application
errorlist_add(errors, NULL, 0, effect->ctx.getError(effect->ctx.shaderContext));
return;
} // if
pd = effect->ctx.getParseData(object->shader.shader);
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
for (j = 0; j < pd->symbol_count; j++)
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
object->shader.sampler_count++;
object->shader.param_count = pd->symbol_count;
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;
for (j = 0; j < pd->symbol_count; j++)
{
int par = findparameter(effect->params,
effect->param_count,
pd->symbols[j].name);
object->shader.params[j] = par;
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
{
object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name;
object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index;
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
if (pd->preshader)
{
object->shader.preshader_param_count = pd->preshader->symbol_count;
object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
for (j = 0; j < pd->preshader->symbol_count; j++)
{
object->shader.preshader_params[j] = findparameter(effect->params,
effect->param_count,
pd->preshader->symbols[j].name);
} // for
} // if
} // else if
else
{
assert(0 && "Small object type unknown!");
} // else
/* 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,
const unsigned int smapcount,
ErrorList *errors)
{
int i, j;
MOJOSHADER_parseData *pd;
MOJOSHADER_malloc m = effect->ctx.m;
MOJOSHADER_free f = effect->ctx.f;
void *d = effect->ctx.malloc_data;
if (numlargeobjects == 0) return;
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];
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;
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);
object->shader.preshader = MOJOSHADER_parsePreshader(*ptr + start, length,
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
{
char mainfn[32];
snprintf(mainfn, sizeof (mainfn), "ShaderFunction%u", (unsigned int) objectIndex);
object->shader.shader = effect->ctx.compileShader(effect->ctx.shaderContext,
mainfn, *ptr, length,
swiz, swizcount,
smap, smapcount);
if (object->shader.shader == NULL)
{
// Bail ASAP, so we can get the error to the application
errorlist_add(errors, NULL, 0, effect->ctx.getError(effect->ctx.shaderContext));
return;
} // if
pd = effect->ctx.getParseData(object->shader.shader);
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
for (j = 0; j < pd->symbol_count; j++)
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
object->shader.sampler_count++;
object->shader.param_count = pd->symbol_count;
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;
for (j = 0; j < pd->symbol_count; j++)
{
int par = findparameter(effect->params,
effect->param_count,
pd->symbols[j].name);
object->shader.params[j] = par;
if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
{
object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name;
object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index;
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
if (pd->preshader)
{
object->shader.preshader_param_count = pd->preshader->symbol_count;
object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
for (j = 0; j < pd->preshader->symbol_count; j++)
{
object->shader.preshader_params[j] = findparameter(effect->params,
effect->param_count,
pd->preshader->symbols[j].name);
} // for
} // if
}
} // if
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
|| 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_VOID) // FIXME: Why? -flibit
{
assert(0 && "Large object type unknown!");
} // else
/* Object block is always a multiple of four */
const uint32 blocklen = (length + 3) - ((length - 1) % 4);
*ptr += blocklen;
*len -= blocklen;
} // for
} // readobjects
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)
{
const uint8 *ptr = (const uint8 *) buf;
uint32 len = (uint32) _len;
ErrorList *errors;
MOJOSHADER_malloc m;
MOJOSHADER_free f;
void *d;
/* Need a backend! */
if (ctx == NULL)
return &MOJOSHADER_need_a_backend_effect;
/* Supply both m and f, or neither */
if ( ((ctx->m == NULL) && (ctx->f != NULL))
|| ((ctx->m != NULL) && (ctx->f == NULL)) )
return &MOJOSHADER_out_of_mem_effect;
/* Use default malloc/free if m/f were not passed */
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;
/* malloc base effect structure */
MOJOSHADER_effect *retval = (MOJOSHADER_effect *) m(sizeof (MOJOSHADER_effect),
ctx->malloc_data);
if (retval == NULL)
return &MOJOSHADER_out_of_mem_effect;
memset(retval, '\0', sizeof (*retval));
/* Store ctx in effect structure */
memcpy(&retval->ctx, ctx, sizeof(MOJOSHADER_effectShaderContext));
retval->ctx.m = m;
retval->ctx.f = f;
if (len < 8)
goto parseEffect_unexpectedEOF;
/* Read in header magic, seek to initial offset */
const uint8 *base = NULL;
uint16 magic;
uint8 version_major;
uint8 version_minor;
read_version_token(&ptr, &len, &magic, &version_major, &version_minor);
if ((magic == 0xBCF0) && (version_major == 0x0B) && (version_minor == 0xCF))
{
/* 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;
read_version_token(&ptr, &len, &magic, &version_major, &version_minor);
} // if
if (!((magic == 0xFEFF) && (version_major == 0x09) && (version_minor == 0x01)))
{
MOJOSHADER_deleteEffect(retval);
return &MOJOSHADER_not_an_effect_effect;
} // if
else