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 GraphicsFuzz.
19
20# Short description: A fragment shader that has nested if condition in do while
21
22# The test passes because shader always writes red.
23
24# Optimized using spirv-opt with the following arguments:
25# '--eliminate-dead-code-aggressive'
26# '--copy-propagate-arrays'
27# '--copy-propagate-arrays'
28# '--vector-dce'
29# '--redundancy-elimination'
30# '--eliminate-local-single-block'
31# '--redundancy-elimination'
32# '--eliminate-dead-branches'
33# '--merge-return'
34# '--eliminate-dead-branches'
35# '--merge-return'
36# '--combine-access-chains'
37# '--redundancy-elimination'
38# '--private-to-local'
39# '--simplify-instructions'
40# '--ccp'
41# '--eliminate-local-multi-store'
42# '--inline-entry-points-exhaustive'
43# '--if-conversion'
44# '--eliminate-dead-inserts'
45# spirv-opt commit hash: ab7ac60f14ae66006bed5c989a2cfd4c4881704c
46
47
48
49SHADER vertex variant_vertex_shader PASSTHROUGH
50
51# variant_fragment_shader is derived from the following GLSL:
52# #version 310 es
53# precision highp float;
54# precision highp int;
55#
56# layout(location = 0) out vec4 _GLF_color;
57#
58# float one()
59# {
60#     return 1.0;
61# }
62#
63# void main()
64# {
65#     vec2 v = vec2(0);
66#     bool alwaysFalse = gl_FragCoord.x < -1.0;
67#
68#     do
69#     {
70#         if (v.x < 2.0) // Always true
71#         {
72#             if (!alwaysFalse)
73#                 v[int(one())] = 1.0;
74#         }
75#     } while (alwaysFalse);
76#
77#     if (v[0] == 0.0 && v[1] == 1.0)
78#         _GLF_color = vec4(1, 0, 0, 1);
79#     else
80#         _GLF_color = vec4(0);
81# }
82SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
83; SPIR-V
84; Version: 1.0
85; Generator: Khronos Glslang Reference Front End; 8
86; Bound: 72
87; Schema: 0
88               OpCapability Shader
89          %1 = OpExtInstImport "GLSL.std.450"
90               OpMemoryModel Logical GLSL450
91               OpEntryPoint Fragment %4 "main" %23 %64
92               OpExecutionMode %4 OriginUpperLeft
93               OpSource ESSL 310
94               OpName %4 "main"
95               OpName %8 "one("
96               OpName %15 "v"
97               OpName %20 "alwaysFalse"
98               OpName %23 "gl_FragCoord"
99               OpName %64 "_GLF_color"
100               OpDecorate %23 BuiltIn FragCoord
101               OpDecorate %64 Location 0
102          %2 = OpTypeVoid
103          %3 = OpTypeFunction %2
104          %6 = OpTypeFloat 32
105          %7 = OpTypeFunction %6
106         %10 = OpConstant %6 1
107         %13 = OpTypeVector %6 2
108         %14 = OpTypePointer Function %13
109         %16 = OpConstant %6 0
110         %17 = OpConstantComposite %13 %16 %16
111         %18 = OpTypeBool
112         %19 = OpTypePointer Function %18
113         %21 = OpTypeVector %6 4
114         %22 = OpTypePointer Input %21
115         %23 = OpVariable %22 Input
116         %24 = OpTypeInt 32 0
117         %25 = OpConstant %24 0
118         %26 = OpTypePointer Input %6
119         %29 = OpConstant %6 -1
120         %35 = OpTypePointer Function %6
121         %38 = OpConstant %6 2
122         %47 = OpTypeInt 32 1
123         %56 = OpConstant %24 1
124         %63 = OpTypePointer Output %21
125         %64 = OpVariable %63 Output
126         %65 = OpConstantComposite %21 %10 %16 %16 %10
127         %67 = OpConstantComposite %21 %16 %16 %16 %16
128          %4 = OpFunction %2 None %3
129          %5 = OpLabel
130         %71 = OpVariable %35 Function
131         %15 = OpVariable %14 Function
132         %20 = OpVariable %19 Function
133               OpStore %15 %17
134         %27 = OpAccessChain %26 %23 %25
135         %28 = OpLoad %6 %27
136         %30 = OpFOrdLessThan %18 %28 %29
137               OpStore %20 %30
138               OpBranch %31
139         %31 = OpLabel
140               OpLoopMerge %33 %34 None
141               OpBranch %32
142         %32 = OpLabel
143         %36 = OpAccessChain %35 %15 %25
144         %37 = OpLoad %6 %36
145         %39 = OpFOrdLessThan %18 %37 %38
146               OpSelectionMerge %41 None
147               OpBranchConditional %39 %40 %41
148         %40 = OpLabel
149         %43 = OpLogicalNot %18 %30
150               OpSelectionMerge %45 None
151               OpBranchConditional %43 %44 %45
152         %44 = OpLabel
153               OpStore %71 %10
154         %46 = OpLoad %6 %71
155         %48 = OpConvertFToS %47 %46
156         %49 = OpAccessChain %35 %15 %48
157               OpStore %49 %10
158               OpBranch %45
159         %45 = OpLabel
160               OpBranch %41
161         %41 = OpLabel
162               OpBranch %34
163         %34 = OpLabel
164               OpBranchConditional %30 %31 %33
165         %33 = OpLabel
166         %52 = OpLoad %6 %36
167         %53 = OpFOrdEqual %18 %52 %16
168               OpSelectionMerge %55 None
169               OpBranchConditional %53 %54 %55
170         %54 = OpLabel
171         %57 = OpAccessChain %35 %15 %56
172         %58 = OpLoad %6 %57
173         %59 = OpFOrdEqual %18 %58 %10
174               OpBranch %55
175         %55 = OpLabel
176         %60 = OpPhi %18 %53 %33 %59 %54
177               OpSelectionMerge %62 None
178               OpBranchConditional %60 %61 %66
179         %61 = OpLabel
180               OpStore %64 %65
181               OpBranch %62
182         %66 = OpLabel
183               OpStore %64 %67
184               OpBranch %62
185         %62 = OpLabel
186               OpReturn
187               OpFunctionEnd
188          %8 = OpFunction %6 None %7
189          %9 = OpLabel
190               OpReturnValue %10
191               OpFunctionEnd
192END
193
194
195BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM
196
197PIPELINE graphics variant_pipeline
198  ATTACH variant_vertex_shader
199  ATTACH variant_fragment_shader
200  FRAMEBUFFER_SIZE 256 256
201  BIND BUFFER variant_framebuffer AS color LOCATION 0
202END
203CLEAR_COLOR variant_pipeline 0 0 0 255
204
205CLEAR variant_pipeline
206RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 256 256
207
208EXPECT variant_framebuffer IDX 0 0 SIZE 256 256 EQ_RGBA 255 0 0 255
209