Add workaround for switch() containing only a default case.

The Mac Radeon GLSL driver crashes when given a switch statement that
only contains a default case and returns a value. Adding a case works
around the crash, and doesn't affect the meaning of the switch.

Change-Id: Iabbd267e0e31e8df7d3b7e747a7204d50931d0be
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/450977
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
This commit is contained in:
John Stiles 2021-09-21 09:24:09 -04:00 committed by SkCQ
parent 8d59fe04b8
commit b8f1651f9b
8 changed files with 119 additions and 0 deletions

View File

@ -398,6 +398,7 @@ sksl_shared_tests = [
"/sksl/shared/StructMaxDepth.sksl",
"/sksl/shared/Switch.sksl",
"/sksl/shared/SwitchContainingDeadCode.sksl",
"/sksl/shared/SwitchDefaultOnly.sksl",
"/sksl/shared/SwitchWithFallthrough.sksl",
"/sksl/shared/SwizzleBoolConstants.sksl",
"/sksl/shared/SwizzleByConstantIndex.sksl",

View File

@ -0,0 +1,7 @@
uniform half4 colorGreen, colorRed;
half4 main(float2 coords) {
switch (int(colorGreen.g)) {
default: return colorGreen;
}
}

View File

@ -1358,6 +1358,12 @@ void GLSLCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
this->writeExpression(*s.value(), Precedence::kTopLevel);
this->writeLine(") {");
fIndentation++;
// If a switch contains only a `default` case and nothing else, this confuses some drivers and
// can lead to a crash. Adding a real case before the default seems to work around the bug,
// and doesn't change the meaning of the switch.
if (s.cases().size() == 1 && !s.cases().front()->as<SwitchCase>().value()) {
this->writeLine("case 0:");
}
for (const std::unique_ptr<Statement>& stmt : s.cases()) {
const SwitchCase& c = stmt->as<SwitchCase>();
if (c.value()) {

View File

@ -324,6 +324,7 @@ SKSL_TEST_ES3(SkSLStaticSwitch, "shared/StaticSwitch.sksl")
SKSL_TEST(SkSLStructArrayFollowedByScalar, "shared/StructArrayFollowedByScalar.sksl")
SKSL_TEST(SkSLStructsInFunctions, "shared/StructsInFunctions.sksl")
SKSL_TEST_ES3(SkSLSwitch, "shared/Switch.sksl")
SKSL_TEST_ES3(SkSLSwitchDefaultOnly, "shared/SwitchDefaultOnly.sksl")
SKSL_TEST(SkSLSwizzleBoolConstants, "shared/SwizzleBoolConstants.sksl")
SKSL_TEST(SkSLSwizzleByConstantIndex, "shared/SwizzleByConstantIndex.sksl")
SKSL_TEST(SkSLSwizzleConstants, "shared/SwizzleConstants.sksl")

View File

@ -64,6 +64,7 @@ bool switch_with_all_returns_b() {
}
bool switch_only_default_b() {
switch (int(unknownInput)) {
case 0:
default:
return true;
}

View File

@ -0,0 +1,70 @@
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %_entrypoint_v "_entrypoint" %sk_FragColor %sk_Clockwise
OpExecutionMode %_entrypoint_v OriginUpperLeft
OpName %sk_FragColor "sk_FragColor"
OpName %sk_Clockwise "sk_Clockwise"
OpName %_UniformBuffer "_UniformBuffer"
OpMemberName %_UniformBuffer 0 "colorGreen"
OpMemberName %_UniformBuffer 1 "colorRed"
OpName %_entrypoint_v "_entrypoint_v"
OpName %main "main"
OpDecorate %sk_FragColor RelaxedPrecision
OpDecorate %sk_FragColor Location 0
OpDecorate %sk_FragColor Index 0
OpDecorate %sk_Clockwise BuiltIn FrontFacing
OpMemberDecorate %_UniformBuffer 0 Offset 0
OpMemberDecorate %_UniformBuffer 0 RelaxedPrecision
OpMemberDecorate %_UniformBuffer 1 Offset 16
OpMemberDecorate %_UniformBuffer 1 RelaxedPrecision
OpDecorate %_UniformBuffer Block
OpDecorate %10 Binding 0
OpDecorate %10 DescriptorSet 0
OpDecorate %30 RelaxedPrecision
OpDecorate %31 RelaxedPrecision
OpDecorate %36 RelaxedPrecision
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%sk_FragColor = OpVariable %_ptr_Output_v4float Output
%bool = OpTypeBool
%_ptr_Input_bool = OpTypePointer Input %bool
%sk_Clockwise = OpVariable %_ptr_Input_bool Input
%_UniformBuffer = OpTypeStruct %v4float %v4float
%_ptr_Uniform__UniformBuffer = OpTypePointer Uniform %_UniformBuffer
%10 = OpVariable %_ptr_Uniform__UniformBuffer Uniform
%void = OpTypeVoid
%15 = OpTypeFunction %void
%v2float = OpTypeVector %float 2
%float_0 = OpConstant %float 0
%19 = OpConstantComposite %v2float %float_0 %float_0
%_ptr_Function_v2float = OpTypePointer Function %v2float
%23 = OpTypeFunction %v4float %_ptr_Function_v2float
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_entrypoint_v = OpFunction %void None %15
%16 = OpLabel
%20 = OpVariable %_ptr_Function_v2float Function
OpStore %20 %19
%22 = OpFunctionCall %v4float %main %20
OpStore %sk_FragColor %22
OpReturn
OpFunctionEnd
%main = OpFunction %v4float None %23
%24 = OpFunctionParameter %_ptr_Function_v2float
%25 = OpLabel
%26 = OpAccessChain %_ptr_Uniform_v4float %10 %int_0
%30 = OpLoad %v4float %26
%31 = OpCompositeExtract %float %30 1
%32 = OpConvertFToS %int %31
OpSelectionMerge %33 None
OpSwitch %32 %34
%34 = OpLabel
%35 = OpAccessChain %_ptr_Uniform_v4float %10 %int_0
%36 = OpLoad %v4float %35
OpReturnValue %36
%33 = OpLabel
OpUnreachable
OpFunctionEnd

View File

@ -0,0 +1,11 @@
out vec4 sk_FragColor;
uniform vec4 colorGreen;
uniform vec4 colorRed;
vec4 main() {
switch (int(colorGreen.y)) {
case 0:
default:
return colorGreen;
}
}

View File

@ -0,0 +1,22 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct Uniforms {
float4 colorGreen;
float4 colorRed;
};
struct Inputs {
};
struct Outputs {
float4 sk_FragColor [[color(0)]];
};
fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
Outputs _out;
(void)_out;
switch (int(_uniforms.colorGreen.y)) {
default:
_out.sk_FragColor = _uniforms.colorGreen;
return _out;
}
return _out;
}