1#!amber
2
3# Copyright 2018 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# A test for a bug found by GraphicsFuzz.
18
19# A fragment shader that uses FragCoord and somewhat complex control flow.
20# Derived from the following GLSL fragment shader.
21# We check that all pixels are red. The test passes because _GLF_color is
22# initially set to red, all other writes to _GLF_color write red, and the discard
23# statement only executes for pixels with an x coordinate >= 10000000.
24
25# #version 310 es
26#
27# precision highp float;
28# precision highp int;
29#
30# layout(location=0) out vec4 _GLF_color;
31#
32# void main() {
33#   _GLF_color = vec4(1.0, 0.0, 0.0, 1.0);
34#   for (int i = 0; i < 1; i++) {
35#     if (int(gl_FragCoord.x) < 10000000) {
36#     } else {
37#
38#       if (int(gl_FragCoord.x) < 10000000) {
39#
40#         for (int j = 0; j < 1; j++) {
41#           for (int k = 0; k < 1; k++) {
42#             _GLF_color = vec4(1.0, 0.0, 0.0, 1.0);
43#           }
44#         }
45#         vec4 backup;
46#         for (int l = 0; l < 1; l++) {
47#           backup = vec4(1.0, 0.0, 0.0, 1.0);
48#         }
49#         _GLF_color = backup;
50#       } else {
51#         discard;
52#       }
53#     }
54#   }
55# }
56
57SHADER vertex variant_vertex_shader PASSTHROUGH
58
59SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
60; SPIR-V
61; Version: 1.0
62; Generator: Khronos SPIR-V Tools Assembler; 0
63; Bound: 63
64; Schema: 0
65               OpCapability Shader
66          %1 = OpExtInstImport "GLSL.std.450"
67               OpMemoryModel Logical GLSL450
68               OpEntryPoint Fragment %2 "main" %3 %4
69               OpExecutionMode %2 OriginUpperLeft
70               OpSource ESSL 310
71               OpName %2 "main"
72               OpName %3 "_GLF_color"
73               OpName %4 "gl_FragCoord"
74               OpDecorate %3 Location 0
75               OpDecorate %4 BuiltIn FragCoord
76          %5 = OpTypeVoid
77          %6 = OpTypeFunction %5
78          %7 = OpTypeFloat 32
79          %8 = OpTypeVector %7 4
80          %9 = OpTypePointer Output %8
81          %3 = OpVariable %9 Output
82         %10 = OpConstant %7 1
83         %11 = OpConstant %7 0
84         %12 = OpConstantComposite %8 %10 %11 %11 %10
85         %13 = OpTypeInt 32 1
86         %14 = OpConstant %13 0
87         %15 = OpConstant %13 1
88         %16 = OpTypeBool
89         %17 = OpTypePointer Input %8
90          %4 = OpVariable %17 Input
91         %18 = OpTypeInt 32 0
92         %19 = OpConstant %18 0
93         %20 = OpTypePointer Input %7
94         %21 = OpConstant %13 10000000
95         %22 = OpUndef %8
96          %2 = OpFunction %5 None %6
97         %23 = OpLabel
98               OpStore %3 %12
99               OpBranch %24
100         %24 = OpLabel
101         %25 = OpPhi %8 %22 %23 %26 %27
102         %28 = OpPhi %13 %14 %23 %29 %27
103         %30 = OpSLessThan %16 %28 %15
104               OpLoopMerge %31 %27 None
105               OpBranchConditional %30 %32 %31
106         %32 = OpLabel
107         %33 = OpAccessChain %20 %4 %19
108         %34 = OpLoad %7 %33
109         %35 = OpConvertFToS %13 %34
110         %36 = OpSLessThan %16 %35 %21
111               OpSelectionMerge %37 None
112               OpBranchConditional %36 %38 %39
113         %38 = OpLabel
114               OpBranch %27
115         %39 = OpLabel
116               OpSelectionMerge %40 None
117               OpBranchConditional %36 %41 %42
118         %41 = OpLabel
119               OpBranch %43
120         %43 = OpLabel
121         %44 = OpPhi %13 %14 %41 %45 %46
122         %47 = OpSLessThan %16 %44 %15
123               OpLoopMerge %48 %46 None
124               OpBranchConditional %47 %49 %48
125         %49 = OpLabel
126               OpBranch %50
127         %50 = OpLabel
128         %51 = OpPhi %13 %14 %49 %52 %53
129         %54 = OpSLessThan %16 %51 %15
130               OpLoopMerge %55 %53 None
131               OpBranchConditional %54 %53 %55
132         %53 = OpLabel
133               OpStore %3 %12
134         %52 = OpIAdd %13 %51 %15
135               OpBranch %50
136         %55 = OpLabel
137               OpBranch %46
138         %46 = OpLabel
139         %45 = OpIAdd %13 %44 %15
140               OpBranch %43
141         %48 = OpLabel
142               OpBranch %56
143         %56 = OpLabel
144         %57 = OpPhi %8 %25 %48 %12 %58
145         %59 = OpPhi %13 %14 %48 %60 %58
146         %61 = OpSLessThan %16 %59 %15
147               OpLoopMerge %62 %58 None
148               OpBranchConditional %61 %58 %62
149         %58 = OpLabel
150         %60 = OpIAdd %13 %59 %15
151               OpBranch %56
152         %62 = OpLabel
153               OpStore %3 %57
154               OpBranch %40
155         %42 = OpLabel
156               OpKill
157         %40 = OpLabel
158               OpBranch %27
159         %37 = OpLabel
160               OpBranch %27
161         %27 = OpLabel
162         %26 = OpPhi %8 %25 %38 %57 %40 %22 %37
163         %29 = OpIAdd %13 %28 %15
164               OpBranch %24
165         %31 = OpLabel
166               OpReturn
167               OpFunctionEnd
168END
169
170BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM
171
172PIPELINE graphics variant_pipeline
173  ATTACH variant_vertex_shader
174  ATTACH variant_fragment_shader
175  FRAMEBUFFER_SIZE 256 256
176  BIND BUFFER variant_framebuffer AS color LOCATION 0
177END
178CLEAR_COLOR variant_pipeline 0 0 0 255
179
180CLEAR variant_pipeline
181RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 256 256
182
183EXPECT variant_framebuffer IDX 0 0 SIZE 256 256 EQ_RGBA 255 0 0 255
184