skia2/resources/sksl/shared/TernaryExpression.sksl
John Stiles 28054added Optimize ternary tests that check a const variable.
This enables the ternary to be optimized away in code like:
   const bool SHINY = true;
   color = SHINY ? add_shine(x) : x; // to --> `color = add_shine(x);`

Without constant propagation.

Also, I added a unit test for ternary expression simplification; I
wasn't able to find an existing one.

When the optimization flag is disabled, this CL actually removes the
optimization of `true ? x : y` --> `x` entirely; previously, this
substitution would be made regardless of optimization settings.

Change-Id: I93a8b9d4027902d35f8a19cfd6417170b209d056
Bug: skia:11343
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/379297
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
2021-03-05 21:41:05 +00:00

27 lines
618 B
Plaintext

uniform half4 colorGreen, colorRed;
half4 main() {
const bool TRUE = true;
const bool FALSE = false;
bool ok = true;
// Literal test
ok = ok && (true ? true : false);
ok = ok && (false ? false : true);
// Constant boolean test
ok = ok && (TRUE ? true : false);
ok = ok && (FALSE ? false : true);
// Constant-foldable test
ok = ok && (1 == 1 ? true : false);
ok = ok && (0 == 1 ? false : true);
// Unknown-value test
ok = ok && (colorGreen.g == 1 ? true : false);
ok = ok && (colorGreen.r == 1 ? false : true);
return ok ? colorGreen : colorRed;
}