xref: /aosp_15_r20/external/skia/resources/sksl/folding/MatrixVectorNoOpFolding.rts (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1uniform float2x2 testMatrix2x2;
2uniform float3x3 testMatrix3x3;
3uniform float4 testInputs;
4uniform half4 colorRed, colorGreen;
5uniform half unknownInput;
6
7bool test_no_op_mat2_X_vec2() {
8    const float2x2 i = float2x2(1.0);
9    const float2x2 z = float2x2(0.0);
10    const float2x2 n = float2x2(-1.0);
11
12    float2 v, vv;
13    v = testInputs.xy * i;
14    v = i * testInputs.xy;
15    if (v != testInputs.xy) return false;
16
17    v = v * i;
18    v = i * v;
19    v *= i;
20    if (v != testInputs.xy) return false;
21
22    v = testInputs.xy * n;
23    v = n * testInputs.xy;
24    if (v != -testInputs.xy) return false;
25
26    vv = v * z;
27    vv = z * v;
28    return vv == z[0];
29}
30
31bool test_no_op_mat3_X_vec3() {
32    const float3x3 i = float3x3(1.0);
33    const float3x3 z = float3x3(0.0);
34    const float3x3 n = float3x3(-1.0);
35
36    float3 v, vv;
37    v = testInputs.xyz * i;
38    v = i * testInputs.xyz;
39    if (v != testInputs.xyz) return false;
40
41    v = v * i;
42    v = i * v;
43    v *= i;
44    if (v != testInputs.xyz) return false;
45
46    v = testInputs.xyz * n;
47    v = n * testInputs.xyz;
48    if (v != -testInputs.xyz) return false;
49
50    vv = v * z;
51    vv = z * v;
52    return vv == z[0];
53}
54
55bool test_no_op_mat4_X_vec4() {
56    const float4x4 i = float4x4(1.0);
57    const float4x4 z = float4x4(0.0);
58    const float4x4 n = float4x4(-1.0);
59
60    float4 v, vv;
61    v = testInputs * i;
62    v = i * testInputs;
63    if (v != testInputs) return false;
64
65    v = v * i;
66    v = i * v;
67    v *= i;
68    if (v != testInputs) return false;
69
70    v = testInputs * n;
71    v = n * testInputs;
72    if (v != -testInputs) return false;
73
74    vv = v * z;
75    vv = z * v;
76    return vv == z[0];
77}
78
79bool test_no_op_vec2_X_mat2() {
80    const float2 n = float2(-1.0);
81    const float2 i = float2(1.0);
82    const float2 z = float2(0.0);
83
84    // These operations can be optimized; multiplying a zero vector across any matrix always results
85    // in a zero vector.
86    float2 v, vv;
87    vv = z * testMatrix2x2;
88    vv = testMatrix2x2 * z;
89    if (vv != z) return false;
90
91    // These operations can't be simplified; they do real work.
92    v = i * testMatrix2x2;
93    if (v != float2(3, 7)) return false;
94    v = testMatrix2x2 * i;
95    if (v != float2(4, 6)) return false;
96
97    v = n * testMatrix2x2;
98    if (v != -float2(3, 7)) return false;
99    v = testMatrix2x2 * n;
100    return v == -float2(4, 6);
101}
102
103bool test_no_op_vec3_X_mat3() {
104    const float3 n = float3(-1.0);
105    const float3 i = float3(1.0);
106    const float3 z = float3(0.0);
107
108    // These operations can be optimized; multiplying a zero vector across any matrix always results
109    // in a zero vector.
110    float3 v, vv;
111    vv = z * testMatrix3x3;
112    vv = testMatrix3x3 * z;
113    if (vv != z) return false;
114
115    // These operations can't be simplified; they do real work.
116    v = i * testMatrix3x3;
117    if (v != float3(6, 15, 24)) return false;
118    v = testMatrix3x3 * i;
119    if (v != float3(12, 15, 18)) return false;
120
121    v = n * testMatrix3x3;
122    if (v != -float3(6, 15, 24)) return false;
123    v = testMatrix3x3 * n;
124    return v == -float3(12, 15, 18);
125}
126
127bool test_no_op_vec4_X_mat4() {
128    const float4 n = float4(-1.0);
129    const float4 i = float4(1.0);
130    const float4 z = float4(0.0);
131    float4x4 testMatrix4x4 = float4x4(testMatrix2x2[0], testMatrix2x2[1],
132                                      testMatrix2x2[0], testMatrix2x2[1],
133                                      testMatrix2x2[0], testMatrix2x2[1],
134                                      testMatrix2x2[0], testMatrix2x2[1]);
135
136    // These operations can be optimized; multiplying a zero vector across any matrix always results
137    // in a zero vector.
138    float4 v, vv;
139    vv = z * testMatrix4x4;
140    vv = testMatrix4x4 * z;
141    if (vv != z) return false;
142
143    // These operations can't be simplified; they do real work.
144    v = i * testMatrix4x4;
145    if (v != float4(10, 10, 10, 10)) return false;
146    v = testMatrix4x4 * i;
147    if (v != float4(4, 8, 12, 16)) return false;
148
149    v = n * testMatrix4x4;
150    if (v != -float4(10, 10, 10, 10)) return false;
151    v = testMatrix4x4 * n;
152    return v == -float4(4, 8, 12, 16);
153}
154
155half4 main(float2 coords) {
156    return test_no_op_mat2_X_vec2() &&
157           test_no_op_mat3_X_vec3() &&
158           test_no_op_mat4_X_vec4() &&
159           test_no_op_vec2_X_mat2() &&
160           test_no_op_vec3_X_mat3() &&
161           test_no_op_vec4_X_mat4() ? colorGreen : colorRed;
162}
163