1#!amber
2
3# Copyright 2020 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 the GraphicsFuzz project.
19
20# Short description: A fragment shader with recurrent always false if statement
21
22# The test passes because the shader always writes color red.
23
24# Optimized using spirv-opt with the following arguments:
25# '--vector-dce'
26# '--vector-dce'
27# '--scalar-replacement=100'
28# '--redundancy-elimination'
29# '--redundancy-elimination'
30# '--eliminate-dead-branches'
31# '--eliminate-dead-branches'
32# '--merge-blocks'
33# '--scalar-replacement=100'
34# '--reduce-load-size'
35# '--copy-propagate-arrays'
36# '--eliminate-dead-branches'
37# '--merge-blocks'
38# '--eliminate-dead-code-aggressive'
39# '--inline-entry-points-exhaustive'
40# '--simplify-instructions'
41# '--redundancy-elimination'
42# '--ccp'
43# '--eliminate-local-single-block'
44# '--scalar-replacement=100'
45# '--reduce-load-size'
46# '--eliminate-local-single-block'
47# '--reduce-load-size'
48# '--eliminate-local-single-store'
49# '--inline-entry-points-exhaustive'
50# spirv-opt commit hash: 230f363e6da32791f640b02205e0645cc67c3f1d
51
52
53
54SHADER vertex variant_vertex_shader PASSTHROUGH
55
56# variant_fragment_shader is derived from the following GLSL:
57# #version 320 es
58# precision highp float;
59#
60# precision highp int;
61#
62#
63# layout(location = 0) out vec4 _GLF_color;
64#
65# layout(set = 0, binding = 0) uniform buf0
66# {
67#     vec2 injectionSwitch; // x == 0.0, y == 1.0
68# };
69# void main()
70# {
71#     const int loop_limit = 100;
72#     int loop_count = 0;
73#     // Always false.
74#     if (injectionSwitch.x > injectionSwitch.y)
75#     {
76#         return;
77#     }
78#     // Always false.
79#     gl_FragCoord.x < log(1.0);
80#     for (; loop_count < loop_limit; loop_count++)
81#     {
82#         // Always false.
83#         if (injectionSwitch.x > injectionSwitch.y)
84#         {
85#             break;
86#         }
87#         // Always false.
88#         if (injectionSwitch.x > injectionSwitch.y)
89#         {
90#             _GLF_color = vec4(1.0);
91#         }
92#         else
93#         {
94#             // Always false.
95#             if (gl_FragCoord.x < 0.0)
96#             {
97#                 return;
98#             }
99#         }
100#         // Always false.
101#         if (injectionSwitch.x > injectionSwitch.y)
102#         {
103#             _GLF_color = vec4(1.0);
104#         }
105#         else
106#         {
107#             _GLF_color = vec4(1.0, 0.0, 0.0, 1.0);
108#         }
109#         // Always false.
110#         if (injectionSwitch.x > injectionSwitch.y)
111#         {
112#             return;
113#         }
114#         // Always false.
115#         if (gl_FragCoord.x < 0.0)
116#         {
117#             for (;  loop_count < loop_limit; loop_count++)
118#             {
119#             }
120#         }
121#     }
122#     if(loop_count >= loop_limit)
123#     {
124#         _GLF_color = vec4(1.0, 0.0, 0.0, 1.0);
125#     }
126#     else
127#     {
128#         _GLF_color = vec4(1.0);
129#     }
130# }
131SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
132; SPIR-V
133; Version: 1.0
134; Generator: Khronos Glslang Reference Front End; 10
135; Bound: 109
136; Schema: 0
137               OpCapability Shader
138          %1 = OpExtInstImport "GLSL.std.450"
139               OpMemoryModel Logical GLSL450
140               OpEntryPoint Fragment %4 "main" %30 %60
141               OpExecutionMode %4 OriginUpperLeft
142               OpSource ESSL 320
143               OpName %4 "main"
144               OpName %8 "loop_count"
145               OpName %12 "buf0"
146               OpMemberName %12 0 "injectionSwitch"
147               OpName %14 ""
148               OpName %30 "gl_FragCoord"
149               OpName %60 "_GLF_color"
150               OpMemberDecorate %12 0 Offset 0
151               OpDecorate %12 Block
152               OpDecorate %14 DescriptorSet 0
153               OpDecorate %14 Binding 0
154               OpDecorate %30 BuiltIn FragCoord
155               OpDecorate %60 Location 0
156          %2 = OpTypeVoid
157          %3 = OpTypeFunction %2
158          %6 = OpTypeInt 32 1
159          %7 = OpTypePointer Function %6
160          %9 = OpConstant %6 0
161         %10 = OpTypeFloat 32
162         %11 = OpTypeVector %10 2
163         %12 = OpTypeStruct %11
164         %13 = OpTypePointer Uniform %12
165         %14 = OpVariable %13 Uniform
166         %15 = OpTypeInt 32 0
167         %16 = OpConstant %15 0
168         %17 = OpTypePointer Uniform %10
169         %20 = OpConstant %15 1
170         %23 = OpTypeBool
171         %28 = OpTypeVector %10 4
172         %29 = OpTypePointer Input %28
173         %30 = OpVariable %29 Input
174         %31 = OpTypePointer Input %10
175         %34 = OpConstant %10 0
176         %42 = OpConstant %6 100
177         %59 = OpTypePointer Output %28
178         %60 = OpVariable %59 Output
179         %61 = OpConstant %10 1
180         %62 = OpConstantComposite %28 %61 %61 %61 %61
181         %78 = OpConstantComposite %28 %61 %34 %34 %61
182        %100 = OpConstant %6 1
183          %4 = OpFunction %2 None %3
184          %5 = OpLabel
185          %8 = OpVariable %7 Function
186               OpStore %8 %9
187         %18 = OpAccessChain %17 %14 %9 %16
188         %19 = OpLoad %10 %18
189         %21 = OpAccessChain %17 %14 %9 %20
190         %22 = OpLoad %10 %21
191         %24 = OpFOrdGreaterThan %23 %19 %22
192               OpSelectionMerge %26 None
193               OpBranchConditional %24 %25 %26
194         %25 = OpLabel
195               OpReturn
196         %26 = OpLabel
197         %32 = OpAccessChain %31 %30 %16
198         %33 = OpLoad %10 %32
199         %35 = OpFOrdLessThan %23 %33 %34
200               OpBranch %36
201         %36 = OpLabel
202         %41 = OpLoad %6 %8
203         %43 = OpSLessThan %23 %41 %42
204               OpLoopMerge %38 %39 None
205               OpBranchConditional %43 %37 %38
206         %37 = OpLabel
207               OpSelectionMerge %50 None
208               OpBranchConditional %24 %49 %50
209         %49 = OpLabel
210               OpBranch %38
211         %50 = OpLabel
212               OpSelectionMerge %58 None
213               OpBranchConditional %24 %57 %63
214         %57 = OpLabel
215               OpStore %60 %62
216               OpBranch %58
217         %63 = OpLabel
218               OpSelectionMerge %68 None
219               OpBranchConditional %35 %67 %68
220         %67 = OpLabel
221               OpReturn
222         %68 = OpLabel
223               OpBranch %58
224         %58 = OpLabel
225               OpSelectionMerge %76 None
226               OpBranchConditional %24 %75 %77
227         %75 = OpLabel
228               OpStore %60 %62
229               OpBranch %76
230         %77 = OpLabel
231               OpStore %60 %78
232               OpBranch %76
233         %76 = OpLabel
234               OpSelectionMerge %85 None
235               OpBranchConditional %24 %84 %85
236         %84 = OpLabel
237               OpReturn
238         %85 = OpLabel
239               OpSelectionMerge %91 None
240               OpBranchConditional %35 %90 %91
241         %90 = OpLabel
242               OpBranch %92
243         %92 = OpLabel
244         %97 = OpLoad %6 %8
245         %98 = OpSLessThan %23 %97 %42
246               OpLoopMerge %94 %93 None
247               OpBranchConditional %98 %93 %94
248         %93 = OpLabel
249         %99 = OpLoad %6 %8
250        %101 = OpIAdd %6 %99 %100
251               OpStore %8 %101
252               OpBranch %92
253         %94 = OpLabel
254               OpBranch %91
255         %91 = OpLabel
256               OpBranch %39
257         %39 = OpLabel
258        %102 = OpLoad %6 %8
259        %103 = OpIAdd %6 %102 %100
260               OpStore %8 %103
261               OpBranch %36
262         %38 = OpLabel
263        %104 = OpLoad %6 %8
264        %105 = OpSGreaterThanEqual %23 %104 %42
265               OpSelectionMerge %107 None
266               OpBranchConditional %105 %106 %108
267        %106 = OpLabel
268               OpStore %60 %78
269               OpBranch %107
270        %108 = OpLabel
271               OpStore %60 %62
272               OpBranch %107
273        %107 = OpLabel
274               OpReturn
275               OpFunctionEnd
276END
277
278# uniforms for variant
279
280# injectionSwitch
281BUFFER variant_injectionSwitch DATA_TYPE vec2<float> STD140 DATA
282 0.0 1.0
283END
284
285BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM
286
287PIPELINE graphics variant_pipeline
288  ATTACH variant_vertex_shader
289  ATTACH variant_fragment_shader
290  FRAMEBUFFER_SIZE 16 16
291  BIND BUFFER variant_framebuffer AS color LOCATION 0
292  BIND BUFFER variant_injectionSwitch AS uniform DESCRIPTOR_SET 0 BINDING 0
293END
294CLEAR_COLOR variant_pipeline 0 0 0 255
295
296CLEAR variant_pipeline
297RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 16 16
298
299EXPECT variant_framebuffer IDX 0 0 SIZE 16 16 EQ_RGBA 255 0 0 255
300