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