/
mojoshader.c
7399 lines (6230 loc) · 239 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: this file really needs to be split up.
12
13
// !!! FIXME: I keep changing coding styles for symbols and typedefs.
14
15
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
16
17
18
// predeclare.
19
typedef struct Context Context;
20
struct ConstantsList;
21
22
// one emit function for each opcode in each profile.
23
typedef void (*emit_function)(Context *ctx);
24
25
// one emit function for starting output in each profile.
26
typedef void (*emit_start)(Context *ctx, const char *profilestr);
27
28
// one emit function for ending output in each profile.
29
typedef void (*emit_end)(Context *ctx);
30
31
32
33
// one emit function for phase opcode output in each profile.
typedef void (*emit_phase)(Context *ctx);
34
35
36
37
38
39
// one emit function for finalizing output in each profile.
typedef void (*emit_finalize)(Context *ctx);
// one emit function for global definitions in each profile.
typedef void (*emit_global)(Context *ctx, RegisterType regtype, int regnum);
40
// one emit function for relative uniform arrays in each profile.
41
typedef void (*emit_array)(Context *ctx, int base, int size);
42
43
44
45
46
47
// one emit function for relative constants arrays in each profile.
typedef void (*emit_const_array)(Context *ctx,
const struct ConstantsList *constslist,
int base, int size);
48
// one emit function for uniforms in each profile.
49
50
typedef void (*emit_uniform)(Context *ctx, RegisterType regtype, int regnum,
int arraybase, int arraysize);
51
52
53
54
// one emit function for samplers in each profile.
typedef void (*emit_sampler)(Context *ctx, int stage, TextureType ttype);
55
56
// one emit function for attributes in each profile.
typedef void (*emit_attribute)(Context *ctx, RegisterType regtype, int regnum,
57
58
MOJOSHADER_usage usage, int index, int wmask,
int flags);
59
60
61
62
// one args function for each possible sequence of opcode arguments.
typedef int (*args_function)(Context *ctx);
63
// one state function for each opcode where we have state machine updates.
64
typedef void (*state_function)(Context *ctx);
65
66
67
68
69
// one function for varnames in each profile.
typedef const char *(*varname_function)(Context *c, RegisterType t, int num);
// one function for const var array in each profile.
70
typedef const char *(*const_array_varname_function)(Context *c, int base, int size);
71
72
73
74
75
76
typedef struct
{
const char *name;
emit_start start_emitter;
emit_end end_emitter;
77
emit_phase phase_emitter;
78
emit_global global_emitter;
79
emit_array array_emitter;
80
emit_const_array const_array_emitter;
81
emit_uniform uniform_emitter;
82
emit_sampler sampler_emitter;
83
emit_attribute attribute_emitter;
84
emit_finalize finalize_emitter;
85
86
varname_function get_varname;
const_array_varname_function get_const_array_varname;
87
} Profile;
88
89
90
91
92
// 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.
93
typedef struct OutputListNode
94
95
{
char *str;
96
97
98
99
100
101
102
struct OutputListNode *next;
} OutputListNode;
typedef struct OutputList
{
OutputListNode head;
OutputListNode *tail;
103
104
} OutputList;
105
106
107
108
109
110
typedef struct ConstantsList
{
MOJOSHADER_constant constant;
struct ConstantsList *next;
} ConstantsList;
111
112
113
114
115
typedef struct VariableList
{
MOJOSHADER_uniformType type;
int index;
int count;
116
ConstantsList *constant;
117
int used;
118
119
struct VariableList *next;
} VariableList;
120
121
122
123
124
typedef struct RegisterList
{
RegisterType regtype;
int regnum;
125
126
127
MOJOSHADER_usage usage;
int index;
int writemask;
128
int misc;
129
const VariableList *array;
130
131
132
struct RegisterList *next;
} RegisterList;
133
134
#define SCRATCH_BUFFER_SIZE 128
135
#define SCRATCH_BUFFERS 32
136
137
// !!! FIXME: the scratch buffers make Context pretty big.
138
139
// !!! FIXME: might be worth having one set of static scratch buffers that
// !!! FIXME: are mutex protected?
140
141
// Context...this is state that changes as we parse through a shader...
142
struct Context
143
{
144
145
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
146
void *malloc_data;
147
148
const uint32 *tokens;
uint32 tokencount;
149
150
const MOJOSHADER_swizzle *swizzles;
unsigned int swizzles_count;
151
152
OutputList *output;
OutputList globals;
153
OutputList helpers;
154
OutputList subroutines;
155
OutputList mainline_intro;
156
OutputList mainline;
157
158
OutputList ignore;
OutputList *output_stack[2];
159
uint8 *output_bytes; // can be used instead of the OutputLists.
160
161
int indent_stack[2];
int output_stack_len;
162
int output_len; // total strlen; prevents walking the lists just to malloc.
163
int indent;
164
const char *shader_type_str;
165
166
167
const char *endline;
int endline_len;
const char *failstr;
168
char scratch[SCRATCH_BUFFERS][SCRATCH_BUFFER_SIZE];
169
170
int scratchidx; // current scratch buffer.
int profileid;
171
const Profile *profile;
172
MOJOSHADER_shaderType shader_type;
173
174
uint8 major_ver;
uint8 minor_ver;
175
DestArgInfo dest_arg;
176
SourceArgInfo source_args[5];
177
SourceArgInfo predicate_arg; // for predicated instructions.
178
uint32 dwords[4];
179
uint32 version_token;
180
int instruction_count;
181
uint32 instruction_controls;
182
183
uint32 previous_opcode;
int loops;
184
int reps;
185
int max_reps;
186
int cmps;
187
188
int scratch_registers;
int max_scratch_registers;
189
190
191
int branch_labels_stack_index;
int branch_labels_stack[32];
int assigned_branch_labels;
192
int assigned_vertex_attributes;
193
int last_address_reg_component;
194
195
RegisterList used_registers;
RegisterList defined_registers;
196
197
int constant_count;
ConstantsList *constants;
198
199
int uniform_count;
RegisterList uniforms;
200
201
int attribute_count;
RegisterList attributes;
202
203
int sampler_count;
RegisterList samplers;
204
VariableList *variables; // variables to register mapping.
205
206
207
208
209
210
211
212
213
int centroid_allowed;
int have_ctab;
int determined_constants_arrays;
int predicated;
int support_nv2;
int support_nv3;
int support_nv4;
int support_glsl120;
int glsl_generated_lit_opcode;
214
215
216
};
217
218
// Convenience functions for allocators...
219
220
221
222
223
224
static MOJOSHADER_parseData out_of_mem_data = {
"Out of memory", 0, 0, 0, 0, MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0
};
static const char *out_of_mem_str = "Out of memory";
static inline int out_of_memory(Context *ctx)
225
{
226
227
228
229
230
if (ctx->failstr == NULL)
ctx->failstr = out_of_mem_str; // fail() would call malloc().
return FAIL;
} // out_of_memory
231
static inline void *Malloc(Context *ctx, const size_t len)
232
{
233
void *retval = ctx->malloc((int) len, ctx->malloc_data);
234
235
236
if (retval == NULL)
out_of_memory(ctx);
return retval;
237
238
239
} // Malloc
240
static inline void Free(Context *ctx, void *ptr)
241
{
242
if (ptr != NULL) // check for NULL in case of dumb free() impl.
243
ctx->free(ptr, ctx->malloc_data);
244
245
246
} // Free
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// 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...
271
272
273
274
275
static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
{
return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 0 : (minor)) );
} // version_ui32
276
static inline int shader_version_supported(const uint8 maj, const uint8 min)
277
278
279
280
{
return (ver_ui32(maj,min) <= ver_ui32(MAX_SHADER_MAJOR, MAX_SHADER_MINOR));
} // shader_version_supported
281
282
static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
const uint8 min)
283
284
285
286
{
return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
} // shader_version_atleast
287
288
289
290
291
292
static inline int shader_version_exactly(const Context *ctx, const uint8 maj,
const uint8 min)
{
return ((ctx->major_ver == maj) && (ctx->minor_ver == min));
} // shader_version_exactly
293
294
295
296
297
298
299
300
301
302
static inline int shader_is_pixel(const Context *ctx)
{
return (ctx->shader_type == MOJOSHADER_TYPE_PIXEL);
} // shader_is_pixel
static inline int shader_is_vertex(const Context *ctx)
{
return (ctx->shader_type == MOJOSHADER_TYPE_VERTEX);
} // shader_is_vertex
303
304
305
306
307
308
static inline int isfail(const Context *ctx)
{
return (ctx->failstr != NULL);
} // isfail
309
310
static inline char *get_scratch_buffer(Context *ctx)
311
{
312
313
314
315
316
317
318
319
320
321
322
323
if ((ctx->scratchidx >= SCRATCH_BUFFERS) && !isfail(ctx))
{
// can't call fail() here, since it calls back into here.
const char *errstr = "BUG: overflowed scratch buffers";
char *failstr = (char *) Malloc(ctx, strlen(errstr) + 1);
if (failstr != NULL)
{
strcpy(failstr, errstr);
ctx->failstr = failstr;
} // if
} // if
324
ctx->scratchidx = (ctx->scratchidx + 1) % SCRATCH_BUFFERS;
325
return ctx->scratch[ctx->scratchidx];
326
327
} // get_scratch_buffer
328
329
330
static int failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static int failf(Context *ctx, const char *fmt, ...)
331
{
332
333
if (ctx->failstr == NULL) // don't change existing error.
{
334
char *scratch = get_scratch_buffer(ctx);
335
336
va_list ap;
va_start(ap, fmt);
337
const int len = vsnprintf(scratch, SCRATCH_BUFFER_SIZE, fmt, ap);
338
339
va_end(ap);
340
char *failstr = (char *) Malloc(ctx, len + 1);
341
if (failstr != NULL)
342
343
{
// see comments about scratch buffer overflow in output_line().
344
if (len < SCRATCH_BUFFER_SIZE)
345
346
347
348
349
350
351
352
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;
353
} // if
354
355
356
357
358
359
} // if
return FAIL;
} // failf
360
static inline int fail(Context *ctx, const char *reason)
361
362
363
364
365
{
return failf(ctx, "%s", reason);
} // fail
366
367
static int output_line(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static int output_line(Context *ctx, const char *fmt, ...)
368
{
369
370
OutputListNode *item = NULL;
371
if (isfail(ctx))
372
373
return FAIL; // we failed previously, don't go on...
374
char *scratch = get_scratch_buffer(ctx);
375
376
const int indent = ctx->indent;
377
378
if (indent > 0)
memset(scratch, '\t', indent);
379
380
381
va_list ap;
va_start(ap, fmt);
382
const int len = vsnprintf(scratch+indent, SCRATCH_BUFFER_SIZE-indent, fmt, ap) + indent;
383
384
va_end(ap);
385
item = (OutputListNode *) Malloc(ctx, sizeof (OutputListNode));
386
if (item == NULL)
387
return FAIL;
388
389
item->str = (char *) Malloc(ctx, len + 1);
390
if (item->str == NULL)
391
{
392
Free(ctx, item);
393
return FAIL;
394
395
} // if
396
397
398
// 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().
399
if (len < SCRATCH_BUFFER_SIZE)
400
401
402
strcpy(item->str, scratch); // copy it over.
else
{
403
404
if (indent > 0)
memset(item->str, '\t', indent);
405
va_start(ap, fmt);
406
vsnprintf(item->str+indent, len + 1, fmt, ap); // rebuild it.
407
408
va_end(ap);
} // else
409
410
item->next = NULL;
411
412
413
ctx->output->tail->next = item;
ctx->output->tail = item;
414
ctx->output_len += len + ctx->endline_len;
415
416
417
418
419
return 0;
} // output_line
420
421
422
423
424
425
426
// this is just to stop gcc whining.
static inline int output_blank_line(Context *ctx)
{
return output_line(ctx, "%s", "");
} // output_blank_line
427
// !!! FIXME: this is sort of nasty.
428
429
static void floatstr(Context *ctx, char *buf, size_t bufsize, float f,
int leavedecimal)
430
431
{
const size_t len = snprintf(buf, bufsize, "%f", f);
432
if ((len+2) >= bufsize)
433
434
435
436
437
438
fail(ctx, "BUG: internal buffer is too small");
else
{
char *end = buf + len;
char *ptr = strchr(buf, '.');
if (ptr == NULL)
439
440
441
{
if (leavedecimal)
strcat(buf, ".0");
442
return; // done.
443
} // if
444
445
446
447
448
449
450
451
452
while (--end != ptr)
{
if (*end != '0')
{
end++;
break;
} // if
} // while
453
454
if ((leavedecimal) && (end == ptr))
end += 2;
455
456
457
458
*end = '\0'; // chop extra '0' or all decimal places off.
} // else
} // floatstr
459
460
461
// Deal with register lists... !!! FIXME: I sort of hate this.
462
static void free_reglist(MOJOSHADER_free f, void *d, RegisterList *item)
463
464
465
466
{
while (item != NULL)
{
RegisterList *next = item->next;
467
f(item, d);
468
469
470
471
472
473
474
475
476
item = next;
} // while
} // free_reglist
static inline uint32 reg_to_ui32(const RegisterType regtype, const int regnum)
{
return ( ((uint32) regtype) | (((uint32) regnum) << 16) );
} // reg_to_uint32
477
478
479
static RegisterList *reglist_insert(Context *ctx, RegisterList *prev,
const RegisterType regtype,
const int regnum)
480
481
482
483
484
485
486
{
const uint32 newval = reg_to_ui32(regtype, regnum);
RegisterList *item = prev->next;
while (item != NULL)
{
const uint32 val = reg_to_ui32(item->regtype, item->regnum);
if (newval == val)
487
return item; // already set, so we're done.
488
489
490
491
492
493
494
495
496
497
498
else if (newval < val) // insert it here.
break;
else // if (newval > val)
{
// keep going, we're not to the insertion point yet.
prev = item;
item = item->next;
} // else
} // while
// we need to insert an entry after (prev).
499
item = (RegisterList *) Malloc(ctx, sizeof (RegisterList));
500
if (item != NULL)
501
502
503
{
item->regtype = regtype;
item->regnum = regnum;
504
item->usage = MOJOSHADER_USAGE_UNKNOWN;
505
506
item->index = 0;
item->writemask = 0;
507
item->misc = 0;
508
item->array = NULL;
509
510
item->next = prev->next;
prev->next = item;
511
} // if
512
513
return item;
514
515
} // reglist_insert
516
517
static RegisterList *reglist_find(const RegisterList *prev,
const RegisterType rtype, const int regnum)
518
{
519
520
const uint32 newval = reg_to_ui32(rtype, regnum);
RegisterList *item = prev->next;
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
while (item != NULL)
{
const uint32 val = reg_to_ui32(item->regtype, item->regnum);
if (newval == val)
return item; // here it is.
else if (newval < val) // should have been here if it existed.
return NULL;
else // if (newval > val)
{
// keep going, we're not to the insertion point yet.
prev = item;
item = item->next;
} // else
} // while
return NULL; // wasn't in the list.
537
538
539
540
541
542
543
} // reglist_find
static inline const RegisterList *reglist_exists(RegisterList *prev,
const RegisterType regtype,
const int regnum)
{
return (reglist_find(prev, regtype, regnum));
544
545
546
547
548
549
550
551
552
553
554
555
} // reglist_exists
static inline void set_used_register(Context *ctx, const RegisterType regtype,
const int regnum)
{
reglist_insert(ctx, &ctx->used_registers, regtype, regnum);
} // set_used_register
static inline int get_used_register(Context *ctx, const RegisterType regtype,
const int regnum)
{
return (reglist_exists(&ctx->used_registers, regtype, regnum) != NULL);
556
} // get_used_register
557
558
559
560
561
562
563
564
565
566
567
568
569
static inline void set_defined_register(Context *ctx, const RegisterType rtype,
const int regnum)
{
reglist_insert(ctx, &ctx->defined_registers, rtype, regnum);
} // set_defined_register
static inline int get_defined_register(Context *ctx, const RegisterType rtype,
const int regnum)
{
return (reglist_exists(&ctx->defined_registers, rtype, regnum) != NULL);
} // get_defined_register
570
571
static void add_attribute_register(Context *ctx, const RegisterType rtype,
const int regnum, const MOJOSHADER_usage usage,
572
const int index, const int writemask, int flags)
573
574
575
576
577
{
RegisterList *item = reglist_insert(ctx, &ctx->attributes, rtype, regnum);
item->usage = usage;
item->index = index;
item->writemask = writemask;
578
item->misc = flags;
579
580
} // add_attribute_register
581
582
583
static inline void add_sampler(Context *ctx, const RegisterType rtype,
const int regnum, const TextureType ttype)
{
584
// !!! FIXME: make sure it doesn't exist?
585
586
587
588
RegisterList *item = reglist_insert(ctx, &ctx->samplers, rtype, regnum);
item->index = (int) ttype;
} // add_sampler
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
static inline int writemask_xyzw(const int writemask)
{
return (writemask == 0xF); // 0xF == 1111. No explicit mask (full!).
} // writemask_xyzw
static inline int writemask_xyz(const int writemask)
{
return (writemask == 0x7); // 0x7 == 0111. (that is: xyz)
} // writemask_xyz
static inline int writemask_xy(const int writemask)
{
return (writemask == 0x3); // 0x3 == 0011. (that is: xy)
} // writemask_xy
static inline int writemask_x(const int writemask)
{
return (writemask == 0x1); // 0x1 == 0001. (that is: x)
} // writemask_x
static inline int writemask_y(const int writemask)
{
return (writemask == 0x2); // 0x1 == 0010. (that is: y)
} // writemask_y
620
621
622
623
624
625
626
627
static inline int replicate_swizzle(const int swizzle)
{
return ( (((swizzle >> 0) & 0x3) == ((swizzle >> 2) & 0x3)) &&
(((swizzle >> 2) & 0x3) == ((swizzle >> 4) & 0x3)) &&
(((swizzle >> 4) & 0x3) == ((swizzle >> 6) & 0x3)) );
} // replicate_swizzle
628
629
static inline int no_swizzle(const int swizzle)
{
630
return (swizzle == 0xE4); // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
631
632
633
} // no_swizzle
634
635
636
637
638
static inline int vecsize_from_writemask(const int m)
{
return (m & 1) + ((m >> 1) & 1) + ((m >> 2) & 1) + ((m >> 3) & 1);
} // vecsize_from_writemask
639
640
641
642
643
644
645
646
static int allocate_scratch_register(Context *ctx)
{
const int retval = ctx->scratch_registers++;
if (retval >= ctx->max_scratch_registers)
ctx->max_scratch_registers = retval + 1;
return retval;
} // allocate_scratch_register
647
static int allocate_branch_label(Context *ctx)
648
{
649
650
return ctx->assigned_branch_labels++;
} // allocate_branch_label
651
652
653
654
// D3D stuff that's used in more than just the d3d profile...
655
656
657
static const char swizzle_channels[] = { 'x', 'y', 'z', 'w' };
658
659
660
661
662
static const char *usagestrs[] = {
"_position", "_blendweight", "_blendindices", "_normal", "_psize",
"_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
"_color", "_fog", "_depth", "_sample"
};
663
664
static const char *get_D3D_register_string(Context *ctx,
665
RegisterType regtype,
666
667
int regnum, char *regnum_str,
size_t regnum_size)
668
{
669
const char *retval = NULL;
670
int has_number = 1;
671
672
switch (regtype)
673
{
674
case REG_TYPE_TEMP:
675
retval = "r";
676
677
break;
678
case REG_TYPE_INPUT:
679
retval = "v";
680
681
break;
682
case REG_TYPE_CONST:
683
684
685
retval = "c";
break;
686
case REG_TYPE_ADDRESS: // (or REG_TYPE_TEXTURE, same value.)
687
retval = shader_is_vertex(ctx) ? "a" : "t";
688
689
break;
690
case REG_TYPE_RASTOUT:
691
switch ((RastOutType) regnum)
692
{
693
694
695
case RASTOUT_TYPE_POSITION: retval = "oPos"; break;
case RASTOUT_TYPE_FOG: retval = "oFog"; break;
case RASTOUT_TYPE_POINT_SIZE: retval = "oPts"; break;
696
} // switch
697
has_number = 0;
698
699
break;
700
case REG_TYPE_ATTROUT:
701
retval = "oD";
702
703
break;
704
case REG_TYPE_OUTPUT: // (or REG_TYPE_TEXCRDOUT, same value.)
705
if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
706
retval = "o";
707
else
708
retval = "oT";
709
710
break;
711
case REG_TYPE_CONSTINT:
712
retval = "i";
713
714
break;
715
case REG_TYPE_COLOROUT:
716
retval = "oC";
717
718
break;
719
case REG_TYPE_DEPTHOUT:
720
retval = "oDepth";
721
has_number = 0;
722
723
break;
724
case REG_TYPE_SAMPLER:
725
retval = "s";
726
727
break;
728
case REG_TYPE_CONSTBOOL:
729
retval = "b";
730
731
break;
732
case REG_TYPE_LOOP:
733
retval = "aL";
734
has_number = 0;
735
736
break;
737
case REG_TYPE_MISCTYPE:
738
switch ((const MiscTypeType) regnum)
739
{
740
741
case MISCTYPE_TYPE_POSITION: retval = "vPos"; break;
case MISCTYPE_TYPE_FACE: retval = "vFace"; break;
742
} // switch
743
has_number = 0;
744
745
break;
746
case REG_TYPE_LABEL:
747
retval = "l";
748
749
break;
750
case REG_TYPE_PREDICATE:
751
retval = "p";
752
break;
753
754
755
756
757
758
759
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string
default:
fail(ctx, "unknown register type");
retval = "???";
has_number = 0;
break;
760
761
} // switch
762
763
764
765
766
if (has_number)
snprintf(regnum_str, regnum_size, "%u", (uint) regnum);
else
regnum_str[0] = '\0';
767
768
769
770
return retval;
} // get_D3D_register_string
771
772
773
774
775
776
777
778
779
#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,
780
static const char *make_D3D_srcarg_string_in_buf(Context *ctx,
781
782
const SourceArgInfo *arg,
char *buf, size_t buflen)
783
784
785
{
const char *premod_str = "";
const char *postmod_str = "";
786
switch (arg->src_mod)
787
788
789
790
791
792
793
794
795
796
797
{
case SRCMOD_NEGATE:
premod_str = "-";
break;
case SRCMOD_BIASNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_BIAS:
postmod_str = "_bias";
break;
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
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;
835
836
837
838
case SRCMOD_NONE:
case SRCMOD_TOTAL:
break; // stop compiler whining.
839
840
841
842
} // switch
char regnum_str[16];
843
844
845
const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
846
847
848
849
if (regtype_str == NULL)
{
fail(ctx, "Unknown source register type.");
850
return "";
851
852
} // if
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
const char *rel_lbracket = "";
const char *rel_rbracket = "";
char rel_swizzle[4] = { '\0' };
char rel_regnum_str[16] = { '\0' };
const char *rel_regtype_str = "";
if (arg->relative)
{
rel_swizzle[0] = '.';
rel_swizzle[1] = swizzle_channels[arg->relative_component];
rel_swizzle[2] = '\0';
rel_lbracket = "[";
rel_rbracket = "]";
rel_regtype_str = get_D3D_register_string(ctx, arg->relative_regtype,
arg->relative_regnum,
rel_regnum_str,
sizeof (rel_regnum_str));
if (regtype_str == NULL)
{
fail(ctx, "Unknown relative source register type.");
return "";
} // if
} // if
877
878
char swizzle_str[6];
int i = 0;
879
880
const int scalar = scalar_register(arg->regtype, arg->regnum);
if (!scalar && !no_swizzle(arg->swizzle))
881
882
{
swizzle_str[i++] = '.';
883
884
885
886
swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
swizzle_str[i++] = swizzle_channels[arg->swizzle_y];
swizzle_str[i++] = swizzle_channels[arg->swizzle_z];
swizzle_str[i++] = swizzle_channels[arg->swizzle_w];
887
888
889
890
// .xyzz is the same as .xyz, .z is the same as .zzzz, etc.
while (swizzle_str[i-1] == swizzle_str[i-2])
i--;
891
892
893
894
} // if
swizzle_str[i] = '\0';
assert(i < sizeof (swizzle_str));
895
896
897
898
snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s",
premod_str, regtype_str, regnum_str, postmod_str,
rel_lbracket, rel_regtype_str, rel_regnum_str, rel_swizzle,
rel_rbracket, swizzle_str);
899
// !!! FIXME: make sure the scratch buffer was large enough.
900
return buf;
901
} // make_D3D_srcarg_string_in_buf
902
903
904
static const char *make_D3D_destarg_string(Context *ctx)
905
{
906
const DestArgInfo *arg = &ctx->dest_arg;
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
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];
const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
if (regtype_str == NULL)
{
fail(ctx, "Unknown destination register type.");
return "";
} // if
char writemask_str[6];
int i = 0;
935
936
const int scalar = scalar_register(arg->regtype, arg->regnum);
if (!scalar && !writemask_xyzw(arg->writemask))
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
{
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));
const char *pred_left = "";
const char *pred_right = "";
char pred[32] = { '\0' };
if (ctx->predicated)
{
pred_left = "(";
pred_right = ") ";
954
make_D3D_srcarg_string_in_buf(ctx, &ctx->predicate_arg,
955
956
957
958
959
960
961
962
963
964
pred, sizeof (pred));
} // if
// may turn out something like "_x2_sat_pp_centroid (!p0.x) r0.xyzw" ...
char *retval = get_scratch_buffer(ctx);
snprintf(retval, SCRATCH_BUFFER_SIZE, "%s%s%s%s %s%s%s%s%s%s",
result_shift_str, sat_str, pp_str, cent_str,
pred_left, pred, pred_right,
regtype_str, regnum_str, writemask_str);
// !!! FIXME: make sure the scratch buffer was large enough.
965
return retval;
966
967
968
} // make_D3D_destarg_string
969
static const char *make_D3D_srcarg_string(Context *ctx, const int idx)
970
971
972
973
974
975
976
977
978
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
fail(ctx, "Too many source args");
return "";
} // if
const SourceArgInfo *arg = &ctx->source_args[idx];
char *buf = get_scratch_buffer(ctx);
979
980
return make_D3D_srcarg_string_in_buf(ctx, arg, buf, SCRATCH_BUFFER_SIZE);
} // make_D3D_srcarg_string
981
982
983
984
985
986
987
988
989
990
991
992
993
static const char *get_D3D_varname(Context *ctx, RegisterType rt, int regnum)
{
char regnum_str[16];
const char *regtype_str = get_D3D_register_string(ctx, rt, regnum,
regnum_str, sizeof (regnum_str));
char *retval = get_scratch_buffer(ctx);
snprintf(retval, SCRATCH_BUFFER_SIZE, "%s%s", regtype_str, regnum_str);
return retval;
} // get_D3D_varname
994
static const char *get_D3D_const_array_varname(Context *ctx, int base, int size)
995
{
996
997
998
char *retval = get_scratch_buffer(ctx);
snprintf(retval, SCRATCH_BUFFER_SIZE, "c_array_%d_%d", base, size);
return retval;
999
1000
} // get_D3D_const_array_varname