forked from espes/mojoshader
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmojoshader.c
9669 lines (8206 loc) · 323 KB
/
mojoshader.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.
*/
// !!! FIXME: this file really needs to be split up.
// !!! FIXME: I keep changing coding styles for symbols and typedefs.
// !!! FIXME: rules from MSDN about temp registers we probably don't check.
// - There are limited temporaries: vs_1_1 has 12 (ps_1_1 has _2_!).
// - SM2 apparently was variable, between 12 and 32. Shader Model 3 has 32.
// - A maximum of three temp registers can be used in a single instruction.
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
typedef struct ConstantsList
{
MOJOSHADER_constant constant;
struct ConstantsList *next;
} ConstantsList;
typedef struct VariableList
{
MOJOSHADER_uniformType type;
int index;
int count;
ConstantsList *constant;
int used;
int emit_position; // used in some profiles.
struct VariableList *next;
} VariableList;
typedef struct RegisterList
{
RegisterType regtype;
int regnum;
MOJOSHADER_usage usage;
unsigned int index;
int writemask;
int misc;
int written;
const VariableList *array;
struct RegisterList *next;
} RegisterList;
typedef struct
{
const uint32 *token; // this is the unmolested token in the stream.
int regnum;
int swizzle; // xyzw (all four, not split out).
int swizzle_x;
int swizzle_y;
int swizzle_z;
int swizzle_w;
SourceMod src_mod;
RegisterType regtype;
int relative;
RegisterType relative_regtype;
int relative_regnum;
int relative_component;
const VariableList *relative_array;
} SourceArgInfo;
struct Profile; // predeclare.
typedef struct CtabData
{
int have_ctab;
int symbol_count;
MOJOSHADER_symbol *symbols;
} CtabData;
// Context...this is state that changes as we parse through a shader...
typedef struct Context
{
int isfail;
int out_of_memory;
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
int current_position;
const uint32 *orig_tokens;
const uint32 *tokens;
uint32 tokencount;
const MOJOSHADER_swizzle *swizzles;
unsigned int swizzles_count;
const MOJOSHADER_samplerMap *samplermap;
unsigned int samplermap_count;
Buffer *output;
Buffer *preflight;
Buffer *globals;
Buffer *helpers;
Buffer *subroutines;
Buffer *mainline_intro;
Buffer *mainline;
Buffer *ignore;
Buffer *output_stack[2];
int indent_stack[2];
int output_stack_len;
int indent;
const char *shader_type_str;
const char *endline;
int endline_len;
int profileid;
const struct Profile *profile;
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
DestArgInfo dest_arg;
SourceArgInfo source_args[5];
SourceArgInfo predicate_arg; // for predicated instructions.
uint32 dwords[4];
uint32 version_token;
int instruction_count;
uint32 instruction_controls;
uint32 previous_opcode;
int coissue;
int loops;
int reps;
int max_reps;
int cmps;
int scratch_registers;
int max_scratch_registers;
int branch_labels_stack_index;
int branch_labels_stack[32];
int assigned_branch_labels;
int assigned_vertex_attributes;
int last_address_reg_component;
RegisterList used_registers;
RegisterList defined_registers;
ErrorList *errors;
int constant_count;
ConstantsList *constants;
int uniform_count;
int uniform_float4_count;
int uniform_int4_count;
int uniform_bool_count;
RegisterList uniforms;
int attribute_count;
RegisterList attributes;
int sampler_count;
RegisterList samplers;
VariableList *variables; // variables to register mapping.
int centroid_allowed;
CtabData ctab;
int have_relative_input_registers;
int have_multi_color_outputs;
int determined_constants_arrays;
int predicated;
int uses_pointsize;
int uses_fog;
int glsl_generated_lit_helper;
int glsl_generated_texldd_setup;
int glsl_generated_texm3x3spec_helper;
int arb1_wrote_position;
int have_preshader;
int ignores_ctab;
int reset_texmpad;
int texm3x2pad_dst0;
int texm3x2pad_src0;
int texm3x3pad_dst0;
int texm3x3pad_src0;
int texm3x3pad_dst1;
int texm3x3pad_src1;
MOJOSHADER_preshader *preshader;
#if SUPPORT_PROFILE_ARB1_NV
int profile_supports_nv2;
int profile_supports_nv3;
int profile_supports_nv4;
#endif
#if SUPPORT_PROFILE_GLSL120
int profile_supports_glsl120;
#endif
} Context;
// Use these macros so we can remove all bits of these profiles from the build.
#if SUPPORT_PROFILE_ARB1_NV
#define support_nv2(ctx) ((ctx)->profile_supports_nv2)
#define support_nv3(ctx) ((ctx)->profile_supports_nv3)
#define support_nv4(ctx) ((ctx)->profile_supports_nv4)
#else
#define support_nv2(ctx) (0)
#define support_nv3(ctx) (0)
#define support_nv4(ctx) (0)
#endif
#if SUPPORT_PROFILE_GLSL120
#define support_glsl120(ctx) ((ctx)->profile_supports_glsl120)
#else
#define support_glsl120(ctx) (0)
#endif
// Profile entry points...
// one emit function for each opcode in each profile.
typedef void (*emit_function)(Context *ctx);
// one emit function for starting output in each profile.
typedef void (*emit_start)(Context *ctx, const char *profilestr);
// one emit function for ending output in each profile.
typedef void (*emit_end)(Context *ctx);
// one emit function for phase opcode output in each profile.
typedef void (*emit_phase)(Context *ctx);
// 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);
// one emit function for relative uniform arrays in each profile.
typedef void (*emit_array)(Context *ctx, VariableList *var);
// 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);
// one emit function for uniforms in each profile.
typedef void (*emit_uniform)(Context *ctx, RegisterType regtype, int regnum,
const VariableList *var);
// one emit function for samplers in each profile.
typedef void (*emit_sampler)(Context *ctx, int stage, TextureType ttype,
int texbem);
// one emit function for attributes in each profile.
typedef void (*emit_attribute)(Context *ctx, RegisterType regtype, int regnum,
MOJOSHADER_usage usage, int index, int wmask,
int flags);
// one args function for each possible sequence of opcode arguments.
typedef int (*args_function)(Context *ctx);
// one state function for each opcode where we have state machine updates.
typedef void (*state_function)(Context *ctx);
// 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.
typedef const char *(*const_array_varname_function)(Context *c, int base, int size);
typedef struct Profile
{
const char *name;
emit_start start_emitter;
emit_end end_emitter;
emit_phase phase_emitter;
emit_global global_emitter;
emit_array array_emitter;
emit_const_array const_array_emitter;
emit_uniform uniform_emitter;
emit_sampler sampler_emitter;
emit_attribute attribute_emitter;
emit_finalize finalize_emitter;
varname_function get_varname;
const_array_varname_function get_const_array_varname;
} Profile;
// Convenience functions for allocators...
#if !MOJOSHADER_FORCE_ALLOCATOR
void *MOJOSHADER_internal_malloc(int bytes, void *d) { return malloc(bytes); }
void MOJOSHADER_internal_free(void *ptr, void *d) { free(ptr); }
#endif
MOJOSHADER_error MOJOSHADER_out_of_mem_error = {
"Out of memory", NULL, MOJOSHADER_POSITION_NONE
};
MOJOSHADER_parseData MOJOSHADER_out_of_mem_data = {
1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0,
MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0
};
// !!! FIXME: cut and paste between every damned source file follows...
// !!! FIXME: We need to make some sort of ContextBase that applies to all
// !!! FIXME: files and move this stuff to mojoshader_common.c ...
static inline void out_of_memory(Context *ctx)
{
ctx->isfail = ctx->out_of_memory = 1;
} // out_of_memory
static inline void *Malloc(Context *ctx, const size_t len)
{
void *retval = ctx->malloc((int) len, ctx->malloc_data);
if (retval == NULL)
out_of_memory(ctx);
return retval;
} // Malloc
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
if (retval != NULL)
strcpy(retval, str);
return retval;
} // StrDup
static inline void Free(Context *ctx, void *ptr)
{
ctx->free(ptr, ctx->malloc_data);
} // Free
static void *MallocBridge(int bytes, void *data)
{
return Malloc((Context *) data, (size_t) bytes);
} // MallocBridge
static void FreeBridge(void *ptr, void *data)
{
Free((Context *) data, ptr);
} // FreeBridge
// jump between output sections in the context...
static int set_output(Context *ctx, Buffer **section)
{
// only create output sections on first use.
if (*section == NULL)
{
*section = buffer_create(256, MallocBridge, FreeBridge, ctx);
if (*section == NULL)
return 0;
} // if
ctx->output = *section;
return 1;
} // set_output
static void push_output(Context *ctx, Buffer **section)
{
assert(ctx->output_stack_len < (int) (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++;
if (!set_output(ctx, section))
return;
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...
static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
{
return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 1 : (minor)) );
} // version_ui32
static inline int shader_version_supported(const uint8 maj, const uint8 min)
{
return (ver_ui32(maj,min) <= ver_ui32(MAX_SHADER_MAJOR, MAX_SHADER_MINOR));
} // shader_version_supported
static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
const uint8 min)
{
return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
} // shader_version_atleast
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
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
static inline int isfail(const Context *ctx)
{
return ctx->isfail;
} // isfail
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void failf(Context *ctx, const char *fmt, ...)
{
ctx->isfail = 1;
if (ctx->out_of_memory)
return;
// no filename at this level (we pass a NULL to errorlist_add_va()...)
va_list ap;
va_start(ap, fmt);
errorlist_add_va(ctx->errors, NULL, ctx->current_position, fmt, ap);
va_end(ap);
} // failf
static inline void fail(Context *ctx, const char *reason)
{
failf(ctx, "%s", reason);
} // fail
static void output_line(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void output_line(Context *ctx, const char *fmt, ...)
{
assert(ctx->output != NULL);
if (isfail(ctx))
return; // we failed previously, don't go on...
const int indent = ctx->indent;
if (indent > 0)
{
char *indentbuf = (char *) alloca(indent);
memset(indentbuf, '\t', indent);
buffer_append(ctx->output, indentbuf, indent);
} // if
va_list ap;
va_start(ap, fmt);
buffer_append_va(ctx->output, fmt, ap);
va_end(ap);
buffer_append(ctx->output, ctx->endline, ctx->endline_len);
} // output_line
static inline void output_blank_line(Context *ctx)
{
assert(ctx->output != NULL);
if (!isfail(ctx))
buffer_append(ctx->output, ctx->endline, ctx->endline_len);
} // output_blank_line
// !!! FIXME: this is sort of nasty.
static void floatstr(Context *ctx, char *buf, size_t bufsize, float f,
int leavedecimal)
{
const size_t len = snprintf(buf, bufsize, "%f", f);
if ((len+2) >= bufsize)
fail(ctx, "BUG: internal buffer is too small");
else
{
char *end = buf + len;
char *ptr = strchr(buf, '.');
if (ptr == NULL)
{
if (leavedecimal)
strcat(buf, ".0");
return; // done.
} // if
while (--end != ptr)
{
if (*end != '0')
{
end++;
break;
} // if
} // while
if ((leavedecimal) && (end == ptr))
end += 2;
*end = '\0'; // chop extra '0' or all decimal places off.
} // else
} // floatstr
static inline TextureType cvtMojoToD3DSamplerType(const MOJOSHADER_samplerType type)
{
return (TextureType) (((int) type) + 2);
} // cvtMojoToD3DSamplerType
static inline MOJOSHADER_samplerType cvtD3DToMojoSamplerType(const TextureType type)
{
return (MOJOSHADER_samplerType) (((int) type) - 2);
} // cvtD3DToMojoSamplerType
// Deal with register lists... !!! FIXME: I sort of hate this.
static void free_reglist(MOJOSHADER_free f, void *d, RegisterList *item)
{
while (item != NULL)
{
RegisterList *next = item->next;
f(item, d);
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
// !!! FIXME: ditch this for a hash table.
static RegisterList *reglist_insert(Context *ctx, RegisterList *prev,
const RegisterType regtype,
const int regnum)
{
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)
return item; // already set, so we're done.
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).
item = (RegisterList *) Malloc(ctx, sizeof (RegisterList));
if (item != NULL)
{
item->regtype = regtype;
item->regnum = regnum;
item->usage = MOJOSHADER_USAGE_UNKNOWN;
item->index = 0;
item->writemask = 0;
item->misc = 0;
item->array = NULL;
item->next = prev->next;
prev->next = item;
} // if
return item;
} // reglist_insert
static RegisterList *reglist_find(const RegisterList *prev,
const RegisterType rtype, const int regnum)
{
const uint32 newval = reg_to_ui32(rtype, regnum);
RegisterList *item = prev->next;
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)
item = item->next;
} // while
return NULL; // wasn't in the list.
} // reglist_find
static inline const RegisterList *reglist_exists(RegisterList *prev,
const RegisterType regtype,
const int regnum)
{
return (reglist_find(prev, regtype, regnum));
} // reglist_exists
static inline int register_was_written(Context *ctx, const RegisterType rtype,
const int regnum)
{
RegisterList *reg = reglist_find(&ctx->used_registers, rtype, regnum);
return (reg && reg->written);
} // register_was_written
static inline RegisterList *set_used_register(Context *ctx,
const RegisterType regtype,
const int regnum,
const int written)
{
RegisterList *reg = NULL;
if ((regtype == REG_TYPE_COLOROUT) && (regnum > 0))
ctx->have_multi_color_outputs = 1;
reg = reglist_insert(ctx, &ctx->used_registers, regtype, regnum);
if (reg && written)
reg->written = 1;
return reg;
} // 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);
} // get_used_register
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
static void add_attribute_register(Context *ctx, const RegisterType rtype,
const int regnum, const MOJOSHADER_usage usage,
const int index, const int writemask, int flags)
{
RegisterList *item = reglist_insert(ctx, &ctx->attributes, rtype, regnum);
item->usage = usage;
item->index = index;
item->writemask = writemask;
item->misc = flags;
if ((rtype == REG_TYPE_OUTPUT) && (usage == MOJOSHADER_USAGE_POINTSIZE))
ctx->uses_pointsize = 1; // note that we have to check this later.
else if ((rtype == REG_TYPE_OUTPUT) && (usage == MOJOSHADER_USAGE_FOG))
ctx->uses_fog = 1; // note that we have to check this later.
} // add_attribute_register
static inline void add_sampler(Context *ctx, const int regnum,
TextureType ttype, const int texbem)
{
const RegisterType rtype = REG_TYPE_SAMPLER;
// !!! FIXME: make sure it doesn't exist?
// !!! FIXME: (ps_1_1 assume we can add it multiple times...)
RegisterList *item = reglist_insert(ctx, &ctx->samplers, rtype, regnum);
if (ctx->samplermap != NULL)
{
unsigned int i;
for (i = 0; i < ctx->samplermap_count; i++)
{
if (ctx->samplermap[i].index == regnum)
{
ttype = cvtMojoToD3DSamplerType(ctx->samplermap[i].type);
break;
} // if
} // for
} // if
item->index = (int) ttype;
item->misc |= texbem;
} // add_sampler
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
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
static inline int no_swizzle(const int swizzle)
{
return (swizzle == 0xE4); // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
} // no_swizzle
static inline int vecsize_from_writemask(const int m)
{
return (m & 1) + ((m >> 1) & 1) + ((m >> 2) & 1) + ((m >> 3) & 1);
} // vecsize_from_writemask
static inline void set_dstarg_writemask(DestArgInfo *dst, const int mask)
{
dst->writemask = mask;
dst->writemask0 = ((mask >> 0) & 1);
dst->writemask1 = ((mask >> 1) & 1);
dst->writemask2 = ((mask >> 2) & 1);
dst->writemask3 = ((mask >> 3) & 1);
} // set_dstarg_writemask
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
static int allocate_branch_label(Context *ctx)
{
return ctx->assigned_branch_labels++;
} // allocate_branch_label
static inline void adjust_token_position(Context *ctx, const int incr)
{
ctx->tokens += incr;
ctx->tokencount -= incr;
ctx->current_position += incr * sizeof (uint32);
} // adjust_token_position
// D3D stuff that's used in more than just the d3d profile...
static int isscalar(Context *ctx, const MOJOSHADER_shaderType shader_type,
const RegisterType rtype, const int rnum)
{
const int uses_psize = ctx->uses_pointsize;
const int uses_fog = ctx->uses_fog;
if ( (rtype == REG_TYPE_OUTPUT) && ((uses_psize) || (uses_fog)) )
{
const RegisterList *reg = reglist_find(&ctx->attributes, rtype, rnum);
if (reg != NULL)
{
const MOJOSHADER_usage usage = reg->usage;
return ( (uses_psize && (usage == MOJOSHADER_USAGE_POINTSIZE)) ||
(uses_fog && (usage == MOJOSHADER_USAGE_FOG)) );
} // if
} // if
return scalar_register(shader_type, rtype, rnum);
} // isscalar
static const char swizzle_channels[] = { 'x', 'y', 'z', 'w' };
static const char *usagestrs[] = {
"_position", "_blendweight", "_blendindices", "_normal", "_psize",
"_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
"_color", "_fog", "_depth", "_sample"
};
static const char *get_D3D_register_string(Context *ctx,
RegisterType regtype,
int regnum, char *regnum_str,
size_t regnum_size)
{
const char *retval = NULL;
int has_number = 1;
switch (regtype)
{
case REG_TYPE_TEMP:
retval = "r";
break;
case REG_TYPE_INPUT:
retval = "v";
break;
case REG_TYPE_CONST:
retval = "c";
break;
case REG_TYPE_ADDRESS: // (or REG_TYPE_TEXTURE, same value.)
retval = shader_is_vertex(ctx) ? "a" : "t";
break;
case REG_TYPE_RASTOUT:
switch ((RastOutType) regnum)
{
case RASTOUT_TYPE_POSITION: retval = "oPos"; break;
case RASTOUT_TYPE_FOG: retval = "oFog"; break;
case RASTOUT_TYPE_POINT_SIZE: retval = "oPts"; break;
} // switch
has_number = 0;
break;
case REG_TYPE_ATTROUT:
retval = "oD";
break;
case REG_TYPE_OUTPUT: // (or REG_TYPE_TEXCRDOUT, same value.)
if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
retval = "o";
else
retval = "oT";
break;
case REG_TYPE_CONSTINT:
retval = "i";
break;
case REG_TYPE_COLOROUT:
retval = "oC";
break;
case REG_TYPE_DEPTHOUT:
retval = "oDepth";
has_number = 0;
break;
case REG_TYPE_SAMPLER:
retval = "s";
break;
case REG_TYPE_CONSTBOOL:
retval = "b";
break;
case REG_TYPE_LOOP:
retval = "aL";
has_number = 0;
break;
case REG_TYPE_MISCTYPE:
switch ((const MiscTypeType) regnum)
{
case MISCTYPE_TYPE_POSITION: retval = "vPos"; break;
case MISCTYPE_TYPE_FACE: retval = "vFace"; break;
} // switch
has_number = 0;
break;
case REG_TYPE_LABEL:
retval = "l";
break;
case REG_TYPE_PREDICATE:
retval = "p";
break;
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string
default:
fail(ctx, "unknown register type");
retval = "???";
has_number = 0;
break;
} // switch
if (has_number)
snprintf(regnum_str, regnum_size, "%u", (uint) regnum);
else
regnum_str[0] = '\0';
return retval;
} // get_D3D_register_string
// !!! FIXME: can we split the profile code out to separate source files?
#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,
static const char *make_D3D_srcarg_string_in_buf(Context *ctx,
const SourceArgInfo *arg,
char *buf, size_t buflen)
{
const char *premod_str = "";
const char *postmod_str = "";
switch (arg->src_mod)
{
case SRCMOD_NEGATE:
premod_str = "-";
break;
case SRCMOD_BIASNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_BIAS:
postmod_str = "_bias";
break;
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;
case SRCMOD_NONE:
case SRCMOD_TOTAL:
break; // stop compiler whining.
} // switch
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 source register type.");
*buf = '\0';
return buf;
} // if
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.");
*buf = '\0';
return buf;
} // if
} // if
char swizzle_str[6];
size_t i = 0;
const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
if (!scalar && !no_swizzle(arg->swizzle))