xref: /aosp_15_r20/external/skia/resources/sksl/shared/MatrixScalarMath.sksl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1uniform half4 colorGreen, colorRed;
2uniform float4 testInputs;  // equals (-1.25, 0, 0.75, 2.25)
3
4const int plus = 1;
5const int minus = 2;
6const int star = 3;
7const int slash = 4;
8
9bool test(int op, float m11, float m12, float m21, float m22, float2x2 expected) {
10
11    float one = colorRed.r;
12
13    float2x2 m2 = float2x2(m11 * one, m12 * one, m21 * one, m22 * one);
14    switch (op) {
15        case plus:  m2 = 1 + m2; break;
16        case minus: m2 -= 1;     break;
17        case star:  m2 *= 2;     break;
18        case slash: m2 = m2 / 2; break;
19    }
20
21    return m2[0][0] == expected[0][0] &&
22           m2[0][1] == expected[0][1] &&
23           m2[1][0] == expected[1][0] &&
24           m2[1][1] == expected[1][1];
25}
26
27bool divisionTest() {
28    float    ten = colorRed.r * 10.0;
29    float2x2 mat = float2x2(ten.xx, ten.xx);
30    float2x2 div = mat / testInputs.x;  // `matrix / scalar` should become `matrix * (1 / scalar)`
31    mat /= testInputs.x;                // `matrix /= scalar` should become `matrix *= (1 / scalar)`
32
33    return all(lessThan(abs(float4(div) + float4(8)), float4(0.01))) &&
34           all(lessThan(abs(float4(mat) + float4(8)), float4(0.01)));
35}
36
37half4 main(float2 coords) {
38    float f1 = 1 * colorGreen.g;
39    float f2 = 2 * colorGreen.g;
40    float f3 = 3 * colorGreen.g;
41    float f4 = 4 * colorGreen.g;
42
43    return true
44           && test(plus,  f1, f2, f3, f4, float2x2(f1+1, f2+1, f3+1, f4+1))
45           && test(minus, f1, f2, f3, f4, float2x2(f1-1, f2-1, f3-1, f4-1))
46           && test(star,  f1, f2, f3, f4, float2x2(f1*2, f2*2, f3*2, f4*2))
47           && test(slash, f1, f2, f3, f4, float2x2(f1/2, f2/2, f3/2, f4/2))
48           && divisionTest()
49           ? colorGreen : colorRed;
50}
51