forked from google/swiftshader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderCore.cpp
2020 lines (1722 loc) · 60.5 KB
/
ShaderCore.cpp
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
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ShaderCore.hpp"
#include "Renderer/Renderer.hpp"
#include "Common/Debug.hpp"
#include <limits.h>
namespace sw
{
extern TranscendentalPrecision logPrecision;
extern TranscendentalPrecision expPrecision;
extern TranscendentalPrecision rcpPrecision;
extern TranscendentalPrecision rsqPrecision;
Vector4s::Vector4s()
{
}
Vector4s::Vector4s(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
{
this->x = Short4(x);
this->y = Short4(y);
this->z = Short4(z);
this->w = Short4(w);
}
Vector4s::Vector4s(const Vector4s &rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
w = rhs.w;
}
Vector4s &Vector4s::operator=(const Vector4s &rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
w = rhs.w;
return *this;
}
Short4 &Vector4s::operator[](int i)
{
switch(i)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
}
return x;
}
Vector4f::Vector4f()
{
}
Vector4f::Vector4f(float x, float y, float z, float w)
{
this->x = Float4(x);
this->y = Float4(y);
this->z = Float4(z);
this->w = Float4(w);
}
Vector4f::Vector4f(const Vector4f &rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
w = rhs.w;
}
Vector4f &Vector4f::operator=(const Vector4f &rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
w = rhs.w;
return *this;
}
Float4 &Vector4f::operator[](int i)
{
switch(i)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
}
return x;
}
Float4 exponential2(RValue<Float4> x, bool pp)
{
// This implementation is based on 2^(i + f) = 2^i * 2^f,
// where i is the integer part of x and f is the fraction.
// For 2^i we can put the integer part directly in the exponent of
// the IEEE-754 floating-point number. Clamp to prevent overflow
// past the representation of infinity.
Float4 x0 = x;
x0 = Min(x0, As<Float4>(Int4(0x43010000))); // 129.00000e+0f
x0 = Max(x0, As<Float4>(Int4(0xC2FDFFFF))); // -126.99999e+0f
Int4 i = RoundInt(x0 - Float4(0.5f));
Float4 ii = As<Float4>((i + Int4(127)) << 23); // Add single-precision bias, and shift into exponent.
// For the fractional part use a polynomial
// which approximates 2^f in the 0 to 1 range.
Float4 f = x0 - Float4(i);
Float4 ff = As<Float4>(Int4(0x3AF61905)); // 1.8775767e-3f
ff = ff * f + As<Float4>(Int4(0x3C134806)); // 8.9893397e-3f
ff = ff * f + As<Float4>(Int4(0x3D64AA23)); // 5.5826318e-2f
ff = ff * f + As<Float4>(Int4(0x3E75EAD4)); // 2.4015361e-1f
ff = ff * f + As<Float4>(Int4(0x3F31727B)); // 6.9315308e-1f
ff = ff * f + Float4(1.0f);
return ii * ff;
}
Float4 logarithm2(RValue<Float4> x, bool absolute, bool pp)
{
Float4 x0;
Float4 x1;
Float4 x2;
Float4 x3;
x0 = x;
x1 = As<Float4>(As<Int4>(x0) & Int4(0x7F800000));
x1 = As<Float4>(As<UInt4>(x1) >> 8);
x1 = As<Float4>(As<Int4>(x1) | As<Int4>(Float4(1.0f)));
x1 = (x1 - Float4(1.4960938f)) * Float4(256.0f); // FIXME: (x1 - 1.4960938f) * 256.0f;
x0 = As<Float4>((As<Int4>(x0) & Int4(0x007FFFFF)) | As<Int4>(Float4(1.0f)));
x2 = (Float4(9.5428179e-2f) * x0 + Float4(4.7779095e-1f)) * x0 + Float4(1.9782813e-1f);
x3 = ((Float4(1.6618466e-2f) * x0 + Float4(2.0350508e-1f)) * x0 + Float4(2.7382900e-1f)) * x0 + Float4(4.0496687e-2f);
x2 /= x3;
x1 += (x0 - Float4(1.0f)) * x2;
Int4 pos_inf_x = CmpEQ(As<Int4>(x), Int4(0x7F800000));
return As<Float4>((pos_inf_x & As<Int4>(x)) | (~pos_inf_x & As<Int4>(x1)));
}
Float4 exponential(RValue<Float4> x, bool pp)
{
// FIXME: Propagate the constant
return exponential2(Float4(1.44269504f) * x, pp); // 1/ln(2)
}
Float4 logarithm(RValue<Float4> x, bool absolute, bool pp)
{
// FIXME: Propagate the constant
return Float4(6.93147181e-1f) * logarithm2(x, absolute, pp); // ln(2)
}
Float4 power(RValue<Float4> x, RValue<Float4> y, bool pp)
{
Float4 log = logarithm2(x, true, pp);
log *= y;
return exponential2(log, pp);
}
Float4 reciprocal(RValue<Float4> x, bool pp, bool finite, bool exactAtPow2)
{
Float4 rcp;
if(!pp && rcpPrecision >= WHQL)
{
rcp = Float4(1.0f) / x;
}
else
{
rcp = Rcp_pp(x, exactAtPow2);
if(!pp)
{
rcp = (rcp + rcp) - (x * rcp * rcp);
}
}
if(finite)
{
int big = 0x7F7FFFFF;
rcp = Min(rcp, Float4((float&)big));
}
return rcp;
}
Float4 reciprocalSquareRoot(RValue<Float4> x, bool absolute, bool pp)
{
Float4 abs = x;
if(absolute)
{
abs = Abs(abs);
}
Float4 rsq;
if(!pp)
{
rsq = Float4(1.0f) / Sqrt(abs);
}
else
{
rsq = RcpSqrt_pp(abs);
if(!pp)
{
rsq = rsq * (Float4(3.0f) - rsq * rsq * abs) * Float4(0.5f);
}
rsq = As<Float4>(CmpNEQ(As<Int4>(abs), Int4(0x7F800000)) & As<Int4>(rsq));
}
return rsq;
}
Float4 modulo(RValue<Float4> x, RValue<Float4> y)
{
return x - y * Floor(x / y);
}
Float4 sine_pi(RValue<Float4> x, bool pp)
{
const Float4 A = Float4(-4.05284734e-1f); // -4/pi^2
const Float4 B = Float4(1.27323954e+0f); // 4/pi
const Float4 C = Float4(7.75160950e-1f);
const Float4 D = Float4(2.24839049e-1f);
// Parabola approximating sine
Float4 sin = x * (Abs(x) * A + B);
// Improve precision from 0.06 to 0.001
if(true)
{
sin = sin * (Abs(sin) * D + C);
}
return sin;
}
Float4 cosine_pi(RValue<Float4> x, bool pp)
{
// cos(x) = sin(x + pi/2)
Float4 y = x + Float4(1.57079632e+0f);
// Wrap around
y -= As<Float4>(CmpNLT(y, Float4(3.14159265e+0f)) & As<Int4>(Float4(6.28318530e+0f)));
return sine_pi(y, pp);
}
// Assumes x is a finite floating point value
static RValue<Float4> clamp(const Float4 &x, const Float4 &min, const Float4 &max)
{
return Min(Max(x, min), max);
}
Float4 sine(RValue<Float4> x, bool pp)
{
// Reduce to [-0.5, 0.5] range
Float4 y = x * Float4(1.59154943e-1f); // 1/2pi
y = y - Round(y);
if(!pp)
{
// From the paper: "A Fast, Vectorizable Algorithm for Producing Single-Precision Sine-Cosine Pairs"
// This implementation passes OpenGL ES 3.0 precision requirements, at the cost of more operations:
// !pp : 17 mul, 7 add, 1 sub, 1 reciprocal
// pp : 4 mul, 2 add, 2 abs
Float4 y2 = y * y;
Float4 c1 = y2 * (y2 * (y2 * Float4(-0.0204391631f) + Float4(0.2536086171f)) + Float4(-1.2336977925f)) + Float4(1.0f);
Float4 s1 = y * (y2 * (y2 * (y2 * Float4(-0.0046075748f) + Float4(0.0796819754f)) + Float4(-0.645963615f)) + Float4(1.5707963235f));
Float4 c2 = (c1 * c1) - (s1 * s1);
Float4 s2 = Float4(2.0f) * s1 * c1;
return Float4(2.0f) * s2 * c2 * reciprocal(s2 * s2 + c2 * c2, pp, true);
}
const Float4 A = Float4(-16.0f);
const Float4 B = Float4(8.0f);
const Float4 C = Float4(7.75160950e-1f);
const Float4 D = Float4(2.24839049e-1f);
// Parabola approximating sine
Float4 sin = y * (Abs(y) * A + B);
// Improve precision from 0.06 to 0.001
if(true)
{
sin = sin * (Abs(sin) * D + C);
}
// TODO(b/151461290): Fix precision loss instead of clamping.
sin = clamp(sin, Float4(-1.0f), Float4(1.0f));
return sin;
}
Float4 cosine(RValue<Float4> x, bool pp)
{
// cos(x) = sin(x + pi/2)
Float4 y = x + Float4(1.57079632e+0f);
auto cos = sine(y, pp);
// TODO(b/151461290): Fix precision loss instead of clamping.
cos = clamp(cos, Float4(-1.0f), Float4(1.0f));
return cos;
}
Float4 tangent(RValue<Float4> x, bool pp)
{
return sine(x, pp) / cosine(x, pp);
}
Float4 arccos(RValue<Float4> x, bool pp)
{
// pi/2 - arcsin(x)
return Float4(1.57079632e+0f) - arcsin(x);
}
Float4 arcsin(RValue<Float4> x, bool pp)
{
if(false) // Simpler implementation fails even lowp precision tests
{
// x*(pi/2-sqrt(1-x*x)*pi/5)
return x * (Float4(1.57079632e+0f) - Sqrt(Float4(1.0f) - x*x) * Float4(6.28318531e-1f));
}
else
{
// From 4.4.45, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
const Float4 half_pi(1.57079632f);
const Float4 a0(1.5707288f);
const Float4 a1(-0.2121144f);
const Float4 a2(0.0742610f);
const Float4 a3(-0.0187293f);
Float4 absx = Abs(x);
return As<Float4>(As<Int4>(half_pi - Sqrt(Float4(1.0f) - absx) * (a0 + absx * (a1 + absx * (a2 + absx * a3)))) ^
(As<Int4>(x) & Int4(0x80000000)));
}
}
// Approximation of atan in [0..1]
Float4 arctan_01(Float4 x, bool pp)
{
if(pp)
{
return x * (Float4(-0.27f) * x + Float4(1.05539816f));
}
else
{
// From 4.4.49, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
const Float4 a2(-0.3333314528f);
const Float4 a4(0.1999355085f);
const Float4 a6(-0.1420889944f);
const Float4 a8(0.1065626393f);
const Float4 a10(-0.0752896400f);
const Float4 a12(0.0429096138f);
const Float4 a14(-0.0161657367f);
const Float4 a16(0.0028662257f);
Float4 x2 = x * x;
return (x + x * (x2 * (a2 + x2 * (a4 + x2 * (a6 + x2 * (a8 + x2 * (a10 + x2 * (a12 + x2 * (a14 + x2 * a16)))))))));
}
}
Float4 arctan(RValue<Float4> x, bool pp)
{
Float4 absx = Abs(x);
Int4 O = CmpNLT(absx, Float4(1.0f));
Float4 y = As<Float4>((O & As<Int4>(Float4(1.0f) / absx)) | (~O & As<Int4>(absx))); // FIXME: Vector select
const Float4 half_pi(1.57079632f);
Float4 theta = arctan_01(y, pp);
return As<Float4>(((O & As<Int4>(half_pi - theta)) | (~O & As<Int4>(theta))) ^ // FIXME: Vector select
(As<Int4>(x) & Int4(0x80000000)));
}
Float4 arctan(RValue<Float4> y, RValue<Float4> x, bool pp)
{
const Float4 pi(3.14159265f); // pi
const Float4 minus_pi(-3.14159265f); // -pi
const Float4 half_pi(1.57079632f); // pi/2
const Float4 quarter_pi(7.85398163e-1f); // pi/4
// Rotate to upper semicircle when in lower semicircle
Int4 S = CmpLT(y, Float4(0.0f));
Float4 theta = As<Float4>(S & As<Int4>(minus_pi));
Float4 x0 = As<Float4>((As<Int4>(y) & Int4(0x80000000)) ^ As<Int4>(x));
Float4 y0 = Abs(y);
// Rotate to right quadrant when in left quadrant
Int4 Q = CmpLT(x0, Float4(0.0f));
theta += As<Float4>(Q & As<Int4>(half_pi));
Float4 x1 = As<Float4>((Q & As<Int4>(y0)) | (~Q & As<Int4>(x0))); // FIXME: Vector select
Float4 y1 = As<Float4>((Q & As<Int4>(-x0)) | (~Q & As<Int4>(y0))); // FIXME: Vector select
// Mirror to first octant when in second octant
Int4 O = CmpNLT(y1, x1);
Float4 x2 = As<Float4>((O & As<Int4>(y1)) | (~O & As<Int4>(x1))); // FIXME: Vector select
Float4 y2 = As<Float4>((O & As<Int4>(x1)) | (~O & As<Int4>(y1))); // FIXME: Vector select
// Approximation of atan in [0..1]
Int4 zero_x = CmpEQ(x2, Float4(0.0f));
Int4 inf_y = IsInf(y2); // Since x2 >= y2, this means x2 == y2 == inf, so we use 45 degrees or pi/4
Float4 atan2_theta = arctan_01(y2 / x2, pp);
theta += As<Float4>((~zero_x & ~inf_y & ((O & As<Int4>(half_pi - atan2_theta)) | (~O & (As<Int4>(atan2_theta))))) | // FIXME: Vector select
(inf_y & As<Int4>(quarter_pi)));
// Recover loss of precision for tiny theta angles
Int4 precision_loss = S & Q & O & ~inf_y; // This combination results in (-pi + half_pi + half_pi - atan2_theta) which is equivalent to -atan2_theta
return As<Float4>((precision_loss & As<Int4>(-atan2_theta)) | (~precision_loss & As<Int4>(theta))); // FIXME: Vector select
}
Float4 sineh(RValue<Float4> x, bool pp)
{
return (exponential(x, pp) - exponential(-x, pp)) * Float4(0.5f);
}
Float4 cosineh(RValue<Float4> x, bool pp)
{
return (exponential(x, pp) + exponential(-x, pp)) * Float4(0.5f);
}
Float4 tangenth(RValue<Float4> x, bool pp)
{
Float4 e_x = exponential(x, pp);
Float4 e_minus_x = exponential(-x, pp);
return (e_x - e_minus_x) / (e_x + e_minus_x);
}
Float4 arccosh(RValue<Float4> x, bool pp)
{
return logarithm(x + Sqrt(x + Float4(1.0f)) * Sqrt(x - Float4(1.0f)), pp);
}
Float4 arcsinh(RValue<Float4> x, bool pp)
{
return logarithm(x + Sqrt(x * x + Float4(1.0f)), pp);
}
Float4 arctanh(RValue<Float4> x, bool pp)
{
return logarithm((Float4(1.0f) + x) / (Float4(1.0f) - x), pp) * Float4(0.5f);
}
Float4 dot2(const Vector4f &v0, const Vector4f &v1)
{
return v0.x * v1.x + v0.y * v1.y;
}
Float4 dot3(const Vector4f &v0, const Vector4f &v1)
{
return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
}
Float4 dot4(const Vector4f &v0, const Vector4f &v1)
{
return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z + v0.w * v1.w;
}
void transpose4x4(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
{
Int2 tmp0 = UnpackHigh(row0, row1);
Int2 tmp1 = UnpackHigh(row2, row3);
Int2 tmp2 = UnpackLow(row0, row1);
Int2 tmp3 = UnpackLow(row2, row3);
row0 = UnpackLow(tmp2, tmp3);
row1 = UnpackHigh(tmp2, tmp3);
row2 = UnpackLow(tmp0, tmp1);
row3 = UnpackHigh(tmp0, tmp1);
}
void transpose4x3(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
{
Int2 tmp0 = UnpackHigh(row0, row1);
Int2 tmp1 = UnpackHigh(row2, row3);
Int2 tmp2 = UnpackLow(row0, row1);
Int2 tmp3 = UnpackLow(row2, row3);
row0 = UnpackLow(tmp2, tmp3);
row1 = UnpackHigh(tmp2, tmp3);
row2 = UnpackLow(tmp0, tmp1);
}
void transpose4x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
{
Float4 tmp0 = UnpackLow(row0, row1);
Float4 tmp1 = UnpackLow(row2, row3);
Float4 tmp2 = UnpackHigh(row0, row1);
Float4 tmp3 = UnpackHigh(row2, row3);
row0 = Float4(tmp0.xy, tmp1.xy);
row1 = Float4(tmp0.zw, tmp1.zw);
row2 = Float4(tmp2.xy, tmp3.xy);
row3 = Float4(tmp2.zw, tmp3.zw);
}
void transpose4x3(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
{
Float4 tmp0 = UnpackLow(row0, row1);
Float4 tmp1 = UnpackLow(row2, row3);
Float4 tmp2 = UnpackHigh(row0, row1);
Float4 tmp3 = UnpackHigh(row2, row3);
row0 = Float4(tmp0.xy, tmp1.xy);
row1 = Float4(tmp0.zw, tmp1.zw);
row2 = Float4(tmp2.xy, tmp3.xy);
}
void transpose4x2(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
{
Float4 tmp0 = UnpackLow(row0, row1);
Float4 tmp1 = UnpackLow(row2, row3);
row0 = Float4(tmp0.xy, tmp1.xy);
row1 = Float4(tmp0.zw, tmp1.zw);
}
void transpose4x1(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
{
Float4 tmp0 = UnpackLow(row0, row1);
Float4 tmp1 = UnpackLow(row2, row3);
row0 = Float4(tmp0.xy, tmp1.xy);
}
void transpose2x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
{
Float4 tmp01 = UnpackLow(row0, row1);
Float4 tmp23 = UnpackHigh(row0, row1);
row0 = tmp01;
row1 = Float4(tmp01.zw, row1.zw);
row2 = tmp23;
row3 = Float4(tmp23.zw, row3.zw);
}
void transpose4xN(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3, int N)
{
switch(N)
{
case 1: transpose4x1(row0, row1, row2, row3); break;
case 2: transpose4x2(row0, row1, row2, row3); break;
case 3: transpose4x3(row0, row1, row2, row3); break;
case 4: transpose4x4(row0, row1, row2, row3); break;
}
}
const Vector4f RegisterFile::operator[](RValue<Int4> index)
{
ASSERT(indirectAddressable);
Int index0 = Extract(index, 0);
Int index1 = Extract(index, 1);
Int index2 = Extract(index, 2);
Int index3 = Extract(index, 3);
Vector4f r;
r.x.x = Extract(x[0][index0], 0);
r.x.y = Extract(x[0][index1], 1);
r.x.z = Extract(x[0][index2], 2);
r.x.w = Extract(x[0][index3], 3);
r.y.x = Extract(y[0][index0], 0);
r.y.y = Extract(y[0][index1], 1);
r.y.z = Extract(y[0][index2], 2);
r.y.w = Extract(y[0][index3], 3);
r.z.x = Extract(z[0][index0], 0);
r.z.y = Extract(z[0][index1], 1);
r.z.z = Extract(z[0][index2], 2);
r.z.w = Extract(z[0][index3], 3);
r.w.x = Extract(w[0][index0], 0);
r.w.y = Extract(w[0][index1], 1);
r.w.z = Extract(w[0][index2], 2);
r.w.w = Extract(w[0][index3], 3);
return r;
}
void RegisterFile::scatter_x(Int4 index, RValue<Float4> r)
{
ASSERT(indirectAddressable);
Int index0 = Extract(index, 0);
Int index1 = Extract(index, 1);
Int index2 = Extract(index, 2);
Int index3 = Extract(index, 3);
x[0][index0] = Insert(x[0][index0], Extract(r, 0), 0);
x[0][index1] = Insert(x[0][index1], Extract(r, 1), 1);
x[0][index2] = Insert(x[0][index2], Extract(r, 2), 2);
x[0][index3] = Insert(x[0][index3], Extract(r, 3), 3);
}
void RegisterFile::scatter_y(Int4 index, RValue<Float4> r)
{
ASSERT(indirectAddressable);
Int index0 = Extract(index, 0);
Int index1 = Extract(index, 1);
Int index2 = Extract(index, 2);
Int index3 = Extract(index, 3);
y[0][index0] = Insert(y[0][index0], Extract(r, 0), 0);
y[0][index1] = Insert(y[0][index1], Extract(r, 1), 1);
y[0][index2] = Insert(y[0][index2], Extract(r, 2), 2);
y[0][index3] = Insert(y[0][index3], Extract(r, 3), 3);
}
void RegisterFile::scatter_z(Int4 index, RValue<Float4> r)
{
ASSERT(indirectAddressable);
Int index0 = Extract(index, 0);
Int index1 = Extract(index, 1);
Int index2 = Extract(index, 2);
Int index3 = Extract(index, 3);
z[0][index0] = Insert(z[0][index0], Extract(r, 0), 0);
z[0][index1] = Insert(z[0][index1], Extract(r, 1), 1);
z[0][index2] = Insert(z[0][index2], Extract(r, 2), 2);
z[0][index3] = Insert(z[0][index3], Extract(r, 3), 3);
}
void RegisterFile::scatter_w(Int4 index, RValue<Float4> r)
{
ASSERT(indirectAddressable);
Int index0 = Extract(index, 0);
Int index1 = Extract(index, 1);
Int index2 = Extract(index, 2);
Int index3 = Extract(index, 3);
w[0][index0] = Insert(w[0][index0], Extract(r, 0), 0);
w[0][index1] = Insert(w[0][index1], Extract(r, 1), 1);
w[0][index2] = Insert(w[0][index2], Extract(r, 2), 2);
w[0][index3] = Insert(w[0][index3], Extract(r, 3), 3);
}
void ShaderCore::mov(Vector4f &dst, const Vector4f &src, bool integerDestination)
{
if(integerDestination)
{
dst.x = As<Float4>(RoundInt(src.x));
dst.y = As<Float4>(RoundInt(src.y));
dst.z = As<Float4>(RoundInt(src.z));
dst.w = As<Float4>(RoundInt(src.w));
}
else
{
dst = src;
}
}
void ShaderCore::neg(Vector4f &dst, const Vector4f &src)
{
dst.x = -src.x;
dst.y = -src.y;
dst.z = -src.z;
dst.w = -src.w;
}
void ShaderCore::ineg(Vector4f &dst, const Vector4f &src)
{
dst.x = As<Float4>(-As<Int4>(src.x));
dst.y = As<Float4>(-As<Int4>(src.y));
dst.z = As<Float4>(-As<Int4>(src.z));
dst.w = As<Float4>(-As<Int4>(src.w));
}
void ShaderCore::f2b(Vector4f &dst, const Vector4f &src)
{
dst.x = As<Float4>(CmpNEQ(src.x, Float4(0.0f)));
dst.y = As<Float4>(CmpNEQ(src.y, Float4(0.0f)));
dst.z = As<Float4>(CmpNEQ(src.z, Float4(0.0f)));
dst.w = As<Float4>(CmpNEQ(src.w, Float4(0.0f)));
}
void ShaderCore::b2f(Vector4f &dst, const Vector4f &src)
{
dst.x = As<Float4>(As<Int4>(src.x) & As<Int4>(Float4(1.0f)));
dst.y = As<Float4>(As<Int4>(src.y) & As<Int4>(Float4(1.0f)));
dst.z = As<Float4>(As<Int4>(src.z) & As<Int4>(Float4(1.0f)));
dst.w = As<Float4>(As<Int4>(src.w) & As<Int4>(Float4(1.0f)));
}
void ShaderCore::f2i(Vector4f &dst, const Vector4f &src)
{
dst.x = As<Float4>(Int4(src.x));
dst.y = As<Float4>(Int4(src.y));
dst.z = As<Float4>(Int4(src.z));
dst.w = As<Float4>(Int4(src.w));
}
void ShaderCore::i2f(Vector4f &dst, const Vector4f &src)
{
dst.x = Float4(As<Int4>(src.x));
dst.y = Float4(As<Int4>(src.y));
dst.z = Float4(As<Int4>(src.z));
dst.w = Float4(As<Int4>(src.w));
}
void ShaderCore::f2u(Vector4f &dst, const Vector4f &src)
{
dst.x = As<Float4>(UInt4(src.x));
dst.y = As<Float4>(UInt4(src.y));
dst.z = As<Float4>(UInt4(src.z));
dst.w = As<Float4>(UInt4(src.w));
}
void ShaderCore::u2f(Vector4f &dst, const Vector4f &src)
{
dst.x = Float4(As<UInt4>(src.x));
dst.y = Float4(As<UInt4>(src.y));
dst.z = Float4(As<UInt4>(src.z));
dst.w = Float4(As<UInt4>(src.w));
}
void ShaderCore::i2b(Vector4f &dst, const Vector4f &src)
{
dst.x = As<Float4>(CmpNEQ(As<Int4>(src.x), Int4(0)));
dst.y = As<Float4>(CmpNEQ(As<Int4>(src.y), Int4(0)));
dst.z = As<Float4>(CmpNEQ(As<Int4>(src.z), Int4(0)));
dst.w = As<Float4>(CmpNEQ(As<Int4>(src.w), Int4(0)));
}
void ShaderCore::b2i(Vector4f &dst, const Vector4f &src)
{
dst.x = As<Float4>(As<Int4>(src.x) & Int4(1));
dst.y = As<Float4>(As<Int4>(src.y) & Int4(1));
dst.z = As<Float4>(As<Int4>(src.z) & Int4(1));
dst.w = As<Float4>(As<Int4>(src.w) & Int4(1));
}
void ShaderCore::add(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = src0.x + src1.x;
dst.y = src0.y + src1.y;
dst.z = src0.z + src1.z;
dst.w = src0.w + src1.w;
}
void ShaderCore::iadd(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = As<Float4>(As<Int4>(src0.x) + As<Int4>(src1.x));
dst.y = As<Float4>(As<Int4>(src0.y) + As<Int4>(src1.y));
dst.z = As<Float4>(As<Int4>(src0.z) + As<Int4>(src1.z));
dst.w = As<Float4>(As<Int4>(src0.w) + As<Int4>(src1.w));
}
void ShaderCore::sub(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = src0.x - src1.x;
dst.y = src0.y - src1.y;
dst.z = src0.z - src1.z;
dst.w = src0.w - src1.w;
}
void ShaderCore::isub(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = As<Float4>(As<Int4>(src0.x) - As<Int4>(src1.x));
dst.y = As<Float4>(As<Int4>(src0.y) - As<Int4>(src1.y));
dst.z = As<Float4>(As<Int4>(src0.z) - As<Int4>(src1.z));
dst.w = As<Float4>(As<Int4>(src0.w) - As<Int4>(src1.w));
}
void ShaderCore::mad(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
{
dst.x = src0.x * src1.x + src2.x;
dst.y = src0.y * src1.y + src2.y;
dst.z = src0.z * src1.z + src2.z;
dst.w = src0.w * src1.w + src2.w;
}
void ShaderCore::imad(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
{
dst.x = As<Float4>(As<Int4>(src0.x) * As<Int4>(src1.x) + As<Int4>(src2.x));
dst.y = As<Float4>(As<Int4>(src0.y) * As<Int4>(src1.y) + As<Int4>(src2.y));
dst.z = As<Float4>(As<Int4>(src0.z) * As<Int4>(src1.z) + As<Int4>(src2.z));
dst.w = As<Float4>(As<Int4>(src0.w) * As<Int4>(src1.w) + As<Int4>(src2.w));
}
void ShaderCore::mul(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = src0.x * src1.x;
dst.y = src0.y * src1.y;
dst.z = src0.z * src1.z;
dst.w = src0.w * src1.w;
}
void ShaderCore::imul(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = As<Float4>(As<Int4>(src0.x) * As<Int4>(src1.x));
dst.y = As<Float4>(As<Int4>(src0.y) * As<Int4>(src1.y));
dst.z = As<Float4>(As<Int4>(src0.z) * As<Int4>(src1.z));
dst.w = As<Float4>(As<Int4>(src0.w) * As<Int4>(src1.w));
}
void ShaderCore::rcpx(Vector4f &dst, const Vector4f &src, bool pp)
{
Float4 rcp = reciprocal(src.x, pp, true, true);
dst.x = rcp;
dst.y = rcp;
dst.z = rcp;
dst.w = rcp;
}
void ShaderCore::div(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = src0.x / src1.x;
dst.y = src0.y / src1.y;
dst.z = src0.z / src1.z;
dst.w = src0.w / src1.w;
}
void ShaderCore::idiv(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
Float4 intMax(As<Float4>(Int4(INT_MAX)));
cmp0i(dst.x, src1.x, intMax, src1.x);
dst.x = As<Float4>(As<Int4>(src0.x) / As<Int4>(dst.x));
cmp0i(dst.y, src1.y, intMax, src1.y);
dst.y = As<Float4>(As<Int4>(src0.y) / As<Int4>(dst.y));
cmp0i(dst.z, src1.z, intMax, src1.z);
dst.z = As<Float4>(As<Int4>(src0.z) / As<Int4>(dst.z));
cmp0i(dst.w, src1.w, intMax, src1.w);
dst.w = As<Float4>(As<Int4>(src0.w) / As<Int4>(dst.w));
}
void ShaderCore::udiv(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
Float4 uintMax(As<Float4>(UInt4(UINT_MAX)));
cmp0i(dst.x, src1.x, uintMax, src1.x);
dst.x = As<Float4>(As<UInt4>(src0.x) / As<UInt4>(dst.x));
cmp0i(dst.y, src1.y, uintMax, src1.y);
dst.y = As<Float4>(As<UInt4>(src0.y) / As<UInt4>(dst.y));
cmp0i(dst.z, src1.z, uintMax, src1.z);
dst.z = As<Float4>(As<UInt4>(src0.z) / As<UInt4>(dst.z));
cmp0i(dst.w, src1.w, uintMax, src1.w);
dst.w = As<Float4>(As<UInt4>(src0.w) / As<UInt4>(dst.w));
}
void ShaderCore::mod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = modulo(src0.x, src1.x);
dst.y = modulo(src0.y, src1.y);
dst.z = modulo(src0.z, src1.z);
dst.w = modulo(src0.w, src1.w);
}
void ShaderCore::imod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
Float4 intMax(As<Float4>(Int4(INT_MAX)));
cmp0i(dst.x, src1.x, intMax, src1.x);
dst.x = As<Float4>(As<Int4>(src0.x) % As<Int4>(dst.x));
cmp0i(dst.y, src1.y, intMax, src1.y);
dst.y = As<Float4>(As<Int4>(src0.y) % As<Int4>(dst.y));
cmp0i(dst.z, src1.z, intMax, src1.z);
dst.z = As<Float4>(As<Int4>(src0.z) % As<Int4>(dst.z));
cmp0i(dst.w, src1.w, intMax, src1.w);
dst.w = As<Float4>(As<Int4>(src0.w) % As<Int4>(dst.w));
}
void ShaderCore::umod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
Float4 uintMax(As<Float4>(UInt4(UINT_MAX)));
cmp0i(dst.x, src1.x, uintMax, src1.x);
dst.x = As<Float4>(As<UInt4>(src0.x) % As<UInt4>(dst.x));
cmp0i(dst.y, src1.y, uintMax, src1.y);
dst.y = As<Float4>(As<UInt4>(src0.y) % As<UInt4>(dst.y));
cmp0i(dst.z, src1.z, uintMax, src1.z);
dst.z = As<Float4>(As<UInt4>(src0.z) % As<UInt4>(dst.z));
cmp0i(dst.w, src1.w, uintMax, src1.w);
dst.w = As<Float4>(As<UInt4>(src0.w) % As<UInt4>(dst.w));
}
void ShaderCore::shl(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = As<Float4>(As<Int4>(src0.x) << As<Int4>(src1.x));
dst.y = As<Float4>(As<Int4>(src0.y) << As<Int4>(src1.y));
dst.z = As<Float4>(As<Int4>(src0.z) << As<Int4>(src1.z));
dst.w = As<Float4>(As<Int4>(src0.w) << As<Int4>(src1.w));
}
void ShaderCore::ishr(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = As<Float4>(As<Int4>(src0.x) >> As<Int4>(src1.x));
dst.y = As<Float4>(As<Int4>(src0.y) >> As<Int4>(src1.y));
dst.z = As<Float4>(As<Int4>(src0.z) >> As<Int4>(src1.z));
dst.w = As<Float4>(As<Int4>(src0.w) >> As<Int4>(src1.w));
}
void ShaderCore::ushr(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
{
dst.x = As<Float4>(As<UInt4>(src0.x) >> As<UInt4>(src1.x));
dst.y = As<Float4>(As<UInt4>(src0.y) >> As<UInt4>(src1.y));
dst.z = As<Float4>(As<UInt4>(src0.z) >> As<UInt4>(src1.z));
dst.w = As<Float4>(As<UInt4>(src0.w) >> As<UInt4>(src1.w));
}
void ShaderCore::rsqx(Vector4f &dst, const Vector4f &src, bool pp)
{
Float4 rsq = reciprocalSquareRoot(src.x, true, pp);
dst.x = rsq;
dst.y = rsq;
dst.z = rsq;
dst.w = rsq;
}
void ShaderCore::sqrt(Vector4f &dst, const Vector4f &src, bool pp)
{
dst.x = Sqrt(src.x);
dst.y = Sqrt(src.y);
dst.z = Sqrt(src.z);
dst.w = Sqrt(src.w);
}
void ShaderCore::rsq(Vector4f &dst, const Vector4f &src, bool pp)
{
dst.x = reciprocalSquareRoot(src.x, false, pp);
dst.y = reciprocalSquareRoot(src.y, false, pp);
dst.z = reciprocalSquareRoot(src.z, false, pp);
dst.w = reciprocalSquareRoot(src.w, false, pp);
}
void ShaderCore::len2(Float4 &dst, const Vector4f &src, bool pp)
{
dst = Sqrt(dot2(src, src));
}
void ShaderCore::len3(Float4 &dst, const Vector4f &src, bool pp)
{
dst = Sqrt(dot3(src, src));
}
void ShaderCore::len4(Float4 &dst, const Vector4f &src, bool pp)
{
dst = Sqrt(dot4(src, src));
}
void ShaderCore::dist1(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
{
dst = Abs(src0.x - src1.x);
}
void ShaderCore::dist2(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
{
Float4 dx = src0.x - src1.x;
Float4 dy = src0.y - src1.y;
Float4 dot2 = dx * dx + dy * dy;
dst = Sqrt(dot2);
}
void ShaderCore::dist3(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
{
Float4 dx = src0.x - src1.x;
Float4 dy = src0.y - src1.y;
Float4 dz = src0.z - src1.z;
Float4 dot3 = dx * dx + dy * dy + dz * dz;
dst = Sqrt(dot3);
}
void ShaderCore::dist4(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
{
Float4 dx = src0.x - src1.x;
Float4 dy = src0.y - src1.y;
Float4 dz = src0.z - src1.z;
Float4 dw = src0.w - src1.w;