/
mojoshader.c
3428 lines (2846 loc) · 105 KB
1
/**
2
3
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
4
5
6
7
8
9
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
10
11
// !!! FIXME: I keep changing coding styles for symbols and typedefs.
12
13
14
15
// !!! FIXME: do DEF* opcodes have to come before instructions?
// !!! FIXME: my reading of the msdn spec suggests no, but it sounds like
// !!! FIXME: something they'd require. DCL_* _does_ have to be first.
16
17
18
19
// Shader bytecode format is described at MSDN:
// http://msdn2.microsoft.com/en-us/library/ms800307.aspx
20
#include <stdio.h>
21
#include <string.h>
22
#include <stdlib.h>
23
#include <stdint.h>
24
#include <stdarg.h>
25
#include <assert.h>
26
27
#include "mojoshader.h"
28
29
30
31
32
33
34
// This is the highest shader version we currently support.
#define MAX_SHADER_MAJOR 3
#define MAX_SHADER_MINOR 0
35
36
37
// If SUPPORT_PROFILE_* isn't defined, we assume an implicit desire to support.
// You get all the profiles unless you go out of your way to disable them.
38
39
40
41
42
43
44
45
46
#ifndef SUPPORT_PROFILE_D3D
#define SUPPORT_PROFILE_D3D 1
#endif
#ifndef SUPPORT_PROFILE_GLSL
#define SUPPORT_PROFILE_GLSL 1
#endif
47
48
49
50
51
52
// Get basic wankery out of the way here...
typedef unsigned int uint; // this is a printf() helper. don't use for code.
typedef uint8_t uint8;
typedef uint32_t uint32;
53
typedef int32_t int32;
54
55
56
57
58
59
60
#ifdef __GNUC__
#define ISPRINTF(x,y) __attribute__((format (printf, x, y)))
#else
#define ISPRINTF(x,y)
#endif
61
62
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
63
64
65
66
67
68
69
#ifdef _WINDOWS // !!! FIXME: bleh
const char *endline_str = "\r\n";
#else
const char *endline_str = "\n";
#endif
70
71
72
// we need to reference this by explicit value occasionally.
#define OPCODE_RET 28
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Byteswap magic...
#if ((defined __GNUC__) && (defined __POWERPC__))
static inline uint32 SWAP32(uint32 x)
{
__asm__ __volatile__("lwbrx %0,0,%1" : "=r" (x) : "r" (&x));
return x;
} // SWAP32
#elif defined(__POWERPC__)
static inline uint32 SWAP32(uint32 x)
{
return ( (((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) |
(((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000) );
} // SWAP32
#else
# define SWAP32(x) (x)
#endif
92
93
// predeclare.
94
typedef struct Context Context;
95
96
// one emit function for each opcode in each profile.
97
typedef void (*emit_function)(Context *ctx);
98
99
// one emit function for comments in each profile.
100
typedef void (*emit_comment)(Context *ctx, const char *str);
101
102
// one emit function for starting output in each profile.
103
typedef void (*emit_start)(Context *ctx);
104
105
// one emit function for ending output in each profile.
106
typedef void (*emit_end)(Context *ctx);
107
108
109
110
// one args function for each possible sequence of opcode arguments.
typedef int (*args_function)(Context *ctx);
111
// one state function for each opcode where we have state machine updates.
112
typedef void (*state_function)(Context *ctx);
113
114
115
116
117
118
119
typedef struct
{
const char *name;
emit_start start_emitter;
emit_end end_emitter;
emit_comment comment_emitter;
120
} Profile;
121
122
typedef MOJOSHADER_shaderType ShaderType;
123
124
125
126
127
128
typedef enum
{
REGISTER_TYPE_TEMP = 0,
REGISTER_TYPE_INPUT = 1,
REGISTER_TYPE_CONST = 2,
129
REGISTER_TYPE_ADDRESS = 3,
130
131
132
133
REGISTER_TYPE_TEXTURE = 3, // ALSO 3!
REGISTER_TYPE_RASTOUT = 4,
REGISTER_TYPE_ATTROUT = 5,
REGISTER_TYPE_TEXCRDOUT = 6,
134
135
136
137
REGISTER_TYPE_OUTPUT = 6, // ALSO 6!
REGISTER_TYPE_CONSTINT = 7,
REGISTER_TYPE_COLOROUT = 8,
REGISTER_TYPE_DEPTHOUT = 9,
138
139
140
141
142
143
144
145
146
147
148
REGISTER_TYPE_SAMPLER = 10,
REGISTER_TYPE_CONST2 = 11,
REGISTER_TYPE_CONST3 = 12,
REGISTER_TYPE_CONST4 = 13,
REGISTER_TYPE_CONSTBOOL = 14,
REGISTER_TYPE_LOOP = 15,
REGISTER_TYPE_TEMPFLOAT16 = 16,
REGISTER_TYPE_MISCTYPE = 17,
REGISTER_TYPE_LABEL = 18,
REGISTER_TYPE_PREDICATE = 19,
REGISTER_TYPE_MAX = 19
149
} RegisterType;
150
151
152
153
154
155
156
typedef enum
{
RASTOUT_TYPE_POSITION = 0,
RASTOUT_TYPE_FOG = 1,
RASTOUT_TYPE_POINT_SIZE = 2,
RASTOUT_TYPE_MAX = 2
157
} RastOutType;
158
159
160
161
162
163
typedef enum
{
MISCTYPE_TYPE_POSITION = 0,
MISCTYPE_TYPE_FACE = 1,
MISCTYPE_TYPE_MAX = 1
164
} MiscTypeType;
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
typedef enum
{
DECLUSAGE_POSITION = 0,
DECLUSAGE_BLENDWEIGHT = 1,
DECLUSAGE_BLENDINDICES = 2,
DECLUSAGE_NORMAL = 3,
DECLUSAGE_PSIZE = 4,
DECLUSAGE_TEXCOORD = 5,
DECLUSAGE_TANGENT = 6,
DECLUSAGE_BINORMAL = 7,
DECLUSAGE_TESSFACTOR = 8,
DECLUSAGE_POSITIONT = 9,
DECLUSAGE_COLOR = 10,
DECLUSAGE_FOG = 11,
DECLUSAGE_DEPTH = 12,
DECLUSAGE_SAMPLE = 13
182
} DeclUsageType;
183
184
185
186
187
188
typedef enum
{
TEXTURE_TYPE_2D = 2,
TEXTURE_TYPE_CUBE = 3,
TEXTURE_TYPE_VOLUME = 4,
189
} TextureType;
190
191
192
193
194
// A simple linked list of strings, so we can build the final output without
// realloc()ing for each new line, and easily insert lines into the middle
// of the output without much trouble.
195
typedef struct OutputListNode
196
197
{
char *str;
198
199
200
201
202
203
204
struct OutputListNode *next;
} OutputListNode;
typedef struct OutputList
{
OutputListNode head;
OutputListNode *tail;
205
206
} OutputList;
207
208
209
210
211
212
// result modifiers.
#define MOD_SATURATE 0x01
#define MOD_PP 0x02
#define MOD_CENTROID 0x04
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// source modifiers.
typedef enum
{
SRCMOD_NONE,
SRCMOD_NEGATE,
SRCMOD_BIAS,
SRCMOD_BIASNEGATE,
SRCMOD_SIGN,
SRCMOD_SIGNNEGATE,
SRCMOD_COMPLEMENT,
SRCMOD_X2,
SRCMOD_X2NEGATE,
SRCMOD_DZ,
SRCMOD_DW,
SRCMOD_ABS,
SRCMOD_ABSNEGATE,
SRCMOD_NOT,
SRCMOD_TOTAL
231
} SourceMod;
232
233
234
235
typedef struct
{
236
const uint32 *token; // this is the unmolested token in the stream.
237
238
int regnum;
int relative;
239
240
241
242
243
int writemask; // xyzw or rgba (all four, not split out).
int writemask0; // x or red
int writemask1; // y or green
int writemask2; // z or blue
int writemask3; // w or alpha
244
245
int result_mod;
int result_shift;
246
RegisterType regtype;
247
248
249
250
} DestArgInfo;
typedef struct
{
251
const uint32 *token; // this is the unmolested token in the stream.
252
253
int regnum;
int relative;
254
int swizzle; // xyzw (all four, not split out).
255
256
257
258
259
int swizzle_x;
int swizzle_y;
int swizzle_z;
int swizzle_w;
int src_mod;
260
RegisterType regtype;
261
262
263
} SourceArgInfo;
264
265
266
267
268
269
270
271
272
typedef enum
{
CTX_FLAGS_GLSL_LIT_OPCODE = (1 << 0),
CTX_FLAGS_GLSL_DST_OPCODE = (1 << 1),
CTX_FLAGS_GLSL_LRP_OPCODE = (1 << 2),
CTX_FLAGS_MASK = 0xFFFFFFFF
} ContextFlags;
273
274
#define SCRATCH_BUFFER_SIZE 256
#define SCRATCH_BUFFERS 10
275
276
277
278
279
280
281
// !!! FIXME: labels_called and the scratch buffers make this pretty big.
// !!! FIXME: might be worth having one set of static scratch buffers that
// !!! FIXME: are mutex protected?
// !!! FIXME: and replace the bit array for labels_called with a linked list?
// !!! FIXME: maybe just malloc() the label list, since it can't be more than
// !!! FIXME: around (bytes_of_shader / 20) elements in the pathological case?
282
// Context...this is state that changes as we parse through a shader...
283
struct Context
284
{
285
286
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
287
288
const uint32 *tokens;
uint32 tokencount;
289
290
OutputList *output;
OutputList globals;
291
OutputList helpers;
292
293
OutputList subroutines;
OutputList mainline;
294
295
296
297
OutputList ignore;
OutputList *output_stack[2];
int indent_stack[2];
int output_stack_len;
298
int output_len; // total strlen; prevents walking the lists just to malloc.
299
int indent;
300
301
302
const char *endline;
int endline_len;
const char *failstr;
303
char scratch[SCRATCH_BUFFERS][SCRATCH_BUFFER_SIZE];
304
305
int scratchidx; // current scratch buffer.
int profileid;
306
307
const Profile *profile;
ShaderType shader_type;
308
309
uint8 major_ver;
uint8 minor_ver;
310
DestArgInfo dest_args[1];
311
SourceArgInfo source_args[5];
312
uint32 dwords[4];
313
int instruction_count;
314
uint32 instruction_controls;
315
316
317
318
uint32 previous_opcode;
ContextFlags flags;
uint8 labels_called[256];
int loops;
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
// jump between output sections in the context...
static inline void push_output(Context *ctx, OutputList *section)
{
assert(ctx->output_stack_len < STATICARRAYLEN(ctx->output_stack));
ctx->output_stack[ctx->output_stack_len] = ctx->output;
ctx->indent_stack[ctx->output_stack_len] = ctx->indent;
ctx->output_stack_len++;
ctx->output = section;
ctx->indent = 0;
} // push_output
static inline void pop_output(Context *ctx)
{
assert(ctx->output_stack_len > 0);
ctx->output_stack_len--;
ctx->output = ctx->output_stack[ctx->output_stack_len];
ctx->indent = ctx->indent_stack[ctx->output_stack_len];
} // pop_output
// Shader model version magic...
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
{
return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 0 : (minor)) );
} // version_ui32
static int shader_version_supported(uint8 maj, uint8 min)
{
return (ver_ui32(maj,min) <= ver_ui32(MAX_SHADER_MAJOR, MAX_SHADER_MINOR));
} // shader_version_supported
static int shader_version_atleast(const Context *ctx, uint8 maj, uint8 min)
{
return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
} // shader_version_atleast
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Bit arrays (for storing large arrays of booleans...
static void set_bit_array(uint8 *array, size_t arraylen, int index, int val)
{
const int byteindex = index / 8;
const int bitindex = index % 8;
assert(byteindex < arraylen);
if (val)
array[byteindex] |= (1 << bitindex);
else
array[byteindex] &= ~(1 << bitindex);
} // set_bit_array
static int get_bit_array(const uint8 *array, size_t arraylen, int index)
{
const int byteindex = index / 8;
const int bitindex = index % 8;
assert(byteindex < arraylen);
const uint8 byte = array[byteindex];
return (byte & (1 << bitindex)) ? 1 : 0;
} // get_bit_array
385
386
static inline char *get_scratch_buffer(Context *ctx)
387
{
388
ctx->scratchidx = (ctx->scratchidx + 1) % SCRATCH_BUFFERS;
389
return ctx->scratch[ctx->scratchidx];
390
391
} // get_scratch_buffer
392
393
394
// Special-case return values from the parsing pipeline...
#define FAIL (-1)
395
396
#define NOFAIL (-2)
#define END_OF_STREAM (-3)
397
398
static inline int isfail(const Context *ctx)
399
400
401
402
403
{
return (ctx->failstr != NULL);
} // isfail
404
405
406
407
408
static MOJOSHADER_parseData out_of_mem_data = {
"Out of memory", 0, 0, 0, MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0
};
static const char *out_of_mem_str = "Out of memory";
409
410
411
static inline int out_of_memory(Context *ctx)
{
if (ctx->failstr == NULL)
412
ctx->failstr = out_of_mem_str; // fail() would call malloc().
413
414
415
416
417
418
return FAIL;
} // out_of_memory
static int failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static int failf(Context *ctx, const char *fmt, ...)
419
{
420
421
if (ctx->failstr == NULL) // don't change existing error.
{
422
char *scratch = get_scratch_buffer(ctx);
423
424
va_list ap;
va_start(ap, fmt);
425
const int len = vsnprintf(scratch,SCRATCH_BUFFER_SIZE,fmt,ap);
426
427
va_end(ap);
428
429
430
431
432
433
char *failstr = (char *) ctx->malloc(len + 1);
if (failstr == NULL)
out_of_memory(ctx);
else
{
// see comments about scratch buffer overflow in output_line().
434
if (len < SCRATCH_BUFFER_SIZE)
435
436
437
438
439
440
441
442
443
strcpy(failstr, scratch); // copy it over.
else
{
va_start(ap, fmt);
vsnprintf(failstr, len + 1, fmt, ap); // rebuild it.
va_end(ap);
} // else
ctx->failstr = failstr;
} // else
444
445
446
447
448
449
} // if
return FAIL;
} // failf
450
static inline int fail(Context *ctx, const char *reason)
451
452
453
454
455
{
return failf(ctx, "%s", reason);
} // fail
456
457
static int output_line(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static int output_line(Context *ctx, const char *fmt, ...)
458
{
459
460
OutputListNode *item = NULL;
461
if (isfail(ctx))
462
463
return FAIL; // we failed previously, don't go on...
464
char *scratch = get_scratch_buffer(ctx);
465
466
const int indent = ctx->indent;
467
468
if (indent > 0)
memset(scratch, '\t', indent);
469
470
471
va_list ap;
va_start(ap, fmt);
472
const int len = vsnprintf(scratch+indent, SCRATCH_BUFFER_SIZE-indent, fmt, ap) + indent;
473
474
va_end(ap);
475
476
477
478
item = (OutputListNode *) ctx->malloc(sizeof (OutputListNode));
if (item == NULL)
return out_of_memory(ctx);
479
480
item->str = (char *) ctx->malloc(len + 1);
if (item->str == NULL)
481
{
482
483
free(item);
return out_of_memory(ctx);
484
485
} // if
486
487
488
// If we overflowed our scratch buffer, that's okay. We were going to
// allocate anyhow...the scratch buffer just lets us avoid a second
// run of vsnprintf().
489
if (len < SCRATCH_BUFFER_SIZE)
490
491
492
strcpy(item->str, scratch); // copy it over.
else
{
493
494
if (indent > 0)
memset(item->str, '\t', indent);
495
va_start(ap, fmt);
496
vsnprintf(item->str+indent, len + 1, fmt, ap); // rebuild it.
497
498
va_end(ap);
} // else
499
500
item->next = NULL;
501
502
503
ctx->output->tail->next = item;
ctx->output->tail = item;
504
ctx->output_len += len + ctx->endline_len;
505
506
507
508
509
return 0;
} // output_line
510
511
512
513
514
515
516
// this is just to stop gcc whining.
static inline int output_blank_line(Context *ctx)
{
return output_line(ctx, "%s", "");
} // output_blank_line
517
// !!! FIXME: this is sort of nasty.
518
519
static void floatstr(Context *ctx, char *buf, size_t bufsize, float f,
int leavedecimal)
520
521
{
const size_t len = snprintf(buf, bufsize, "%f", f);
522
if ((len+2) >= bufsize)
523
524
525
526
527
528
fail(ctx, "BUG: internal buffer is too small");
else
{
char *end = buf + len;
char *ptr = strchr(buf, '.');
if (ptr == NULL)
529
530
531
{
if (leavedecimal)
strcat(buf, ".0");
532
return; // done.
533
} // if
534
535
536
537
538
539
540
541
542
while (--end != ptr)
{
if (*end != '0')
{
end++;
break;
} // if
} // while
543
544
if ((leavedecimal) && (end == ptr))
end += 2;
545
546
547
548
*end = '\0'; // chop extra '0' or all decimal places off.
} // else
} // floatstr
549
550
// This is used in more than just the d3d profile.
551
static const char *get_D3D_register_string(Context *ctx,
552
RegisterType regtype,
553
554
int regnum, char *regnum_str,
size_t regnum_size)
555
{
556
const char *retval = NULL;
557
int has_number = 1;
558
559
switch (regtype)
560
561
{
case REGISTER_TYPE_TEMP:
562
retval = "r";
563
564
565
break;
case REGISTER_TYPE_INPUT:
566
retval = "v";
567
568
569
break;
case REGISTER_TYPE_CONST:
570
571
572
retval = "c";
break;
573
case REGISTER_TYPE_CONST2:
574
575
576
577
retval = "c";
regnum += 2048;
break;
578
case REGISTER_TYPE_CONST3:
579
580
581
582
retval = "c";
regnum += 4096;
break;
583
case REGISTER_TYPE_CONST4:
584
retval = "c";
585
regnum += 6144;
586
587
break;
588
case REGISTER_TYPE_ADDRESS: // (or REGISTER_TYPE_TEXTURE, same value.)
589
retval = (ctx->shader_type == MOJOSHADER_TYPE_VERTEX) ? "a" : "t";
590
591
592
break;
case REGISTER_TYPE_RASTOUT:
593
switch ((RastOutType) regnum)
594
{
595
596
597
case RASTOUT_TYPE_POSITION: retval = "oPos"; break;
case RASTOUT_TYPE_FOG: retval = "oFog"; break;
case RASTOUT_TYPE_POINT_SIZE: retval = "oPts"; break;
598
} // switch
599
has_number = 0;
600
601
602
break;
case REGISTER_TYPE_ATTROUT:
603
retval = "oD";
604
605
break;
606
case REGISTER_TYPE_OUTPUT: // (or REGISTER_TYPE_TEXCRDOUT, same value.)
607
if ((ctx->shader_type==MOJOSHADER_TYPE_VERTEX) && (ctx->major_ver>=3))
608
retval = "o";
609
else
610
retval = "oT";
611
612
613
break;
case REGISTER_TYPE_CONSTINT:
614
retval = "i";
615
616
617
break;
case REGISTER_TYPE_COLOROUT:
618
retval = "oC";
619
620
621
break;
case REGISTER_TYPE_DEPTHOUT:
622
retval = "oDepth";
623
has_number = 0;
624
625
626
break;
case REGISTER_TYPE_SAMPLER:
627
retval = "s";
628
629
630
break;
case REGISTER_TYPE_CONSTBOOL:
631
retval = "b";
632
633
634
break;
case REGISTER_TYPE_LOOP:
635
retval = "aL";
636
has_number = 0;
637
638
639
break;
// !!! FIXME: don't know what the asm string is for this..
640
case REGISTER_TYPE_TEMPFLOAT16:
641
fail(ctx, "don't know the ASM for tempfloat16 register");
642
643
644
retval = "???";
has_number = 0;
break;
645
646
case REGISTER_TYPE_MISCTYPE:
647
switch ((MiscTypeType) regnum)
648
{
649
650
case MISCTYPE_TYPE_POSITION: retval = "vPos"; break;
case MISCTYPE_TYPE_FACE: retval = "vFace"; break;
651
} // switch
652
has_number = 0;
653
654
655
break;
case REGISTER_TYPE_LABEL:
656
retval = "l";
657
658
659
break;
case REGISTER_TYPE_PREDICATE:
660
retval = "p";
661
662
663
break;
} // switch
664
665
666
667
668
if (has_number)
snprintf(regnum_str, regnum_size, "%u", (uint) regnum);
else
regnum_str[0] = '\0';
669
670
671
672
return retval;
} // get_D3D_register_string
673
674
675
676
677
678
679
680
681
682
#define AT_LEAST_ONE_PROFILE 0
#if !SUPPORT_PROFILE_D3D
#define PROFILE_EMITTER_D3D(op)
#else
#undef AT_LEAST_ONE_PROFILE
#define AT_LEAST_ONE_PROFILE 1
#define PROFILE_EMITTER_D3D(op) emit_D3D_##op,
683
static const char *make_D3D_destarg_string(Context *ctx, const int idx)
684
685
686
687
{
if (idx >= STATICARRAYLEN(ctx->dest_args))
{
fail(ctx, "Too many destination args");
688
return "";
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
} // if
const DestArgInfo *arg = &ctx->dest_args[idx];
const char *result_shift_str = "";
switch (arg->result_shift)
{
case 0x1: result_shift_str = "_x2"; break;
case 0x2: result_shift_str = "_x4"; break;
case 0x3: result_shift_str = "_x8"; break;
case 0xD: result_shift_str = "_d8"; break;
case 0xE: result_shift_str = "_d4"; break;
case 0xF: result_shift_str = "_d2"; break;
} // switch
const char *sat_str = (arg->result_mod & MOD_SATURATE) ? "_sat" : "";
const char *pp_str = (arg->result_mod & MOD_PP) ? "_pp" : "";
const char *cent_str = (arg->result_mod & MOD_CENTROID) ? "_centroid" : "";
char regnum_str[16];
709
710
711
const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
712
713
714
if (regtype_str == NULL)
{
fail(ctx, "Unknown destination register type.");
715
return "";
716
717
} // if
718
719
720
721
722
723
724
725
726
727
728
729
char writemask_str[6];
int i = 0;
if (arg->writemask != 0xF) // 0xF == 1111. No explicit mask.
{
writemask_str[i++] = '.';
if (arg->writemask0) writemask_str[i++] = 'x';
if (arg->writemask1) writemask_str[i++] = 'y';
if (arg->writemask2) writemask_str[i++] = 'z';
if (arg->writemask3) writemask_str[i++] = 'w';
} // if
writemask_str[i] = '\0';
assert(i < sizeof (writemask_str));
730
731
// may turn out something like "_x2_sat_pp_centroid r0.xyzw" ...
732
char *retval = get_scratch_buffer(ctx);
733
snprintf(retval, SCRATCH_BUFFER_SIZE, "%s%s%s%s %s%s%s",
734
result_shift_str, sat_str, pp_str, cent_str,
735
regtype_str, regnum_str, writemask_str);
736
// !!! FIXME: make sure the scratch buffer was large enough.
737
return retval;
738
} // make_D3D_destarg_string
739
740
741
static const char *make_D3D_sourcearg_string(Context *ctx, const int idx)
742
{
743
744
745
if (idx >= STATICARRAYLEN(ctx->source_args))
{
fail(ctx, "Too many source args");
746
return "";
747
} // if
748
749
750
751
752
const SourceArgInfo *arg = &ctx->source_args[idx];
const char *premod_str = "";
const char *postmod_str = "";
753
switch ((SourceMod) arg->src_mod)
754
755
756
757
758
759
760
761
762
763
764
{
case SRCMOD_NEGATE:
premod_str = "-";
break;
case SRCMOD_BIASNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_BIAS:
postmod_str = "_bias";
break;
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
case SRCMOD_SIGNNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_SIGN:
postmod_str = "_bx2";
break;
case SRCMOD_COMPLEMENT:
premod_str = "1-";
break;
case SRCMOD_X2NEGATE:
premod_str = "-";
// fall through.
case SRCMOD_X2:
postmod_str = "_x2";
break;
case SRCMOD_DZ:
postmod_str = "_dz";
break;
case SRCMOD_DW:
postmod_str = "_dw";
break;
case SRCMOD_ABSNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_ABS:
postmod_str = "_abs";
break;
case SRCMOD_NOT:
premod_str = "!";
break;
802
803
804
805
case SRCMOD_NONE:
case SRCMOD_TOTAL:
break; // stop compiler whining.
806
807
808
809
} // switch
char regnum_str[16];
810
811
812
const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
813
814
815
816
if (regtype_str == NULL)
{
fail(ctx, "Unknown source register type.");
817
return "";
818
819
820
821
} // if
char swizzle_str[6];
int i = 0;
822
if (arg->swizzle != 0xE4) // 0xE4 == 11100100 ... 3 2 1 0. No swizzle.
823
824
825
{
static const char channel[] = { 'x', 'y', 'z', 'w' };
swizzle_str[i++] = '.';
826
827
828
829
830
831
832
833
swizzle_str[i++] = channel[arg->swizzle_x];
swizzle_str[i++] = channel[arg->swizzle_y];
swizzle_str[i++] = channel[arg->swizzle_z];
swizzle_str[i++] = channel[arg->swizzle_w];
// .xyzz is the same as .xyz, .z is the same as .zzzz, etc.
while (swizzle_str[i-1] == swizzle_str[i-2])
i--;
834
835
836
837
} // if
swizzle_str[i] = '\0';
assert(i < sizeof (swizzle_str));
838
char *retval = get_scratch_buffer(ctx);
839
840
snprintf(retval, SCRATCH_BUFFER_SIZE, "%s%s%s%s%s",
premod_str, regtype_str, regnum_str, postmod_str, swizzle_str);
841
// !!! FIXME: make sure the scratch buffer was large enough.
842
return retval;
843
} // make_D3D_sourcearg_string
844
845
846
static void emit_D3D_start(Context *ctx)
847
848
849
{
const uint major = (uint) ctx->major_ver;
const uint minor = (uint) ctx->minor_ver;
850
851
852
853
854
855
856
857
858
const char *shadertype_str = NULL;
char minor_str[16];
if (minor == 0xFF)
strcpy(minor_str, "sw");
else if (minor == 0x1) // apparently this is "vs_2_x". Weird.
strcpy(minor_str, "x");
else
snprintf(minor_str, sizeof (minor_str), "%u", (uint) minor);
859
860
if (ctx->shader_type == MOJOSHADER_TYPE_PIXEL)
861
shadertype_str = "ps";
862
else if (ctx->shader_type == MOJOSHADER_TYPE_VERTEX)
863
shadertype_str = "vs";
864
else
865
866
867
{
failf(ctx, "Shader type %u unsupported in this profile.",
(uint) ctx->shader_type);
868
return;
869
} // else
870
871
output_line(ctx, "%s_%u_%s", shadertype_str, major, minor_str);
872
873
} // emit_D3D_start
874
875
static void emit_D3D_end(Context *ctx)
876
{
877
output_line(ctx, "end");
878
} // emit_D3D_end
879
880
881
static void emit_D3D_comment(Context *ctx, const char *str)
882
{
883
884
output_line(ctx, "; %s", str);
} // emit_D3D_comment
885
886
887
static void emit_D3D_RESERVED(Context *ctx)
888
{
889
890
891
// do nothing; fails in the state machine.
} // emit_D3D_RESERVED
892
893
894
895
// Generic D3D opcode emitters. A list of macros generate all the entry points
// that call into these...
896
897
898
899
900
901
902
903
904
905
906
907
static char *lowercase(char *dst, const char *src)
{
int i = 0;
do
{
const char ch = src[i];
dst[i] = (((ch >= 'A') && (ch <= 'Z')) ? (ch - ('A' - 'a')) : ch);
} while (src[i++]);
return dst;
} // lowercase
908
static void emit_D3D_opcode_d(Context *ctx, const char *opcode)
909
{
910
const char *dst0 = make_D3D_destarg_string(ctx, 0);
911
opcode = lowercase(get_scratch_buffer(ctx), opcode);
912
output_line(ctx, "%s%s", opcode, dst0);
913
914
915
} // emit_D3D_opcode_d
916
static void emit_D3D_opcode_s(Context *ctx, const char *opcode)
917
{
918
const char *src0 = make_D3D_sourcearg_string(ctx, 0);
919
opcode = lowercase(get_scratch_buffer(ctx), opcode);
920
output_line(ctx, "%s %s", opcode, src0);
921
922
923
} // emit_D3D_opcode_s
924
static void emit_D3D_opcode_ss(Context *ctx, const char *opcode)
925
{
926
927
const char *src0 = make_D3D_sourcearg_string(ctx, 0);
const char *src1 = make_D3D_sourcearg_string(ctx, 1);
928
opcode = lowercase(get_scratch_buffer(ctx), opcode);
929
output_line(ctx, "%s %s, %s", opcode, src0, src1);
930
} // emit_D3D_opcode_ss
931
932
933
static void emit_D3D_opcode_ds(Context *ctx, const char *opcode)
934
{
935
936
const char *dst0 = make_D3D_destarg_string(ctx, 0);
const char *src0 = make_D3D_sourcearg_string(ctx, 0);
937
opcode = lowercase(get_scratch_buffer(ctx), opcode);
938
output_line(ctx, "%s%s, %s", opcode, dst0, src0);
939
940
941
} // emit_D3D_opcode_ds
942
static void emit_D3D_opcode_dss(Context *ctx, const char *opcode)
943
{
944
945
946
const char *dst0 = make_D3D_destarg_string(ctx, 0);
const char *src0 = make_D3D_sourcearg_string(ctx, 0);
const char *src1 = make_D3D_sourcearg_string(ctx, 1);
947
opcode = lowercase(get_scratch_buffer(ctx), opcode);
948
output_line(ctx, "%s%s, %s, %s", opcode, dst0, src0, src1);
949
950
951
} // emit_D3D_opcode_dss
952
static void emit_D3D_opcode_dsss(Context *ctx, const char *opcode)
953
{
954
955
956
957
const char *dst0 = make_D3D_destarg_string(ctx, 0);
const char *src0 = make_D3D_sourcearg_string(ctx, 0);
const char *src1 = make_D3D_sourcearg_string(ctx, 1);
const char *src2 = make_D3D_sourcearg_string(ctx, 2);
958
opcode = lowercase(get_scratch_buffer(ctx), opcode);
959
output_line(ctx, "%s%s, %s, %s, %s", opcode, dst0, src0, src1, src2);
960
961
962
} // emit_D3D_opcode_dsss
963
static void emit_D3D_opcode_dssss(Context *ctx, const char *opcode)
964
{
965
966
967
968
969
const char *dst0 = make_D3D_destarg_string(ctx, 0);
const char *src0 = make_D3D_sourcearg_string(ctx, 0);
const char *src1 = make_D3D_sourcearg_string(ctx, 1);
const char *src2 = make_D3D_sourcearg_string(ctx, 2);
const char *src3 = make_D3D_sourcearg_string(ctx, 3);
970
opcode = lowercase(get_scratch_buffer(ctx), opcode);
971
output_line(ctx,"%s%s, %s, %s, %s, %s",opcode,dst0,src0,src1,src2,src3);
972
973
974
} // emit_D3D_opcode_dssss
975
976
977
978
979
980
981
static void emit_D3D_opcode(Context *ctx, const char *opcode)
{
opcode = lowercase(get_scratch_buffer(ctx), opcode);
output_line(ctx, "%s", opcode);
} // emit_D3D_opcode_dssss
982
#define EMIT_D3D_OPCODE_FUNC(op) \
983
static void emit_D3D_##op(Context *ctx) { \
984
emit_D3D_opcode(ctx, #op); \
985
986
}
#define EMIT_D3D_OPCODE_D_FUNC(op) \
987
static void emit_D3D_##op(Context *ctx) { \
988
989
990
emit_D3D_opcode_d(ctx, #op); \
}
#define EMIT_D3D_OPCODE_S_FUNC(op) \
991
static void emit_D3D_##op(Context *ctx) { \
992
993
994
emit_D3D_opcode_s(ctx, #op); \
}
#define EMIT_D3D_OPCODE_SS_FUNC(op) \
995
static void emit_D3D_##op(Context *ctx) { \
996
997
998
emit_D3D_opcode_ss(ctx, #op); \
}
#define EMIT_D3D_OPCODE_DS_FUNC(op) \
999
static void emit_D3D_##op(Context *ctx) { \
1000
emit_D3D_opcode_ds(ctx, #op); \