1#!amber
2
3# Copyright 2021 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 coverage-gap found by the GraphicsFuzz project.
19
20# Short description: A fragment shader that covers specific LLVM code paths
21
22# The test passes because the shader always writes red.
23
24SHADER vertex variant_vertex_shader PASSTHROUGH
25
26# variant_fragment_shader is derived from the following GLSL:
27# #version 320 es
28#
29# #define _GLF_MAKE_IN_BOUNDS_INT(IDX, SZ)  clamp(IDX, 0, SZ - 1)
30# #define _int_1 _GLF_uniform_int_values[0]
31# #define _int_0 _GLF_uniform_int_values[1]
32# #define _int_3 _GLF_uniform_int_values[2]
33# #define _int_8 _GLF_uniform_int_values[3]
34# #define _int_5 _GLF_uniform_int_values[4]
35# #define _float_0_0 _GLF_uniform_float_values[0]
36# #define _float_5_0 _GLF_uniform_float_values[1]
37# #define _float_6_0 _GLF_uniform_float_values[2]
38# #define _float_7_0 _GLF_uniform_float_values[3]
39# #define _float_8_0 _GLF_uniform_float_values[4]
40# #define _float_9_0 _GLF_uniform_float_values[5]
41# #define _float_1_0 _GLF_uniform_float_values[6]
42# #define _float_2_0 _GLF_uniform_float_values[7]
43# #define _float_3_0 _GLF_uniform_float_values[8]
44# #define _float_4_0 _GLF_uniform_float_values[9]
45# #define _float_10_0 _GLF_uniform_float_values[10]
46#
47# precision highp float;
48# precision highp int;
49#
50# // Contents of _GLF_uniform_float_values: [0.0, 5.0, 6.0, 7.0, 8.0, 9.0, 1.0, 2.0, 3.0, 4.0, 10.0]
51# layout(set = 0, binding = 0) uniform buf0
52# {
53#     float _GLF_uniform_float_values[11];
54# };
55# // Contents of _GLF_uniform_int_values: [1, 0, 3, 8, 5]
56# layout(set = 0, binding = 1) uniform buf1
57# {
58#     int _GLF_uniform_int_values[5];
59# };
60# layout(location = 0) out vec4 _GLF_color;
61#
62# void main()
63# {
64#     _GLF_color = vec4(_int_1, _int_0, _int_0, _int_1);
65#     mat2x4 m24 = mat2x4(_float_0_0);
66#     int a = _int_0;
67#
68#     for(int i = _int_1; i < _int_5; i++)
69#     {
70#         for(int j = 2; j < 5; j ++)
71#         {
72#             for(int k = _int_0; k < i; k++)
73#             {
74#                 for(int l = 0; l < j; l++)
75#                 {
76#                     switch(a)
77#                     {
78#                         // a is 2 when is is 1 and j is 4. At that point k loops from 0 to 1 and l from 0 to 4.
79#                         // This makes all the elements of m24 of value 1.
80#                         case 2u:
81#                             m24[_GLF_MAKE_IN_BOUNDS_INT(k, 2)][_GLF_MAKE_IN_BOUNDS_INT(l, 4)] = _float_1_0;
82#                             break;
83#                         // The cases below are never hit.
84#                         case 553u:
85#                             discard;
86#                         case 554u:
87#                             discard;
88#                         case 556u:
89#                             discard;
90#                         case 558u:
91#                             if(gl_FragCoord.y < _float_1_0)
92#                             {
93#                                 discard;
94#                             }
95#                     }
96#                 }
97#             }
98#             a++;
99#         }
100#     }
101#
102#     float arr[9] = float[9](_float_1_0, _float_2_0, _float_3_0, _float_4_0, _float_5_0, _float_6_0, _float_7_0, _float_8_0, _float_9_0);
103#     int b = _int_1;
104#
105#     for(int i = 0; i < 5; i++)
106#     {
107#         for(int j = 1; j < 5; j++)
108#         {
109#             if(gl_FragCoord.x < _float_10_0)
110#             {
111#                 continue;
112#             }
113#
114#             // Some pixels hit the continue above, but the switch-case below
115#             // only affects indices  5 and 7 which are not used for the final
116#             // color.
117#             int idx = min(b, _int_8);
118#             arr[idx] = _float_0_0;
119#
120#             for(int k = 0; k < i; k++)
121#             {
122#                 for(int l = _int_0; l < j; l++)
123#                 {
124#                     // Same as switch(idx) case 5 and 7.
125#                     switch(idx - _int_5)
126#                     {
127#                         case 0:
128#                             arr[idx] += _float_1_0;
129#                             break;
130#                         case 2:
131#                             arr[idx] += m24[_int_1][_int_1];
132#                             break;
133#                     }
134#                 }
135#             }
136#             b++;
137#         }
138#     }
139#
140#
141#     // No other array indices can be read without the coverage point disappearing.
142#     _GLF_color.x = arr[0];
143# }
144SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
145; SPIR-V
146; Version: 1.0
147; Generator: Khronos Glslang Reference Front End; 10
148; Bound: 267
149; Schema: 0
150               OpCapability Shader
151          %1 = OpExtInstImport "GLSL.std.450"
152               OpMemoryModel Logical GLSL450
153               OpEntryPoint Fragment %4 "main" %9 %119
154               OpExecutionMode %4 OriginUpperLeft
155               OpSource ESSL 320
156               OpName %4 "main"
157               OpName %9 "_GLF_color"
158               OpName %14 "buf1"
159               OpMemberName %14 0 "_GLF_uniform_int_values"
160               OpName %16 ""
161               OpName %35 "m24"
162               OpName %38 "buf0"
163               OpMemberName %38 0 "_GLF_uniform_float_values"
164               OpName %40 ""
165               OpName %50 "a"
166               OpName %53 "i"
167               OpName %67 "j"
168               OpName %77 "k"
169               OpName %88 "l"
170               OpName %119 "gl_FragCoord"
171               OpName %144 "arr"
172               OpName %167 "b"
173               OpName %170 "i"
174               OpName %178 "j"
175               OpName %196 "idx"
176               OpName %205 "k"
177               OpName %214 "l"
178               OpDecorate %9 Location 0
179               OpDecorate %13 ArrayStride 16
180               OpMemberDecorate %14 0 Offset 0
181               OpDecorate %14 Block
182               OpDecorate %16 DescriptorSet 0
183               OpDecorate %16 Binding 1
184               OpDecorate %37 ArrayStride 16
185               OpMemberDecorate %38 0 Offset 0
186               OpDecorate %38 Block
187               OpDecorate %40 DescriptorSet 0
188               OpDecorate %40 Binding 0
189               OpDecorate %119 BuiltIn FragCoord
190          %2 = OpTypeVoid
191          %3 = OpTypeFunction %2
192          %6 = OpTypeFloat 32
193          %7 = OpTypeVector %6 4
194          %8 = OpTypePointer Output %7
195          %9 = OpVariable %8 Output
196         %10 = OpTypeInt 32 1
197         %11 = OpTypeInt 32 0
198         %12 = OpConstant %11 5
199         %13 = OpTypeArray %10 %12
200         %14 = OpTypeStruct %13
201         %15 = OpTypePointer Uniform %14
202         %16 = OpVariable %15 Uniform
203         %17 = OpConstant %10 0
204         %18 = OpTypePointer Uniform %10
205         %22 = OpConstant %10 1
206         %33 = OpTypeMatrix %7 2
207         %34 = OpTypePointer Function %33
208         %36 = OpConstant %11 11
209         %37 = OpTypeArray %6 %36
210         %38 = OpTypeStruct %37
211         %39 = OpTypePointer Uniform %38
212         %40 = OpVariable %39 Uniform
213         %41 = OpTypePointer Uniform %6
214         %44 = OpConstant %6 1
215         %45 = OpConstant %6 0
216         %49 = OpTypePointer Function %10
217         %62 = OpConstant %10 4
218         %65 = OpTypeBool
219         %68 = OpConstant %10 2
220         %75 = OpConstant %10 5
221        %107 = OpConstant %10 3
222        %109 = OpConstant %10 6
223        %112 = OpTypePointer Function %6
224        %118 = OpTypePointer Input %7
225        %119 = OpVariable %118 Input
226        %120 = OpConstant %11 1
227        %121 = OpTypePointer Input %6
228        %141 = OpConstant %11 9
229        %142 = OpTypeArray %6 %141
230        %143 = OpTypePointer Function %142
231        %147 = OpConstant %10 7
232        %150 = OpConstant %10 8
233        %153 = OpConstant %10 9
234        %186 = OpConstant %11 0
235        %189 = OpConstant %10 10
236        %265 = OpTypePointer Output %6
237          %4 = OpFunction %2 None %3
238          %5 = OpLabel
239         %35 = OpVariable %34 Function
240         %50 = OpVariable %49 Function
241         %53 = OpVariable %49 Function
242         %67 = OpVariable %49 Function
243         %77 = OpVariable %49 Function
244         %88 = OpVariable %49 Function
245        %144 = OpVariable %143 Function
246        %167 = OpVariable %49 Function
247        %170 = OpVariable %49 Function
248        %178 = OpVariable %49 Function
249        %196 = OpVariable %49 Function
250        %205 = OpVariable %49 Function
251        %214 = OpVariable %49 Function
252         %19 = OpAccessChain %18 %16 %17 %17
253         %20 = OpLoad %10 %19
254         %21 = OpConvertSToF %6 %20
255         %23 = OpAccessChain %18 %16 %17 %22
256         %24 = OpLoad %10 %23
257         %25 = OpConvertSToF %6 %24
258         %26 = OpAccessChain %18 %16 %17 %22
259         %27 = OpLoad %10 %26
260         %28 = OpConvertSToF %6 %27
261         %29 = OpAccessChain %18 %16 %17 %17
262         %30 = OpLoad %10 %29
263         %31 = OpConvertSToF %6 %30
264         %32 = OpCompositeConstruct %7 %21 %25 %28 %31
265               OpStore %9 %32
266         %42 = OpAccessChain %41 %40 %17 %17
267         %43 = OpLoad %6 %42
268         %46 = OpCompositeConstruct %7 %43 %45 %45 %45
269         %47 = OpCompositeConstruct %7 %45 %43 %45 %45
270         %48 = OpCompositeConstruct %33 %46 %47
271               OpStore %35 %48
272         %51 = OpAccessChain %18 %16 %17 %22
273         %52 = OpLoad %10 %51
274               OpStore %50 %52
275         %54 = OpAccessChain %18 %16 %17 %17
276         %55 = OpLoad %10 %54
277               OpStore %53 %55
278               OpBranch %56
279         %56 = OpLabel
280               OpLoopMerge %58 %59 None
281               OpBranch %60
282         %60 = OpLabel
283         %61 = OpLoad %10 %53
284         %63 = OpAccessChain %18 %16 %17 %62
285         %64 = OpLoad %10 %63
286         %66 = OpSLessThan %65 %61 %64
287               OpBranchConditional %66 %57 %58
288         %57 = OpLabel
289               OpStore %67 %68
290               OpBranch %69
291         %69 = OpLabel
292               OpLoopMerge %71 %72 None
293               OpBranch %73
294         %73 = OpLabel
295         %74 = OpLoad %10 %67
296         %76 = OpSLessThan %65 %74 %75
297               OpBranchConditional %76 %70 %71
298         %70 = OpLabel
299         %78 = OpAccessChain %18 %16 %17 %22
300         %79 = OpLoad %10 %78
301               OpStore %77 %79
302               OpBranch %80
303         %80 = OpLabel
304               OpLoopMerge %82 %83 None
305               OpBranch %84
306         %84 = OpLabel
307         %85 = OpLoad %10 %77
308         %86 = OpLoad %10 %53
309         %87 = OpSLessThan %65 %85 %86
310               OpBranchConditional %87 %81 %82
311         %81 = OpLabel
312               OpStore %88 %17
313               OpBranch %89
314         %89 = OpLabel
315               OpLoopMerge %91 %92 None
316               OpBranch %93
317         %93 = OpLabel
318         %94 = OpLoad %10 %88
319         %95 = OpLoad %10 %67
320         %96 = OpSLessThan %65 %94 %95
321               OpBranchConditional %96 %90 %91
322         %90 = OpLabel
323         %97 = OpLoad %10 %50
324               OpSelectionMerge %103 None
325               OpSwitch %97 %103 2 %98 553 %99 554 %100 556 %101 558 %102
326         %98 = OpLabel
327        %104 = OpLoad %10 %77
328        %105 = OpExtInst %10 %1 SClamp %104 %17 %22
329        %106 = OpLoad %10 %88
330        %108 = OpExtInst %10 %1 SClamp %106 %17 %107
331        %110 = OpAccessChain %41 %40 %17 %109
332        %111 = OpLoad %6 %110
333        %113 = OpAccessChain %112 %35 %105 %108
334               OpStore %113 %111
335               OpBranch %103
336         %99 = OpLabel
337               OpKill
338        %100 = OpLabel
339               OpKill
340        %101 = OpLabel
341               OpKill
342        %102 = OpLabel
343        %122 = OpAccessChain %121 %119 %120
344        %123 = OpLoad %6 %122
345        %124 = OpAccessChain %41 %40 %17 %109
346        %125 = OpLoad %6 %124
347        %126 = OpFOrdLessThan %65 %123 %125
348               OpSelectionMerge %128 None
349               OpBranchConditional %126 %127 %128
350        %127 = OpLabel
351               OpKill
352        %128 = OpLabel
353               OpBranch %103
354        %103 = OpLabel
355               OpBranch %92
356         %92 = OpLabel
357        %131 = OpLoad %10 %88
358        %132 = OpIAdd %10 %131 %22
359               OpStore %88 %132
360               OpBranch %89
361         %91 = OpLabel
362               OpBranch %83
363         %83 = OpLabel
364        %133 = OpLoad %10 %77
365        %134 = OpIAdd %10 %133 %22
366               OpStore %77 %134
367               OpBranch %80
368         %82 = OpLabel
369        %135 = OpLoad %10 %50
370        %136 = OpIAdd %10 %135 %22
371               OpStore %50 %136
372               OpBranch %72
373         %72 = OpLabel
374        %137 = OpLoad %10 %67
375        %138 = OpIAdd %10 %137 %22
376               OpStore %67 %138
377               OpBranch %69
378         %71 = OpLabel
379               OpBranch %59
380         %59 = OpLabel
381        %139 = OpLoad %10 %53
382        %140 = OpIAdd %10 %139 %22
383               OpStore %53 %140
384               OpBranch %56
385         %58 = OpLabel
386        %145 = OpAccessChain %41 %40 %17 %109
387        %146 = OpLoad %6 %145
388        %148 = OpAccessChain %41 %40 %17 %147
389        %149 = OpLoad %6 %148
390        %151 = OpAccessChain %41 %40 %17 %150
391        %152 = OpLoad %6 %151
392        %154 = OpAccessChain %41 %40 %17 %153
393        %155 = OpLoad %6 %154
394        %156 = OpAccessChain %41 %40 %17 %22
395        %157 = OpLoad %6 %156
396        %158 = OpAccessChain %41 %40 %17 %68
397        %159 = OpLoad %6 %158
398        %160 = OpAccessChain %41 %40 %17 %107
399        %161 = OpLoad %6 %160
400        %162 = OpAccessChain %41 %40 %17 %62
401        %163 = OpLoad %6 %162
402        %164 = OpAccessChain %41 %40 %17 %75
403        %165 = OpLoad %6 %164
404        %166 = OpCompositeConstruct %142 %146 %149 %152 %155 %157 %159 %161 %163 %165
405               OpStore %144 %166
406        %168 = OpAccessChain %18 %16 %17 %17
407        %169 = OpLoad %10 %168
408               OpStore %167 %169
409               OpStore %170 %17
410               OpBranch %171
411        %171 = OpLabel
412               OpLoopMerge %173 %174 None
413               OpBranch %175
414        %175 = OpLabel
415        %176 = OpLoad %10 %170
416        %177 = OpSLessThan %65 %176 %75
417               OpBranchConditional %177 %172 %173
418        %172 = OpLabel
419               OpStore %178 %22
420               OpBranch %179
421        %179 = OpLabel
422               OpLoopMerge %181 %182 None
423               OpBranch %183
424        %183 = OpLabel
425        %184 = OpLoad %10 %178
426        %185 = OpSLessThan %65 %184 %75
427               OpBranchConditional %185 %180 %181
428        %180 = OpLabel
429        %187 = OpAccessChain %121 %119 %186
430        %188 = OpLoad %6 %187
431        %190 = OpAccessChain %41 %40 %17 %189
432        %191 = OpLoad %6 %190
433        %192 = OpFOrdLessThan %65 %188 %191
434               OpSelectionMerge %194 None
435               OpBranchConditional %192 %193 %194
436        %193 = OpLabel
437               OpBranch %182
438        %194 = OpLabel
439        %197 = OpLoad %10 %167
440        %198 = OpAccessChain %18 %16 %17 %107
441        %199 = OpLoad %10 %198
442        %200 = OpExtInst %10 %1 SMin %197 %199
443               OpStore %196 %200
444        %201 = OpLoad %10 %196
445        %202 = OpAccessChain %41 %40 %17 %17
446        %203 = OpLoad %6 %202
447        %204 = OpAccessChain %112 %144 %201
448               OpStore %204 %203
449               OpStore %205 %17
450               OpBranch %206
451        %206 = OpLabel
452               OpLoopMerge %208 %209 None
453               OpBranch %210
454        %210 = OpLabel
455        %211 = OpLoad %10 %205
456        %212 = OpLoad %10 %170
457        %213 = OpSLessThan %65 %211 %212
458               OpBranchConditional %213 %207 %208
459        %207 = OpLabel
460        %215 = OpAccessChain %18 %16 %17 %22
461        %216 = OpLoad %10 %215
462               OpStore %214 %216
463               OpBranch %217
464        %217 = OpLabel
465               OpLoopMerge %219 %220 None
466               OpBranch %221
467        %221 = OpLabel
468        %222 = OpLoad %10 %214
469        %223 = OpLoad %10 %178
470        %224 = OpSLessThan %65 %222 %223
471               OpBranchConditional %224 %218 %219
472        %218 = OpLabel
473        %225 = OpLoad %10 %196
474        %226 = OpAccessChain %18 %16 %17 %62
475        %227 = OpLoad %10 %226
476        %228 = OpISub %10 %225 %227
477               OpSelectionMerge %231 None
478               OpSwitch %228 %231 0 %229 2 %230
479        %229 = OpLabel
480        %232 = OpLoad %10 %196
481        %233 = OpAccessChain %41 %40 %17 %109
482        %234 = OpLoad %6 %233
483        %235 = OpAccessChain %112 %144 %232
484        %236 = OpLoad %6 %235
485        %237 = OpFAdd %6 %236 %234
486        %238 = OpAccessChain %112 %144 %232
487               OpStore %238 %237
488               OpBranch %231
489        %230 = OpLabel
490        %240 = OpLoad %10 %196
491        %241 = OpAccessChain %18 %16 %17 %17
492        %242 = OpLoad %10 %241
493        %243 = OpAccessChain %18 %16 %17 %17
494        %244 = OpLoad %10 %243
495        %245 = OpAccessChain %112 %35 %242 %244
496        %246 = OpLoad %6 %245
497        %247 = OpAccessChain %112 %144 %240
498        %248 = OpLoad %6 %247
499        %249 = OpFAdd %6 %248 %246
500        %250 = OpAccessChain %112 %144 %240
501               OpStore %250 %249
502               OpBranch %231
503        %231 = OpLabel
504               OpBranch %220
505        %220 = OpLabel
506        %253 = OpLoad %10 %214
507        %254 = OpIAdd %10 %253 %22
508               OpStore %214 %254
509               OpBranch %217
510        %219 = OpLabel
511               OpBranch %209
512        %209 = OpLabel
513        %255 = OpLoad %10 %205
514        %256 = OpIAdd %10 %255 %22
515               OpStore %205 %256
516               OpBranch %206
517        %208 = OpLabel
518        %257 = OpLoad %10 %167
519        %258 = OpIAdd %10 %257 %22
520               OpStore %167 %258
521               OpBranch %182
522        %182 = OpLabel
523        %259 = OpLoad %10 %178
524        %260 = OpIAdd %10 %259 %22
525               OpStore %178 %260
526               OpBranch %179
527        %181 = OpLabel
528               OpBranch %174
529        %174 = OpLabel
530        %261 = OpLoad %10 %170
531        %262 = OpIAdd %10 %261 %22
532               OpStore %170 %262
533               OpBranch %171
534        %173 = OpLabel
535        %263 = OpAccessChain %112 %144 %17
536        %264 = OpLoad %6 %263
537        %266 = OpAccessChain %265 %9 %186
538               OpStore %266 %264
539               OpReturn
540               OpFunctionEnd
541END
542
543# uniforms for variant
544
545# _GLF_uniform_int_values
546BUFFER variant__GLF_uniform_int_values DATA_TYPE int32[] STD140 DATA
547 1 0 3 8 5
548END
549# _GLF_uniform_float_values
550BUFFER variant__GLF_uniform_float_values DATA_TYPE float[] STD140 DATA
551 0.0 5.0 6.0 7.0 8.0 9.0 1.0 2.0 3.0 4.0 10.0
552END
553
554BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM
555
556PIPELINE graphics variant_pipeline
557  ATTACH variant_vertex_shader
558  ATTACH variant_fragment_shader
559  FRAMEBUFFER_SIZE 256 256
560  BIND BUFFER variant_framebuffer AS color LOCATION 0
561  BIND BUFFER variant__GLF_uniform_int_values AS uniform DESCRIPTOR_SET 0 BINDING 1
562  BIND BUFFER variant__GLF_uniform_float_values AS uniform DESCRIPTOR_SET 0 BINDING 0
563END
564CLEAR_COLOR variant_pipeline 0 0 0 255
565
566CLEAR variant_pipeline
567RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 256 256
568
569EXPECT variant_framebuffer IDX 0 0 SIZE 256 256 EQ_RGBA 255 0 0 255
570