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
|
[require]
GLSL >= 4.00
[vertex shader]
#version 400
#define FORCE_EARLY_Z layout(early_fragment_tests) in
#extension GL_ARB_shading_language_420pack : enable
#define ATTRIBUTE_LOCATION(x)
#define FRAGMENT_OUTPUT_LOCATION(x)
#define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y)
#define UBO_BINDING(packing, x) layout(packing, binding = x)
#define SAMPLER_BINDING(x) layout(binding = x)
#define SSBO_BINDING(x) layout(binding = x)
#define VARYING_LOCATION(x)
#extension GL_ARB_shader_storage_buffer_object : enable
#extension GL_ARB_shader_image_load_store : enable
#define float2 vec2
#define float3 vec3
#define float4 vec4
#define uint2 uvec2
#define uint3 uvec3
#define uint4 uvec4
#define int2 ivec2
#define int3 ivec3
#define int4 ivec4
#define frac fract
#define lerp mix
// Vertex UberShader
struct Light {
int4 color;
float4 cosatt;
float4 distatt;
float4 pos;
float4 dir;
};
UBO_BINDING(std140, 2) uniform VSBlock {
uint components;
uint xfmem_dualTexInfo;
uint xfmem_numColorChans;
float4 cpnmtx[6];
float4 cproj[4];
int4 cmtrl[4];
Light clights[8];
float4 ctexmtx[24];
float4 ctrmtx[64];
float4 cnmtx[32];
float4 cpostmtx[64];
float4 cpixelcenter;
float2 cviewport;
uint4 xfmem_pack1[8];
#define xfmem_texMtxInfo(i) (xfmem_pack1[(i)].x)
#define xfmem_postMtxInfo(i) (xfmem_pack1[(i)].y)
#define xfmem_color(i) (xfmem_pack1[(i)].z)
#define xfmem_alpha(i) (xfmem_pack1[(i)].w)
};
struct VS_OUTPUT {
float4 pos;
float4 colors_0;
float4 colors_1;
float4 clipPos;
float clipDist0;
float clipDist1;
};
int4 CalculateLighting(uint index, uint attnfunc, uint diffusefunc, float3 pos, float3 normal) {
float3 ldir, h, cosAttn, distAttn;
float dist, dist2, attn;
switch (attnfunc) {
case 0u: // LIGNTATTN_NONE
case 2u: // LIGHTATTN_DIR
ldir = normalize(clights[index].pos.xyz - pos.xyz);
attn = 1.0;
if (length(ldir) == 0.0)
ldir = normal;
break;
case 1u: // LIGHTATTN_SPEC
ldir = normalize(clights[index].pos.xyz - pos.xyz);
attn = (dot(normal, ldir) >= 0.0) ? max(0.0, dot(normal, clights[index].dir.xyz)) : 0.0;
cosAttn = clights[index].cosatt.xyz;
if (diffusefunc == 0u) // LIGHTDIF_NONE
distAttn = clights[index].distatt.xyz;
else
distAttn = normalize(clights[index].distatt.xyz);
attn = max(0.0, dot(cosAttn, float3(1.0, attn, attn*attn))) / dot(distAttn, float3(1.0, attn, attn*attn));
break;
case 3u: // LIGHTATTN_SPOT
ldir = clights[index].pos.xyz - pos.xyz;
dist2 = dot(ldir, ldir);
dist = sqrt(dist2);
ldir = ldir / dist;
attn = max(0.0, dot(ldir, clights[index].dir.xyz));
attn = max(0.0, clights[index].cosatt.x + clights[index].cosatt.y * attn + clights[index].cosatt.z * attn * attn) / dot(clights[index].distatt.xyz, float3(1.0, dist, dist2));
break;
default:
attn = 1.0;
ldir = normal;
break;
}
switch (diffusefunc) {
case 0u: // LIGHTDIF_NONE
return int4(round(attn * float4(clights[index].color)));
case 1u: // LIGHTDIF_SIGN
return int4(round(attn * dot(ldir, normal) * float4(clights[index].color)));
case 2u: // LIGHTDIF_CLAMP
return int4(round(attn * max(0.0, dot(ldir, normal)) * float4(clights[index].color)));
default:
return int4(0, 0, 0, 0);
}
}
ATTRIBUTE_LOCATION(0) in float4 rawpos;
ATTRIBUTE_LOCATION(1) in uint4 posmtx;
ATTRIBUTE_LOCATION(2) in float3 rawnorm0;
ATTRIBUTE_LOCATION(3) in float3 rawnorm1;
ATTRIBUTE_LOCATION(4) in float3 rawnorm2;
ATTRIBUTE_LOCATION(5) in float4 rawcolor0;
ATTRIBUTE_LOCATION(6) in float4 rawcolor1;
ATTRIBUTE_LOCATION(8) in float3 rawtex0;
ATTRIBUTE_LOCATION(9) in float3 rawtex1;
ATTRIBUTE_LOCATION(10) in float3 rawtex2;
ATTRIBUTE_LOCATION(11) in float3 rawtex3;
ATTRIBUTE_LOCATION(12) in float3 rawtex4;
ATTRIBUTE_LOCATION(13) in float3 rawtex5;
ATTRIBUTE_LOCATION(14) in float3 rawtex6;
ATTRIBUTE_LOCATION(15) in float3 rawtex7;
VARYING_LOCATION(0) out VertexData {
float4 pos;
float4 colors_0;
float4 colors_1;
float4 clipPos;
float clipDist0;
float clipDist1;
} vs;
void main()
{
VS_OUTPUT o;
// Position matrix
float4 P0;
float4 P1;
float4 P2;
// Normal matrix
float3 N0;
float3 N1;
float3 N2;
if ((components & 2u) != 0u) {// VB_HAS_POSMTXIDX
// Vertex format has a per-vertex matrix
int posidx = int(posmtx.r);
P0 = ctrmtx[posidx];
P1 = ctrmtx[posidx+1];
P2 = ctrmtx[posidx+2];
int normidx = posidx >= 32 ? (posidx - 32) : posidx;
N0 = cnmtx[normidx].xyz;
N1 = cnmtx[normidx+1].xyz;
N2 = cnmtx[normidx+2].xyz;
} else {
// One shared matrix
P0 = cpnmtx[0];
P1 = cpnmtx[1];
P2 = cpnmtx[2];
N0 = cpnmtx[3].xyz;
N1 = cpnmtx[4].xyz;
N2 = cpnmtx[5].xyz;
}
float4 pos = float4(dot(P0, rawpos), dot(P1, rawpos), dot(P2, rawpos), 1.0);
o.pos = float4(dot(cproj[0], pos), dot(cproj[1], pos), dot(cproj[2], pos), dot(cproj[3], pos));
// Only the first normal gets normalized (TODO: why?)
float3 _norm0 = float3(0.0, 0.0, 0.0);
if ((components & 1024u) != 0u) // VB_HAS_NRM0
_norm0 = normalize(float3(dot(N0, rawnorm0), dot(N1, rawnorm0), dot(N2, rawnorm0)));
float3 _norm1 = float3(0.0, 0.0, 0.0);
if ((components & 2048u) != 0u) // VB_HAS_NRM1
_norm1 = float3(dot(N0, rawnorm1), dot(N1, rawnorm1), dot(N2, rawnorm1));
float3 _norm2 = float3(0.0, 0.0, 0.0);
if ((components & 4096u) != 0u) // VB_HAS_NRM2
_norm2 = float3(dot(N0, rawnorm2), dot(N1, rawnorm2), dot(N2, rawnorm2));
// Lighting
for (uint chan = 0u; chan < xfmem_numColorChans; chan++) {
uint colorreg = xfmem_color(chan);
uint alphareg = xfmem_alpha(chan);
int4 mat = cmtrl[chan + 2u];
int4 lacc = int4(255, 255, 255, 255);
if (bitfieldExtract(colorreg, 0, 1) != 0u) {
if ((components & (8192u << chan)) != 0u) // VB_HAS_COL0
mat.xyz = int3(round(((chan == 0u) ? rawcolor0.xyz : rawcolor1.xyz) * 255.0));
else if ((components & 8192u) != 0u) // VB_HAS_COLO0
mat.xyz = int3(round(rawcolor0.xyz * 255.0));
else
mat.xyz = int3(255, 255, 255);
}
if (bitfieldExtract(alphareg, 0, 1) != 0u) {
if ((components & (8192u << chan)) != 0u) // VB_HAS_COL0
mat.w = int(round(((chan == 0u) ? rawcolor0.w : rawcolor1.w) * 255.0));
else if ((components & 8192u) != 0u) // VB_HAS_COLO0
mat.w = int(round(rawcolor0.w * 255.0));
else
mat.w = 255;
} else {
mat.w = cmtrl [chan + 2u].w;
}
if (bitfieldExtract(colorreg, 1, 1) != 0u) {
if (bitfieldExtract(colorreg, 6, 1) != 0u) {
if ((components & (8192u << chan)) != 0u) // VB_HAS_COL0
lacc.xyz = int3(round(((chan == 0u) ? rawcolor0.xyz : rawcolor1.xyz) * 255.0));
else if ((components & 8192u) != 0u) // VB_HAS_COLO0
lacc.xyz = int3(round(rawcolor0.xyz * 255.0));
else
lacc.xyz = int3(255, 255, 255);
} else {
lacc.xyz = cmtrl [chan].xyz;
}
uint light_mask = bitfieldExtract(colorreg, 2, 4) | (bitfieldExtract(colorreg, 11, 4) << 4u);
uint attnfunc = bitfieldExtract(colorreg, 9, 2);
uint diffusefunc = bitfieldExtract(colorreg, 7, 2);
for (uint light_index = 0u; light_index < 8u; light_index++) {
if ((light_mask & (1u << light_index)) != 0u)
lacc.xyz += CalculateLighting(light_index, attnfunc, diffusefunc, pos.xyz, _norm0).xyz;
}
}
if (bitfieldExtract(alphareg, 1, 1) != 0u) {
if (bitfieldExtract(alphareg, 6, 1) != 0u) {
if ((components & (8192u << chan)) != 0u) // VB_HAS_COL0
lacc.w = int(round(((chan == 0u) ? rawcolor0.w : rawcolor1.w) * 255.0));
else if ((components & 8192u) != 0u) // VB_HAS_COLO0
lacc.w = int(round(rawcolor0.w * 255.0));
else
lacc.w = 255;
} else {
lacc.w = cmtrl [chan].w;
}
uint light_mask = bitfieldExtract(alphareg, 2, 4) | (bitfieldExtract(alphareg, 11, 4) << 4u);
uint attnfunc = bitfieldExtract(alphareg, 9, 2);
uint diffusefunc = bitfieldExtract(alphareg, 7, 2);
for (uint light_index = 0u; light_index < 8u; light_index++) {
if ((light_mask & (1u << light_index)) != 0u)
lacc.w += CalculateLighting(light_index, attnfunc, diffusefunc, pos.xyz, _norm0).w;
}
}
lacc = clamp(lacc, 0, 255);
// Hopefully GPUs that can support dynamic indexing will optimize this.
float4 lit_color = float4((mat * (lacc + (lacc >> 7))) >> 8) / 255.0;
switch (chan) {
case 0u: o.colors_0 = lit_color; break;
case 1u: o.colors_1 = lit_color; break;
}
}
if (xfmem_numColorChans < 2u && (components & 16384u) == 0u)
o.colors_1 = o.colors_0;
o.clipPos = o.pos;
float clipDepth = o.pos.z * (1.0 - 1e-7);
o.clipDist0 = clipDepth + o.pos.w;
o.clipDist1 = -clipDepth;
o.pos.z = o.pos.w * cpixelcenter.w - o.pos.z * cpixelcenter.z;
o.pos.xy *= sign(cpixelcenter.xy * float2(1.0, -1.0));
o.pos.xy = o.pos.xy - o.pos.w * cpixelcenter.xy;
vs.pos = o.pos;
vs.colors_0 = o.colors_0;
vs.colors_1 = o.colors_1;
vs.clipPos = o.clipPos;
vs.clipDist0 = o.clipDist0;
vs.clipDist1 = o.clipDist1;
gl_ClipDistance[0] = o.clipDist0;
gl_ClipDistance[1] = o.clipDist1;
gl_Position = o.pos;
}
[fragment shader]
#version 400
#define FORCE_EARLY_Z layout(early_fragment_tests) in
#extension GL_ARB_shading_language_420pack : enable
#define ATTRIBUTE_LOCATION(x)
#define FRAGMENT_OUTPUT_LOCATION(x)
#define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y)
#define UBO_BINDING(packing, x) layout(packing, binding = x)
#define SAMPLER_BINDING(x) layout(binding = x)
#define SSBO_BINDING(x) layout(binding = x)
#define VARYING_LOCATION(x)
#extension GL_ARB_shader_storage_buffer_object : enable
#extension GL_ARB_shader_image_load_store : enable
#define float2 vec2
#define float3 vec3
#define float4 vec4
#define uint2 uvec2
#define uint3 uvec3
#define uint4 uvec4
#define int2 ivec2
#define int3 ivec3
#define int4 ivec4
#define frac fract
#define lerp mix
// Pixel UberShader for 0 texgens, early-depth
int idot(int3 x, int3 y)
{
int3 tmp = x * y;
return tmp.x + tmp.y + tmp.z;
}
int idot(int4 x, int4 y)
{
int4 tmp = x * y;
return tmp.x + tmp.y + tmp.z + tmp.w;
}
int iround(float x) { return int (round(x)); }
int2 iround(float2 x) { return int2(round(x)); }
int3 iround(float3 x) { return int3(round(x)); }
int4 iround(float4 x) { return int4(round(x)); }
SAMPLER_BINDING(0) uniform sampler2DArray samp[8];
UBO_BINDING(std140, 1) uniform PSBlock {
int4 color[4];
int4 k[4];
int4 alphaRef;
float4 texdim[8];
int4 czbias[2];
int4 cindscale[2];
int4 cindmtx[6];
int4 cfogcolor;
int4 cfogi;
float4 cfogf[2];
float4 czslope;
float2 cefbscale;
uint bpmem_genmode;
uint bpmem_alphaTest;
uint bpmem_fogParam3;
uint bpmem_fogRangeBase;
uint bpmem_dstalpha;
uint bpmem_ztex_op;
bool bpmem_late_ztest;
bool bpmem_rgba6_format;
bool bpmem_dither;
bool bpmem_bounding_box;
uint4 bpmem_pack1[16];
uint4 bpmem_pack2[8];
int4 konstLookup[32];
};
#define bpmem_combiners(i) (bpmem_pack1[(i)].xy)
#define bpmem_tevind(i) (bpmem_pack1[(i)].z)
#define bpmem_iref(i) (bpmem_pack1[(i)].w)
#define bpmem_tevorder(i) (bpmem_pack2[(i)].x)
#define bpmem_tevksel(i) (bpmem_pack2[(i)].y)
struct VS_OUTPUT {
float4 pos;
float4 colors_0;
float4 colors_1;
float4 clipPos;
float clipDist0;
float clipDist1;
};
FRAGMENT_OUTPUT_LOCATION_INDEXED(0, 0) out vec4 ocol0;
FRAGMENT_OUTPUT_LOCATION_INDEXED(0, 1) out vec4 ocol1;
VARYING_LOCATION(0) in VertexData {
float4 pos;
float4 colors_0;
float4 colors_1;
float4 clipPos;
float clipDist0;
float clipDist1;
};
int4 sampleTexture(uint sampler_num, float2 uv) {
return iround(texture(samp[sampler_num], float3(uv, 0.0)) * 255.0);
}
int4 Swizzle(uint s, int4 color) {
// AKA: Color Channel Swapping
int4 ret;
ret.r = color[bitfieldExtract(bpmem_tevksel(s * 2u), 0, 2)];
ret.g = color[bitfieldExtract(bpmem_tevksel(s * 2u), 2, 2)];
ret.b = color[bitfieldExtract(bpmem_tevksel(s * 2u + 1u), 0, 2)];
ret.a = color[bitfieldExtract(bpmem_tevksel(s * 2u + 1u), 2, 2)];
return ret;
}
int Wrap(int coord, uint mode) {
if (mode == 0u) // ITW_OFF
return coord;
else if (mode < 6u) // ITW_256 to ITW_16
return coord & (0xfffe >> mode);
else // ITW_0
return 0;
}
// TEV's Linear Interpolate, plus bias, add/subtract and scale
int tevLerp(int A, int B, int C, int D, uint bias, bool op, bool alpha, uint shift) {
// Scale C from 0..255 to 0..256
C += C >> 7;
// Add bias to D
if (bias == 1u) D += 128;
else if (bias == 2u) D -= 128;
int lerp = (A << 8) + (B - A)*C;
if (shift != 3u) {
lerp = lerp << shift;
D = D << shift;
}
if ((shift == 3u) == alpha)
lerp = lerp + (op ? 127 : 128);
int result = lerp >> 8;
// Add/Subtract D
if(op) // Subtract
result = D - result;
else // Add
result = D + result;
// Most of the Shift was moved inside the lerp for improved percision
// But we still do the divide by 2 here
if (shift == 3u)
result = result >> 1;
return result;
}
// TEV's Linear Interpolate, plus bias, add/subtract and scale
int3 tevLerp3(int3 A, int3 B, int3 C, int3 D, uint bias, bool op, bool alpha, uint shift) {
// Scale C from 0..255 to 0..256
C += C >> 7;
// Add bias to D
if (bias == 1u) D += 128;
else if (bias == 2u) D -= 128;
int3 lerp = (A << 8) + (B - A)*C;
if (shift != 3u) {
lerp = lerp << shift;
D = D << shift;
}
if ((shift == 3u) == alpha)
lerp = lerp + (op ? 127 : 128);
int3 result = lerp >> 8;
// Add/Subtract D
if(op) // Subtract
result = D - result;
else // Add
result = D + result;
// Most of the Shift was moved inside the lerp for improved percision
// But we still do the divide by 2 here
if (shift == 3u)
result = result >> 1;
return result;
}
// Implements operations 0-5 of tev's compare mode,
// which are common to both color and alpha channels
bool tevCompare(uint op, int3 color_A, int3 color_B) {
switch (op) {
case 0u: // TEVCMP_R8_GT
return (color_A.r > color_B.r);
case 1u: // TEVCMP_R8_EQ
return (color_A.r == color_B.r);
case 2u: // TEVCMP_GR16_GT
int A_16 = (color_A.r | (color_A.g << 8));
int B_16 = (color_B.r | (color_B.g << 8));
return A_16 > B_16;
case 3u: // TEVCMP_GR16_EQ
return (color_A.r == color_B.r && color_A.g == color_B.g);
case 4u: // TEVCMP_BGR24_GT
int A_24 = (color_A.r | (color_A.g << 8) | (color_A.b << 16));
int B_24 = (color_B.r | (color_B.g << 8) | (color_B.b << 16));
return A_24 > B_24;
case 5u: // TEVCMP_BGR24_EQ
return (color_A.r == color_B.r && color_A.g == color_B.g && color_A.b == color_B.b);
default:
return false;
}
}
// Helper function for Alpha Test
bool alphaCompare(int a, int b, uint compare) {
switch (compare) {
case 0u: // NEVER
return false;
case 1u: // LESS
return a < b;
case 2u: // EQUAL
return a == b;
case 3u: // LEQUAL
return a <= b;
case 4u: // GREATER
return a > b;
case 5u: // NEQUAL;
return a != b;
case 6u: // GEQUAL
return a >= b;
case 7u: // ALWAYS
return true;
}
}
struct State {
int4 Reg[4];
int4 TexColor;
int AlphaBump;
};
struct StageState {
uint stage;
uint order;
uint cc;
uint ac;
};
int4 getRasColor(State s, StageState ss, float4 colors_0, float4 colors_1);
int4 getKonstColor(State s, StageState ss);
int3 selectColorInput(State s, StageState ss, float4 colors_0, float4 colors_1, uint index) {
switch (index) {
case 0u: // prev.rgb
return s.Reg[0].rgb;
case 1u: // prev.aaa
return s.Reg[0].aaa;
case 2u: // c0.rgb
return s.Reg[1].rgb;
case 3u: // c0.aaa
return s.Reg[1].aaa;
case 4u: // c1.rgb
return s.Reg[2].rgb;
case 5u: // c1.aaa
return s.Reg[2].aaa;
case 6u: // c2.rgb
return s.Reg[3].rgb;
case 7u: // c2.aaa
return s.Reg[3].aaa;
case 8u:
return s.TexColor.rgb;
case 9u:
return s.TexColor.aaa;
case 10u:
return getRasColor(s, ss, colors_0, colors_1).rgb;
case 11u:
return getRasColor(s, ss, colors_0, colors_1).aaa;
case 12u: // One
return int3(255, 255, 255);
case 13u: // Half
return int3(128, 128, 128);
case 14u:
return getKonstColor(s, ss).rgb;
case 15u: // Zero
return int3(0, 0, 0);
}
}
int selectAlphaInput(State s, StageState ss, float4 colors_0, float4 colors_1, uint index) {
switch (index) {
case 0u: // prev.a
return s.Reg[0].a;
case 1u: // c0.a
return s.Reg[1].a;
case 2u: // c1.a
return s.Reg[2].a;
case 3u: // c2.a
return s.Reg[3].a;
case 4u:
return s.TexColor.a;
case 5u:
return getRasColor(s, ss, colors_0, colors_1).a;
case 6u:
return getKonstColor(s, ss).a;
case 7u: // Zero
return 0;
}
}
int4 getTevReg(in State s, uint index) {
switch (index) {
case 0u: // prev
return s.Reg[0];
case 1u: // c0
return s.Reg[1];
case 2u: // c1
return s.Reg[2];
case 3u: // c2
return s.Reg[3];
default: // prev
return s.Reg[0];
}
}
void setRegColor(inout State s, uint index, int3 color) {
switch (index) {
case 0u: // prev
s.Reg[0].rgb = color;
break;
case 1u: // c0
s.Reg[1].rgb = color;
break;
case 2u: // c1
s.Reg[2].rgb = color;
break;
case 3u: // c2
s.Reg[3].rgb = color;
break;
}
}
void setRegAlpha(inout State s, uint index, int alpha) {
switch (index) {
case 0u: // prev
s.Reg[0].a = alpha;
break;
case 1u: // c0
s.Reg[1].a = alpha;
break;
case 2u: // c1
s.Reg[2].a = alpha;
break;
case 3u: // c2
s.Reg[3].a = alpha;
break;
}
}
FORCE_EARLY_Z;
void main()
{
float4 rawpos = gl_FragCoord;
int3 tevcoord = int3(0, 0, 0);
State s;
s.TexColor = int4(0, 0, 0, 0);
s.AlphaBump = 0;
s.Reg[0] = color[0];
s.Reg[1] = color[1];
s.Reg[2] = color[2];
s.Reg[3] = color[3];
uint num_stages = bitfieldExtract(bpmem_genmode, 10, 4);
// Main tev loop
for(uint stage = 0u; stage <= num_stages; stage++)
{
StageState ss;
ss.stage = stage;
ss.cc = bpmem_combiners(stage).x;
ss.ac = bpmem_combiners(stage).y;
ss.order = bpmem_tevorder(stage>>1);
if ((stage & 1u) == 1u)
ss.order = ss.order >> 12;
// This is the Meat of TEV
{
// Color Combiner
uint color_a = bitfieldExtract(ss.cc, 12, 4);
uint color_b = bitfieldExtract(ss.cc, 8, 4);
uint color_c = bitfieldExtract(ss.cc, 4, 4);
uint color_d = bitfieldExtract(ss.cc, 0, 4);
uint color_bias = bitfieldExtract(ss.cc, 16, 2);
bool color_op = bool(bitfieldExtract(ss.cc, 18, 1));
bool color_clamp = bool(bitfieldExtract(ss.cc, 19, 1));
uint color_shift = bitfieldExtract(ss.cc, 20, 2);
uint color_dest = bitfieldExtract(ss.cc, 22, 2);
uint color_compare_op = color_shift << 1 | uint(color_op);
int3 color_A = selectColorInput(s, ss, colors_0, colors_1, color_a) & int3(255, 255, 255);
int3 color_B = selectColorInput(s, ss, colors_0, colors_1, color_b) & int3(255, 255, 255);
int3 color_C = selectColorInput(s, ss, colors_0, colors_1, color_c) & int3(255, 255, 255);
int3 color_D = selectColorInput(s, ss, colors_0, colors_1, color_d); // 10 bits + sign
int3 color;
if(color_bias != 3u) { // Normal mode
color = tevLerp3(color_A, color_B, color_C, color_D, color_bias, color_op, false, color_shift);
} else { // Compare mode
// op 6 and 7 do a select per color channel
if (color_compare_op == 6u) {
// TEVCMP_RGB8_GT
color.r = (color_A.r > color_B.r) ? color_C.r : 0;
color.g = (color_A.g > color_B.g) ? color_C.g : 0;
color.b = (color_A.b > color_B.b) ? color_C.b : 0;
} else if (color_compare_op == 7u) {
// TEVCMP_RGB8_EQ
color.r = (color_A.r == color_B.r) ? color_C.r : 0;
color.g = (color_A.g == color_B.g) ? color_C.g : 0;
color.b = (color_A.b == color_B.b) ? color_C.b : 0;
} else {
// The remaining ops do one compare which selects all 3 channels
color = tevCompare(color_compare_op, color_A, color_B) ? color_C : int3(0, 0, 0);
}
color = color_D + color;
}
// Clamp result
if (color_clamp)
color = clamp(color, 0, 255);
else
color = clamp(color, -1024, 1023);
// Write result to the correct input register of the next stage
setRegColor(s, color_dest, color);
// Alpha Combiner
uint alpha_a = bitfieldExtract(ss.ac, 13, 3);
uint alpha_b = bitfieldExtract(ss.ac, 10, 3);
uint alpha_c = bitfieldExtract(ss.ac, 7, 3);
uint alpha_d = bitfieldExtract(ss.ac, 4, 3);
uint alpha_bias = bitfieldExtract(ss.ac, 16, 2);
bool alpha_op = bool(bitfieldExtract(ss.ac, 18, 1));
bool alpha_clamp = bool(bitfieldExtract(ss.ac, 19, 1));
uint alpha_shift = bitfieldExtract(ss.ac, 20, 2);
uint alpha_dest = bitfieldExtract(ss.ac, 22, 2);
uint alpha_compare_op = alpha_shift << 1 | uint(alpha_op);
int alpha_A;
int alpha_B;
if (alpha_bias != 3u || alpha_compare_op > 5u) {
// Small optimisation here: alpha_A and alpha_B are unused by compare ops 0-5
alpha_A = selectAlphaInput(s, ss, colors_0, colors_1, alpha_a) & 255;
alpha_B = selectAlphaInput(s, ss, colors_0, colors_1, alpha_b) & 255;
};
int alpha_C = selectAlphaInput(s, ss, colors_0, colors_1, alpha_c) & 255;
int alpha_D = selectAlphaInput(s, ss, colors_0, colors_1, alpha_d); // 10 bits + sign
int alpha;
if(alpha_bias != 3u) { // Normal mode
alpha = tevLerp(alpha_A, alpha_B, alpha_C, alpha_D, alpha_bias, alpha_op, true, alpha_shift);
} else { // Compare mode
if (alpha_compare_op == 6u) {
// TEVCMP_A8_GT
alpha = (alpha_A > alpha_B) ? alpha_C : 0;
} else if (alpha_compare_op == 7u) {
// TEVCMP_A8_EQ
alpha = (alpha_A == alpha_B) ? alpha_C : 0;
} else {
// All remaining alpha compare ops actually compare the color channels
alpha = tevCompare(alpha_compare_op, color_A, color_B) ? alpha_C : 0;
}
alpha = alpha_D + alpha;
}
// Clamp result
if (alpha_clamp)
alpha = clamp(alpha, 0, 255);
else
alpha = clamp(alpha, -1024, 1023);
// Write result to the correct input register of the next stage
setRegAlpha(s, alpha_dest, alpha);
}
} // Main tev loop
int4 TevResult;
TevResult.xyz = getTevReg(s, bitfieldExtract(bpmem_combiners(num_stages).x, 22, 2)).xyz;
TevResult.w = getTevReg(s, bitfieldExtract(bpmem_combiners(num_stages).y, 22, 2)).w;
TevResult &= 255;
int zCoord = int(rawpos.z * 16777216.0);
zCoord = clamp(zCoord, 0, 0xFFFFFF);
// Depth Texture
int early_zCoord = zCoord;
if (bpmem_ztex_op != 0u) {
int ztex = int(czbias[1].w); // fixed bias
// Whatever texture was in our last stage, it's now our depth texture
ztex += idot(s.TexColor.xyzw, czbias[0].xyzw);
ztex += (bpmem_ztex_op == 1u) ? zCoord : 0;
zCoord = ztex & 0xFFFFFF;
}
// Alpha Test
if (bpmem_alphaTest != 0u) {
bool comp0 = alphaCompare(TevResult.a, alphaRef.r, bitfieldExtract(bpmem_alphaTest, 16, 3));
bool comp1 = alphaCompare(TevResult.a, alphaRef.g, bitfieldExtract(bpmem_alphaTest, 19, 3));
// These if statements are written weirdly to work around intel and qualcom bugs with handling booleans.
switch (bitfieldExtract(bpmem_alphaTest, 22, 2)) {
case 0u: // AND
if (comp0 && comp1) break; else discard; break;
case 1u: // OR
if (comp0 || comp1) break; else discard; break;
case 2u: // XOR
if (comp0 != comp1) break; else discard; break;
case 3u: // XNOR
if (comp0 == comp1) break; else discard; break;
}
}
if (bpmem_dither) {
// Flipper uses a standard 2x2 Bayer Matrix for 6 bit dithering
// Here the matrix is encoded into the two factor constants
int2 dither = int2(rawpos.xy) & 1;
TevResult.rgb = (TevResult.rgb - (TevResult.rgb >> 6)) + abs(dither.y * 3 - dither.x * 2);
}
// Fog
uint fog_function = bitfieldExtract(bpmem_fogParam3, 21, 3);
if (fog_function != 0u) {
// TODO: This all needs to be converted from float to fixed point
float ze;
if (bitfieldExtract(bpmem_fogParam3, 20, 1) == 0u) {
// perspective
// ze = A/(B - (Zs >> B_SHF)
ze = (cfogf[1].x * 16777216.0) / float(cfogi.y - (zCoord >> cfogi.w));
} else {
// orthographic
// ze = a*Zs (here, no B_SHF)
ze = cfogf[1].x * float(zCoord) / 16777216.0;
}
if (bool(bitfieldExtract(bpmem_fogRangeBase, 10, 1))) {
// x_adjust = sqrt((x-center)^2 + k^2)/k
// ze *= x_adjust
// TODO Instead of this theoretical calculation, we should use the
// coefficient table given in the fog range BP registers!
float x_adjust = (2.0 * (rawpos.x / cfogf[0].y)) - 1.0 - cfogf[0].x;
x_adjust = sqrt(x_adjust * x_adjust + cfogf[0].z * cfogf[0].z) / cfogf[0].z;
ze *= x_adjust;
}
float fog = clamp(ze - cfogf[1].z, 0.0, 1.0);
if (fog_function > 3u) {
switch (fog_function) {
case 4u:
fog = 1.0 - exp2(-8.0 * fog);
break;
case 5u:
fog = 1.0 - exp2(-8.0 * fog * fog);
break;
case 6u:
fog = exp2(-8.0 * (1.0 - fog));
break;
case 7u:
fog = 1.0 - fog;
fog = exp2(-8.0 * fog * fog);
break;
}
}
int ifog = iround(fog * 256.0);
TevResult.rgb = (TevResult.rgb * (256 - ifog) + cfogcolor.rgb * ifog) >> 8;
}
if (bpmem_rgba6_format)
ocol0.rgb = float3(TevResult.rgb >> 2) / 63.0;
else
ocol0.rgb = float3(TevResult.rgb) / 255.0;
if (bpmem_dstalpha != 0u)
ocol0.a = float(bitfieldExtract(bpmem_dstalpha, 0, 8) >> 2) / 63.0;
else
ocol0.a = float(TevResult.a >> 2) / 63.0;
// Dest alpha override (dual source blending)
// Colors will be blended against the alpha from ocol1 and
// the alpha from ocol0 will be written to the framebuffer.
ocol1 = float4(0.0, 0.0, 0.0, float(TevResult.a) / 255.0);
}
int4 getRasColor(State s, StageState ss, float4 colors_0, float4 colors_1) {
// Select Ras for stage
uint ras = bitfieldExtract(ss.order, 7, 3);
if (ras < 2u) { // Lighting Channel 0 or 1
int4 color = iround(((ras == 0u) ? colors_0 : colors_1) * 255.0);
uint swap = bitfieldExtract(ss.ac, 0, 2);
return Swizzle(swap, color);
} else if (ras == 5u) { // Alpha Bumb
return int4(s.AlphaBump, s.AlphaBump, s.AlphaBump, s.AlphaBump);
} else if (ras == 6u) { // Normalzied Alpha Bump
int normalized = s.AlphaBump | s.AlphaBump >> 5;
return int4(normalized, normalized, normalized, normalized);
} else {
return int4(0, 0, 0, 0);
}
}
int4 getKonstColor(State s, StageState ss) {
// Select Konst for stage
// TODO: a switch case might be better here than an dynamically // indexed uniform lookup
uint tevksel = bpmem_tevksel(ss.stage>>1);
if ((ss.stage & 1u) == 0u)
return int4(konstLookup[bitfieldExtract(tevksel, 4, 5)].rgb, konstLookup[bitfieldExtract(tevksel, 9, 5)].a);
else
return int4(konstLookup[bitfieldExtract(tevksel, 14, 5)].rgb, konstLookup[bitfieldExtract(tevksel, 19, 5)].a);
}
|