skia2/tests/sksl/shared/ArrayCast.glsl
John Stiles 26487162fe Add support for array-cast syntax in SkSL.
Compiling a program with "allow narrowing conversions" actually fixes up
narrowing casts in the program by inserting casts wherever they would be
needed for type-correctness. For instance, compiling the statement
    `half h = myFloat;`
inserts an appropriate narrowing cast:
    `half h = half(myFloat);`.

The Pipeline stage code generator relies on this behavior, as when it
re-emits a runtime effect into a complete SkSL program, the narrowing-
conversions flag will no longer be set, but that is okay, because the
emitted code now contains typecasts anywhere they would be necessary.

Logically, this implies that anything which supports narrowing
conversions must be castable between high and low precision. In GLSL and
SPIR-V, such a cast is trivial, because the types are the same and the
precision qualifiers are treated as individual hints on each variable.
In Metal, we dodge the issue by only emitting full-precision types. But
we also need to emit raw SkSL from an SkSL program (that is what the
Pipeline stage generator does).

SkSL already supported every typical cast, but GLSL lacked any syntax
for casting an array to a different type. This meant SkSL had no array
casting syntax as well. SkSL now has array-cast syntax, but it is only
allowed for casting low/high-precision arrays to the same base type.
(You can't cast an int array to float, or a signed array to unsigned.)

Change-Id: Ia20933541c3bd4a946c1ea38209f93008acdb9cb
Bug: skia:12248
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/437687
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
2021-08-11 12:56:40 +00:00

22 lines
635 B
GLSL

#version 400
precision mediump float;
precision mediump sampler2D;
out mediump vec4 sk_FragColor;
uniform mediump vec4 colorGreen;
uniform mediump vec4 colorRed;
mediump vec4 main() {
highp float f[4] = float[4](1.0, 2.0, 3.0, 4.0);
mediump float h[4] = f;
f = h;
h = f;
highp ivec3 i3[3] = ivec3[3](ivec3(1), ivec3(2), ivec3(3));
mediump ivec3 s3[3] = i3;
i3 = s3;
s3 = i3;
mediump mat2 h2x2[2] = mat2[2](mat2(1.0, 2.0, 3.0, 4.0), mat2(5.0, 6.0, 7.0, 8.0));
highp mat2 f2x2[2] = h2x2;
f2x2 = h2x2;
h2x2 = f2x2;
return (f == h && i3 == s3) && f2x2 == h2x2 ? colorGreen : colorRed;
}