skia2/resources/sksl/intrinsics/MixFloat.sksl
John Stiles d774558eb1 Implement compile-time optimization for mix().
mix() has many overloads:
$genType mix($genType x, $genType y, $genType a);
$genType mix($genType x, $genType y, float a);
$genHType mix($genHType x, $genHType y, $genHType a);
$genHType mix($genHType x, $genHType y, half a);
$genType mix($genType x, $genType y, $genBType a);
$genHType mix($genHType x, $genHType y, $genBType a);
$genIType mix($genIType x, $genIType y, $genBType a);
$genBType mix($genBType x, $genBType y, $genBType a);

The top half were simple to implement via `evaluate_3_way_intrinsic`.

The bottom half--`x, y, $genBType`--required adding basic support into
`evaluate_n_way_intrinsic_of_type` for mixed argument types, since `x`
and `y` could be of any numeric type, but `a` is always boolean.
Fortunately, this didn't require major changes.

Change-Id: I015471f053c90d5a5c3ac67cc230d0f90950ff60
Bug: skia:12034
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/414443
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
2021-06-01 19:36:05 +00:00

32 lines
2.3 KiB
Plaintext

uniform half4 colorGreen, colorRed, colorBlack, colorWhite, testInputs;
half4 main(float2 coords) {
const half4 constBlack = half4(0, 0, 0, 1);
const half4 constWhite = half4(1);
const half4 constVal = half4(-1.25, 0, 0.75, 2.25);
half4 expectedBW = half4(0.5, 0.5, 0.5, 1);
half4 expectedWT = half4(1, 0.5, 1, 2.25);
return (mix(colorGreen, colorRed, 0) == half4(0, 1, 0, 1) &&
mix(colorGreen, colorRed, 0.25) == half4(0.25, 0.75, 0, 1) &&
mix(colorGreen, colorRed, 0.75) == half4(0.75, 0.25, 0, 1) &&
mix(colorGreen, colorRed, 1) == half4(1, 0, 0, 1) &&
mix(colorBlack.x, colorWhite.x, 0.5) == expectedBW.x &&
mix(colorBlack.xy, colorWhite.xy, 0.5) == expectedBW.xy &&
mix(colorBlack.xyz, colorWhite.xyz, 0.5) == expectedBW.xyz &&
mix(colorBlack.xyzw, colorWhite.xyzw, 0.5) == expectedBW.xyzw &&
mix(constBlack.x, constWhite.x, 0.5) == expectedBW.x &&
mix(constBlack.xy, constWhite.xy, 0.5) == expectedBW.xy &&
mix(constBlack.xyz, constWhite.xyz, 0.5) == expectedBW.xyz &&
mix(constBlack.xyzw, constWhite.xyzw, 0.5) == expectedBW.xyzw &&
mix(colorWhite.x, testInputs.x, 0) == expectedWT.x &&
mix(colorWhite.xy, testInputs.xy, half2(0, 0.5)) == expectedWT.xy &&
mix(colorWhite.xyz, testInputs.xyz, half3(0, 0.5, 0)) == expectedWT.xyz &&
mix(colorWhite.xyzw, testInputs.xyzw, half4(0, 0.5, 0, 1)) == expectedWT.xyzw &&
mix(constWhite.x, constVal.x, 0) == expectedWT.x &&
mix(constWhite.xy, constVal.xy, half2(0, 0.5)) == expectedWT.xy &&
mix(constWhite.xyz, constVal.xyz, half3(0, 0.5, 0)) == expectedWT.xyz &&
mix(constWhite.xyzw, constVal.xyzw, half4(0, 0.5, 0, 1)) == expectedWT.xyzw)
? colorGreen : colorRed;
}