skia2/resources/sksl/shared/VectorScalarMath.sksl
John Stiles 2febb5b423 Add SkSL test for vector-scalar math.
We had constant-folding tests for vector-scalar arithmetic, but didn't
have an equivalent SkSL unit test for vector-scalar arithmetic that
actually needs to be computed at runtime.

This exposes two SPIR-V bugs: one was previously known, but the other
is a new discovery.

Change-Id: I28737128f20b445797c6c29872335d05f94cc95c
Bug: skia:11267, skia:11788
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/388739
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
2021-03-25 23:30:34 +00:00

115 lines
2.7 KiB
Plaintext

uniform half4 colorRed, colorGreen;
uniform half unknownInput;
bool test_half() {
bool ok = true;
half4 inputRed = colorRed;
half4 inputGreen = colorGreen;
// Vector op scalar
half4 x = inputRed + 2;
ok = ok && (x == half4(3, 2, 2, 3));
x = inputGreen.garb - 2;
ok = ok && (x == half4(-1, -1, -2, -2));
x = inputRed + inputGreen.g;
ok = ok && (x == half4(2, 1, 1, 2));
x.xyz = inputGreen.aga * 9;
ok = ok && (x == half4(9, 9, 9, 2));
x.xy = x.zw / 0.5;
ok = ok && (x == half4(18, 4, 9, 2));
// (Vector op scalar).swizzle
x = (inputRed * 5).yxwz;
ok = ok && (x == half4(0, 5, 5, 0));
// Scalar op vector
x = 2 + inputRed;
ok = ok && (x == half4(3, 2, 2, 3));
x = 10 - inputGreen.garb;
ok = ok && (x == half4(9, 9, 10, 10));
x = inputRed.r + inputGreen;
ok = ok && (x == half4(1, 2, 1, 2));
x.xyz = 9 * inputGreen.aga;
ok = ok && (x == half4(9, 9, 9, 2));
x.xy = 36 / x.zw;
ok = ok && (x == half4(4, 18, 9, 2));
// (Scalar op vector).swizzle
x = (36 / x).yxwz;
ok = ok && (x == half4(2, 9, 18, 4));
// X op= scalar.
x += 2;
x *= 2;
x -= 4;
x /= 2;
ok = ok && (x == half4(2, 9, 18, 4));
// X = X op scalar.
x = x + 2;
x = x * 2;
x = x - 4;
x = x / 2;
ok = ok && (x == half4(2, 9, 18, 4));
return ok;
}
bool test_int() {
bool ok = true;
int4 inputRed = int4(colorRed);
int4 inputGreen = int4(colorGreen);
// Vector op scalar
int4 x = inputRed + 2;
ok = ok && (x == int4(3, 2, 2, 3));
x = inputGreen.garb - 2;
ok = ok && (x == int4(-1, -1, -2, -2));
x = inputRed + inputGreen.g;
ok = ok && (x == int4(2, 1, 1, 2));
x.xyz = inputGreen.aga * 9;
ok = ok && (x == int4(9, 9, 9, 2));
x.xy = x.zw / 3;
ok = ok && (x == int4(3, 0, 9, 2));
// (Vector op scalar).swizzle
x = (inputRed * 5).yxwz;
ok = ok && (x == int4(0, 5, 5, 0));
// Scalar op vector
x = 2 + inputRed;
ok = ok && (x == int4(3, 2, 2, 3));
x = 10 - inputGreen.garb;
ok = ok && (x == int4(9, 9, 10, 10));
x = inputRed.r + inputGreen;
ok = ok && (x == int4(1, 2, 1, 2));
x.xyz = 9 * inputGreen.aga;
ok = ok && (x == int4(9, 9, 9, 2));
x.xy = 36 / x.zw;
ok = ok && (x == int4(4, 18, 9, 2));
// (Scalar op vector).swizzle
x = (36 / x).yxwz;
ok = ok && (x == int4(2, 9, 18, 4));
// X op= scalar.
x += 2;
x *= 2;
x -= 4;
x /= 2;
ok = ok && (x == int4(2, 9, 18, 4));
// X = X op scalar.
x = x + 2;
x = x * 2;
x = x - 4;
x = x / 2;
ok = ok && (x == int4(2, 9, 18, 4));
return ok;
}
half4 main() {
return test_half() && test_int() ? colorGreen : colorRed;
}