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: A fragment shader with a call, if, while, switch
21
22# The test passes because the fragment shader does some control flow and then writes the color red.
23
24# Optimized using spirv-opt with the following arguments:
25# '-O'
26# spirv-opt commit hash: 06407250a169c6a03b3765e86619075af1a8c187
27
28
29
30SHADER vertex variant_vertex_shader PASSTHROUGH
31
32# variant_fragment_shader is derived from the following GLSL:
33# #version 310 es
34# precision highp float;
35#
36# layout(location = 0) out vec4 _GLF_color;
37#
38# layout(set = 0, binding = 0) uniform buf0
39# {
40#   vec2 injectionSwitch;
41# };
42# int data[10];
43#
44# void merge(int from)
45# {
46#   int i = 1;
47#   if (1 < data[1])
48#   {
49#     i++;
50#   }
51#   while (i < 3)
52#   {
53#     i++;
54#     switch (int(injectionSwitch.x))
55#     {
56#     case 19:
57#       from++;
58#     case 38:
59#     case 23:
60#       break;
61#     case 78:
62#       _GLF_color = vec4(1.0);
63#     default:
64#       1;
65#     }
66#   }
67#   data[from] = 1;
68# }
69# void main()
70# {
71#   merge(1);
72#   _GLF_color = vec4(1.0, 0.0, 0.0, 1.0);
73# }
74SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
75; SPIR-V
76; Version: 1.0
77; Generator: Khronos Glslang Reference Front End; 7
78; Bound: 112
79; Schema: 0
80               OpCapability Shader
81          %1 = OpExtInstImport "GLSL.std.450"
82               OpMemoryModel Logical GLSL450
83               OpEntryPoint Fragment %4 "main" %59
84               OpExecutionMode %4 OriginUpperLeft
85               OpSource ESSL 310
86               OpName %4 "main"
87               OpName %18 "data"
88               OpName %40 "buf0"
89               OpMemberName %40 0 "injectionSwitch"
90               OpName %42 ""
91               OpName %59 "_GLF_color"
92               OpDecorate %18 RelaxedPrecision
93               OpMemberDecorate %40 0 Offset 0
94               OpDecorate %40 Block
95               OpDecorate %42 DescriptorSet 0
96               OpDecorate %42 Binding 0
97               OpDecorate %59 Location 0
98               OpDecorate %71 RelaxedPrecision
99               OpDecorate %85 RelaxedPrecision
100               OpDecorate %95 RelaxedPrecision
101               OpDecorate %110 RelaxedPrecision
102               OpDecorate %99 RelaxedPrecision
103          %2 = OpTypeVoid
104          %3 = OpTypeFunction %2
105          %6 = OpTypeInt 32 1
106          %7 = OpTypePointer Function %6
107         %13 = OpConstant %6 1
108         %14 = OpTypeInt 32 0
109         %15 = OpConstant %14 10
110         %16 = OpTypeArray %6 %15
111         %22 = OpTypeBool
112         %34 = OpConstant %6 3
113         %38 = OpTypeFloat 32
114         %39 = OpTypeVector %38 2
115         %40 = OpTypeStruct %39
116         %41 = OpTypePointer Uniform %40
117         %42 = OpVariable %41 Uniform
118         %43 = OpConstant %6 0
119         %44 = OpConstant %14 0
120         %45 = OpTypePointer Uniform %38
121         %57 = OpTypeVector %38 4
122         %58 = OpTypePointer Output %57
123         %59 = OpVariable %58 Output
124         %60 = OpConstant %38 1
125         %61 = OpConstantComposite %57 %60 %60 %60 %60
126         %67 = OpConstant %38 0
127         %68 = OpConstantComposite %57 %60 %67 %67 %60
128         %98 = OpTypePointer Function %16
129        %109 = OpConstant %6 2
130          %4 = OpFunction %2 None %3
131          %5 = OpLabel
132         %18 = OpVariable %98 Function
133         %70 = OpAccessChain %7 %18 %13
134         %71 = OpLoad %6 %70
135         %72 = OpSLessThan %22 %13 %71
136        %110 = OpSelect %6 %72 %109 %13
137               OpBranch %77
138         %77 = OpLabel
139        %101 = OpPhi %6 %13 %5 %106 %89
140         %99 = OpPhi %6 %110 %5 %85 %89
141         %82 = OpSLessThan %22 %99 %34
142               OpLoopMerge %78 %89 None
143               OpBranchConditional %82 %83 %78
144         %83 = OpLabel
145         %85 = OpIAdd %6 %99 %13
146         %86 = OpAccessChain %45 %42 %43 %44
147         %87 = OpLoad %38 %86
148         %88 = OpConvertFToS %6 %87
149               OpSelectionMerge %111 None
150               OpSwitch %88 %90 19 %91 38 %92 23 %92 78 %93
151         %90 = OpLabel
152               OpBranch %89
153         %91 = OpLabel
154         %95 = OpIAdd %6 %101 %13
155               OpBranch %92
156         %92 = OpLabel
157        %108 = OpPhi %6 %101 %83 %95 %91
158               OpBranch %89
159         %93 = OpLabel
160               OpStore %59 %61
161               OpBranch %90
162        %111 = OpLabel
163               OpBranch %89
164         %89 = OpLabel
165        %106 = OpPhi %6 %101 %90 %108 %92 %43 %111
166               OpBranch %77
167         %78 = OpLabel
168         %97 = OpAccessChain %7 %18 %101
169               OpStore %97 %13
170               OpStore %59 %68
171               OpReturn
172               OpFunctionEnd
173END
174
175# uniforms for variant
176
177# injectionSwitch
178BUFFER variant_injectionSwitch DATA_TYPE vec2<float> DATA
179 0.0 1.0
180END
181
182BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM
183
184PIPELINE graphics variant_pipeline
185  ATTACH variant_vertex_shader
186  ATTACH variant_fragment_shader
187  FRAMEBUFFER_SIZE 256 256
188  BIND BUFFER variant_framebuffer AS color LOCATION 0
189  BIND BUFFER variant_injectionSwitch AS uniform DESCRIPTOR_SET 0 BINDING 0
190END
191CLEAR_COLOR variant_pipeline 0 0 0 255
192
193CLEAR variant_pipeline
194RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 256 256
195
196EXPECT variant_framebuffer IDX 0 0 SIZE 256 256 EQ_RGBA 255 0 0 255
197