1#!amber 2 3# Copyright 2019 Google LLC 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17 18# A test for a bug found by GraphicsFuzz. 19 20# Short description: A shader with a switch statement containing unreachable discards 21 22# The test passes because the shader always writes the color red. 23 24# Optimized using spirv-opt with the following arguments: 25# '-O' 26# spirv-opt commit hash: 6b072126595dd8c2448eb1fda616251c5e6d7079 27 28 29 30SHADER vertex variant_vertex_shader PASSTHROUGH 31 32# variant_fragment_shader is derived from the following GLSL: 33# #version 310 es 34# precision highp float; 35# 36# layout(location = 0) out vec4 _GLF_color; 37# 38# layout(set = 0, binding = 0) uniform buf0 { 39# vec2 injectionSwitch; 40# }; 41# vec3 drawShape(vec2 square) 42# { 43# switch(int(injectionSwitch.x)) 44# { 45# case 0: 46# return vec3(1.0, 0.0, 0.0); 47# case 67: 48# do 49# { 50# if(1.0 < square.x) 51# { 52# } 53# else 54# { 55# discard; 56# } 57# discard; 58# } 59# while(true); 60# } 61# return vec3(1.0); 62# } 63# void main() 64# { 65# vec2 center; 66# vec3 color = vec3(0.0); 67# for( 68# int i = 0; 69# i < 1; 70# i++ 71# ) 72# { 73# color = drawShape(center); 74# if(length(color) <= 0.0) 75# { 76# continue; 77# } 78# } 79# _GLF_color = vec4(color, 1.0); 80# } 81SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0 82; SPIR-V 83; Version: 1.0 84; Generator: Khronos Glslang Reference Front End; 7 85; Bound: 139 86; Schema: 0 87 OpCapability Shader 88 %1 = OpExtInstImport "GLSL.std.450" 89 OpMemoryModel Logical GLSL450 90 OpEntryPoint Fragment %4 "main" %78 91 OpExecutionMode %4 OriginUpperLeft 92 OpSource ESSL 310 93 OpName %4 "main" 94 OpName %14 "buf0" 95 OpMemberName %14 0 "injectionSwitch" 96 OpName %16 "" 97 OpName %78 "_GLF_color" 98 OpMemberDecorate %14 0 Offset 0 99 OpDecorate %14 Block 100 OpDecorate %16 DescriptorSet 0 101 OpDecorate %16 Binding 0 102 OpDecorate %75 RelaxedPrecision 103 OpDecorate %78 Location 0 104 OpDecorate %132 RelaxedPrecision 105 OpDecorate %132 RelaxedPrecision 106 OpDecorate %132 RelaxedPrecision 107 %2 = OpTypeVoid 108 %3 = OpTypeFunction %2 109 %6 = OpTypeFloat 32 110 %7 = OpTypeVector %6 2 111 %9 = OpTypeVector %6 3 112 %14 = OpTypeStruct %7 113 %15 = OpTypePointer Uniform %14 114 %16 = OpVariable %15 Uniform 115 %17 = OpTypeInt 32 1 116 %18 = OpConstant %17 0 117 %19 = OpTypeInt 32 0 118 %20 = OpConstant %19 0 119 %21 = OpTypePointer Uniform %6 120 %28 = OpConstant %6 1 121 %29 = OpConstant %6 0 122 %30 = OpConstantComposite %9 %28 %29 %29 123 %39 = OpTypeBool 124 %48 = OpConstantComposite %9 %28 %28 %28 125 %53 = OpConstantComposite %9 %29 %29 %29 126 %62 = OpConstant %17 1 127 %76 = OpTypeVector %6 4 128 %77 = OpTypePointer Output %76 129 %78 = OpVariable %77 Output 130 %135 = OpUndef %7 131 %4 = OpFunction %2 None %3 132 %5 = OpLabel 133 OpBranch %56 134 %56 = OpLabel 135 %133 = OpPhi %9 %53 %5 %137 %59 136 %132 = OpPhi %17 %18 %5 %75 %59 137 %63 = OpSLessThan %39 %132 %62 138 OpLoopMerge %58 %59 None 139 OpBranchConditional %63 %57 %58 140 %57 = OpLabel 141 OpBranch %104 142 %104 = OpLabel 143 OpLoopMerge %105 %106 None 144 OpBranch %107 145 %107 = OpLabel 146 %108 = OpAccessChain %21 %16 %18 %20 147 %109 = OpLoad %6 %108 148 %110 = OpConvertFToS %17 %109 149 OpSelectionMerge %111 None 150 OpSwitch %110 %111 0 %112 67 %113 151 %113 = OpLabel 152 OpBranch %114 153 %114 = OpLabel 154 OpLoopMerge %115 %116 None 155 OpBranch %117 156 %117 = OpLabel 157 %119 = OpCompositeExtract %6 %135 0 158 %120 = OpFOrdLessThan %39 %28 %119 159 OpSelectionMerge %122 None 160 OpBranchConditional %120 %122 %123 161 %123 = OpLabel 162 %124 = OpFunctionCall %2 %84 163 %125 = OpUndef %9 164 OpBranch %115 165 %122 = OpLabel 166 %126 = OpFunctionCall %2 %84 167 %127 = OpUndef %9 168 OpBranch %115 169 %116 = OpLabel 170 OpBranch %114 171 %115 = OpLabel 172 %138 = OpPhi %9 %125 %123 %127 %122 173 OpBranch %105 174 %112 = OpLabel 175 OpBranch %105 176 %111 = OpLabel 177 OpBranch %105 178 %106 = OpLabel 179 OpBranch %104 180 %105 = OpLabel 181 %137 = OpPhi %9 %138 %115 %30 %112 %48 %111 182 %69 = OpExtInst %6 %1 Length %137 183 %70 = OpFOrdLessThanEqual %39 %69 %29 184 OpSelectionMerge %72 None 185 OpBranchConditional %70 %71 %72 186 %71 = OpLabel 187 OpBranch %59 188 %72 = OpLabel 189 OpBranch %59 190 %59 = OpLabel 191 %75 = OpIAdd %17 %132 %62 192 OpBranch %56 193 %58 = OpLabel 194 %80 = OpCompositeExtract %6 %133 0 195 %81 = OpCompositeExtract %6 %133 1 196 %82 = OpCompositeExtract %6 %133 2 197 %83 = OpCompositeConstruct %76 %80 %81 %82 %28 198 OpStore %78 %83 199 OpReturn 200 OpFunctionEnd 201 %84 = OpFunction %2 None %3 202 %85 = OpLabel 203 OpKill 204 OpFunctionEnd 205END 206 207# uniforms for variant 208 209# injectionSwitch 210BUFFER variant_injectionSwitch DATA_TYPE vec2<float> DATA 211 0.0 1.0 212END 213 214BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM 215 216PIPELINE graphics variant_pipeline 217 ATTACH variant_vertex_shader 218 ATTACH variant_fragment_shader 219 FRAMEBUFFER_SIZE 256 256 220 BIND BUFFER variant_framebuffer AS color LOCATION 0 221 BIND BUFFER variant_injectionSwitch AS uniform DESCRIPTOR_SET 0 BINDING 0 222END 223CLEAR_COLOR variant_pipeline 0 0 0 255 224 225CLEAR variant_pipeline 226RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 256 256 227 228EXPECT variant_framebuffer IDX 0 0 SIZE 256 256 EQ_RGBA 255 0 0 255 229