Debian Codegraph @

test/fuzz/transformation_add_image_sample_unused_components_test.cpp

raw
1 // Copyright (c) 2020 André Perez Maselco
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/fuzz/transformation_add_image_sample_unused_components.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/fuzz/instruction_descriptor.h"
20 #include "test/fuzz/fuzz_test_util.h"
21
22 namespace spvtools {
23 namespace fuzz {
24 namespace {
25
26 TEST(TransformationAddImageSampleUnusedComponentsTest, IsApplicable) {
27 std::string shader = R"(
28 OpCapability Shader
29 OpCapability LiteralSampler
30 %1 = OpExtInstImport "GLSL.std.450"
31 OpMemoryModel Logical GLSL450
32 OpEntryPoint Fragment %18 "main" %17
33 OpExecutionMode %18 OriginUpperLeft
34 OpSource ESSL 310
35 OpName %18 "main"
36 %2 = OpTypeVoid
37 %3 = OpTypeFunction %2
38 %4 = OpTypeFloat 32
39 %5 = OpTypeVector %4 2
40 %6 = OpTypeVector %4 3
41 %7 = OpTypeVector %4 4
42 %8 = OpTypeImage %4 2D 0 0 0 1 Rgba32f
43 %9 = OpTypePointer Image %8
44 %10 = OpTypeSampledImage %8
45 %11 = OpTypeSampler
46 %12 = OpConstant %4 1
47 %13 = OpConstant %4 2
48 %14 = OpConstant %4 3
49 %15 = OpConstant %4 4
50 %16 = OpConstantSampler %11 None 0 Linear
51 %17 = OpVariable %9 Image
52 %18 = OpFunction %2 None %3
53 %19 = OpLabel
54 %20 = OpLoad %8 %17
55 %21 = OpSampledImage %10 %20 %16
56 %22 = OpCompositeConstruct %5 %12 %13
57 %23 = OpCompositeConstruct %6 %22 %14
58 %24 = OpCompositeConstruct %7 %23 %15
59 %25 = OpImageSampleImplicitLod %7 %21 %22
60 %26 = OpImageSampleExplicitLod %7 %21 %23 Lod %12
61 %27 = OpImageSampleExplicitLod %7 %21 %24 Lod %12
62 OpReturn
63 OpFunctionEnd
64 )";
65
66 const auto env = SPV_ENV_UNIVERSAL_1_5;
67 const auto consumer = nullptr;
68 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
69 spvtools::ValidatorOptions validator_options;
70 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
71 kConsoleMessageConsumer));
72 TransformationContext transformation_context(
73 MakeUnique<FactManager>(context.get()), validator_options);
74 // Tests applicable image instruction.
75 auto instruction_descriptor =
76 MakeInstructionDescriptor(25, spv::Op::OpImageSampleImplicitLod, 0);
77 auto transformation =
78 TransformationAddImageSampleUnusedComponents(23, instruction_descriptor);
79 ASSERT_TRUE(
80 transformation.IsApplicable(context.get(), transformation_context));
81
82 instruction_descriptor =
83 MakeInstructionDescriptor(26, spv::Op::OpImageSampleExplicitLod, 0);
84 transformation =
85 TransformationAddImageSampleUnusedComponents(24, instruction_descriptor);
86 ASSERT_TRUE(
87 transformation.IsApplicable(context.get(), transformation_context));
88
89 // Tests undefined image instructions.
90 instruction_descriptor =
91 MakeInstructionDescriptor(27, spv::Op::OpImageSampleImplicitLod, 0);
92 transformation =
93 TransformationAddImageSampleUnusedComponents(23, instruction_descriptor);
94 ASSERT_FALSE(
95 transformation.IsApplicable(context.get(), transformation_context));
96
97 instruction_descriptor =
98 MakeInstructionDescriptor(28, spv::Op::OpImageSampleExplicitLod, 0);
99 transformation =
100 TransformationAddImageSampleUnusedComponents(23, instruction_descriptor);
101 ASSERT_FALSE(
102 transformation.IsApplicable(context.get(), transformation_context));
103
104 // Tests non-image instructions.
105 instruction_descriptor = MakeInstructionDescriptor(19, spv::Op::OpLabel, 0);
106 transformation =
107 TransformationAddImageSampleUnusedComponents(24, instruction_descriptor);
108 ASSERT_FALSE(
109 transformation.IsApplicable(context.get(), transformation_context));
110
111 instruction_descriptor = MakeInstructionDescriptor(20, spv::Op::OpLoad, 0);
112 transformation =
113 TransformationAddImageSampleUnusedComponents(24, instruction_descriptor);
114 ASSERT_FALSE(
115 transformation.IsApplicable(context.get(), transformation_context));
116
117 // Tests coordinate operand being a vec4.
118 instruction_descriptor =
119 MakeInstructionDescriptor(27, spv::Op::OpImageSampleExplicitLod, 0);
120 transformation =
121 TransformationAddImageSampleUnusedComponents(22, instruction_descriptor);
122 ASSERT_FALSE(
123 transformation.IsApplicable(context.get(), transformation_context));
124
125 // Tests undefined coordinate with unused operands.
126 instruction_descriptor =
127 MakeInstructionDescriptor(25, spv::Op::OpImageSampleImplicitLod, 0);
128 transformation =
129 TransformationAddImageSampleUnusedComponents(27, instruction_descriptor);
130 ASSERT_FALSE(
131 transformation.IsApplicable(context.get(), transformation_context));
132
133 // Tests coordinate with unused operands being a non-OpCompositeConstruct
134 // instruction.
135 instruction_descriptor =
136 MakeInstructionDescriptor(25, spv::Op::OpImageSampleImplicitLod, 0);
137 transformation =
138 TransformationAddImageSampleUnusedComponents(21, instruction_descriptor);
139 ASSERT_FALSE(
140 transformation.IsApplicable(context.get(), transformation_context));
141
142 // Tests the first OpCompositeConstruct constituent not being the original
143 // coordinate.
144 instruction_descriptor =
145 MakeInstructionDescriptor(25, spv::Op::OpImageSampleImplicitLod, 0);
146 transformation =
147 TransformationAddImageSampleUnusedComponents(22, instruction_descriptor);
148 ASSERT_FALSE(
149 transformation.IsApplicable(context.get(), transformation_context));
150 }
151
152 TEST(TransformationAddImageSampleUnusedComponentsTest, Apply) {
153 std::string reference_shader = R"(
154 OpCapability Shader
155 OpCapability LiteralSampler
156 %1 = OpExtInstImport "GLSL.std.450"
157 OpMemoryModel Logical GLSL450
158 OpEntryPoint Fragment %18 "main" %17
159 OpExecutionMode %18 OriginUpperLeft
160 OpSource ESSL 310
161 OpName %18 "main"
162 %2 = OpTypeVoid
163 %3 = OpTypeFunction %2
164 %4 = OpTypeFloat 32
165 %5 = OpTypeVector %4 2
166 %6 = OpTypeVector %4 3
167 %7 = OpTypeVector %4 4
168 %8 = OpTypeImage %4 2D 0 0 0 1 Rgba32f
169 %9 = OpTypePointer Image %8
170 %10 = OpTypeSampledImage %8
171 %11 = OpTypeSampler
172 %12 = OpConstant %4 1
173 %13 = OpConstant %4 2
174 %14 = OpConstant %4 3
175 %15 = OpConstant %4 4
176 %16 = OpConstantSampler %11 None 0 Linear
177 %17 = OpVariable %9 Image
178 %18 = OpFunction %2 None %3
179 %19 = OpLabel
180 %20 = OpLoad %8 %17
181 %21 = OpSampledImage %10 %20 %16
182 %22 = OpCompositeConstruct %5 %12 %13
183 %23 = OpCompositeConstruct %6 %22 %14
184 %24 = OpCompositeConstruct %7 %23 %15
185 %25 = OpImageSampleImplicitLod %7 %21 %22
186 %26 = OpImageSampleExplicitLod %7 %21 %23 Lod %12
187 OpReturn
188 OpFunctionEnd
189 )";
190
191 const auto env = SPV_ENV_UNIVERSAL_1_5;
192 const auto consumer = nullptr;
193 const auto context =
194 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
195 spvtools::ValidatorOptions validator_options;
196 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
197 kConsoleMessageConsumer));
198 TransformationContext transformation_context(
199 MakeUnique<FactManager>(context.get()), validator_options);
200 auto instruction_descriptor =
201 MakeInstructionDescriptor(25, spv::Op::OpImageSampleImplicitLod, 0);
202 auto transformation =
203 TransformationAddImageSampleUnusedComponents(23, instruction_descriptor);
204 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
205
206 instruction_descriptor =
207 MakeInstructionDescriptor(26, spv::Op::OpImageSampleExplicitLod, 0);
208 transformation =
209 TransformationAddImageSampleUnusedComponents(24, instruction_descriptor);
210 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
211
212 std::string variant_shader = R"(
213 OpCapability Shader
214 OpCapability LiteralSampler
215 %1 = OpExtInstImport "GLSL.std.450"
216 OpMemoryModel Logical GLSL450
217 OpEntryPoint Fragment %18 "main" %17
218 OpExecutionMode %18 OriginUpperLeft
219 OpSource ESSL 310
220 OpName %18 "main"
221 %2 = OpTypeVoid
222 %3 = OpTypeFunction %2
223 %4 = OpTypeFloat 32
224 %5 = OpTypeVector %4 2
225 %6 = OpTypeVector %4 3
226 %7 = OpTypeVector %4 4
227 %8 = OpTypeImage %4 2D 0 0 0 1 Rgba32f
228 %9 = OpTypePointer Image %8
229 %10 = OpTypeSampledImage %8
230 %11 = OpTypeSampler
231 %12 = OpConstant %4 1
232 %13 = OpConstant %4 2
233 %14 = OpConstant %4 3
234 %15 = OpConstant %4 4
235 %16 = OpConstantSampler %11 None 0 Linear
236 %17 = OpVariable %9 Image
237 %18 = OpFunction %2 None %3
238 %19 = OpLabel
239 %20 = OpLoad %8 %17
240 %21 = OpSampledImage %10 %20 %16
241 %22 = OpCompositeConstruct %5 %12 %13
242 %23 = OpCompositeConstruct %6 %22 %14
243 %24 = OpCompositeConstruct %7 %23 %15
244 %25 = OpImageSampleImplicitLod %7 %21 %23
245 %26 = OpImageSampleExplicitLod %7 %21 %24 Lod %12
246 OpReturn
247 OpFunctionEnd
248 )";
249
250 ASSERT_TRUE(IsEqual(env, variant_shader, context.get()));
251 }
252
253 } // namespace
254 } // namespace fuzz
255 } // namespace spvtools