skia2/resources/sksl/folding/MatrixFoldingES3.sksl
John Stiles 2ac7682b53 Implement constant-folding for vector/matrix multiplication.
This closes one of the last gaps in SkSL's constant-folding abilities.

Change-Id: I65c0f2e5fe11a7d47ab2069b2992403fca78b8a7
Bug: skia:12819
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/524761
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Arman Uguray <armansito@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
2022-03-25 21:42:47 +00:00

141 lines
5.1 KiB
Plaintext

uniform half4 colorRed, colorGreen;
bool test_eq_float() {
bool ok = true;
ok = ok && (float3x2(2) == float3x2(float2(2.0, 0.0), float2(0.0, 2.0), float2(0.0)));
ok = ok && (float3x2(1, 2, 3, 4, 5, 6) == float3x2(float4x2(1, 2, 3, 4, 5, 6, 7, 8)));
return ok;
}
bool test_eq_half() {
bool ok = true;
ok = ok && (half3x2(2) == half3x2(half2(2.0, 0.0), half2(0.0, 2.0), half2(0.0)));
ok = ok && (half3x2(1, 2, 3, 4, 5, 6) == half3x2(half4x2(1, 2, 3, 4, 5, 6, 7, 8)));
return ok;
}
bool test_matrix_op_matrix_float() {
bool ok = true;
// Addition, subtraction and division operate componentwise.
{
const float3x2 splat_4 = float3x2(4, 4, 4, 4, 4, 4);
ok = ok && ((float3x2(2) + splat_4) == float3x2(6, 4, 4, 6, 4, 4));
ok = ok && ((float3x2(2) - splat_4) == float3x2(-2, -4, -4, -2, -4, -4));
ok = ok && ((float3x2(2) / splat_4) == float3x2(0.5));
}
{
const float2x3 splat_4 = float2x3(4, 4, 4, 4, 4, 4);
ok = ok && (splat_4 + (float2x3(2)) == float2x3(6, 4, 4, 4, 6, 4));
ok = ok && (splat_4 - (float2x3(2)) == float2x3(2, 4, 4, 4, 2, 4));
ok = ok && (splat_4 / (float2x3(2, 2, 2, 2, 2, 2)) == float2x3(2, 2, 2, 2, 2, 2));
}
ok = ok && (float4x3(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) +
float4x3(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5) ==
float4x3(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17));
ok = ok && (float4x2(10, 20, 30, 40, 50, 60, 70, 80) -
float4x2(1, 2, 3, 4, 5, 6, 7, 8) ==
float4x2(9, 18, 27, 36, 45, 54, 63, 72));
ok = ok && (float2x4(10, 20, 30, 40, 10, 20, 30, 40) /
float2x4(10, 10, 10, 10, 5, 5, 5, 5) ==
float2x4(1, 2, 3, 4, 2, 4, 6, 8));
// Multiplication performs a proper matrix multiply.
ok = ok && (float3x2(1, 4, 2, 5, 3, 6) *
float2x3(7, 9, 11, 8, 10, 12) ==
float2x2(58, 139, 64, 154));
return ok;
}
bool test_matrix_op_matrix_half() {
bool ok = true;
// Addition, subtraction and division operate componentwise.
{
const half3x2 splat_4 = half3x2(4, 4, 4, 4, 4, 4);
ok = ok && ((half3x2(2) + splat_4) == half3x2(6, 4, 4, 6, 4, 4));
ok = ok && ((half3x2(2) - splat_4) == half3x2(-2, -4, -4, -2, -4, -4));
ok = ok && ((half3x2(2) / splat_4) == half3x2(0.5));
}
{
const half2x3 splat_4 = half2x3(4, 4, 4, 4, 4, 4);
ok = ok && (splat_4 + (half2x3(2)) == half2x3(6, 4, 4, 4, 6, 4));
ok = ok && (splat_4 - (half2x3(2)) == half2x3(2, 4, 4, 4, 2, 4));
ok = ok && (splat_4 / (half2x3(2, 2, 2, 2, 2, 2)) == half2x3(2, 2, 2, 2, 2, 2));
}
ok = ok && (half4x3(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) +
half4x3(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5) ==
half4x3(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17));
ok = ok && (half4x2(10, 20, 30, 40, 50, 60, 70, 80) -
half4x2(1, 2, 3, 4, 5, 6, 7, 8) ==
half4x2(9, 18, 27, 36, 45, 54, 63, 72));
ok = ok && (half2x4(10, 20, 30, 40, 10, 20, 30, 40) /
half2x4(10, 10, 10, 10, 5, 5, 5, 5) ==
half2x4(1, 2, 3, 4, 2, 4, 6, 8));
// Multiplication performs a proper matrix multiply.
ok = ok && (half3x2(1, 4, 2, 5, 3, 6) *
half2x3(7, 9, 11, 8, 10, 12) ==
half2x2(58, 139, 64, 154));
return ok;
}
bool test_vector_op_matrix_float() {
bool ok = true;
ok = ok && (float2(1, 2) * float4x2(3, 4, 5, 6, 7, 8, 9, 10) == float4(11, 17, 23, 29));
ok = ok && (float3(1, 2, 3) * float2x3(-1, 0, 0, 1, 2, 0) == float2(-1, 5));
ok = ok && (float4(1, 2, 3, 4) * float3x4(1,0,2, 3,-1,0, 0,1,2, 0,1,0) == float3(19, 3, 5));
return ok;
}
bool test_vector_op_matrix_half() {
bool ok = true;
ok = ok && (half2(1, 2) * half4x2(3, 4, 5, 6, 7, 8, 9, 10) == half4(11, 17, 23, 29));
ok = ok && (half3(1, 2, 3) * half2x3(-1, 0, 0, 1, 2, 0) == half2(-1, 5));
ok = ok && (half4(1, 2, 3, 4) * half3x4(1,0,2, 3,-1,0, 0,1,2, 0,1,0) == half3(19, 3, 5));
return ok;
}
bool test_matrix_op_vector_float() {
bool ok = true;
ok = ok && (float2x4(1, 0, 2, 3, -1, 0, 0, 1) * float2(1, 2) == float4(-1, 0, 2, 5));
ok = ok && (float3x2(-1, 0, 0, 1, 2, 0) * float3(1, 2, 3) == float2(5, 2));
ok = ok && (float4x3(3,4,5,6, 7,8,9,10, 11,12,13,14) * float4(1,2,3,4) == float3(90, 100, 110));
return ok;
}
bool test_matrix_op_vector_half() {
bool ok = true;
ok = ok && (half2x4(1, 0, 2, 3, -1, 0, 0, 1) * half2(1, 2) == half4(-1, 0, 2, 5));
ok = ok && (half3x2(-1, 0, 0, 1, 2, 0) * half3(1, 2, 3) == half2(5, 2));
ok = ok && (half4x3(3,4,5,6, 7,8,9,10, 11,12,13,14) * half4(1,2,3,4) == half3(90, 100, 110));
return ok;
}
half4 main(float2 coords) {
return (test_eq_float() &&
test_eq_half() &&
test_matrix_op_matrix_float() &&
test_matrix_op_matrix_half() &&
test_vector_op_matrix_float() &&
test_vector_op_matrix_half() &&
test_matrix_op_vector_float() &&
test_matrix_op_vector_half()) ? colorGreen : colorRed;
}