xref: /aosp_15_r20/external/skia/resources/sksl/folding/MatrixFoldingES2.rts (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1uniform float2x2 testMatrix2x2;
2uniform half4 colorRed, colorGreen;
3uniform half unknownInput;
4
5bool test_eq() {
6    // A non-folding version of these tests can be found in shared/MatrixEquality.
7    bool ok = true;
8    ok = ok &&  (float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ==
9                 float2x2(float2(1.0, 0.0), float2(0.0, 1.0)));
10    ok = ok && !(float2x2(float2(1.0, 0.0), float2(1.0, 1.0)) ==
11                 float2x2(float2(1.0, 0.0), float2(0.0, 1.0)));
12
13    ok = ok &&  ( float2x2(1)  == float2x2(1));
14    ok = ok && !( float2x2(1)  == float2x2(0));
15    ok = ok &&  ( float2x2(-1) == -float2x2(1));
16    ok = ok &&  ( float2x2(0)  == -float2x2(0));
17    ok = ok &&  (-float2x2(-1) ==  float2x2(1));
18    ok = ok &&  (-float2x2(0)  == -float2x2(-0));
19
20    ok = ok &&  (float2x2(1) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)));
21    ok = ok && !(float2x2(2) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)));
22
23    ok = ok && !(float2x2(1) != float2x2(1));
24    ok = ok &&  (float2x2(1) != float2x2(0));
25    ok = ok &&  (float3x3(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), float3(0.0, 0.0, 1.0)) ==
26                 float3x3(float2x2(1.0)));
27    ok = ok &&  (float3x3(float3(9.0, 0.0, 0.0), float3(0.0, 9.0, 0.0), float3(0.0, 0.0, 1.0)) ==
28                 float3x3(float2x2(9.0)));
29    ok = ok &&  (float3x3(unknownInput) == float3x3(float2x2(1.0)));
30    ok = ok &&  (float3x3(float3(9).x00, float3(9).0x0, float3(unknownInput).00x) ==
31                 float3x3(float2x2(9.0)));
32    ok = ok &&  (float2x2(float3x3(1.0)) == float2x2(1.0));
33    ok = ok &&  (float2x2(float3x3(1.0)) == float2x2(1.0));
34    ok = ok &&  (float2x2(float4(1.0, 0.0, 0.0, 1.0)) == float2x2(1.0));
35    ok = ok &&  (float2x2(1.0, 0.0, float2(0.0, 1.0)) == float2x2(1.0));
36    ok = ok &&  (float2x2(float2(1.0, 0.0), 0.0, 1.0) == float2x2(1.0));
37
38    ok = ok &&  (float4(testMatrix2x2) * float4(1)) == float4(1, 2, 3, 4);
39    ok = ok &&  (float4(testMatrix2x2) * float4(1)) == float4(testMatrix2x2);
40    ok = ok &&  (float4(testMatrix2x2) * float4(0)) == float4(0);
41
42    ok = ok &&  (float2x2(5.0)[0] == float2(5.0, 0.0));
43    ok = ok &&  (float2x2(5.0)[1] == float2(0.0, 5.0));
44
45    ok = ok &&  (float2x2(5.0)[0][0] == 5.0);
46    ok = ok &&  (float2x2(5.0)[0][1] == 0.0);
47    ok = ok &&  (float2x2(5.0)[1][0] == 0.0);
48    ok = ok &&  (float2x2(5.0)[1][1] == 5.0);
49
50    const float3x3 m = float3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
51    ok = ok &&  (m[0] == float3(1, 2, 3));
52    ok = ok &&  (m[1] == float3(4, 5, 6));
53    ok = ok &&  (m[2] == float3(7, 8, 9));
54
55    ok = ok &&  (m[0][0] == 1);
56    ok = ok &&  (m[0][1] == 2);
57    ok = ok &&  (m[0][2] == 3);
58    ok = ok &&  (m[1][0] == 4);
59    ok = ok &&  (m[1][1] == 5);
60    ok = ok &&  (m[1][2] == 6);
61    ok = ok &&  (m[2][0] == 7);
62    ok = ok &&  (m[2][1] == 8);
63    ok = ok &&  (m[2][2] == 9);
64
65    {
66        // This `five` is constant and should always fold.
67        const float five = 5.0;
68        ok = ok &&  (float2x2(five)[0] == float2(five, 0.0));
69        ok = ok &&  (float2x2(five)[1] == float2(0.0, five));
70
71        ok = ok &&  (float2x2(five)[0][0] == five);
72        ok = ok &&  (float2x2(five)[0][1] == 0.0);
73        ok = ok &&  (float2x2(five)[1][0] == 0.0);
74        ok = ok &&  (float2x2(five)[1][1] == five);
75
76        ok = ok &&  (float3x3(1, 2, 3, 4, five, 6, 7, 8, 9)[0] == float3(1, 2, 3));
77        ok = ok &&  (float3x3(1, 2, 3, 4, five, 6, 7, 8, 9)[1] == float3(4, five, 6));
78        ok = ok &&  (float3x3(1, 2, 3, 4, five, 6, 7, 8, 9)[2] == float3(7, 8, 9));
79    }
80    {
81        // This `five` cannot be folded, but the first and third columns should still be foldable.
82        float five = 5.0;
83        ok = ok &&  (float3x3(1, 2, 3, 4, five, 6, 7, 8, 9)[0] == float3(1, 2, 3));
84        ok = ok &&  (float3x3(1, 2, 3, 4, five, 6, 7, 8, 9)[1] == float3(4, five, 6));
85        ok = ok &&  (float3x3(1, 2, 3, 4, five, 6, 7, 8, 9)[2] == float3(7, 8, 9));
86    }
87    {
88        // The upper-left 2x2 of the matrix is unknown, but the bottom two rows are still foldable.
89        ok = ok && float4x4(half3x3(testMatrix2x2))[0] == float4(1, 2, 0, 0);
90        ok = ok && float4x4(half3x3(testMatrix2x2))[1] == float4(3, 4, 0, 0);
91        ok = ok && float4x4(half3x3(testMatrix2x2))[2] == float4(0, 0, 1, 0);
92        ok = ok && float4x4(half3x3(testMatrix2x2))[3] == float4(0, 0, 0, 1);
93    }
94
95    return ok;
96}
97
98bool test_matrix_op_scalar_float() {
99    bool ok = true;
100
101    ok = ok && ((float3x3(2) + 4) == float3x3(6, 4, 4, 4, 6, 4, 4, 4, 6));
102    ok = ok && ((float3x3(2) - 4) == float3x3(-2, -4, -4, -4, -2, -4, -4, -4, -2));
103    ok = ok && ((float3x3(2) * 4) == float3x3(8));
104    ok = ok && ((float3x3(2) / 4) == float3x3(0.5));
105
106    ok = ok && (4 + (float3x3(2)) == float3x3(6, 4, 4, 4, 6, 4, 4, 4, 6));
107    ok = ok && (4 - (float3x3(2)) == float3x3(2, 4, 4, 4, 2, 4, 4, 4, 2));
108    ok = ok && (4 * (float3x3(2)) == float3x3(8));
109    ok = ok && (4 / (float2x2(2, 2, 2, 2)) == float2x2(2, 2, 2, 2));
110
111    return ok;
112}
113
114bool test_matrix_op_scalar_half() {
115    bool ok = true;
116
117    ok = ok && ((half3x3(2) + 4) == half3x3(6, 4, 4, 4, 6, 4, 4, 4, 6));
118    ok = ok && ((half3x3(2) - 4) == half3x3(-2, -4, -4, -4, -2, -4, -4, -4, -2));
119    ok = ok && ((half3x3(2) * 4) == half3x3(8));
120    ok = ok && ((half3x3(2) / 4) == half3x3(0.5));
121
122    ok = ok && (4 + (half3x3(2)) == half3x3(6, 4, 4, 4, 6, 4, 4, 4, 6));
123    ok = ok && (4 - (half3x3(2)) == half3x3(2, 4, 4, 4, 2, 4, 4, 4, 2));
124    ok = ok && (4 * (half3x3(2)) == half3x3(8));
125    ok = ok && (4 / (half2x2(2, 2, 2, 2)) == half2x2(2, 2, 2, 2));
126
127    return ok;
128}
129
130bool test_matrix_op_matrix_float() {
131    bool ok = true;
132
133    // Addition, subtraction and division operate componentwise.
134    const float3x3 splat_4 = float3x3(4, 4, 4, 4, 4, 4, 4, 4, 4);
135
136    ok = ok && ((float3x3(2) + splat_4) == float3x3(6, 4, 4, 4, 6, 4, 4, 4, 6));
137    ok = ok && ((float3x3(2) - splat_4) == float3x3(-2, -4, -4, -4, -2, -4, -4, -4, -2));
138    ok = ok && ((float3x3(2) / splat_4) == float3x3(0.5));
139
140    ok = ok && (splat_4 + (float3x3(2)) == float3x3(6, 4, 4, 4, 6, 4, 4, 4, 6));
141    ok = ok && (splat_4 - (float3x3(2)) == float3x3(2, 4, 4, 4, 2, 4, 4, 4, 2));
142    ok = ok && (float2x2(4, 4, 4, 4) / (float2x2(2, 2, 2, 2)) == float2x2(2, 2, 2, 2));
143
144    ok = ok && (float4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) +
145                float4x4(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) ==
146                float4x4(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17));
147
148    ok = ok && (float2x2(10, 20, 30, 40) - float2x2(1, 2, 3, 4) == float2x2(9, 18, 27, 36));
149
150    ok = ok && (float2x2(10, 20, 30, 40) / float2x2(5, 4, 30, 1) == float2x2(2, 5, 1, 40));
151
152    // Multiplication performs a proper matrix multiply.
153    ok = ok && (float2x2(1, 2, 7, 4) * float2x2(3, 5, 3, 2) == float2x2(38, 26, 17, 14));
154    ok = ok && (float3x3(10, 4, 2, 20, 5, 3, 10, 6, 5) *
155                float3x3(3, 3, 4, 2, 3, 4, 4, 9, 2) ==
156                float3x3(130, 51, 35, 120, 47, 33, 240, 73, 45));
157
158    return ok;
159}
160
161bool test_matrix_op_matrix_half() {
162    bool ok = true;
163
164    // Addition, subtraction and division operate componentwise.
165    const half3x3 splat_4 = half3x3(4, 4, 4, 4, 4, 4, 4, 4, 4);
166
167    ok = ok && ((half3x3(2) + splat_4) == half3x3(6, 4, 4, 4, 6, 4, 4, 4, 6));
168    ok = ok && ((half3x3(2) - splat_4) == half3x3(-2, -4, -4, -4, -2, -4, -4, -4, -2));
169    ok = ok && ((half3x3(2) / splat_4) == half3x3(0.5));
170
171    ok = ok && (splat_4 + (half3x3(2)) == half3x3(6, 4, 4, 4, 6, 4, 4, 4, 6));
172    ok = ok && (splat_4 - (half3x3(2)) == half3x3(2, 4, 4, 4, 2, 4, 4, 4, 2));
173    ok = ok && (half2x2(4, 4, 4, 4) / (half2x2(2, 2, 2, 2)) == half2x2(2, 2, 2, 2));
174
175    ok = ok && (half4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) +
176                half4x4(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) ==
177                half4x4(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17));
178
179    ok = ok && (half2x2(10, 20, 30, 40) - half2x2(1, 2, 3, 4) == half2x2(9, 18, 27, 36));
180
181    ok = ok && (half2x2(10, 20, 30, 40) / half2x2(5, 4, 30, 1) == half2x2(2, 5, 1, 40));
182
183    // Multiplication performs a proper matrix multiply.
184    ok = ok && (half2x2(1, 2, 7, 4) * half2x2(3, 5, 3, 2) == half2x2(38, 26, 17, 14));
185    ok = ok && (half3x3(10, 4, 2, 20, 5, 3, 10, 6, 5) *
186                half3x3(3, 3, 4, 2, 3, 4, 4, 9, 2) ==
187                half3x3(130, 51, 35, 120, 47, 33, 240, 73, 45));
188
189    return ok;
190}
191
192bool test_vector_op_matrix_float() {
193    bool ok = true;
194
195    ok = ok && (float2(1, 2) * float2x2(3, 4, 5, 6) == float2(11, 17));
196    ok = ok && (float3(1, 2, 3) * float3x3(-1, 0, 1, 0, 1, 0, 2, 0, 1) == float3(2, 2, 5));
197    ok = ok && (float4(1, 2, 3, 4) * float4x4(1, 0, 3, 0, 0, 2, 0, 0, 1, 0, 2, 1, 0, 2, 0, 1) ==
198                    float4(10, 4, 11, 8));
199    return ok;
200}
201
202bool test_vector_op_matrix_half() {
203    bool ok = true;
204
205    ok = ok && (half2(1, 2) * half2x2(3, 4, 5, 6) == half2(11, 17));
206    ok = ok && (half3(1, 2, 3) * half3x3(-1, 0, 1, 0, 1, 0, 2, 0, 1) == half3(2, 2, 5));
207    ok = ok && (half4(1, 2, 3, 4) * half4x4(1, 0, 3, 0, 0, 2, 0, 0, 1, 0, 2, 1, 0, 2, 0, 1) ==
208                    half4(10, 4, 11, 8));
209    return ok;
210}
211
212bool test_matrix_op_vector_float() {
213    bool ok = true;
214
215    ok = ok && (float2x2(3, 4, 5, 6) * float2(1, 2) == float2(13, 16));
216    ok = ok && (float3x3(-1, 0, 1, 0, 1, 0, 2, 0, 1) * float3(1, 2, 3) == float3(5, 2, 4));
217    ok = ok && (float4x4(1, 0, 3, 0, 0, 2, 0, 0, 1, 0, 2, 1, 0, 2, 0, 1) * float4(1, 2, 3, 4) ==
218                    float4(4, 12, 9, 7));
219    return ok;
220}
221
222bool test_matrix_op_vector_half() {
223    bool ok = true;
224
225    ok = ok && (half2x2(3, 4, 5, 6) * half2(1, 2) == half2(13, 16));
226    ok = ok && (half3x3(-1, 0, 1, 0, 1, 0, 2, 0, 1) * half3(1, 2, 3) == half3(5, 2, 4));
227    ok = ok && (half4x4(1, 0, 3, 0, 0, 2, 0, 0, 1, 0, 2, 1, 0, 2, 0, 1) * half4(1, 2, 3, 4) ==
228                    half4(4, 12, 9, 7));
229    return ok;
230}
231
232half4 main(float2 coords) {
233    return (test_eq() &&
234            test_matrix_op_scalar_float() &&
235            test_matrix_op_scalar_half() &&
236            test_matrix_op_matrix_float() &&
237            test_matrix_op_matrix_half() &&
238            test_vector_op_matrix_float() &&
239            test_vector_op_matrix_half() &&
240            test_matrix_op_vector_float() &&
241            test_matrix_op_vector_half())? colorGreen : colorRed;
242}
243