Skip to content

Latest commit

 

History

History
2252 lines (1922 loc) · 79.5 KB

mojoshader_profile_arb1.c

File metadata and controls

2252 lines (1922 loc) · 79.5 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_profile.h"
Apr 23, 2019
Apr 23, 2019
13
14
#pragma GCC visibility push(hidden)
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
#if SUPPORT_PROFILE_ARB1
static inline const char *get_ARB1_register_string(Context *ctx,
const RegisterType regtype, const int regnum,
char *regnum_str, const size_t regnum_size)
{
// turns out these are identical at the moment.
return get_D3D_register_string(ctx,regtype,regnum,regnum_str,regnum_size);
} // get_ARB1_register_string
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
int allocate_branch_label(Context *ctx)
{
return ctx->assigned_branch_labels++;
} // allocate_branch_label
const char *allocate_ARB1_scratch_reg_name(Context *ctx, char *buf,
const size_t buflen)
{
const int scratch = allocate_scratch_register(ctx);
snprintf(buf, buflen, "scratch%d", scratch);
return buf;
} // allocate_ARB1_scratch_reg_name
static inline const char *get_ARB1_branch_label_name(Context *ctx, const int id,
char *buf, const size_t buflen)
{
snprintf(buf, buflen, "branch_label%d", id);
return buf;
} // get_ARB1_branch_label_name
const char *get_ARB1_varname_in_buf(Context *ctx, const RegisterType rt,
const int regnum, char *buf,
const size_t buflen)
{
// turns out these are identical at the moment.
return get_D3D_varname_in_buf(ctx, rt, regnum, buf, buflen);
} // get_ARB1_varname_in_buf
const char *get_ARB1_varname(Context *ctx, const RegisterType rt,
const int regnum)
{
// turns out these are identical at the moment.
return get_D3D_varname(ctx, rt, regnum);
} // get_ARB1_varname
static inline const char *get_ARB1_const_array_varname_in_buf(Context *ctx,
const int base, const int size,
char *buf, const size_t buflen)
{
snprintf(buf, buflen, "c_array_%d_%d", base, size);
return buf;
} // get_ARB1_const_array_varname_in_buf
const char *get_ARB1_const_array_varname(Context *ctx, int base, int size)
{
char buf[64];
get_ARB1_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
return StrDup(ctx, buf);
} // get_ARB1_const_array_varname
const char *make_ARB1_srcarg_string_in_buf(Context *ctx,
const SourceArgInfo *arg,
char *buf, size_t buflen)
{
// !!! FIXME: this can hit pathological cases where we look like this...
//
// dp3 r1.xyz, t0_bx2, t0_bx2
// mad r1.xyz, t0_bias, 1-r1, t0_bx2
//
// ...which do a lot of duplicate work in arb1...
//
// SUB scratch0, t0, { 0.5, 0.5, 0.5, 0.5 };
// MUL scratch0, scratch0, { 2.0, 2.0, 2.0, 2.0 };
// SUB scratch1, t0, { 0.5, 0.5, 0.5, 0.5 };
// MUL scratch1, scratch1, { 2.0, 2.0, 2.0, 2.0 };
// DP3 r1.xyz, scratch0, scratch1;
// SUB scratch0, t0, { 0.5, 0.5, 0.5, 0.5 };
// SUB scratch1, { 1.0, 1.0, 1.0, 1.0 }, r1;
// SUB scratch2, t0, { 0.5, 0.5, 0.5, 0.5 };
// MUL scratch2, scratch2, { 2.0, 2.0, 2.0, 2.0 };
// MAD r1.xyz, scratch0, scratch1, scratch2;
//
// ...notice that the dp3 calculates the same value into two scratch
// registers. This case is easier to handle; just see if multiple
// source args are identical, build it up once, and use the same
// scratch register for multiple arguments in that opcode.
// Even better still, only calculate things once across instructions,
// and be smart about letting it linger in a scratch register until we
// definitely don't need the calculation anymore. That's harder to
// write, though.
char regnum_str[16] = { '\0' };
// !!! FIXME: use get_ARB1_varname_in_buf() instead?
const char *regtype_str = NULL;
if (!arg->relative)
{
regtype_str = get_ARB1_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
} // if
const char *rel_lbracket = "";
char rel_offset[32] = { '\0' };
const char *rel_rbracket = "";
char rel_swizzle[4] = { '\0' };
const char *rel_regtype_str = "";
if (arg->relative)
{
rel_regtype_str = get_ARB1_varname_in_buf(ctx, arg->relative_regtype,
arg->relative_regnum,
(char *) alloca(64), 64);
rel_swizzle[0] = '.';
rel_swizzle[1] = swizzle_channels[arg->relative_component];
rel_swizzle[2] = '\0';
if (!support_nv2(ctx))
{
// The address register in ARB1 only allows the '.x' component, so
// we need to load the component we need from a temp vector
// register into .x as needed.
assert(arg->relative_regtype == REG_TYPE_ADDRESS);
assert(arg->relative_regnum == 0);
if (ctx->last_address_reg_component != arg->relative_component)
{
output_line(ctx, "ARL %s.x, addr%d.%c;", rel_regtype_str,
arg->relative_regnum,
swizzle_channels[arg->relative_component]);
ctx->last_address_reg_component = arg->relative_component;
} // if
rel_swizzle[1] = 'x';
} // if
if (arg->regtype == REG_TYPE_INPUT)
regtype_str = "vertex.attrib";
else
{
assert(arg->regtype == REG_TYPE_CONST);
const int arrayidx = arg->relative_array->index;
const int arraysize = arg->relative_array->count;
const int offset = arg->regnum - arrayidx;
assert(offset >= 0);
regtype_str = get_ARB1_const_array_varname_in_buf(ctx, arrayidx,
arraysize, (char *) alloca(64), 64);
if (offset != 0)
snprintf(rel_offset, sizeof (rel_offset), " + %d", offset);
} // else
rel_lbracket = "[";
rel_rbracket = "]";
} // if
// This is the source register with everything but swizzle and source mods.
snprintf(buf, buflen, "%s%s%s%s%s%s%s", regtype_str, regnum_str,
rel_lbracket, rel_regtype_str, rel_swizzle, rel_offset,
rel_rbracket);
// Some of the source mods need to generate instructions to a temp
// register, in which case we'll replace the register name.
const SourceMod mod = arg->src_mod;
const int inplace = ( (mod == SRCMOD_NONE) || (mod == SRCMOD_NEGATE) ||
((mod == SRCMOD_ABS) && support_nv2(ctx)) );
if (!inplace)
{
const size_t len = 64;
char *stackbuf = (char *) alloca(len);
regtype_str = allocate_ARB1_scratch_reg_name(ctx, stackbuf, len);
regnum_str[0] = '\0'; // move value to scratch register.
rel_lbracket = ""; // scratch register won't use array.
rel_rbracket = "";
rel_offset[0] = '\0';
rel_swizzle[0] = '\0';
rel_regtype_str = "";
} // if
const char *premod_str = "";
const char *postmod_str = "";
switch (mod)
{
case SRCMOD_NEGATE:
premod_str = "-";
break;
case SRCMOD_BIASNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_BIAS:
output_line(ctx, "SUB %s, %s, { 0.5, 0.5, 0.5, 0.5 };",
regtype_str, buf);
break;
case SRCMOD_SIGNNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_SIGN:
output_line(ctx,
"MAD %s, %s, { 2.0, 2.0, 2.0, 2.0 }, { -1.0, -1.0, -1.0, -1.0 };",
regtype_str, buf);
break;
case SRCMOD_COMPLEMENT:
output_line(ctx, "SUB %s, { 1.0, 1.0, 1.0, 1.0 }, %s;",
regtype_str, buf);
break;
case SRCMOD_X2NEGATE:
premod_str = "-";
// fall through.
case SRCMOD_X2:
output_line(ctx, "MUL %s, %s, { 2.0, 2.0, 2.0, 2.0 };",
regtype_str, buf);
break;
case SRCMOD_DZ:
fail(ctx, "SRCMOD_DZ currently unsupported in arb1");
postmod_str = "_dz";
break;
case SRCMOD_DW:
fail(ctx, "SRCMOD_DW currently unsupported in arb1");
postmod_str = "_dw";
break;
case SRCMOD_ABSNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_ABS:
if (!support_nv2(ctx)) // GL_NV_vertex_program2_option adds this.
output_line(ctx, "ABS %s, %s;", regtype_str, buf);
else
{
premod_str = (mod == SRCMOD_ABSNEGATE) ? "-|" : "|";
postmod_str = "|";
} // else
break;
case SRCMOD_NOT:
fail(ctx, "SRCMOD_NOT currently unsupported in arb1");
premod_str = "!";
break;
case SRCMOD_NONE:
case SRCMOD_TOTAL:
break; // stop compiler whining.
} // switch
char swizzle_str[6];
size_t i = 0;
if (support_nv4(ctx)) // vFace must be output as "vFace.x" in nv4.
{
if (arg->regtype == REG_TYPE_MISCTYPE)
{
if ( ((const MiscTypeType) arg->regnum) == MISCTYPE_TYPE_FACE )
{
swizzle_str[i++] = '.';
swizzle_str[i++] = 'x';
} // if
} // if
} // if
const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
if (!scalar && !no_swizzle(arg->swizzle))
{
swizzle_str[i++] = '.';
// .xxxx is the same as .x, but .xx is illegal...scalar or full!
if (replicate_swizzle(arg->swizzle))
swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
else
{
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];
} // else
} // if
swizzle_str[i] = '\0';
assert(i < sizeof (swizzle_str));
snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s", premod_str,
regtype_str, regnum_str, rel_lbracket,
rel_regtype_str, rel_swizzle, rel_offset, rel_rbracket,
swizzle_str, postmod_str);
// !!! FIXME: make sure the scratch buffer was large enough.
return buf;
} // make_ARB1_srcarg_string_in_buf
const char *get_ARB1_destarg_varname(Context *ctx, char *buf,
const size_t buflen)
{
const DestArgInfo *arg = &ctx->dest_arg;
return get_ARB1_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, buflen);
} // get_ARB1_destarg_varname
const char *get_ARB1_srcarg_varname(Context *ctx, const size_t idx,
char *buf, const size_t buflen)
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
fail(ctx, "Too many source args");
*buf = '\0';
return buf;
} // if
const SourceArgInfo *arg = &ctx->source_args[idx];
return get_ARB1_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, buflen);
} // get_ARB1_srcarg_varname
const char *make_ARB1_destarg_string(Context *ctx, char *buf,
const size_t buflen)
{
const DestArgInfo *arg = &ctx->dest_arg;
*buf = '\0';
const char *sat_str = "";
if (arg->result_mod & MOD_SATURATE)
{
// nv4 can use ".SAT" in all program types.
// For less than nv4, the "_SAT" modifier is only available in
// fragment shaders. Every thing else will fake it later in
// emit_ARB1_dest_modifiers() ...
if (support_nv4(ctx))
sat_str = ".SAT";
else if (shader_is_pixel(ctx))
sat_str = "_SAT";
} // if
const char *pp_str = "";
if (arg->result_mod & MOD_PP)
{
// Most ARB1 profiles can't do partial precision (MOD_PP), but that's
// okay. The spec says lots of Direct3D implementations ignore the
// flag anyhow.
if (support_nv4(ctx))
pp_str = "H";
} // if
// CENTROID only allowed in DCL opcodes, which shouldn't come through here.
assert((arg->result_mod & MOD_CENTROID) == 0);
char regnum_str[16];
const char *regtype_str = get_ARB1_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
if (regtype_str == NULL)
{
fail(ctx, "Unknown destination register type.");
return buf;
} // if
char writemask_str[6];
size_t i = 0;
const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
if (!scalar && !writemask_xyzw(arg->writemask))
{
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)
{
fail(ctx, "dest register predication currently unsupported in arb1");
return buf;
//pred_left = "(";
//pred_right = ") ";
make_ARB1_srcarg_string_in_buf(ctx, &ctx->predicate_arg,
pred, sizeof (pred));
} // if
snprintf(buf, buflen, "%s%s %s%s%s", pp_str, sat_str,
regtype_str, regnum_str, writemask_str);
// !!! FIXME: make sure the scratch buffer was large enough.
return buf;
} // make_ARB1_destarg_string
void emit_ARB1_dest_modifiers(Context *ctx)
{
const DestArgInfo *arg = &ctx->dest_arg;
if (arg->result_shift != 0x0)
{
char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
const char *multiplier = NULL;
switch (arg->result_shift)
{
case 0x1: multiplier = "2.0"; break;
case 0x2: multiplier = "4.0"; break;
case 0x3: multiplier = "8.0"; break;
case 0xD: multiplier = "0.125"; break;
case 0xE: multiplier = "0.25"; break;
case 0xF: multiplier = "0.5"; break;
} // switch
if (multiplier != NULL)
{
char var[64]; get_ARB1_destarg_varname(ctx, var, sizeof (var));
output_line(ctx, "MUL%s, %s, %s;", dst, var, multiplier);
} // if
} // if
if (arg->result_mod & MOD_SATURATE)
{
// nv4 and/or pixel shaders just used the "SAT" modifier, instead.
if ( (!support_nv4(ctx)) && (!shader_is_pixel(ctx)) )
{
char var[64]; get_ARB1_destarg_varname(ctx, var, sizeof (var));
char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
output_line(ctx, "MIN%s, %s, 1.0;", dst, var);
output_line(ctx, "MAX%s, %s, 0.0;", dst, var);
} // if
} // if
} // emit_ARB1_dest_modifiers
const char *make_ARB1_srcarg_string(Context *ctx, const size_t idx,
char *buf, const size_t buflen)
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
fail(ctx, "Too many source args");
*buf = '\0';
return buf;
} // if
const SourceArgInfo *arg = &ctx->source_args[idx];
return make_ARB1_srcarg_string_in_buf(ctx, arg, buf, buflen);
} // make_ARB1_srcarg_string
void emit_ARB1_opcode_ds(Context *ctx, const char *opcode)
{
char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
output_line(ctx, "%s%s, %s;", opcode, dst, src0);
emit_ARB1_dest_modifiers(ctx);
} // emit_ARB1_opcode_ds
void emit_ARB1_opcode_dss(Context *ctx, const char *opcode)
{
char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
output_line(ctx, "%s%s, %s, %s;", opcode, dst, src0, src1);
emit_ARB1_dest_modifiers(ctx);
} // emit_ARB1_opcode_dss
void emit_ARB1_opcode_dsss(Context *ctx, const char *opcode)
{
char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
output_line(ctx, "%s%s, %s, %s, %s;", opcode, dst, src0, src1, src2);
emit_ARB1_dest_modifiers(ctx);
} // emit_ARB1_opcode_dsss
#define EMIT_ARB1_OPCODE_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
emit_ARB1_opcode(ctx, #op); \
}
#define EMIT_ARB1_OPCODE_D_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
emit_ARB1_opcode_d(ctx, #op); \
}
#define EMIT_ARB1_OPCODE_S_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
emit_ARB1_opcode_s(ctx, #op); \
}
#define EMIT_ARB1_OPCODE_SS_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
emit_ARB1_opcode_ss(ctx, #op); \
}
#define EMIT_ARB1_OPCODE_DS_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
emit_ARB1_opcode_ds(ctx, #op); \
}
#define EMIT_ARB1_OPCODE_DSS_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
emit_ARB1_opcode_dss(ctx, #op); \
}
#define EMIT_ARB1_OPCODE_DSSS_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
emit_ARB1_opcode_dsss(ctx, #op); \
}
#define EMIT_ARB1_OPCODE_DSSSS_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
emit_ARB1_opcode_dssss(ctx, #op); \
}
#define EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(op) \
void emit_ARB1_##op(Context *ctx) { \
failf(ctx, #op " unimplemented in %s profile", ctx->profile->name); \
}
void emit_ARB1_start(Context *ctx, const char *profilestr)
{
const char *shader_str = NULL;
const char *shader_full_str = NULL;
if (shader_is_vertex(ctx))
{
shader_str = "vp";
shader_full_str = "vertex";
} // if
else if (shader_is_pixel(ctx))
{
shader_str = "fp";
shader_full_str = "fragment";
} // else if
else
{
failf(ctx, "Shader type %u unsupported in this profile.",
(uint) ctx->shader_type);
return;
} // if
set_output(ctx, &ctx->preflight);
if (strcmp(profilestr, MOJOSHADER_PROFILE_ARB1) == 0)
output_line(ctx, "!!ARB%s1.0", shader_str);
#if SUPPORT_PROFILE_ARB1_NV
else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV2) == 0)
{
ctx->profile_supports_nv2 = 1;
output_line(ctx, "!!ARB%s1.0", shader_str);
output_line(ctx, "OPTION NV_%s_program2;", shader_full_str);
} // else if
else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV3) == 0)
{
// there's no NV_fragment_program3, so just use 2.
const int ver = shader_is_pixel(ctx) ? 2 : 3;
ctx->profile_supports_nv2 = 1;
ctx->profile_supports_nv3 = 1;
output_line(ctx, "!!ARB%s1.0", shader_str);
output_line(ctx, "OPTION NV_%s_program%d;", shader_full_str, ver);
} // else if
else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV4) == 0)
{
ctx->profile_supports_nv2 = 1;
ctx->profile_supports_nv3 = 1;
ctx->profile_supports_nv4 = 1;
output_line(ctx, "!!NV%s4.0", shader_str);
} // else if
#endif
else
{
failf(ctx, "Profile '%s' unsupported or unknown.", profilestr);
} // else
set_output(ctx, &ctx->mainline);
} // emit_ARB1_start
void emit_ARB1_end(Context *ctx)
{
// ps_1_* writes color to r0 instead oC0. We move it to the right place.
// We don't have to worry about a RET opcode messing this up, since
// RET isn't available before ps_2_0.
if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
{
set_used_register(ctx, REG_TYPE_COLOROUT, 0, 1);
output_line(ctx, "MOV oC0, r0;");
} // if
output_line(ctx, "END");
} // emit_ARB1_end
void emit_ARB1_phase(Context *ctx)
{
// no-op in arb1.
} // emit_ARB1_phase
static inline const char *arb1_float_temp(const Context *ctx)
{
// nv4 lets you specify data type.
return (support_nv4(ctx)) ? "FLOAT TEMP" : "TEMP";
} // arb1_float_temp
void emit_ARB1_finalize(Context *ctx)
{
push_output(ctx, &ctx->preflight);
if (shader_is_vertex(ctx) && !ctx->arb1_wrote_position)
output_line(ctx, "OPTION ARB_position_invariant;");
if (shader_is_pixel(ctx) && ctx->have_multi_color_outputs)
output_line(ctx, "OPTION ARB_draw_buffers;");
pop_output(ctx);
const char *tmpstr = arb1_float_temp(ctx);
int i;
push_output(ctx, &ctx->globals);
for (i = 0; i < ctx->max_scratch_registers; i++)
{
char buf[64];
allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
output_line(ctx, "%s %s;", tmpstr, buf);
} // for
// nv2 fragment programs (and anything nv4) have a real REP/ENDREP.
if ( (support_nv2(ctx)) && (!shader_is_pixel(ctx)) && (!support_nv4(ctx)) )
{
// set up temps for nv2 REP/ENDREP emulation through branching.
for (i = 0; i < ctx->max_reps; i++)
output_line(ctx, "TEMP rep%d;", i);
} // if
pop_output(ctx);
assert(ctx->scratch_registers == ctx->max_scratch_registers);
} // emit_ARB1_finalize
void emit_ARB1_global(Context *ctx, RegisterType regtype, int regnum)
{
// !!! FIXME: dependency on ARB1 profile. // !!! FIXME about FIXME: huh?
char varname[64];
get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
push_output(ctx, &ctx->globals);
switch (regtype)
{
case REG_TYPE_ADDRESS:
if (shader_is_pixel(ctx)) // actually REG_TYPE_TEXTURE.
{
// We have to map texture registers to temps for ps_1_1, since
// they work like temps, initialize with tex coords, and the
// ps_1_1 TEX opcode expects to overwrite it.
if (!shader_version_atleast(ctx, 1, 4))
{
output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
push_output(ctx, &ctx->mainline_top);
output_line(ctx, "MOV %s, fragment.texcoord[%d];",
varname, regnum);
pop_output(ctx);
} // if
break;
} // if
// nv4 replaced address registers with generic int registers.
if (support_nv4(ctx))
output_line(ctx, "INT TEMP %s;", varname);
else
{
// nv2 has four-component address already, but stock arb1 has
// to emulate it in a temporary, and move components to the
// scalar ADDRESS register on demand.
output_line(ctx, "ADDRESS %s;", varname);
if (!support_nv2(ctx))
output_line(ctx, "TEMP addr%d;", regnum);
} // else
break;
//case REG_TYPE_PREDICATE:
// output_line(ctx, "bvec4 %s;", varname);
// break;
case REG_TYPE_TEMP:
output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
break;
//case REG_TYPE_LOOP:
// break; // no-op. We declare these in for loops at the moment.
//case REG_TYPE_LABEL:
// break; // no-op. If we see it here, it means we optimized it out.
default:
fail(ctx, "BUG: we used a register we don't know how to define.");
break;
} // switch
pop_output(ctx);
} // emit_ARB1_global
void emit_ARB1_array(Context *ctx, VariableList *var)
{
// All uniforms are now packed tightly into the program.local array,
// instead of trying to map them to the d3d registers. So this needs to
// map to the next piece of the array we haven't used yet. Thankfully,
// arb1 lets you make a PARAM array that maps to a subset of another
// array; we don't need to do offsets, since myarray[0] can map to
// program.local[5] without any extra math from us.
const int base = var->index;
const int size = var->count;
const int arb1base = ctx->uniform_float4_count +
ctx->uniform_int4_count +
ctx->uniform_bool_count;
char varname[64];
get_ARB1_const_array_varname_in_buf(ctx, base, size, varname, sizeof (varname));
push_output(ctx, &ctx->globals);
output_line(ctx, "PARAM %s[%d] = { program.local[%d..%d] };", varname,
size, arb1base, (arb1base + size) - 1);
pop_output(ctx);
var->emit_position = arb1base;
} // emit_ARB1_array
void emit_ARB1_const_array(Context *ctx, const ConstantsList *clist,
int base, int size)
{
char varname[64];
get_ARB1_const_array_varname_in_buf(ctx, base, size, varname, sizeof (varname));
int i;
push_output(ctx, &ctx->globals);
output_line(ctx, "PARAM %s[%d] = {", varname, size);
ctx->indent++;
for (i = 0; i < size; i++)
{
while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT)
clist = clist->next;
assert(clist->constant.index == (base + i));
char val0[32];
char val1[32];
char val2[32];
char val3[32];
floatstr(ctx, val0, sizeof (val0), clist->constant.value.f[0], 1);
floatstr(ctx, val1, sizeof (val1), clist->constant.value.f[1], 1);
floatstr(ctx, val2, sizeof (val2), clist->constant.value.f[2], 1);
floatstr(ctx, val3, sizeof (val3), clist->constant.value.f[3], 1);
output_line(ctx, "{ %s, %s, %s, %s }%s", val0, val1, val2, val3,
(i < (size-1)) ? "," : "");
clist = clist->next;
} // for
ctx->indent--;
output_line(ctx, "};");
pop_output(ctx);
} // emit_ARB1_const_array
void emit_ARB1_uniform(Context *ctx, RegisterType regtype, int regnum,
const VariableList *var)
{
// We pack these down into the program.local array, so if we only use
// register c439, it'll actually map to program.local[0]. This will
// prevent overflows when we actually have enough resources to run.
const char *arrayname = "program.local";
int index = 0;
char varname[64];
get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
push_output(ctx, &ctx->globals);
if (var == NULL)
{
// all types share one array (rather, all types convert to float4).
index = ctx->uniform_float4_count + ctx->uniform_int4_count +
ctx->uniform_bool_count;
} // if
else
{
const int arraybase = var->index;
if (var->constant)
{
const int arraysize = var->count;
arrayname = get_ARB1_const_array_varname_in_buf(ctx, arraybase,
arraysize, (char *) alloca(64), 64);
index = (regnum - arraybase);
} // if
else
{
assert(var->emit_position != -1);
index = (regnum - arraybase) + var->emit_position;
} // else
} // else
output_line(ctx, "PARAM %s = %s[%d];", varname, arrayname, index);
pop_output(ctx);
} // emit_ARB1_uniform
void emit_ARB1_sampler(Context *ctx,int stage,TextureType ttype,int tb)
{
// this is mostly a no-op...you don't predeclare samplers in arb1.
if (tb) // This sampler used a ps_1_1 TEXBEM opcode?
{
const int index = ctx->uniform_float4_count + ctx->uniform_int4_count +
ctx->uniform_bool_count;
char var[64];
get_ARB1_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage, var, sizeof(var));
push_output(ctx, &ctx->globals);
output_line(ctx, "PARAM %s_texbem = program.local[%d];", var, index);
output_line(ctx, "PARAM %s_texbeml = program.local[%d];", var, index+1);
pop_output(ctx);
ctx->uniform_float4_count += 2;
} // if
} // emit_ARB1_sampler
// !!! FIXME: a lot of cut-and-paste here from emit_GLSL_attribute().
void emit_ARB1_attribute(Context *ctx, RegisterType regtype, int regnum,
MOJOSHADER_usage usage, int index, int wmask,
int flags)
{
// !!! FIXME: this function doesn't deal with write masks at all yet!
const char *usage_str = NULL;
const char *arrayleft = "";
const char *arrayright = "";
char index_str[16] = { '\0' };
char varname[64];
get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
//assert((flags & MOD_PP) == 0); // !!! FIXME: is PP allowed?
if (index != 0) // !!! FIXME: a lot of these MUST be zero.
snprintf(index_str, sizeof (index_str), "%u", (uint) index);
if (shader_is_vertex(ctx))
{
// pre-vs3 output registers.
// these don't ever happen in DCL opcodes, I think. Map to vs_3_*
// output registers.
if (!shader_version_atleast(ctx, 3, 0))
{
if (regtype == REG_TYPE_RASTOUT)
{
regtype = REG_TYPE_OUTPUT;
index = regnum;
switch ((const RastOutType) regnum)
{
case RASTOUT_TYPE_POSITION:
usage = MOJOSHADER_USAGE_POSITION;
break;
case RASTOUT_TYPE_FOG:
usage = MOJOSHADER_USAGE_FOG;
break;
case RASTOUT_TYPE_POINT_SIZE:
usage = MOJOSHADER_USAGE_POINTSIZE;
break;
} // switch
} // if
else if (regtype == REG_TYPE_ATTROUT)
{
regtype = REG_TYPE_OUTPUT;
usage = MOJOSHADER_USAGE_COLOR;
index = regnum;
} // else if
else if (regtype == REG_TYPE_TEXCRDOUT)
{
regtype = REG_TYPE_OUTPUT;
usage = MOJOSHADER_USAGE_TEXCOORD;
index = regnum;
} // else if
} // if
// to avoid limitations of various GL entry points for input
// attributes (glSecondaryColorPointer() can only take 3 component
// items, glVertexPointer() can't do GL_UNSIGNED_BYTE, many other
// issues), we set up all inputs as generic vertex attributes, so we
// can pass data in just about any form, and ignore the built-in GLSL
// attributes like gl_SecondaryColor. Output needs to use the the
// built-ins, though, but we don't have to worry about the GL entry
// point limitations there.
if (regtype == REG_TYPE_INPUT)
{
const int attr = ctx->assigned_vertex_attributes++;
push_output(ctx, &ctx->globals);
output_line(ctx, "ATTRIB %s = vertex.attrib[%d];", varname, attr);
pop_output(ctx);
} // if
else if (regtype == REG_TYPE_OUTPUT)
{
switch (usage)
{
case MOJOSHADER_USAGE_POSITION:
ctx->arb1_wrote_position = 1;
usage_str = "result.position";
break;
case MOJOSHADER_USAGE_POINTSIZE:
usage_str = "result.pointsize";
break;
case MOJOSHADER_USAGE_COLOR:
index_str[0] = '\0'; // no explicit number.
if (index == 0)
usage_str = "result.color.primary";
else if (index == 1)
usage_str = "result.color.secondary";
break;
case MOJOSHADER_USAGE_FOG:
usage_str = "result.fogcoord";
break;
case MOJOSHADER_USAGE_TEXCOORD:
snprintf(index_str, sizeof (index_str), "%u", (uint) index);
usage_str = "result.texcoord";
arrayleft = "[";
arrayright = "]";
break;
default:
// !!! FIXME: we need to deal with some more built-in varyings here.
break;
} // switch
// !!! FIXME: the #define is a little hacky, but it means we don't
// !!! FIXME: have to track these separately if this works.
push_output(ctx, &ctx->globals);
// no mapping to built-in var? Just make it a regular global, pray.
if (usage_str == NULL)
output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
else
{
output_line(ctx, "OUTPUT %s = %s%s%s%s;", varname, usage_str,
arrayleft, index_str, arrayright);
} // else
pop_output(ctx);
} // else if
else
{
fail(ctx, "unknown vertex shader attribute register");
} // else
} // if
else if (shader_is_pixel(ctx))
{
const char *paramtype_str = "ATTRIB";
// samplers DCLs get handled in emit_ARB1_sampler().
if (flags & MOD_CENTROID)
{
if (!support_nv4(ctx)) // GL_NV_fragment_program4 adds centroid.
{
// !!! FIXME: should we just wing it without centroid here?
failf(ctx, "centroid unsupported in %s profile",
ctx->profile->name);
return;
} // if
paramtype_str = "CENTROID ATTRIB";
} // if
if (regtype == REG_TYPE_COLOROUT)
{
paramtype_str = "OUTPUT";
usage_str = "result.color";
if (ctx->have_multi_color_outputs)
{
// We have to gamble that you have GL_ARB_draw_buffers.
// You probably do at this point if you have a sane setup.
snprintf(index_str, sizeof (index_str), "%u", (uint) regnum);
arrayleft = "[";
arrayright = "]";
} // if
} // if
else if (regtype == REG_TYPE_DEPTHOUT)
{
paramtype_str = "OUTPUT";
usage_str = "result.depth";
} // else if
// !!! FIXME: can you actualy have a texture register with COLOR usage?