Fix SwitchWithFallthrough test on iOS.

It looks like returning from inside a switch on iOS gives wrong results
in GLSL.

Change-Id: I9d6d8971a7a54600268e27443815444fca6f3c61
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/450994
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
This commit is contained in:
John Stiles 2021-09-21 14:30:14 -04:00 committed by SkCQ
parent 828987893b
commit d668d4da68
4 changed files with 134 additions and 121 deletions

View File

@ -1,22 +1,26 @@
uniform half4 colorGreen, colorRed; uniform half4 colorGreen, colorRed;
bool switch_fallthrough(int value) { bool switch_fallthrough(int value) {
bool ok = false;
switch (value) { switch (value) {
case 2: return false; case 2: break;
case 1: case 1:
case 0: return true; case 0: ok = true; break;
default: return false; default: break;
} }
return ok;
} }
bool switch_fallthrough_twice(int value) { bool switch_fallthrough_twice(int value) {
bool ok = false;
switch (value) { switch (value) {
case 0: return false; case 0: break;
case 1: case 1:
case 2: case 2:
case 3: return true; case 3: ok = true; break;
default: return false; default: break;
} }
return ok;
} }
half4 main(float2 coords) { half4 main(float2 coords) {

View File

@ -9,10 +9,11 @@ OpName %_UniformBuffer "_UniformBuffer"
OpMemberName %_UniformBuffer 0 "colorGreen" OpMemberName %_UniformBuffer 0 "colorGreen"
OpMemberName %_UniformBuffer 1 "colorRed" OpMemberName %_UniformBuffer 1 "colorRed"
OpName %_entrypoint_v "_entrypoint_v" OpName %_entrypoint_v "_entrypoint_v"
OpName %switch_fallthrough_bi "switch_fallthrough_bi"
OpName %switch_fallthrough_twice_bi "switch_fallthrough_twice_bi" OpName %switch_fallthrough_twice_bi "switch_fallthrough_twice_bi"
OpName %ok "ok"
OpName %main "main" OpName %main "main"
OpName %x "x" OpName %x "x"
OpName %_0_ok "_0_ok"
OpDecorate %sk_FragColor RelaxedPrecision OpDecorate %sk_FragColor RelaxedPrecision
OpDecorate %sk_FragColor Location 0 OpDecorate %sk_FragColor Location 0
OpDecorate %sk_FragColor Index 0 OpDecorate %sk_FragColor Index 0
@ -22,13 +23,15 @@ OpMemberDecorate %_UniformBuffer 0 RelaxedPrecision
OpMemberDecorate %_UniformBuffer 1 Offset 16 OpMemberDecorate %_UniformBuffer 1 Offset 16
OpMemberDecorate %_UniformBuffer 1 RelaxedPrecision OpMemberDecorate %_UniformBuffer 1 RelaxedPrecision
OpDecorate %_UniformBuffer Block OpDecorate %_UniformBuffer Block
OpDecorate %12 Binding 0 OpDecorate %11 Binding 0
OpDecorate %12 DescriptorSet 0 OpDecorate %11 DescriptorSet 0
OpDecorate %54 RelaxedPrecision OpDecorate %40 RelaxedPrecision
OpDecorate %55 RelaxedPrecision OpDecorate %48 RelaxedPrecision
OpDecorate %72 RelaxedPrecision OpDecorate %49 RelaxedPrecision
OpDecorate %58 RelaxedPrecision
OpDecorate %71 RelaxedPrecision
OpDecorate %74 RelaxedPrecision
OpDecorate %75 RelaxedPrecision OpDecorate %75 RelaxedPrecision
OpDecorate %76 RelaxedPrecision
%float = OpTypeFloat 32 %float = OpTypeFloat 32
%v4float = OpTypeVector %float 4 %v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float %_ptr_Output_v4float = OpTypePointer Output %v4float
@ -38,104 +41,104 @@ OpDecorate %76 RelaxedPrecision
%sk_Clockwise = OpVariable %_ptr_Input_bool Input %sk_Clockwise = OpVariable %_ptr_Input_bool Input
%_UniformBuffer = OpTypeStruct %v4float %v4float %_UniformBuffer = OpTypeStruct %v4float %v4float
%_ptr_Uniform__UniformBuffer = OpTypePointer Uniform %_UniformBuffer %_ptr_Uniform__UniformBuffer = OpTypePointer Uniform %_UniformBuffer
%12 = OpVariable %_ptr_Uniform__UniformBuffer Uniform %11 = OpVariable %_ptr_Uniform__UniformBuffer Uniform
%void = OpTypeVoid %void = OpTypeVoid
%17 = OpTypeFunction %void %16 = OpTypeFunction %void
%v2float = OpTypeVector %float 2 %v2float = OpTypeVector %float 2
%float_0 = OpConstant %float 0 %float_0 = OpConstant %float 0
%21 = OpConstantComposite %v2float %float_0 %float_0 %20 = OpConstantComposite %v2float %float_0 %float_0
%_ptr_Function_v2float = OpTypePointer Function %v2float %_ptr_Function_v2float = OpTypePointer Function %v2float
%int = OpTypeInt 32 1 %int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int %_ptr_Function_int = OpTypePointer Function %int
%26 = OpTypeFunction %bool %_ptr_Function_int %25 = OpTypeFunction %bool %_ptr_Function_int
%_ptr_Function_bool = OpTypePointer Function %bool
%false = OpConstantFalse %bool %false = OpConstantFalse %bool
%true = OpConstantTrue %bool %true = OpConstantTrue %bool
%47 = OpTypeFunction %v4float %_ptr_Function_v2float %41 = OpTypeFunction %v4float %_ptr_Function_v2float
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
%int_0 = OpConstant %int 0 %int_0 = OpConstant %int 0
%_ptr_Function_v4float = OpTypePointer Function %v4float %_ptr_Function_v4float = OpTypePointer Function %v4float
%int_1 = OpConstant %int 1 %int_1 = OpConstant %int 1
%_entrypoint_v = OpFunction %void None %17 %_entrypoint_v = OpFunction %void None %16
%18 = OpLabel %17 = OpLabel
%22 = OpVariable %_ptr_Function_v2float Function %21 = OpVariable %_ptr_Function_v2float Function
OpStore %22 %21 OpStore %21 %20
%24 = OpFunctionCall %v4float %main %22 %23 = OpFunctionCall %v4float %main %21
OpStore %sk_FragColor %24 OpStore %sk_FragColor %23
OpReturn OpReturn
OpFunctionEnd OpFunctionEnd
%switch_fallthrough_bi = OpFunction %bool None %26 %switch_fallthrough_twice_bi = OpFunction %bool None %25
%28 = OpFunctionParameter %_ptr_Function_int %27 = OpFunctionParameter %_ptr_Function_int
%29 = OpLabel %28 = OpLabel
%30 = OpLoad %int %28 %ok = OpVariable %_ptr_Function_bool Function
OpSelectionMerge %31 None OpStore %ok %false
OpSwitch %30 %35 2 %32 1 %33 0 %34 %32 = OpLoad %int %27
%32 = OpLabel OpSelectionMerge %33 None
OpReturnValue %false OpSwitch %32 %38 0 %34 1 %35 2 %36 3 %37
%33 = OpLabel
OpBranch %34
%34 = OpLabel %34 = OpLabel
OpReturnValue %true OpBranch %33
%35 = OpLabel %35 = OpLabel
OpReturnValue %false OpBranch %36
%31 = OpLabel %36 = OpLabel
OpUnreachable OpBranch %37
%37 = OpLabel
OpStore %ok %true
OpBranch %33
%38 = OpLabel
OpBranch %33
%33 = OpLabel
%40 = OpLoad %bool %ok
OpReturnValue %40
OpFunctionEnd OpFunctionEnd
%switch_fallthrough_twice_bi = OpFunction %bool None %26 %main = OpFunction %v4float None %41
%38 = OpFunctionParameter %_ptr_Function_int %42 = OpFunctionParameter %_ptr_Function_v2float
%39 = OpLabel
%40 = OpLoad %int %38
OpSelectionMerge %41 None
OpSwitch %40 %46 0 %42 1 %43 2 %44 3 %45
%42 = OpLabel
OpReturnValue %false
%43 = OpLabel %43 = OpLabel
OpBranch %44
%44 = OpLabel
OpBranch %45
%45 = OpLabel
OpReturnValue %true
%46 = OpLabel
OpReturnValue %false
%41 = OpLabel
OpUnreachable
OpFunctionEnd
%main = OpFunction %v4float None %47
%48 = OpFunctionParameter %_ptr_Function_v2float
%49 = OpLabel
%x = OpVariable %_ptr_Function_int Function %x = OpVariable %_ptr_Function_int Function
%58 = OpVariable %_ptr_Function_int Function %_0_ok = OpVariable %_ptr_Function_bool Function
%63 = OpVariable %_ptr_Function_int Function %62 = OpVariable %_ptr_Function_int Function
%66 = OpVariable %_ptr_Function_v4float Function %65 = OpVariable %_ptr_Function_v4float Function
%51 = OpAccessChain %_ptr_Uniform_v4float %12 %int_0 %45 = OpAccessChain %_ptr_Uniform_v4float %11 %int_0
%54 = OpLoad %v4float %51 %48 = OpLoad %v4float %45
%55 = OpCompositeExtract %float %54 1 %49 = OpCompositeExtract %float %48 1
%56 = OpConvertFToS %int %55 %50 = OpConvertFToS %int %49
OpStore %x %56 OpStore %x %50
%57 = OpLoad %int %x OpStore %_0_ok %false
OpStore %58 %57 %52 = OpLoad %int %x
%59 = OpFunctionCall %bool %switch_fallthrough_bi %58 OpSelectionMerge %53 None
OpSelectionMerge %61 None OpSwitch %52 %57 2 %54 1 %55 0 %56
OpBranchConditional %59 %60 %61 %54 = OpLabel
OpBranch %53
%55 = OpLabel
OpBranch %56
%56 = OpLabel
OpStore %_0_ok %true
OpBranch %53
%57 = OpLabel
OpBranch %53
%53 = OpLabel
%58 = OpLoad %bool %_0_ok
OpSelectionMerge %60 None
OpBranchConditional %58 %59 %60
%59 = OpLabel
%61 = OpLoad %int %x
OpStore %62 %61
%63 = OpFunctionCall %bool %switch_fallthrough_twice_bi %62
OpBranch %60
%60 = OpLabel %60 = OpLabel
%62 = OpLoad %int %x %64 = OpPhi %bool %false %53 %63 %59
OpStore %63 %62 OpSelectionMerge %69 None
%64 = OpFunctionCall %bool %switch_fallthrough_twice_bi %63 OpBranchConditional %64 %67 %68
OpBranch %61 %67 = OpLabel
%61 = OpLabel %70 = OpAccessChain %_ptr_Uniform_v4float %11 %int_0
%65 = OpPhi %bool %false %49 %64 %60 %71 = OpLoad %v4float %70
OpSelectionMerge %70 None OpStore %65 %71
OpBranchConditional %65 %68 %69 OpBranch %69
%68 = OpLabel %68 = OpLabel
%71 = OpAccessChain %_ptr_Uniform_v4float %12 %int_0 %72 = OpAccessChain %_ptr_Uniform_v4float %11 %int_1
%72 = OpLoad %v4float %71 %74 = OpLoad %v4float %72
OpStore %66 %72 OpStore %65 %74
OpBranch %70 OpBranch %69
%69 = OpLabel %69 = OpLabel
%73 = OpAccessChain %_ptr_Uniform_v4float %12 %int_1 %75 = OpLoad %v4float %65
%75 = OpLoad %v4float %73 OpReturnValue %75
OpStore %66 %75
OpBranch %70
%70 = OpLabel
%76 = OpLoad %v4float %66
OpReturnValue %76
OpFunctionEnd OpFunctionEnd

View File

@ -2,30 +2,33 @@
out vec4 sk_FragColor; out vec4 sk_FragColor;
uniform vec4 colorGreen; uniform vec4 colorGreen;
uniform vec4 colorRed; uniform vec4 colorRed;
bool switch_fallthrough_bi(int value) {
switch (value) {
case 2:
return false;
case 1:
case 0:
return true;
default:
return false;
}
}
bool switch_fallthrough_twice_bi(int value) { bool switch_fallthrough_twice_bi(int value) {
bool ok = false;
switch (value) { switch (value) {
case 0: case 0:
return false; break;
case 1: case 1:
case 2: case 2:
case 3: case 3:
return true; ok = true;
break;
default: default:
return false; break;
} }
return ok;
} }
vec4 main() { vec4 main() {
int x = int(colorGreen.y); int x = int(colorGreen.y);
return switch_fallthrough_bi(x) && switch_fallthrough_twice_bi(x) ? colorGreen : colorRed; bool _0_ok = false;
switch (x) {
case 2:
break;
case 1:
case 0:
_0_ok = true;
break;
default:
break;
}
return _0_ok && switch_fallthrough_twice_bi(x) ? colorGreen : colorRed;
} }

View File

@ -10,33 +10,36 @@ struct Inputs {
struct Outputs { struct Outputs {
float4 sk_FragColor [[color(0)]]; float4 sk_FragColor [[color(0)]];
}; };
bool switch_fallthrough_bi(int value) {
switch (value) {
case 2:
return false;
case 1:
case 0:
return true;
default:
return false;
}
}
bool switch_fallthrough_twice_bi(int value) { bool switch_fallthrough_twice_bi(int value) {
bool ok = false;
switch (value) { switch (value) {
case 0: case 0:
return false; break;
case 1: case 1:
case 2: case 2:
case 3: case 3:
return true; ok = true;
break;
default: default:
return false; break;
} }
return ok;
} }
fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
Outputs _out; Outputs _out;
(void)_out; (void)_out;
int x = int(_uniforms.colorGreen.y); int x = int(_uniforms.colorGreen.y);
_out.sk_FragColor = switch_fallthrough_bi(x) && switch_fallthrough_twice_bi(x) ? _uniforms.colorGreen : _uniforms.colorRed; bool _0_ok = false;
switch (x) {
case 2:
break;
case 1:
case 0:
_0_ok = true;
break;
default:
break;
}
_out.sk_FragColor = _0_ok && switch_fallthrough_twice_bi(x) ? _uniforms.colorGreen : _uniforms.colorRed;
return _out; return _out;
} }