skia2/tests/sksl/folding/ArrayFolding.glsl
John Stiles 76c1ff1566 Optimize indexing into an array with a constant-expression.
Previously, SkSL was unable to resolve the constant expression `x[y]`
for a constant-array `x` and a constant-integer-scalar `y`. Now, if `x`
and `y` are known, we can replace `x[y]` with the indexed array element.

Note that we need to be careful here, as it's not a valid optimization
to eliminate array elements that have side effects. We preserve side-
effecting expressions using the comma operator.

Change-Id: I5721337eb42b48c0b05f919c1cadfae19dd3b84f
Bug: skia:12472
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/469839
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2021-11-10 19:43:58 +00:00

19 lines
584 B
GLSL

out vec4 sk_FragColor;
uniform vec4 colorRed;
uniform vec4 colorGreen;
int globalValue = 0;
int side_effecting_ii(int value) {
globalValue++;
return value;
}
vec4 main() {
int _7_flatten0 = 1;
int _8_flatten1 = 2;
int _9_flatten2 = 3;
int _10_noFlatten0 = int[3](1, side_effecting_ii(2), 3)[0];
int _11_noFlatten1 = int[3](side_effecting_ii(1), 2, 3)[1];
int _12_noFlatten2 = int[3](1, 2, side_effecting_ii(3))[2];
return (_7_flatten0 == _10_noFlatten0 && _8_flatten1 == _11_noFlatten1) && _9_flatten2 == _12_noFlatten2 ? colorGreen : colorRed;
}