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 fragment shader with discard and add in function in loop
21
22# The test passes because main always outputs the color red and returns.
23# red() has an unreachable discard and adds zero to the returned value.
24
25# Optimized using spirv-opt with the following arguments:
26# '-O'
27# spirv-opt commit hash: 6b072126595dd8c2448eb1fda616251c5e6d7079
28
29
30
31SHADER vertex variant_vertex_shader PASSTHROUGH
32
33# variant_fragment_shader is derived from the following GLSL:
34# #version 310 es
35# precision highp float;
36#
37# layout(location = 0) out vec4 _GLF_color;
38#
39# layout(set = 0, binding = 0) uniform buf0 {
40#   vec2 injectionSwitch;
41# };
42#
43# vec4 red()
44# {
45#   if(injectionSwitch.x > 0.0) // always false
46#     discard;
47#
48#   return vec4(1.0, 0.0, 0.0, 1.0) + injectionSwitch.x; // add zero
49# }
50#
51# void main()
52# {
53#   while(true)
54#   {
55#     while(injectionSwitch.y > 0.0) // always true
56#     {
57#       _GLF_color = red();
58#       return;
59#     }
60#   }
61# }
62SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
63; SPIR-V
64; Version: 1.0
65; Generator: Khronos Glslang Reference Front End; 7
66; Bound: 102
67; Schema: 0
68               OpCapability Shader
69          %1 = OpExtInstImport "GLSL.std.450"
70               OpMemoryModel Logical GLSL450
71               OpEntryPoint Fragment %4 "main" %52
72               OpExecutionMode %4 OriginUpperLeft
73               OpSource ESSL 310
74               OpName %4 "main"
75               OpName %12 "buf0"
76               OpMemberName %12 0 "injectionSwitch"
77               OpName %14 ""
78               OpName %52 "_GLF_color"
79               OpMemberDecorate %12 0 Offset 0
80               OpDecorate %12 Block
81               OpDecorate %14 DescriptorSet 0
82               OpDecorate %14 Binding 0
83               OpDecorate %52 Location 0
84          %2 = OpTypeVoid
85          %3 = OpTypeFunction %2
86          %6 = OpTypeFloat 32
87          %7 = OpTypeVector %6 4
88         %11 = OpTypeVector %6 2
89         %12 = OpTypeStruct %11
90         %13 = OpTypePointer Uniform %12
91         %14 = OpVariable %13 Uniform
92         %15 = OpTypeInt 32 1
93         %16 = OpConstant %15 0
94         %17 = OpTypeInt 32 0
95         %18 = OpConstant %17 0
96         %19 = OpTypePointer Uniform %6
97         %22 = OpConstant %6 0
98         %23 = OpTypeBool
99         %28 = OpConstant %6 1
100         %29 = OpConstantComposite %7 %28 %22 %22 %28
101         %41 = OpConstantTrue %23
102         %47 = OpConstant %17 1
103         %51 = OpTypePointer Output %7
104         %52 = OpVariable %51 Output
105         %63 = OpConstantFalse %23
106          %4 = OpFunction %2 None %3
107          %5 = OpLabel
108               OpBranch %60
109         %60 = OpLabel
110               OpLoopMerge %59 %62 None
111               OpBranch %36
112         %36 = OpLabel
113        %100 = OpPhi %23 %63 %60 %98 %66
114               OpLoopMerge %38 %66 None
115               OpBranch %42
116         %42 = OpLabel
117         %48 = OpAccessChain %19 %14 %16 %47
118         %49 = OpLoad %6 %48
119         %50 = OpFOrdGreaterThan %23 %49 %22
120               OpLoopMerge %44 %45 None
121               OpBranchConditional %50 %43 %44
122         %43 = OpLabel
123               OpBranch %81
124         %81 = OpLabel
125               OpLoopMerge %82 %83 None
126               OpBranch %84
127         %84 = OpLabel
128         %85 = OpAccessChain %19 %14 %16 %18
129         %86 = OpLoad %6 %85
130         %87 = OpFOrdGreaterThan %23 %86 %22
131               OpSelectionMerge %88 None
132               OpBranchConditional %87 %89 %88
133         %89 = OpLabel
134         %90 = OpFunctionCall %2 %55
135         %91 = OpUndef %7
136               OpBranch %82
137         %88 = OpLabel
138         %94 = OpCompositeConstruct %7 %86 %86 %86 %86
139         %95 = OpFAdd %7 %29 %94
140               OpBranch %82
141         %83 = OpLabel
142               OpBranch %81
143         %82 = OpLabel
144         %97 = OpPhi %7 %91 %89 %95 %88
145               OpStore %52 %97
146               OpBranch %44
147         %45 = OpLabel
148               OpBranch %42
149         %44 = OpLabel
150         %98 = OpPhi %23 %100 %42 %41 %82
151               OpSelectionMerge %101 None
152               OpBranchConditional %98 %38 %66
153        %101 = OpLabel
154               OpBranch %66
155         %66 = OpLabel
156               OpBranch %36
157         %38 = OpLabel
158               OpSelectionMerge %68 None
159               OpBranchConditional %98 %59 %68
160         %68 = OpLabel
161               OpBranch %59
162         %62 = OpLabel
163               OpBranch %60
164         %59 = OpLabel
165               OpReturn
166               OpFunctionEnd
167         %55 = OpFunction %2 None %3
168         %56 = OpLabel
169               OpKill
170               OpFunctionEnd
171END
172
173# uniforms for variant
174
175# injectionSwitch
176BUFFER variant_injectionSwitch DATA_TYPE vec2<float> DATA
177 0.0 1.0
178END
179
180BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM
181
182PIPELINE graphics variant_pipeline
183  ATTACH variant_vertex_shader
184  ATTACH variant_fragment_shader
185  FRAMEBUFFER_SIZE 256 256
186  BIND BUFFER variant_framebuffer AS color LOCATION 0
187  BIND BUFFER variant_injectionSwitch AS uniform DESCRIPTOR_SET 0 BINDING 0
188END
189CLEAR_COLOR variant_pipeline 0 0 0 255
190
191CLEAR variant_pipeline
192RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 256 256
193
194EXPECT variant_framebuffer IDX 0 0 SIZE 256 256 EQ_RGBA 255 0 0 255
195