xref: /aosp_15_r20/external/skia/resources/sksl/folding/MatrixFoldingES3.sksl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1uniform half4 colorRed, colorGreen;
2
3bool test_eq_float() {
4    bool ok = true;
5    ok = ok && (float3x2(2) == float3x2(float2(2.0, 0.0), float2(0.0, 2.0), float2(0.0)));
6    ok = ok && (float3x2(1, 2, 3, 4, 5, 6) == float3x2(float4x2(1, 2, 3, 4, 5, 6, 7, 8)));
7    return ok;
8}
9
10bool test_eq_half() {
11    bool ok = true;
12    ok = ok && (half3x2(2) == half3x2(half2(2.0, 0.0), half2(0.0, 2.0), half2(0.0)));
13    ok = ok && (half3x2(1, 2, 3, 4, 5, 6) == half3x2(half4x2(1, 2, 3, 4, 5, 6, 7, 8)));
14    return ok;
15}
16
17bool test_matrix_op_matrix_float() {
18    bool ok = true;
19
20    // Addition, subtraction and division operate componentwise.
21    {
22        const float3x2 splat_4 = float3x2(4, 4, 4, 4, 4, 4);
23
24        ok = ok && ((float3x2(2) + splat_4) == float3x2(6, 4, 4, 6, 4, 4));
25        ok = ok && ((float3x2(2) - splat_4) == float3x2(-2, -4, -4, -2, -4, -4));
26        ok = ok && ((float3x2(2) / splat_4) == float3x2(0.5));
27    }
28    {
29        const float2x3 splat_4 = float2x3(4, 4, 4, 4, 4, 4);
30
31        ok = ok && (splat_4 + (float2x3(2)) == float2x3(6, 4, 4, 4, 6, 4));
32        ok = ok && (splat_4 - (float2x3(2)) == float2x3(2, 4, 4, 4, 2, 4));
33        ok = ok && (splat_4 / (float2x3(2, 2, 2, 2, 2, 2)) == float2x3(2, 2, 2, 2, 2, 2));
34    }
35    ok = ok && (float4x3(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) +
36                float4x3(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5) ==
37                float4x3(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17));
38
39    ok = ok && (float4x2(10, 20, 30, 40, 50, 60, 70, 80) -
40                float4x2(1, 2, 3, 4, 5, 6, 7, 8) ==
41                float4x2(9, 18, 27, 36, 45, 54, 63, 72));
42
43    ok = ok && (float2x4(10, 20, 30, 40, 10, 20, 30, 40) /
44                float2x4(10, 10, 10, 10, 5, 5, 5, 5) ==
45                float2x4(1, 2, 3, 4, 2, 4, 6, 8));
46
47    // Multiplication performs a proper matrix multiply.
48    ok = ok && (float3x2(1, 4, 2, 5, 3, 6) *
49                float2x3(7, 9, 11, 8, 10, 12) ==
50                float2x2(58, 139, 64, 154));
51    return ok;
52}
53
54bool test_matrix_op_matrix_half() {
55    bool ok = true;
56
57    // Addition, subtraction and division operate componentwise.
58    {
59        const half3x2 splat_4 = half3x2(4, 4, 4, 4, 4, 4);
60
61        ok = ok && ((half3x2(2) + splat_4) == half3x2(6, 4, 4, 6, 4, 4));
62        ok = ok && ((half3x2(2) - splat_4) == half3x2(-2, -4, -4, -2, -4, -4));
63        ok = ok && ((half3x2(2) / splat_4) == half3x2(0.5));
64    }
65    {
66        const half2x3 splat_4 = half2x3(4, 4, 4, 4, 4, 4);
67
68        ok = ok && (splat_4 + (half2x3(2)) == half2x3(6, 4, 4, 4, 6, 4));
69        ok = ok && (splat_4 - (half2x3(2)) == half2x3(2, 4, 4, 4, 2, 4));
70        ok = ok && (splat_4 / (half2x3(2, 2, 2, 2, 2, 2)) == half2x3(2, 2, 2, 2, 2, 2));
71    }
72    ok = ok && (half4x3(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) +
73                half4x3(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5) ==
74                half4x3(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17));
75
76    ok = ok && (half4x2(10, 20, 30, 40, 50, 60, 70, 80) -
77                half4x2(1, 2, 3, 4, 5, 6, 7, 8) ==
78                half4x2(9, 18, 27, 36, 45, 54, 63, 72));
79
80    ok = ok && (half2x4(10, 20, 30, 40, 10, 20, 30, 40) /
81                half2x4(10, 10, 10, 10, 5, 5, 5, 5) ==
82                half2x4(1, 2, 3, 4, 2, 4, 6, 8));
83
84    // Multiplication performs a proper matrix multiply.
85    ok = ok && (half3x2(1, 4, 2, 5, 3, 6) *
86                half2x3(7, 9, 11, 8, 10, 12) ==
87                half2x2(58, 139, 64, 154));
88    return ok;
89}
90
91bool test_vector_op_matrix_float() {
92    bool ok = true;
93
94    ok = ok && (float2(1, 2) * float4x2(3, 4, 5, 6, 7, 8, 9, 10) == float4(11, 17, 23, 29));
95    ok = ok && (float3(1, 2, 3) * float2x3(-1, 0, 0, 1, 2, 0) == float2(-1, 5));
96    ok = ok && (float4(1, 2, 3, 4) * float3x4(1,0,2, 3,-1,0, 0,1,2, 0,1,0) == float3(19, 3, 5));
97
98    return ok;
99}
100
101bool test_vector_op_matrix_half() {
102    bool ok = true;
103
104    ok = ok && (half2(1, 2) * half4x2(3, 4, 5, 6, 7, 8, 9, 10) == half4(11, 17, 23, 29));
105    ok = ok && (half3(1, 2, 3) * half2x3(-1, 0, 0, 1, 2, 0) == half2(-1, 5));
106    ok = ok && (half4(1, 2, 3, 4) * half3x4(1,0,2, 3,-1,0, 0,1,2, 0,1,0) == half3(19, 3, 5));
107
108    return ok;
109}
110
111bool test_matrix_op_vector_float() {
112    bool ok = true;
113
114    ok = ok && (float2x4(1, 0, 2, 3, -1, 0, 0, 1) * float2(1, 2) == float4(-1, 0, 2, 5));
115    ok = ok && (float3x2(-1, 0, 0, 1, 2, 0) * float3(1, 2, 3) == float2(5, 2));
116    ok = ok && (float4x3(3,4,5,6, 7,8,9,10, 11,12,13,14) * float4(1,2,3,4) == float3(90, 100, 110));
117
118    return ok;
119}
120
121bool test_matrix_op_vector_half() {
122    bool ok = true;
123
124    ok = ok && (half2x4(1, 0, 2, 3, -1, 0, 0, 1) * half2(1, 2) == half4(-1, 0, 2, 5));
125    ok = ok && (half3x2(-1, 0, 0, 1, 2, 0) * half3(1, 2, 3) == half2(5, 2));
126    ok = ok && (half4x3(3,4,5,6, 7,8,9,10, 11,12,13,14) * half4(1,2,3,4) == half3(90, 100, 110));
127
128    return ok;
129}
130
131half4 main(float2 coords) {
132    return (test_eq_float() &&
133            test_eq_half() &&
134            test_matrix_op_matrix_float() &&
135            test_matrix_op_matrix_half() &&
136            test_vector_op_matrix_float() &&
137            test_vector_op_matrix_half() &&
138            test_matrix_op_vector_float() &&
139            test_matrix_op_vector_half()) ? colorGreen : colorRed;
140}
141