skia2/tests/sksl/shared/Enum.sksl
John Stiles 712fd6bbb9 Add support for enums in Metal code generator.
Enums are an SkSL-only concept--when we output code, we emit plain
IntLiterals--so the fix is simply to ignore the Enum program element
when we encounter it. This is what GLSLCodeGen does as well.

Also added a unit test to confirm that enums work normally, and that
enums are subject to optimization and static-comparison checks just as
ints would be.

Change-Id: Ic4f8da7a27983add9eb41b936d46f6638d22bd4b
Bug: skia:11003
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/338800
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
2020-11-30 15:14:34 +00:00

49 lines
1.8 KiB
Plaintext

// Declare a user enum.
enum class E {
kZero = 0,
kOne = 1,
kTwo = 2,
kThree = 3
};
void main() {
// Test that the user enum is subject to the same optimizations as a plain int.
E e = E::kZero;
if (e == E::kZero) { sk_FragColor = half4(1); }
@if (e == E::kZero) { sk_FragColor = half4(2); }
@if (e != E::kZero) { sk_FragColor = half4(3); }
if (e == E::kOne) { sk_FragColor = half4(4); }
@if (e == E::kOne) { sk_FragColor = half4(5); }
@if (e != E::kOne) { sk_FragColor = half4(6); }
sk_FragColor = (e == E::kZero) ? half4(7) : half4(-7);
sk_FragColor = (e != E::kZero) ? half4(8) : half4(-8);
sk_FragColor = (e == E::kOne) ? half4(9) : half4(-9);
sk_FragColor = (e != E::kOne) ? half4(10) : half4(-10);
switch (e) {
case E::kZero: sk_FragColor = half4(11); break;
case E::kOne: sk_FragColor = half4(12); break;
}
@switch (e) {
case E::kZero: sk_FragColor = half4(13); break;
case E::kOne: sk_FragColor = half4(14); break;
}
// Test that built-in enums work equally well.
SkBlendMode m = SkBlendMode::kClear;
if (m == SkBlendMode::kClear) { sk_FragColor = half4(15); }
@if (m == SkBlendMode::kClear) { sk_FragColor = half4(16); }
if (m == SkBlendMode::kSrc) { sk_FragColor = half4(17); }
@if (m != SkBlendMode::kDst) { sk_FragColor = half4(18); }
sk_FragColor = (m == SkBlendMode::kClear) ? half4(19) : half4(-19);
sk_FragColor = (m != SkBlendMode::kSrc) ? half4(20) : half4(-20);
@switch (m) {
case SkBlendMode::kClear: sk_FragColor = half4(21); break;
case SkBlendMode::kSrc: sk_FragColor = half4(22); break;
case SkBlendMode::kDst: sk_FragColor = half4(23); break;
}
}