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: Fragment shader that writes red before loop break
21
22# The test passes because the write of red is the only reachable write and is
23# guaranteed to be reached, and all loops are designed to terminate after a small
24# number of iterations.  For the loop with guard '1 < z' this is guaranteed due
25# to the runtime value of 'injected'.
26
27SHADER vertex variant_vertex_shader PASSTHROUGH
28
29# variant_fragment_shader is derived from the following GLSL:
30# #version 310 es
31# precision highp float;
32#
33# layout(location = 0) out vec4 _GLF_color;
34#
35# layout(set = 0, binding = 0) uniform buf0 {
36#  int injected;
37# };
38#
39# void main()
40# {
41#  int idx = 0;
42#  mat4x3 m43 = mat4x3(1.0);
43#  float GLF_live6sums[9];
44#  int ll_1 = 0;
45#  for(int GLF_live6rows = 2;;)
46#   {
47#      if(ll_1 >= injected) {
48#      _GLF_color = vec4(1.0, 0.0, 0.0, 1.0);
49#      break;
50#    }
51#    ll_1++;
52#
53#    int z = injected;
54#    int ll_2 = 0;
55#    for(
56#        int ctr = 0;
57#        ctr < 1;
58#        ctr ++
59#    )
60#     {
61#      if(ll_2 >= injected) {
62#        break;
63#      }
64#      ll_2++;
65#
66#      mat4x3 tempm43;
67#      tempm43 = m43;
68#      int ll_3 = 0;
69#      for(
70#          int c = 0;
71#          1 < z; // False, because 'injected' is 1
72#          c ++
73#      )
74#       {
75#        int d = 0;
76#        tempm43[c >= 0 && c < 4 ? c : 0][d >= 0 && d < 3 ? d : 0] = 1.0;
77#       }
78#      GLF_live6sums[idx >= 0 && idx < 9 ? idx : 0] += m43[ctr][1];
79#     }
80#    idx ++;
81#   }
82# }
83SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
84; SPIR-V
85; Version: 1.0
86; Generator: Khronos Glslang Reference Front End; 7
87; Bound: 126
88; Schema: 0
89               OpCapability Shader
90          %1 = OpExtInstImport "GLSL.std.450"
91               OpMemoryModel Logical GLSL450
92               OpEntryPoint Fragment %4 "main" %42
93               OpExecutionMode %4 OriginUpperLeft
94               OpSource ESSL 310
95               OpName %4 "main"
96               OpName %8 "idx"
97               OpName %14 "m43"
98               OpName %22 "ll_1"
99               OpName %23 "GLF_live6rows"
100               OpName %30 "buf0"
101               OpMemberName %30 0 "injected"
102               OpName %32 ""
103               OpName %42 "_GLF_color"
104               OpName %48 "z"
105               OpName %51 "ll_2"
106               OpName %52 "ctr"
107               OpName %69 "tempm43"
108               OpName %71 "ll_3"
109               OpName %72 "c"
110               OpName %80 "d"
111               OpName %105 "GLF_live6sums"
112               OpDecorate %8 RelaxedPrecision
113               OpDecorate %22 RelaxedPrecision
114               OpDecorate %23 RelaxedPrecision
115               OpDecorate %29 RelaxedPrecision
116               OpMemberDecorate %30 0 RelaxedPrecision
117               OpMemberDecorate %30 0 Offset 0
118               OpDecorate %30 Block
119               OpDecorate %32 DescriptorSet 0
120               OpDecorate %32 Binding 0
121               OpDecorate %35 RelaxedPrecision
122               OpDecorate %42 Location 0
123               OpDecorate %45 RelaxedPrecision
124               OpDecorate %47 RelaxedPrecision
125               OpDecorate %48 RelaxedPrecision
126               OpDecorate %50 RelaxedPrecision
127               OpDecorate %51 RelaxedPrecision
128               OpDecorate %52 RelaxedPrecision
129               OpDecorate %58 RelaxedPrecision
130               OpDecorate %60 RelaxedPrecision
131               OpDecorate %62 RelaxedPrecision
132               OpDecorate %67 RelaxedPrecision
133               OpDecorate %68 RelaxedPrecision
134               OpDecorate %71 RelaxedPrecision
135               OpDecorate %72 RelaxedPrecision
136               OpDecorate %78 RelaxedPrecision
137               OpDecorate %80 RelaxedPrecision
138               OpDecorate %81 RelaxedPrecision
139               OpDecorate %83 RelaxedPrecision
140               OpDecorate %87 RelaxedPrecision
141               OpDecorate %89 RelaxedPrecision
142               OpDecorate %91 RelaxedPrecision
143               OpDecorate %95 RelaxedPrecision
144               OpDecorate %99 RelaxedPrecision
145               OpDecorate %100 RelaxedPrecision
146               OpDecorate %106 RelaxedPrecision
147               OpDecorate %108 RelaxedPrecision
148               OpDecorate %112 RelaxedPrecision
149               OpDecorate %114 RelaxedPrecision
150               OpDecorate %122 RelaxedPrecision
151               OpDecorate %123 RelaxedPrecision
152               OpDecorate %124 RelaxedPrecision
153               OpDecorate %125 RelaxedPrecision
154          %2 = OpTypeVoid
155          %3 = OpTypeFunction %2
156          %6 = OpTypeInt 32 1
157          %7 = OpTypePointer Function %6
158          %9 = OpConstant %6 0
159         %10 = OpTypeFloat 32
160         %11 = OpTypeVector %10 3
161         %12 = OpTypeMatrix %11 4
162         %13 = OpTypePointer Function %12
163         %15 = OpConstant %10 1
164         %16 = OpConstant %10 0
165         %17 = OpConstantComposite %11 %15 %16 %16
166         %18 = OpConstantComposite %11 %16 %15 %16
167         %19 = OpConstantComposite %11 %16 %16 %15
168         %20 = OpConstantComposite %11 %16 %16 %16
169         %21 = OpConstantComposite %12 %17 %18 %19 %20
170         %24 = OpConstant %6 2
171         %30 = OpTypeStruct %6
172         %31 = OpTypePointer Uniform %30
173         %32 = OpVariable %31 Uniform
174         %33 = OpTypePointer Uniform %6
175         %36 = OpTypeBool
176         %40 = OpTypeVector %10 4
177         %41 = OpTypePointer Output %40
178         %42 = OpVariable %41 Output
179         %43 = OpConstantComposite %40 %15 %16 %16 %15
180         %46 = OpConstant %6 1
181         %84 = OpConstant %6 4
182         %92 = OpConstant %6 3
183         %97 = OpTypePointer Function %10
184        %101 = OpTypeInt 32 0
185        %102 = OpConstant %101 9
186        %103 = OpTypeArray %10 %102
187        %104 = OpTypePointer Function %103
188        %109 = OpConstant %6 9
189        %115 = OpConstant %101 1
190          %4 = OpFunction %2 None %3
191          %5 = OpLabel
192          %8 = OpVariable %7 Function
193         %14 = OpVariable %13 Function
194         %22 = OpVariable %7 Function
195         %23 = OpVariable %7 Function
196         %48 = OpVariable %7 Function
197         %51 = OpVariable %7 Function
198         %52 = OpVariable %7 Function
199         %69 = OpVariable %13 Function
200         %71 = OpVariable %7 Function
201         %72 = OpVariable %7 Function
202         %80 = OpVariable %7 Function
203        %105 = OpVariable %104 Function
204               OpStore %8 %9
205               OpStore %14 %21
206               OpStore %22 %9
207               OpStore %23 %24
208               OpBranch %25
209         %25 = OpLabel
210               OpLoopMerge %27 %28 None
211               OpBranch %26
212         %26 = OpLabel
213         %29 = OpLoad %6 %22
214         %34 = OpAccessChain %33 %32 %9
215         %35 = OpLoad %6 %34
216         %37 = OpSGreaterThanEqual %36 %29 %35
217               OpSelectionMerge %39 None
218               OpBranchConditional %37 %38 %39
219         %38 = OpLabel
220               OpStore %42 %43
221               OpBranch %27
222         %39 = OpLabel
223         %45 = OpLoad %6 %22
224         %47 = OpIAdd %6 %45 %46
225               OpStore %22 %47
226         %49 = OpAccessChain %33 %32 %9
227         %50 = OpLoad %6 %49
228               OpStore %48 %50
229               OpStore %51 %9
230               OpStore %52 %9
231               OpBranch %53
232         %53 = OpLabel
233               OpLoopMerge %55 %56 None
234               OpBranch %57
235         %57 = OpLabel
236         %58 = OpLoad %6 %52
237         %59 = OpSLessThan %36 %58 %46
238               OpBranchConditional %59 %54 %55
239         %54 = OpLabel
240         %60 = OpLoad %6 %51
241         %61 = OpAccessChain %33 %32 %9
242         %62 = OpLoad %6 %61
243         %63 = OpSGreaterThanEqual %36 %60 %62
244               OpSelectionMerge %65 None
245               OpBranchConditional %63 %64 %65
246         %64 = OpLabel
247               OpBranch %55
248         %65 = OpLabel
249         %67 = OpLoad %6 %51
250         %68 = OpIAdd %6 %67 %46
251               OpStore %51 %68
252         %70 = OpLoad %12 %14
253               OpStore %69 %70
254               OpStore %71 %9
255               OpStore %72 %9
256               OpBranch %73
257         %73 = OpLabel
258               OpLoopMerge %75 %76 None
259               OpBranch %77
260         %77 = OpLabel
261         %78 = OpLoad %6 %48
262         %79 = OpSLessThan %36 %46 %78
263               OpBranchConditional %79 %74 %75
264         %74 = OpLabel
265               OpStore %80 %9
266         %81 = OpLoad %6 %72
267         %82 = OpSGreaterThanEqual %36 %81 %9
268         %83 = OpLoad %6 %72
269         %85 = OpSLessThan %36 %83 %84
270         %86 = OpLogicalAnd %36 %82 %85
271         %87 = OpLoad %6 %72
272         %88 = OpSelect %6 %86 %87 %9
273         %89 = OpLoad %6 %80
274         %90 = OpSGreaterThanEqual %36 %89 %9
275         %91 = OpLoad %6 %80
276         %93 = OpSLessThan %36 %91 %92
277         %94 = OpLogicalAnd %36 %90 %93
278         %95 = OpLoad %6 %80
279         %96 = OpSelect %6 %94 %95 %9
280         %98 = OpAccessChain %97 %69 %88 %96
281               OpStore %98 %15
282               OpBranch %76
283         %76 = OpLabel
284         %99 = OpLoad %6 %72
285        %100 = OpIAdd %6 %99 %46
286               OpStore %72 %100
287               OpBranch %73
288         %75 = OpLabel
289        %106 = OpLoad %6 %8
290        %107 = OpSGreaterThanEqual %36 %106 %9
291        %108 = OpLoad %6 %8
292        %110 = OpSLessThan %36 %108 %109
293        %111 = OpLogicalAnd %36 %107 %110
294        %112 = OpLoad %6 %8
295        %113 = OpSelect %6 %111 %112 %9
296        %114 = OpLoad %6 %52
297        %116 = OpAccessChain %97 %14 %114 %115
298        %117 = OpLoad %10 %116
299        %118 = OpAccessChain %97 %105 %113
300        %119 = OpLoad %10 %118
301        %120 = OpFAdd %10 %119 %117
302        %121 = OpAccessChain %97 %105 %113
303               OpStore %121 %120
304               OpBranch %56
305         %56 = OpLabel
306        %122 = OpLoad %6 %52
307        %123 = OpIAdd %6 %122 %46
308               OpStore %52 %123
309               OpBranch %53
310         %55 = OpLabel
311        %124 = OpLoad %6 %8
312        %125 = OpIAdd %6 %124 %46
313               OpStore %8 %125
314               OpBranch %28
315         %28 = OpLabel
316               OpBranch %25
317         %27 = OpLabel
318               OpReturn
319               OpFunctionEnd
320END
321
322# uniforms for variant
323
324# injected
325BUFFER variant_injected DATA_TYPE int32 DATA
326 1
327END
328
329BUFFER framebuffer FORMAT B8G8R8A8_UNORM
330
331PIPELINE graphics gfz_pipeline
332  ATTACH variant_vertex_shader
333  ATTACH variant_fragment_shader
334  FRAMEBUFFER_SIZE 256 256
335  BIND BUFFER framebuffer AS color LOCATION 0
336  BIND BUFFER variant_injected AS uniform DESCRIPTOR_SET 0 BINDING 0
337END
338CLEAR_COLOR gfz_pipeline 0 0 0 255
339
340CLEAR gfz_pipeline
341RUN gfz_pipeline DRAW_RECT POS 0 0 SIZE 256 256
342EXPECT framebuffer IDX 0 0 SIZE 256 256 EQ_RGB 255 0 0