Debian Codegraph @

test/opt/convert_relaxed_to_half_test.cpp

raw
1 // Copyright (c) 2019 Valve Corporation
2 // Copyright (c) 2019 LunarG Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 // Convert Relaxed to Half tests
17
18 #include <string>
19 #include <vector>
20
21 #include "test/opt/assembly_builder.h"
22 #include "test/opt/pass_fixture.h"
23 #include "test/opt/pass_utils.h"
24
25 namespace spvtools {
26 namespace opt {
27 namespace {
28
29 using ConvertToHalfTest = PassTest<::testing::Test>;
30
31 TEST_F(ConvertToHalfTest, ConvertToHalfBasic) {
32 // The resulting SPIR-V was processed with --relax-float-ops.
33 //
34 // clang-format off
35 //
36 // SamplerState g_sSamp : register(s0);
37 // uniform Texture1D <float4> g_tTex1df4 : register(t0);
38 //
39 // struct PS_INPUT
40 // {
41 // float Tex0 : TEXCOORD0;
42 // };
43 //
44 // struct PS_OUTPUT
45 // {
46 // float4 Color : SV_Target0;
47 // };
48 //
49 // cbuffer cbuff{
50 // float c;
51 // }
52 //
53 // PS_OUTPUT main(PS_INPUT i)
54 // {
55 // PS_OUTPUT psout;
56 // psout.Color = g_tTex1df4.Sample(g_sSamp, i.Tex0) * c;
57 // return psout;
58 // }
59 //
60 // clang-format on
61
62 const std::string defs_before =
63 R"(OpCapability Shader
64 OpCapability Sampled1D
65 %1 = OpExtInstImport "GLSL.std.450"
66 OpMemoryModel Logical GLSL450
67 OpEntryPoint Fragment %main "main" %i_Tex0 %_entryPointOutput_Color
68 OpExecutionMode %main OriginUpperLeft
69 OpSource HLSL 500
70 OpName %main "main"
71 OpName %g_tTex1df4 "g_tTex1df4"
72 OpName %g_sSamp "g_sSamp"
73 OpName %cbuff "cbuff"
74 OpMemberName %cbuff 0 "c"
75 OpName %_ ""
76 OpName %i_Tex0 "i.Tex0"
77 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
78 OpDecorate %g_tTex1df4 DescriptorSet 0
79 OpDecorate %g_tTex1df4 Binding 0
80 OpDecorate %g_sSamp DescriptorSet 0
81 OpDecorate %g_sSamp Binding 0
82 OpMemberDecorate %cbuff 0 Offset 0
83 OpDecorate %cbuff Block
84 OpDecorate %_ DescriptorSet 0
85 OpDecorate %_ Binding 1
86 OpDecorate %i_Tex0 Location 0
87 OpDecorate %_entryPointOutput_Color Location 0
88 OpDecorate %48 RelaxedPrecision
89 OpDecorate %63 RelaxedPrecision
90 OpDecorate %65 RelaxedPrecision
91 OpDecorate %66 RelaxedPrecision
92 %void = OpTypeVoid
93 %3 = OpTypeFunction %void
94 %float = OpTypeFloat 32
95 %v4float = OpTypeVector %float 4
96 %int = OpTypeInt 32 1
97 %int_0 = OpConstant %int 0
98 %19 = OpTypeImage %float 1D 0 0 0 1 Unknown
99 %_ptr_UniformConstant_19 = OpTypePointer UniformConstant %19
100 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_19 UniformConstant
101 %23 = OpTypeSampler
102 %_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
103 %g_sSamp = OpVariable %_ptr_UniformConstant_23 UniformConstant
104 %27 = OpTypeSampledImage %19
105 %cbuff = OpTypeStruct %float
106 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
107 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
108 %_ptr_Uniform_float = OpTypePointer Uniform %float
109 %_ptr_Input_float = OpTypePointer Input %float
110 %i_Tex0 = OpVariable %_ptr_Input_float Input
111 %_ptr_Output_v4float = OpTypePointer Output %v4float
112 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
113 )";
114
115 const std::string defs_after =
116 R"(OpCapability Shader
117 OpCapability Sampled1D
118 OpCapability Float16
119 %1 = OpExtInstImport "GLSL.std.450"
120 OpMemoryModel Logical GLSL450
121 OpEntryPoint Fragment %main "main" %i_Tex0 %_entryPointOutput_Color
122 OpExecutionMode %main OriginUpperLeft
123 OpSource HLSL 500
124 OpName %main "main"
125 OpName %g_tTex1df4 "g_tTex1df4"
126 OpName %g_sSamp "g_sSamp"
127 OpName %cbuff "cbuff"
128 OpMemberName %cbuff 0 "c"
129 OpName %_ ""
130 OpName %i_Tex0 "i.Tex0"
131 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
132 OpDecorate %g_tTex1df4 DescriptorSet 0
133 OpDecorate %g_tTex1df4 Binding 0
134 OpDecorate %g_sSamp DescriptorSet 0
135 OpDecorate %g_sSamp Binding 0
136 OpMemberDecorate %cbuff 0 Offset 0
137 OpDecorate %cbuff Block
138 OpDecorate %_ DescriptorSet 0
139 OpDecorate %_ Binding 1
140 OpDecorate %i_Tex0 Location 0
141 OpDecorate %_entryPointOutput_Color Location 0
142 %void = OpTypeVoid
143 %3 = OpTypeFunction %void
144 %float = OpTypeFloat 32
145 %v4float = OpTypeVector %float 4
146 %int = OpTypeInt 32 1
147 %int_0 = OpConstant %int 0
148 %19 = OpTypeImage %float 1D 0 0 0 1 Unknown
149 %_ptr_UniformConstant_19 = OpTypePointer UniformConstant %19
150 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_19 UniformConstant
151 %23 = OpTypeSampler
152 %_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
153 %g_sSamp = OpVariable %_ptr_UniformConstant_23 UniformConstant
154 %27 = OpTypeSampledImage %19
155 %cbuff = OpTypeStruct %float
156 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
157 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
158 %_ptr_Uniform_float = OpTypePointer Uniform %float
159 %_ptr_Input_float = OpTypePointer Input %float
160 %i_Tex0 = OpVariable %_ptr_Input_float Input
161 %_ptr_Output_v4float = OpTypePointer Output %v4float
162 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
163 %half = OpTypeFloat 16
164 %v4half = OpTypeVector %half 4
165 )";
166
167 const std::string func_before =
168 R"(%main = OpFunction %void None %3
169 %5 = OpLabel
170 %48 = OpLoad %float %i_Tex0
171 %58 = OpLoad %19 %g_tTex1df4
172 %59 = OpLoad %23 %g_sSamp
173 %60 = OpSampledImage %27 %58 %59
174 %63 = OpImageSampleImplicitLod %v4float %60 %48
175 %64 = OpAccessChain %_ptr_Uniform_float %_ %int_0
176 %65 = OpLoad %float %64
177 %66 = OpVectorTimesScalar %v4float %63 %65
178 OpStore %_entryPointOutput_Color %66
179 OpReturn
180 OpFunctionEnd
181 )";
182
183 const std::string func_after =
184 R"(%main = OpFunction %void None %3
185 %5 = OpLabel
186 %48 = OpLoad %float %i_Tex0
187 %58 = OpLoad %19 %g_tTex1df4
188 %59 = OpLoad %23 %g_sSamp
189 %60 = OpSampledImage %27 %58 %59
190 %63 = OpImageSampleImplicitLod %v4float %60 %48
191 %64 = OpAccessChain %_ptr_Uniform_float %_ %int_0
192 %65 = OpLoad %float %64
193 %69 = OpFConvert %v4half %63
194 %70 = OpFConvert %half %65
195 %66 = OpVectorTimesScalar %v4half %69 %70
196 %71 = OpFConvert %v4float %66
197 OpStore %_entryPointOutput_Color %71
198 OpReturn
199 OpFunctionEnd
200 )";
201
202 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
203 SinglePassRunAndCheck<ConvertToHalfPass>(defs_before + func_before,
204 defs_after + func_after, true, true);
205 }
206
207 TEST_F(ConvertToHalfTest, ConvertToHalfForLinkage) {
208 const std::string before =
209 R"(OpCapability Shader
210 OpCapability Linkage
211 OpMemoryModel Logical GLSL450
212 OpSource HLSL 630
213 OpName %type_cbuff "type.cbuff"
214 OpMemberName %type_cbuff 0 "c"
215 OpName %cbuff "cbuff"
216 OpName %main "main"
217 OpName %BaseColor "BaseColor"
218 OpName %bb_entry "bb.entry"
219 OpName %v "v"
220 OpDecorate %main LinkageAttributes "main" Export
221 OpDecorate %cbuff DescriptorSet 0
222 OpDecorate %cbuff Binding 0
223 OpMemberDecorate %type_cbuff 0 Offset 0
224 OpDecorate %type_cbuff Block
225 OpDecorate %18 RelaxedPrecision
226 %int = OpTypeInt 32 1
227 %int_0 = OpConstant %int 0
228 %float = OpTypeFloat 32
229 %type_cbuff = OpTypeStruct %float
230 %_ptr_Uniform_type_cbuff = OpTypePointer Uniform %type_cbuff
231 %v4float = OpTypeVector %float 4
232 %_ptr_Function_v4float = OpTypePointer Function %v4float
233 %9 = OpTypeFunction %v4float %_ptr_Function_v4float
234 %_ptr_Uniform_float = OpTypePointer Uniform %float
235 %cbuff = OpVariable %_ptr_Uniform_type_cbuff Uniform
236 %main = OpFunction %v4float None %9
237 %BaseColor = OpFunctionParameter %_ptr_Function_v4float
238 %bb_entry = OpLabel
239 %v = OpVariable %_ptr_Function_v4float Function
240 %14 = OpLoad %v4float %BaseColor
241 %16 = OpAccessChain %_ptr_Uniform_float %cbuff %int_0
242 %17 = OpLoad %float %16
243 %18 = OpVectorTimesScalar %v4float %14 %17
244 OpStore %v %18
245 %19 = OpLoad %v4float %v
246 OpReturnValue %19
247 OpFunctionEnd
248 )";
249
250 const std::string after =
251 R"(OpCapability Shader
252 OpCapability Linkage
253 OpCapability Float16
254 OpMemoryModel Logical GLSL450
255 OpSource HLSL 630
256 OpName %type_cbuff "type.cbuff"
257 OpMemberName %type_cbuff 0 "c"
258 OpName %cbuff "cbuff"
259 OpName %main "main"
260 OpName %BaseColor "BaseColor"
261 OpName %bb_entry "bb.entry"
262 OpName %v "v"
263 OpDecorate %main LinkageAttributes "main" Export
264 OpDecorate %cbuff DescriptorSet 0
265 OpDecorate %cbuff Binding 0
266 OpMemberDecorate %type_cbuff 0 Offset 0
267 OpDecorate %type_cbuff Block
268 %int = OpTypeInt 32 1
269 %int_0 = OpConstant %int 0
270 %float = OpTypeFloat 32
271 %type_cbuff = OpTypeStruct %float
272 %_ptr_Uniform_type_cbuff = OpTypePointer Uniform %type_cbuff
273 %v4float = OpTypeVector %float 4
274 %_ptr_Function_v4float = OpTypePointer Function %v4float
275 %14 = OpTypeFunction %v4float %_ptr_Function_v4float
276 %_ptr_Uniform_float = OpTypePointer Uniform %float
277 %cbuff = OpVariable %_ptr_Uniform_type_cbuff Uniform
278 %half = OpTypeFloat 16
279 %v4half = OpTypeVector %half 4
280 %main = OpFunction %v4float None %14
281 %BaseColor = OpFunctionParameter %_ptr_Function_v4float
282 %bb_entry = OpLabel
283 %v = OpVariable %_ptr_Function_v4float Function
284 %16 = OpLoad %v4float %BaseColor
285 %17 = OpAccessChain %_ptr_Uniform_float %cbuff %int_0
286 %18 = OpLoad %float %17
287 %22 = OpFConvert %v4half %16
288 %23 = OpFConvert %half %18
289 %7 = OpVectorTimesScalar %v4half %22 %23
290 %24 = OpFConvert %v4float %7
291 OpStore %v %24
292 %19 = OpLoad %v4float %v
293 OpReturnValue %19
294 OpFunctionEnd
295 )";
296
297 SinglePassRunAndCheck<ConvertToHalfPass>(before, after, true, true);
298 }
299 TEST_F(ConvertToHalfTest, ConvertToHalfWithDrefSample) {
300 // The resulting SPIR-V was processed with --relax-float-ops.
301 //
302 // clang-format off
303 //
304 // SamplerComparisonState g_sSamp : register(s0);
305 // uniform Texture1D <float4> g_tTex1df4 : register(t0);
306 //
307 // cbuffer cbuff{
308 // float c1;
309 // float c2;
310 // };
311 //
312 // struct PS_INPUT
313 // {
314 // float Tex0 : TEXCOORD0;
315 // float Tex1 : TEXCOORD1;
316 // };
317 //
318 // struct PS_OUTPUT
319 // {
320 // float Color : SV_Target0;
321 // };
322 //
323 // PS_OUTPUT main(PS_INPUT i)
324 // {
325 // PS_OUTPUT psout;
326 // float txval10 = g_tTex1df4.SampleCmp(g_sSamp, i.Tex0 * 0.1, c1 + 0.1);
327 // float txval11 = g_tTex1df4.SampleCmp(g_sSamp, i.Tex1 * 0.2, c2 + 0.2);
328 // float t = txval10 + txval11;
329 // float t2 = t / 2.0;
330 // psout.Color = t2;
331 // return psout;
332 // }
333 //
334 // clang-format on
335
336 const std::string defs_before =
337 R"(OpCapability Shader
338 OpCapability Sampled1D
339 %1 = OpExtInstImport "GLSL.std.450"
340 OpMemoryModel Logical GLSL450
341 OpEntryPoint Fragment %main "main" %i_Tex0 %i_Tex1 %_entryPointOutput_Color
342 OpExecutionMode %main OriginUpperLeft
343 OpSource HLSL 500
344 OpName %main "main"
345 OpName %g_tTex1df4 "g_tTex1df4"
346 OpName %g_sSamp "g_sSamp"
347 OpName %cbuff "cbuff"
348 OpMemberName %cbuff 0 "c1"
349 OpMemberName %cbuff 1 "c2"
350 OpName %_ ""
351 OpName %i_Tex0 "i.Tex0"
352 OpName %i_Tex1 "i.Tex1"
353 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
354 OpDecorate %g_tTex1df4 DescriptorSet 0
355 OpDecorate %g_tTex1df4 Binding 0
356 OpDecorate %g_sSamp DescriptorSet 0
357 OpDecorate %g_sSamp Binding 0
358 OpMemberDecorate %cbuff 0 Offset 0
359 OpMemberDecorate %cbuff 1 Offset 4
360 OpDecorate %cbuff Block
361 OpDecorate %_ DescriptorSet 0
362 OpDecorate %_ Binding 1
363 OpDecorate %i_Tex0 Location 0
364 OpDecorate %i_Tex1 Location 1
365 OpDecorate %_entryPointOutput_Color Location 0
366 OpDecorate %100 RelaxedPrecision
367 OpDecorate %76 RelaxedPrecision
368 OpDecorate %79 RelaxedPrecision
369 OpDecorate %98 RelaxedPrecision
370 OpDecorate %101 RelaxedPrecision
371 OpDecorate %110 RelaxedPrecision
372 OpDecorate %102 RelaxedPrecision
373 OpDecorate %112 RelaxedPrecision
374 OpDecorate %104 RelaxedPrecision
375 OpDecorate %113 RelaxedPrecision
376 OpDecorate %114 RelaxedPrecision
377 OpDecorate %116 RelaxedPrecision
378 OpDecorate %119 RelaxedPrecision
379 OpDecorate %121 RelaxedPrecision
380 %void = OpTypeVoid
381 %3 = OpTypeFunction %void
382 %float = OpTypeFloat 32
383 %16 = OpTypeImage %float 1D 1 0 0 1 Unknown
384 %_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
385 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_16 UniformConstant
386 %20 = OpTypeSampler
387 %_ptr_UniformConstant_20 = OpTypePointer UniformConstant %20
388 %g_sSamp = OpVariable %_ptr_UniformConstant_20 UniformConstant
389 %24 = OpTypeSampledImage %16
390 %int = OpTypeInt 32 1
391 %int_0 = OpConstant %int 0
392 %float_0_100000001 = OpConstant %float 0.100000001
393 %cbuff = OpTypeStruct %float %float
394 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
395 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
396 %_ptr_Uniform_float = OpTypePointer Uniform %float
397 %v2float = OpTypeVector %float 2
398 %int_1 = OpConstant %int 1
399 %float_0_200000003 = OpConstant %float 0.200000003
400 %_ptr_Input_float = OpTypePointer Input %float
401 %i_Tex0 = OpVariable %_ptr_Input_float Input
402 %i_Tex1 = OpVariable %_ptr_Input_float Input
403 %_ptr_Output_float = OpTypePointer Output %float
404 %_entryPointOutput_Color = OpVariable %_ptr_Output_float Output
405 %float_0_5 = OpConstant %float 0.5
406 )";
407
408 const std::string defs_after =
409 R"(OpCapability Shader
410 OpCapability Sampled1D
411 OpCapability Float16
412 %1 = OpExtInstImport "GLSL.std.450"
413 OpMemoryModel Logical GLSL450
414 OpEntryPoint Fragment %main "main" %i_Tex0 %i_Tex1 %_entryPointOutput_Color
415 OpExecutionMode %main OriginUpperLeft
416 OpSource HLSL 500
417 OpName %main "main"
418 OpName %g_tTex1df4 "g_tTex1df4"
419 OpName %g_sSamp "g_sSamp"
420 OpName %cbuff "cbuff"
421 OpMemberName %cbuff 0 "c1"
422 OpMemberName %cbuff 1 "c2"
423 OpName %_ ""
424 OpName %i_Tex0 "i.Tex0"
425 OpName %i_Tex1 "i.Tex1"
426 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
427 OpDecorate %g_tTex1df4 DescriptorSet 0
428 OpDecorate %g_tTex1df4 Binding 0
429 OpDecorate %g_sSamp DescriptorSet 0
430 OpDecorate %g_sSamp Binding 0
431 OpMemberDecorate %cbuff 0 Offset 0
432 OpMemberDecorate %cbuff 1 Offset 4
433 OpDecorate %cbuff Block
434 OpDecorate %_ DescriptorSet 0
435 OpDecorate %_ Binding 1
436 OpDecorate %i_Tex0 Location 0
437 OpDecorate %i_Tex1 Location 1
438 OpDecorate %_entryPointOutput_Color Location 0
439 %void = OpTypeVoid
440 %25 = OpTypeFunction %void
441 %float = OpTypeFloat 32
442 %27 = OpTypeImage %float 1D 1 0 0 1 Unknown
443 %_ptr_UniformConstant_27 = OpTypePointer UniformConstant %27
444 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_27 UniformConstant
445 %29 = OpTypeSampler
446 %_ptr_UniformConstant_29 = OpTypePointer UniformConstant %29
447 %g_sSamp = OpVariable %_ptr_UniformConstant_29 UniformConstant
448 %31 = OpTypeSampledImage %27
449 %int = OpTypeInt 32 1
450 %int_0 = OpConstant %int 0
451 %float_0_100000001 = OpConstant %float 0.100000001
452 %cbuff = OpTypeStruct %float %float
453 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
454 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
455 %_ptr_Uniform_float = OpTypePointer Uniform %float
456 %v2float = OpTypeVector %float 2
457 %int_1 = OpConstant %int 1
458 %float_0_200000003 = OpConstant %float 0.200000003
459 %_ptr_Input_float = OpTypePointer Input %float
460 %i_Tex0 = OpVariable %_ptr_Input_float Input
461 %i_Tex1 = OpVariable %_ptr_Input_float Input
462 %_ptr_Output_float = OpTypePointer Output %float
463 %_entryPointOutput_Color = OpVariable %_ptr_Output_float Output
464 %float_0_5 = OpConstant %float 0.5
465 %half = OpTypeFloat 16
466 %v2half = OpTypeVector %half 2
467 )";
468
469 const std::string func_before =
470 R"(%main = OpFunction %void None %3
471 %5 = OpLabel
472 %76 = OpLoad %float %i_Tex0
473 %79 = OpLoad %float %i_Tex1
474 %93 = OpLoad %16 %g_tTex1df4
475 %94 = OpLoad %20 %g_sSamp
476 %95 = OpSampledImage %24 %93 %94
477 %98 = OpFMul %float %76 %float_0_100000001
478 %99 = OpAccessChain %_ptr_Uniform_float %_ %int_0
479 %100 = OpLoad %float %99
480 %101 = OpFAdd %float %100 %float_0_100000001
481 %102 = OpCompositeConstruct %v2float %98 %101
482 %104 = OpImageSampleDrefImplicitLod %float %95 %102 %101
483 %105 = OpLoad %16 %g_tTex1df4
484 %106 = OpLoad %20 %g_sSamp
485 %107 = OpSampledImage %24 %105 %106
486 %110 = OpFMul %float %79 %float_0_200000003
487 %111 = OpAccessChain %_ptr_Uniform_float %_ %int_1
488 %112 = OpLoad %float %111
489 %113 = OpFAdd %float %112 %float_0_200000003
490 %114 = OpCompositeConstruct %v2float %110 %113
491 %116 = OpImageSampleDrefImplicitLod %float %107 %114 %113
492 %119 = OpFAdd %float %104 %116
493 %121 = OpFMul %float %119 %float_0_5
494 OpStore %_entryPointOutput_Color %121
495 OpReturn
496 OpFunctionEnd
497 )";
498
499 const std::string func_after =
500 R"(%main = OpFunction %void None %25
501 %43 = OpLabel
502 %11 = OpLoad %float %i_Tex0
503 %12 = OpLoad %float %i_Tex1
504 %44 = OpLoad %27 %g_tTex1df4
505 %45 = OpLoad %29 %g_sSamp
506 %46 = OpSampledImage %31 %44 %45
507 %53 = OpFConvert %half %11
508 %54 = OpFConvert %half %float_0_100000001
509 %13 = OpFMul %half %53 %54
510 %47 = OpAccessChain %_ptr_Uniform_float %_ %int_0
511 %10 = OpLoad %float %47
512 %55 = OpFConvert %half %10
513 %56 = OpFConvert %half %float_0_100000001
514 %14 = OpFAdd %half %55 %56
515 %16 = OpCompositeConstruct %v2half %13 %14
516 %58 = OpFConvert %float %14
517 %59 = OpFConvert %v2float %16
518 %18 = OpImageSampleDrefImplicitLod %float %46 %59 %58
519 %48 = OpLoad %27 %g_tTex1df4
520 %49 = OpLoad %29 %g_sSamp
521 %50 = OpSampledImage %31 %48 %49
522 %60 = OpFConvert %half %12
523 %61 = OpFConvert %half %float_0_200000003
524 %15 = OpFMul %half %60 %61
525 %51 = OpAccessChain %_ptr_Uniform_float %_ %int_1
526 %17 = OpLoad %float %51
527 %62 = OpFConvert %half %17
528 %63 = OpFConvert %half %float_0_200000003
529 %19 = OpFAdd %half %62 %63
530 %20 = OpCompositeConstruct %v2half %15 %19
531 %64 = OpFConvert %float %19
532 %65 = OpFConvert %v2float %20
533 %21 = OpImageSampleDrefImplicitLod %float %50 %65 %64
534 %66 = OpFConvert %half %18
535 %67 = OpFConvert %half %21
536 %22 = OpFAdd %half %66 %67
537 %68 = OpFConvert %half %float_0_5
538 %23 = OpFMul %half %22 %68
539 %69 = OpFConvert %float %23
540 OpStore %_entryPointOutput_Color %69
541 OpReturn
542 OpFunctionEnd
543 )";
544
545 SinglePassRunAndCheck<ConvertToHalfPass>(defs_before + func_before,
546 defs_after + func_after, true, true);
547 }
548
549 TEST_F(ConvertToHalfTest, ConvertToHalfWithVectorMatrixMult) {
550 // The resulting SPIR-V was processed with --relax-float-ops.
551 //
552 // clang-format off
553 //
554 // SamplerState g_sSamp : register(s0);
555 // uniform Texture1D <float4> g_tTex1df4 : register(t0);
556 //
557 // struct PS_OUTPUT
558 // {
559 // float4 Color : SV_Target0;
560 // };
561 //
562 // cbuffer cbuff{
563 // float4x4 M;
564 // }
565 //
566 // PS_OUTPUT main()
567 // {
568 // PS_OUTPUT psout;
569 // float4 txval10 = g_tTex1df4.Sample(g_sSamp, 0.1);
570 // float4 t = mul(txval10, M);
571 // psout.Color = t;
572 // return psout;
573 //}
574 //
575 // clang-format on
576
577 const std::string defs_before =
578 R"(OpCapability Shader
579 OpCapability Sampled1D
580 %1 = OpExtInstImport "GLSL.std.450"
581 OpMemoryModel Logical GLSL450
582 OpEntryPoint Fragment %main "main" %_entryPointOutput_Color
583 OpExecutionMode %main OriginUpperLeft
584 OpSource HLSL 500
585 OpName %main "main"
586 OpName %g_tTex1df4 "g_tTex1df4"
587 OpName %g_sSamp "g_sSamp"
588 OpName %cbuff "cbuff"
589 OpMemberName %cbuff 0 "M"
590 OpName %_ ""
591 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
592 OpDecorate %g_tTex1df4 DescriptorSet 0
593 OpDecorate %g_tTex1df4 Binding 0
594 OpDecorate %g_sSamp DescriptorSet 0
595 OpDecorate %g_sSamp Binding 0
596 OpMemberDecorate %cbuff 0 RowMajor
597 OpMemberDecorate %cbuff 0 Offset 0
598 OpMemberDecorate %cbuff 0 MatrixStride 16
599 OpDecorate %cbuff Block
600 OpDecorate %_ DescriptorSet 0
601 OpDecorate %_ Binding 1
602 OpDecorate %_entryPointOutput_Color Location 0
603 OpDecorate %56 RelaxedPrecision
604 OpDecorate %58 RelaxedPrecision
605 OpDecorate %60 RelaxedPrecision
606 %void = OpTypeVoid
607 %3 = OpTypeFunction %void
608 %float = OpTypeFloat 32
609 %v4float = OpTypeVector %float 4
610 %14 = OpTypeImage %float 1D 0 0 0 1 Unknown
611 %_ptr_UniformConstant_14 = OpTypePointer UniformConstant %14
612 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_14 UniformConstant
613 %18 = OpTypeSampler
614 %_ptr_UniformConstant_18 = OpTypePointer UniformConstant %18
615 %g_sSamp = OpVariable %_ptr_UniformConstant_18 UniformConstant
616 %22 = OpTypeSampledImage %14
617 %float_0_100000001 = OpConstant %float 0.100000001
618 %mat4v4float = OpTypeMatrix %v4float 4
619 %cbuff = OpTypeStruct %mat4v4float
620 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
621 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
622 %int = OpTypeInt 32 1
623 %int_0 = OpConstant %int 0
624 %_ptr_Uniform_mat4v4float = OpTypePointer Uniform %mat4v4float
625 %_ptr_Output_v4float = OpTypePointer Output %v4float
626 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
627 )";
628
629 const std::string defs_after =
630 R"(OpCapability Shader
631 OpCapability Sampled1D
632 OpCapability Float16
633 %1 = OpExtInstImport "GLSL.std.450"
634 OpMemoryModel Logical GLSL450
635 OpEntryPoint Fragment %main "main" %_entryPointOutput_Color
636 OpExecutionMode %main OriginUpperLeft
637 OpSource HLSL 500
638 OpName %main "main"
639 OpName %g_tTex1df4 "g_tTex1df4"
640 OpName %g_sSamp "g_sSamp"
641 OpName %cbuff "cbuff"
642 OpMemberName %cbuff 0 "M"
643 OpName %_ ""
644 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
645 OpDecorate %g_tTex1df4 DescriptorSet 0
646 OpDecorate %g_tTex1df4 Binding 0
647 OpDecorate %g_sSamp DescriptorSet 0
648 OpDecorate %g_sSamp Binding 0
649 OpMemberDecorate %cbuff 0 RowMajor
650 OpMemberDecorate %cbuff 0 Offset 0
651 OpMemberDecorate %cbuff 0 MatrixStride 16
652 OpDecorate %cbuff Block
653 OpDecorate %_ DescriptorSet 0
654 OpDecorate %_ Binding 1
655 OpDecorate %_entryPointOutput_Color Location 0
656 %void = OpTypeVoid
657 %3 = OpTypeFunction %void
658 %float = OpTypeFloat 32
659 %v4float = OpTypeVector %float 4
660 %14 = OpTypeImage %float 1D 0 0 0 1 Unknown
661 %_ptr_UniformConstant_14 = OpTypePointer UniformConstant %14
662 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_14 UniformConstant
663 %18 = OpTypeSampler
664 %_ptr_UniformConstant_18 = OpTypePointer UniformConstant %18
665 %g_sSamp = OpVariable %_ptr_UniformConstant_18 UniformConstant
666 %22 = OpTypeSampledImage %14
667 %float_0_100000001 = OpConstant %float 0.100000001
668 %mat4v4float = OpTypeMatrix %v4float 4
669 %cbuff = OpTypeStruct %mat4v4float
670 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
671 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
672 %int = OpTypeInt 32 1
673 %int_0 = OpConstant %int 0
674 %_ptr_Uniform_mat4v4float = OpTypePointer Uniform %mat4v4float
675 %_ptr_Output_v4float = OpTypePointer Output %v4float
676 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
677 %half = OpTypeFloat 16
678 %v4half = OpTypeVector %half 4
679 %mat4v4half = OpTypeMatrix %v4half 4
680 )";
681
682 const std::string func_before =
683 R"(%main = OpFunction %void None %3
684 %5 = OpLabel
685 %53 = OpLoad %14 %g_tTex1df4
686 %54 = OpLoad %18 %g_sSamp
687 %55 = OpSampledImage %22 %53 %54
688 %56 = OpImageSampleImplicitLod %v4float %55 %float_0_100000001
689 %57 = OpAccessChain %_ptr_Uniform_mat4v4float %_ %int_0
690 %58 = OpLoad %mat4v4float %57
691 %60 = OpMatrixTimesVector %v4float %58 %56
692 OpStore %_entryPointOutput_Color %60
693 OpReturn
694 OpFunctionEnd
695 )";
696
697 const std::string func_after =
698 R"(%main = OpFunction %void None %3
699 %5 = OpLabel
700 %53 = OpLoad %14 %g_tTex1df4
701 %54 = OpLoad %18 %g_sSamp
702 %55 = OpSampledImage %22 %53 %54
703 %56 = OpImageSampleImplicitLod %v4float %55 %float_0_100000001
704 %57 = OpAccessChain %_ptr_Uniform_mat4v4float %_ %int_0
705 %58 = OpLoad %mat4v4float %57
706 %67 = OpCompositeExtract %v4float %58 0
707 %68 = OpFConvert %v4half %67
708 %69 = OpCompositeExtract %v4float %58 1
709 %70 = OpFConvert %v4half %69
710 %71 = OpCompositeExtract %v4float %58 2
711 %72 = OpFConvert %v4half %71
712 %73 = OpCompositeExtract %v4float %58 3
713 %74 = OpFConvert %v4half %73
714 %75 = OpCompositeConstruct %mat4v4half %68 %70 %72 %74
715 %64 = OpCopyObject %mat4v4float %58
716 %65 = OpFConvert %v4half %56
717 %60 = OpMatrixTimesVector %v4half %75 %65
718 %66 = OpFConvert %v4float %60
719 OpStore %_entryPointOutput_Color %66
720 OpReturn
721 OpFunctionEnd
722 )";
723
724 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
725 SinglePassRunAndCheck<ConvertToHalfPass>(defs_before + func_before,
726 defs_after + func_after, true, true);
727 }
728
729 TEST_F(ConvertToHalfTest, ConvertToHalfWithPhi) {
730 // The resulting SPIR-V was processed with --relax-float-ops.
731 //
732 // clang-format off
733 //
734 // SamplerState g_sSamp : register(s0);
735 // uniform Texture1D <float4> g_tTex1df4 : register(t0);
736 //
737 // struct PS_OUTPUT
738 // {
739 // float4 Color : SV_Target0;
740 // };
741 //
742 // cbuffer cbuff{
743 // bool b;
744 // float4x4 M;
745 // }
746 //
747 // PS_OUTPUT main()
748 // {
749 // PS_OUTPUT psout;
750 // float4 t;
751 //
752 // if (b)
753 // t = g_tTex1df4.Sample(g_sSamp, 0.1);
754 // else
755 // t = float4(0.0, 0.0, 0.0, 0.0);
756 //
757 // float4 t2 = t * 2.0;
758 // psout.Color = t2;
759 // return psout;
760 // }
761 //
762 // clang-format on
763
764 const std::string defs_before =
765 R"(OpCapability Shader
766 OpCapability Sampled1D
767 %1 = OpExtInstImport "GLSL.std.450"
768 OpMemoryModel Logical GLSL450
769 OpEntryPoint Fragment %main "main" %_entryPointOutput_Color
770 OpExecutionMode %main OriginUpperLeft
771 OpSource HLSL 500
772 OpName %main "main"
773 OpName %cbuff "cbuff"
774 OpMemberName %cbuff 0 "b"
775 OpMemberName %cbuff 1 "M"
776 OpName %_ ""
777 OpName %g_tTex1df4 "g_tTex1df4"
778 OpName %g_sSamp "g_sSamp"
779 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
780 OpMemberDecorate %cbuff 0 Offset 0
781 OpMemberDecorate %cbuff 1 RowMajor
782 OpMemberDecorate %cbuff 1 Offset 16
783 OpMemberDecorate %cbuff 1 MatrixStride 16
784 OpDecorate %cbuff Block
785 OpDecorate %_ DescriptorSet 0
786 OpDecorate %_ Binding 1
787 OpDecorate %g_tTex1df4 DescriptorSet 0
788 OpDecorate %g_tTex1df4 Binding 0
789 OpDecorate %g_sSamp DescriptorSet 0
790 OpDecorate %g_sSamp Binding 0
791 OpDecorate %_entryPointOutput_Color Location 0
792 OpDecorate %72 RelaxedPrecision
793 OpDecorate %85 RelaxedPrecision
794 OpDecorate %74 RelaxedPrecision
795 %void = OpTypeVoid
796 %3 = OpTypeFunction %void
797 %float = OpTypeFloat 32
798 %v4float = OpTypeVector %float 4
799 %uint = OpTypeInt 32 0
800 %mat4v4float = OpTypeMatrix %v4float 4
801 %cbuff = OpTypeStruct %uint %mat4v4float
802 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
803 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
804 %int = OpTypeInt 32 1
805 %int_0 = OpConstant %int 0
806 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
807 %bool = OpTypeBool
808 %uint_0 = OpConstant %uint 0
809 %29 = OpTypeImage %float 1D 0 0 0 1 Unknown
810 %_ptr_UniformConstant_29 = OpTypePointer UniformConstant %29
811 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_29 UniformConstant
812 %33 = OpTypeSampler
813 %_ptr_UniformConstant_33 = OpTypePointer UniformConstant %33
814 %g_sSamp = OpVariable %_ptr_UniformConstant_33 UniformConstant
815 %37 = OpTypeSampledImage %29
816 %float_0_100000001 = OpConstant %float 0.100000001
817 %float_0 = OpConstant %float 0
818 %43 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
819 %float_2 = OpConstant %float 2
820 %_ptr_Output_v4float = OpTypePointer Output %v4float
821 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
822 )";
823
824 const std::string defs_after =
825 R"(OpCapability Shader
826 OpCapability Sampled1D
827 OpCapability Float16
828 %1 = OpExtInstImport "GLSL.std.450"
829 OpMemoryModel Logical GLSL450
830 OpEntryPoint Fragment %main "main" %_entryPointOutput_Color
831 OpExecutionMode %main OriginUpperLeft
832 OpSource HLSL 500
833 OpName %main "main"
834 OpName %cbuff "cbuff"
835 OpMemberName %cbuff 0 "b"
836 OpMemberName %cbuff 1 "M"
837 OpName %_ ""
838 OpName %g_tTex1df4 "g_tTex1df4"
839 OpName %g_sSamp "g_sSamp"
840 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
841 OpMemberDecorate %cbuff 0 Offset 0
842 OpMemberDecorate %cbuff 1 RowMajor
843 OpMemberDecorate %cbuff 1 Offset 16
844 OpMemberDecorate %cbuff 1 MatrixStride 16
845 OpDecorate %cbuff Block
846 OpDecorate %_ DescriptorSet 0
847 OpDecorate %_ Binding 1
848 OpDecorate %g_tTex1df4 DescriptorSet 0
849 OpDecorate %g_tTex1df4 Binding 0
850 OpDecorate %g_sSamp DescriptorSet 0
851 OpDecorate %g_sSamp Binding 0
852 OpDecorate %_entryPointOutput_Color Location 0
853 %void = OpTypeVoid
854 %3 = OpTypeFunction %void
855 %float = OpTypeFloat 32
856 %v4float = OpTypeVector %float 4
857 %uint = OpTypeInt 32 0
858 %mat4v4float = OpTypeMatrix %v4float 4
859 %cbuff = OpTypeStruct %uint %mat4v4float
860 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
861 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
862 %int = OpTypeInt 32 1
863 %int_0 = OpConstant %int 0
864 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
865 %bool = OpTypeBool
866 %uint_0 = OpConstant %uint 0
867 %29 = OpTypeImage %float 1D 0 0 0 1 Unknown
868 %_ptr_UniformConstant_29 = OpTypePointer UniformConstant %29
869 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_29 UniformConstant
870 %33 = OpTypeSampler
871 %_ptr_UniformConstant_33 = OpTypePointer UniformConstant %33
872 %g_sSamp = OpVariable %_ptr_UniformConstant_33 UniformConstant
873 %37 = OpTypeSampledImage %29
874 %float_0_100000001 = OpConstant %float 0.100000001
875 %float_0 = OpConstant %float 0
876 %43 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
877 %float_2 = OpConstant %float 2
878 %_ptr_Output_v4float = OpTypePointer Output %v4float
879 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
880 %half = OpTypeFloat 16
881 %v4half = OpTypeVector %half 4
882 )";
883
884 const std::string func_before =
885 R"(%main = OpFunction %void None %3
886 %5 = OpLabel
887 %63 = OpAccessChain %_ptr_Uniform_uint %_ %int_0
888 %64 = OpLoad %uint %63
889 %65 = OpINotEqual %bool %64 %uint_0
890 OpSelectionMerge %66 None
891 OpBranchConditional %65 %67 %68
892 %67 = OpLabel
893 %69 = OpLoad %29 %g_tTex1df4
894 %70 = OpLoad %33 %g_sSamp
895 %71 = OpSampledImage %37 %69 %70
896 %72 = OpImageSampleImplicitLod %v4float %71 %float_0_100000001
897 OpBranch %66
898 %68 = OpLabel
899 OpBranch %66
900 %66 = OpLabel
901 %85 = OpPhi %v4float %72 %67 %43 %68
902 %74 = OpVectorTimesScalar %v4float %85 %float_2
903 OpStore %_entryPointOutput_Color %74
904 OpReturn
905 OpFunctionEnd
906 )";
907
908 const std::string func_after =
909 R"(%main = OpFunction %void None %3
910 %5 = OpLabel
911 %63 = OpAccessChain %_ptr_Uniform_uint %_ %int_0
912 %64 = OpLoad %uint %63
913 %65 = OpINotEqual %bool %64 %uint_0
914 OpSelectionMerge %66 None
915 OpBranchConditional %65 %67 %68
916 %67 = OpLabel
917 %69 = OpLoad %29 %g_tTex1df4
918 %70 = OpLoad %33 %g_sSamp
919 %71 = OpSampledImage %37 %69 %70
920 %72 = OpImageSampleImplicitLod %v4float %71 %float_0_100000001
921 %88 = OpFConvert %v4half %72
922 OpBranch %66
923 %68 = OpLabel
924 %89 = OpFConvert %v4half %43
925 OpBranch %66
926 %66 = OpLabel
927 %85 = OpPhi %v4half %88 %67 %89 %68
928 %90 = OpFConvert %half %float_2
929 %74 = OpVectorTimesScalar %v4half %85 %90
930 %91 = OpFConvert %v4float %74
931 OpStore %_entryPointOutput_Color %91
932 OpReturn
933 OpFunctionEnd
934 )";
935
936 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
937 SinglePassRunAndCheck<ConvertToHalfPass>(defs_before + func_before,
938 defs_after + func_after, true, true);
939 }
940
941 TEST_F(ConvertToHalfTest, ConvertToHalfWithLoopAndFConvert) {
942 // The resulting SPIR-V was processed with --relax-float-ops.
943 //
944 // The loop causes an FConvert to be generated at the bottom of the loop
945 // for the Phi. The FConvert is later processed and turned into a (dead)
946 // copy.
947 //
948 // clang-format off
949 //
950 // struct PS_OUTPUT
951 // {
952 // float4 Color : SV_Target0;
953 // };
954 //
955 // cbuffer cbuff{
956 // float4 a[10];
957 // }
958 //
959 // PS_OUTPUT main()
960 // {
961 // PS_OUTPUT psout;
962 // float4 t = 0.0;;
963 //
964 // for (int i = 0; i<10; ++i)
965 // t = t + a[i];
966 //
967 // float4 t2 = t / 10.0;
968 // psout.Color = t2;
969 // return psout;
970 // }
971 //
972 // clang-format on
973
974 const std::string defs_before =
975 R"(OpCapability Shader
976 %1 = OpExtInstImport "GLSL.std.450"
977 OpMemoryModel Logical GLSL450
978 OpEntryPoint Fragment %main "main" %_entryPointOutput_Color
979 OpExecutionMode %main OriginUpperLeft
980 OpSource HLSL 500
981 OpName %main "main"
982 OpName %cbuff "cbuff"
983 OpMemberName %cbuff 0 "a"
984 OpName %_ ""
985 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
986 OpDecorate %_arr_v4float_uint_10 ArrayStride 16
987 OpMemberDecorate %cbuff 0 Offset 0
988 OpDecorate %cbuff Block
989 OpDecorate %_ DescriptorSet 0
990 OpDecorate %_ Binding 0
991 OpDecorate %_entryPointOutput_Color Location 0
992 OpDecorate %96 RelaxedPrecision
993 OpDecorate %81 RelaxedPrecision
994 OpDecorate %75 RelaxedPrecision
995 OpDecorate %76 RelaxedPrecision
996 %void = OpTypeVoid
997 %3 = OpTypeFunction %void
998 %float = OpTypeFloat 32
999 %v4float = OpTypeVector %float 4
1000 %float_0 = OpConstant %float 0
1001 %15 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
1002 %int = OpTypeInt 32 1
1003 %int_0 = OpConstant %int 0
1004 %int_10 = OpConstant %int 10
1005 %bool = OpTypeBool
1006 %uint = OpTypeInt 32 0
1007 %uint_10 = OpConstant %uint 10
1008 %_arr_v4float_uint_10 = OpTypeArray %v4float %uint_10
1009 %cbuff = OpTypeStruct %_arr_v4float_uint_10
1010 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
1011 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
1012 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
1013 %int_1 = OpConstant %int 1
1014 %_ptr_Output_v4float = OpTypePointer Output %v4float
1015 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
1016 %float_0_100000001 = OpConstant %float 0.100000001
1017 %94 = OpConstantComposite %v4float %float_0_100000001 %float_0_100000001 %float_0_100000001 %float_0_100000001
1018 )";
1019
1020 const std::string defs_after =
1021 R"(OpCapability Shader
1022 OpCapability Float16
1023 %1 = OpExtInstImport "GLSL.std.450"
1024 OpMemoryModel Logical GLSL450
1025 OpEntryPoint Fragment %main "main" %_entryPointOutput_Color
1026 OpExecutionMode %main OriginUpperLeft
1027 OpSource HLSL 500
1028 OpName %main "main"
1029 OpName %cbuff "cbuff"
1030 OpMemberName %cbuff 0 "a"
1031 OpName %_ ""
1032 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
1033 OpDecorate %_arr_v4float_uint_10 ArrayStride 16
1034 OpMemberDecorate %cbuff 0 Offset 0
1035 OpDecorate %cbuff Block
1036 OpDecorate %_ DescriptorSet 0
1037 OpDecorate %_ Binding 0
1038 OpDecorate %_entryPointOutput_Color Location 0
1039 %void = OpTypeVoid
1040 %3 = OpTypeFunction %void
1041 %float = OpTypeFloat 32
1042 %v4float = OpTypeVector %float 4
1043 %float_0 = OpConstant %float 0
1044 %15 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
1045 %int = OpTypeInt 32 1
1046 %int_0 = OpConstant %int 0
1047 %int_10 = OpConstant %int 10
1048 %bool = OpTypeBool
1049 %uint = OpTypeInt 32 0
1050 %uint_10 = OpConstant %uint 10
1051 %_arr_v4float_uint_10 = OpTypeArray %v4float %uint_10
1052 %cbuff = OpTypeStruct %_arr_v4float_uint_10
1053 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
1054 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
1055 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
1056 %int_1 = OpConstant %int 1
1057 %_ptr_Output_v4float = OpTypePointer Output %v4float
1058 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
1059 %float_0_100000001 = OpConstant %float 0.100000001
1060 %94 = OpConstantComposite %v4float %float_0_100000001 %float_0_100000001 %float_0_100000001 %float_0_100000001
1061 %half = OpTypeFloat 16
1062 %v4half = OpTypeVector %half 4
1063 )";
1064
1065 const std::string func_before =
1066 R"(%main = OpFunction %void None %3
1067 %5 = OpLabel
1068 OpBranch %65
1069 %65 = OpLabel
1070 %96 = OpPhi %v4float %15 %5 %76 %71
1071 %95 = OpPhi %int %int_0 %5 %78 %71
1072 %70 = OpSLessThan %bool %95 %int_10
1073 OpLoopMerge %66 %71 None
1074 OpBranchConditional %70 %71 %66
1075 %71 = OpLabel
1076 %74 = OpAccessChain %_ptr_Uniform_v4float %_ %int_0 %95
1077 %75 = OpLoad %v4float %74
1078 %76 = OpFAdd %v4float %96 %75
1079 %78 = OpIAdd %int %95 %int_1
1080 OpBranch %65
1081 %66 = OpLabel
1082 %81 = OpFMul %v4float %96 %94
1083 OpStore %_entryPointOutput_Color %81
1084 OpReturn
1085 OpFunctionEnd
1086 )";
1087
1088 const std::string func_after =
1089 R"(%main = OpFunction %void None %3
1090 %5 = OpLabel
1091 %99 = OpFConvert %v4half %15
1092 OpBranch %65
1093 %65 = OpLabel
1094 %96 = OpPhi %v4half %99 %5 %100 %71
1095 %95 = OpPhi %int %int_0 %5 %78 %71
1096 %70 = OpSLessThan %bool %95 %int_10
1097 OpLoopMerge %66 %71 None
1098 OpBranchConditional %70 %71 %66
1099 %71 = OpLabel
1100 %74 = OpAccessChain %_ptr_Uniform_v4float %_ %int_0 %95
1101 %75 = OpLoad %v4float %74
1102 %103 = OpFConvert %v4half %75
1103 %76 = OpFAdd %v4half %96 %103
1104 %78 = OpIAdd %int %95 %int_1
1105 %100 = OpCopyObject %v4half %76
1106 OpBranch %65
1107 %66 = OpLabel
1108 %101 = OpFConvert %v4half %94
1109 %81 = OpFMul %v4half %96 %101
1110 %102 = OpFConvert %v4float %81
1111 OpStore %_entryPointOutput_Color %102
1112 OpReturn
1113 OpFunctionEnd
1114 )";
1115
1116 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1117 SinglePassRunAndCheck<ConvertToHalfPass>(defs_before + func_before,
1118 defs_after + func_after, true, true);
1119 }
1120
1121 TEST_F(ConvertToHalfTest, ConvertToHalfWithExtracts) {
1122 // The resulting SPIR-V was processed with --relax-float-ops.
1123 //
1124 // The extra converts in the func_after can be DCE'd.
1125 //
1126 // clang-format off
1127 //
1128 // SamplerState g_sSamp : register(s0);
1129 // uniform Texture1D <float4> g_tTex1df4 : register(t0);
1130 //
1131 // struct PS_INPUT
1132 // {
1133 // float Tex0 : TEXCOORD0;
1134 // };
1135 //
1136 // struct PS_OUTPUT
1137 // {
1138 // float4 Color : SV_Target0;
1139 // };
1140 //
1141 // cbuffer cbuff{
1142 // float c;
1143 // }
1144 //
1145 // PS_OUTPUT main(PS_INPUT i)
1146 // {
1147 // PS_OUTPUT psout;
1148 // float4 tx = g_tTex1df4.Sample(g_sSamp, i.Tex0);
1149 // float4 t = float4(tx.y, tx.z, tx.x, tx.w) * c;
1150 // psout.Color = t;
1151 // return psout;
1152 // }
1153 //
1154 // clang-format on
1155
1156 const std::string defs_before =
1157 R"(OpCapability Shader
1158 OpCapability Sampled1D
1159 %1 = OpExtInstImport "GLSL.std.450"
1160 OpMemoryModel Logical GLSL450
1161 OpEntryPoint Fragment %main "main" %i_Tex0 %_entryPointOutput_Color
1162 OpExecutionMode %main OriginUpperLeft
1163 OpSource HLSL 500
1164 OpName %main "main"
1165 OpName %g_tTex1df4 "g_tTex1df4"
1166 OpName %g_sSamp "g_sSamp"
1167 OpName %cbuff "cbuff"
1168 OpMemberName %cbuff 0 "c"
1169 OpName %_ ""
1170 OpName %i_Tex0 "i.Tex0"
1171 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
1172 OpDecorate %g_tTex1df4 DescriptorSet 0
1173 OpDecorate %g_tTex1df4 Binding 0
1174 OpDecorate %g_sSamp DescriptorSet 0
1175 OpDecorate %g_sSamp Binding 0
1176 OpMemberDecorate %cbuff 0 Offset 0
1177 OpDecorate %cbuff Block
1178 OpDecorate %_ DescriptorSet 0
1179 OpDecorate %_ Binding 1
1180 OpDecorate %i_Tex0 Location 0
1181 OpDecorate %_entryPointOutput_Color Location 0
1182 OpDecorate %65 RelaxedPrecision
1183 OpDecorate %82 RelaxedPrecision
1184 OpDecorate %84 RelaxedPrecision
1185 OpDecorate %86 RelaxedPrecision
1186 OpDecorate %88 RelaxedPrecision
1187 OpDecorate %90 RelaxedPrecision
1188 OpDecorate %91 RelaxedPrecision
1189 OpDecorate %93 RelaxedPrecision
1190 OpDecorate %94 RelaxedPrecision
1191 %void = OpTypeVoid
1192 %3 = OpTypeFunction %void
1193 %float = OpTypeFloat 32
1194 %v4float = OpTypeVector %float 4
1195 %17 = OpTypeImage %float 1D 0 0 0 1 Unknown
1196 %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
1197 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
1198 %21 = OpTypeSampler
1199 %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
1200 %g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
1201 %25 = OpTypeSampledImage %17
1202 %int = OpTypeInt 32 1
1203 %int_0 = OpConstant %int 0
1204 %cbuff = OpTypeStruct %float
1205 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
1206 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
1207 %_ptr_Uniform_float = OpTypePointer Uniform %float
1208 %_ptr_Input_float = OpTypePointer Input %float
1209 %i_Tex0 = OpVariable %_ptr_Input_float Input
1210 %_ptr_Output_v4float = OpTypePointer Output %v4float
1211 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
1212 )";
1213
1214 const std::string defs_after =
1215 R"(OpCapability Shader
1216 OpCapability Sampled1D
1217 OpCapability Float16
1218 %1 = OpExtInstImport "GLSL.std.450"
1219 OpMemoryModel Logical GLSL450
1220 OpEntryPoint Fragment %main "main" %i_Tex0 %_entryPointOutput_Color
1221 OpExecutionMode %main OriginUpperLeft
1222 OpSource HLSL 500
1223 OpName %main "main"
1224 OpName %g_tTex1df4 "g_tTex1df4"
1225 OpName %g_sSamp "g_sSamp"
1226 OpName %cbuff "cbuff"
1227 OpMemberName %cbuff 0 "c"
1228 OpName %_ ""
1229 OpName %i_Tex0 "i.Tex0"
1230 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
1231 OpDecorate %g_tTex1df4 DescriptorSet 0
1232 OpDecorate %g_tTex1df4 Binding 0
1233 OpDecorate %g_sSamp DescriptorSet 0
1234 OpDecorate %g_sSamp Binding 0
1235 OpMemberDecorate %cbuff 0 Offset 0
1236 OpDecorate %cbuff Block
1237 OpDecorate %_ DescriptorSet 0
1238 OpDecorate %_ Binding 1
1239 OpDecorate %i_Tex0 Location 0
1240 OpDecorate %_entryPointOutput_Color Location 0
1241 %void = OpTypeVoid
1242 %3 = OpTypeFunction %void
1243 %float = OpTypeFloat 32
1244 %v4float = OpTypeVector %float 4
1245 %17 = OpTypeImage %float 1D 0 0 0 1 Unknown
1246 %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
1247 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
1248 %21 = OpTypeSampler
1249 %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
1250 %g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
1251 %25 = OpTypeSampledImage %17
1252 %int = OpTypeInt 32 1
1253 %int_0 = OpConstant %int 0
1254 %cbuff = OpTypeStruct %float
1255 %_ptr_Uniform_cbuff = OpTypePointer Uniform %cbuff
1256 %_ = OpVariable %_ptr_Uniform_cbuff Uniform
1257 %_ptr_Uniform_float = OpTypePointer Uniform %float
1258 %_ptr_Input_float = OpTypePointer Input %float
1259 %i_Tex0 = OpVariable %_ptr_Input_float Input
1260 %_ptr_Output_v4float = OpTypePointer Output %v4float
1261 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
1262 %half = OpTypeFloat 16
1263 %v4half = OpTypeVector %half 4
1264 )";
1265
1266 const std::string func_before =
1267 R"(%main = OpFunction %void None %3
1268 %5 = OpLabel
1269 %65 = OpLoad %float %i_Tex0
1270 %77 = OpLoad %17 %g_tTex1df4
1271 %78 = OpLoad %21 %g_sSamp
1272 %79 = OpSampledImage %25 %77 %78
1273 %82 = OpImageSampleImplicitLod %v4float %79 %65
1274 %84 = OpCompositeExtract %float %82 1
1275 %86 = OpCompositeExtract %float %82 2
1276 %88 = OpCompositeExtract %float %82 0
1277 %90 = OpCompositeExtract %float %82 3
1278 %91 = OpCompositeConstruct %v4float %84 %86 %88 %90
1279 %92 = OpAccessChain %_ptr_Uniform_float %_ %int_0
1280 %93 = OpLoad %float %92
1281 %94 = OpVectorTimesScalar %v4float %91 %93
1282 OpStore %_entryPointOutput_Color %94
1283 OpReturn
1284 OpFunctionEnd
1285 )";
1286
1287 const std::string func_after =
1288 R"(%main = OpFunction %void None %3
1289 %5 = OpLabel
1290 %65 = OpLoad %float %i_Tex0
1291 %77 = OpLoad %17 %g_tTex1df4
1292 %78 = OpLoad %21 %g_sSamp
1293 %79 = OpSampledImage %25 %77 %78
1294 %82 = OpImageSampleImplicitLod %v4float %79 %65
1295 %97 = OpFConvert %v4half %82
1296 %84 = OpCompositeExtract %half %97 1
1297 %98 = OpFConvert %v4half %82
1298 %86 = OpCompositeExtract %half %98 2
1299 %99 = OpFConvert %v4half %82
1300 %88 = OpCompositeExtract %half %99 0
1301 %100 = OpFConvert %v4half %82
1302 %90 = OpCompositeExtract %half %100 3
1303 %91 = OpCompositeConstruct %v4half %84 %86 %88 %90
1304 %92 = OpAccessChain %_ptr_Uniform_float %_ %int_0
1305 %93 = OpLoad %float %92
1306 %101 = OpFConvert %half %93
1307 %94 = OpVectorTimesScalar %v4half %91 %101
1308 %102 = OpFConvert %v4float %94
1309 OpStore %_entryPointOutput_Color %102
1310 OpReturn
1311 OpFunctionEnd
1312 )";
1313
1314 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1315 SinglePassRunAndCheck<ConvertToHalfPass>(defs_before + func_before,
1316 defs_after + func_after, true, true);
1317 }
1318
1319 TEST_F(ConvertToHalfTest, ConvertToHalfWithClosure) {
1320 // Include as many contiguous composite instructions as possible into
1321 // half-precision computations
1322 //
1323 // Compiled with glslang -V -Os
1324 //
1325 // clang-format off
1326 //
1327 // #version 410 core
1328 //
1329 // precision mediump float;
1330 //
1331 // layout(location = 1) in vec3 foo;
1332 // layout(location = 2) in mat2 bar;
1333 // layout(location = 1) out vec3 res;
1334 //
1335 // vec3 func(vec3 tap, mat2 M) {
1336 // return vec3(M * tap.xy, 1.0);
1337 // }
1338 //
1339 // void main() {
1340 // res = func(foo, bar);
1341 // }
1342 //
1343 // clang-format on
1344
1345 const std::string defs =
1346 R"(OpCapability Shader
1347 ; CHECK: OpCapability Float16
1348 %1 = OpExtInstImport "GLSL.std.450"
1349 OpMemoryModel Logical GLSL450
1350 OpEntryPoint Fragment %main "main" %res %foo %bar
1351 OpExecutionMode %main OriginUpperLeft
1352 OpSource GLSL 410
1353 OpName %main "main"
1354 OpName %res "res"
1355 OpName %foo "foo"
1356 OpName %bar "bar"
1357 OpDecorate %res RelaxedPrecision
1358 ; CHECK-NOT: OpDecorate %res RelaxedPrecision
1359 OpDecorate %res Location 1
1360 OpDecorate %foo RelaxedPrecision
1361 ; CHECK-NOT: OpDecorate %foo RelaxedPrecision
1362 OpDecorate %foo Location 1
1363 OpDecorate %bar RelaxedPrecision
1364 ; CHECK-NOT: OpDecorate %bar RelaxedPrecision
1365 OpDecorate %bar Location 2
1366 OpDecorate %34 RelaxedPrecision
1367 OpDecorate %36 RelaxedPrecision
1368 OpDecorate %41 RelaxedPrecision
1369 OpDecorate %42 RelaxedPrecision
1370 ; CHECK-NOT: OpDecorate %34 RelaxedPrecision
1371 ; CHECK-NOT: OpDecorate %36 RelaxedPrecision
1372 ; CHECK-NOT: OpDecorate %41 RelaxedPrecision
1373 ; CHECK-NOT: OpDecorate %42 RelaxedPrecision
1374 %void = OpTypeVoid
1375 %3 = OpTypeFunction %void
1376 %float = OpTypeFloat 32
1377 %v3float = OpTypeVector %float 3
1378 %v2float = OpTypeVector %float 2
1379 %mat2v2float = OpTypeMatrix %v2float 2
1380 %float_1 = OpConstant %float 1
1381 %_ptr_Output_v3float = OpTypePointer Output %v3float
1382 %res = OpVariable %_ptr_Output_v3float Output
1383 %_ptr_Input_v3float = OpTypePointer Input %v3float
1384 %foo = OpVariable %_ptr_Input_v3float Input
1385 %_ptr_Input_mat2v2float = OpTypePointer Input %mat2v2float
1386 %bar = OpVariable %_ptr_Input_mat2v2float Input
1387 )";
1388
1389 const std::string func =
1390 R"(%main = OpFunction %void None %3
1391 %5 = OpLabel
1392 %34 = OpLoad %v3float %foo
1393 %36 = OpLoad %mat2v2float %bar
1394 ; CHECK: %48 = OpFConvert %v3half %34
1395 ; CHECK: %49 = OpFConvert %v3half %34
1396 %41 = OpVectorShuffle %v2float %34 %34 0 1
1397 ; CHECK-NOT: %41 = OpVectorShuffle %v2float %34 %34 0 1
1398 ; CHECK: %41 = OpVectorShuffle %v2half %48 %49 0 1
1399 %42 = OpMatrixTimesVector %v2float %36 %41
1400 ; CHECK-NOT: %42 = OpMatrixTimesVector %v2float %36 %41
1401 ; CHECK: %55 = OpCompositeExtract %v2float %36 0
1402 ; CHECK: %56 = OpFConvert %v2half %55
1403 ; CHECK: %57 = OpCompositeExtract %v2float %36 1
1404 ; CHECK: %58 = OpFConvert %v2half %57
1405 ; CHECK: %59 = OpCompositeConstruct %mat2v2half %56 %58
1406 ; CHECK: %52 = OpCopyObject %mat2v2float %36
1407 ; CHECK: %42 = OpMatrixTimesVector %v2half %59 %41
1408 %43 = OpCompositeExtract %float %42 0
1409 %44 = OpCompositeExtract %float %42 1
1410 ; CHECK-NOT: %43 = OpCompositeExtract %float %42 0
1411 ; CHECK-NOT: %44 = OpCompositeExtract %float %42 1
1412 ; CHECK: %43 = OpCompositeExtract %half %42 0
1413 ; CHECK: %44 = OpCompositeExtract %half %42 1
1414 %45 = OpCompositeConstruct %v3float %43 %44 %float_1
1415 ; CHECK-NOT: %45 = OpCompositeConstruct %v3float %43 %44 %float_1
1416 ; CHECK: %53 = OpFConvert %float %43
1417 ; CHECK: %54 = OpFConvert %float %44
1418 ; CHECK: %45 = OpCompositeConstruct %v3float %53 %54 %float_1
1419 OpStore %res %45
1420 OpReturn
1421 OpFunctionEnd
1422 )";
1423
1424 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1425 SinglePassRunAndMatch<ConvertToHalfPass>(defs + func, true);
1426 }
1427
1428 TEST_F(ConvertToHalfTest, RemoveRelaxDec) {
1429 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/4117
1430
1431 // This test is a case where the relax precision decorations need to be
1432 // removed, but the body of the function does not change because there are not
1433 // arithmetic operations. So, there is not need for the Float16 capability.
1434 const std::string test =
1435 R"(
1436 ; CHECK-NOT: OpCapability Float16
1437 ; GLSL seems to generate this decoration on the load of a texture, which seems odd to me.
1438 ; This pass does not currently remove it, and I'm not sure what we should do with it, so I will leave it.
1439 ; CHECK: OpDecorate [[tex:%\w+]] RelaxedPrecision
1440 ; CHECK-NOT: OpDecorate {{%\w+}} RelaxedPrecision
1441 ; CHECK: OpLabel
1442 ; CHECK: [[tex]] = OpLoad {{%\w+}} %sTexture
1443 ; CHECK: [[coord:%\w+]] = OpLoad %v2float
1444 ; CHECK: [[retval:%\w+]] = OpImageSampleImplicitLod %v4float {{%\w+}} [[coord]]
1445 ; CHECK: OpStore %outFragColor [[retval]]
1446 OpCapability Shader
1447 %1 = OpExtInstImport "GLSL.std.450"
1448 OpMemoryModel Logical GLSL450
1449 OpEntryPoint Fragment %main "main" %outFragColor %v_texcoord
1450 OpExecutionMode %main OriginUpperLeft
1451 OpSource ESSL 310
1452 OpName %main "main"
1453 OpName %outFragColor "outFragColor"
1454 OpName %sTexture "sTexture"
1455 OpName %v_texcoord "v_texcoord"
1456 OpDecorate %outFragColor RelaxedPrecision
1457 OpDecorate %outFragColor Location 0
1458 OpDecorate %sTexture RelaxedPrecision
1459 OpDecorate %sTexture DescriptorSet 0
1460 OpDecorate %sTexture Binding 0
1461 OpDecorate %14 RelaxedPrecision
1462 OpDecorate %v_texcoord RelaxedPrecision
1463 OpDecorate %v_texcoord Location 0
1464 OpDecorate %18 RelaxedPrecision
1465 OpDecorate %19 RelaxedPrecision
1466 %void = OpTypeVoid
1467 %3 = OpTypeFunction %void
1468 %float = OpTypeFloat 32
1469 %v4float = OpTypeVector %float 4
1470 %_ptr_Output_v4float = OpTypePointer Output %v4float
1471 %outFragColor = OpVariable %_ptr_Output_v4float Output
1472 %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
1473 %11 = OpTypeSampledImage %10
1474 %_ptr_UniformConstant_11 = OpTypePointer UniformConstant %11
1475 %sTexture = OpVariable %_ptr_UniformConstant_11 UniformConstant
1476 %v2float = OpTypeVector %float 2
1477 %_ptr_Input_v2float = OpTypePointer Input %v2float
1478 %v_texcoord = OpVariable %_ptr_Input_v2float Input
1479 %main = OpFunction %void None %3
1480 %5 = OpLabel
1481 %14 = OpLoad %11 %sTexture
1482 %18 = OpLoad %v2float %v_texcoord
1483 %19 = OpImageSampleImplicitLod %v4float %14 %18
1484 OpStore %outFragColor %19
1485 OpReturn
1486 OpFunctionEnd
1487 )";
1488
1489 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1490 auto result = SinglePassRunAndMatch<ConvertToHalfPass>(test, true);
1491 EXPECT_EQ(Pass::Status::SuccessWithChange, std::get<1>(result));
1492 }
1493
1494 TEST_F(ConvertToHalfTest, HandleNonRelaxedPhi) {
1495 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/4452
1496
1497 // This test is a case with a non-relaxed phi with a relaxed operand.
1498 // A convert must be inserted at the end of the block associated with
1499 // the operand.
1500 const std::string test =
1501 R"(
1502 ; CHECK: [[fcvt:%\w+]] = OpFConvert %v3float {{%\w+}}
1503 ; CHECK-NEXT: OpSelectionMerge {{%\w+}} None
1504 ; CHECK: {{%\w+}} = OpPhi %v3float [[fcvt]] {{%\w+}} {{%\w+}} {{%\w+}}
1505 OpCapability Shader
1506 %1 = OpExtInstImport "GLSL.std.450"
1507 OpMemoryModel Logical GLSL450
1508 OpEntryPoint Fragment %main "main" %output_color
1509 OpExecutionMode %main OriginUpperLeft
1510 OpSource GLSL 450
1511 OpName %main "main"
1512 OpName %MaterialParams "MaterialParams"
1513 OpMemberName %MaterialParams 0 "foo"
1514 OpName %materialParams "materialParams"
1515 OpName %output_color "output_color"
1516 OpMemberDecorate %MaterialParams 0 Offset 0
1517 OpDecorate %MaterialParams Block
1518 OpDecorate %materialParams DescriptorSet 0
1519 OpDecorate %materialParams Binding 5
1520 OpDecorate %output_color Location 0
1521 OpDecorate %57 RelaxedPrecision
1522 %void = OpTypeVoid
1523 %3 = OpTypeFunction %void
1524 %float = OpTypeFloat 32
1525 %v3float = OpTypeVector %float 3
1526 %MaterialParams = OpTypeStruct %float
1527 %_ptr_Uniform_MaterialParams = OpTypePointer Uniform %MaterialParams
1528 %materialParams = OpVariable %_ptr_Uniform_MaterialParams Uniform
1529 %int = OpTypeInt 32 1
1530 %int_0 = OpConstant %int 0
1531 %_ptr_Uniform_float = OpTypePointer Uniform %float
1532 %float_0 = OpConstant %float 0
1533 %bool = OpTypeBool
1534 %v4float = OpTypeVector %float 4
1535 %_ptr_Output_v4float = OpTypePointer Output %v4float
1536 %output_color = OpVariable %_ptr_Output_v4float Output
1537 %uint = OpTypeInt 32 0
1538 %uint_0 = OpConstant %uint 0
1539 %_ptr_Output_float = OpTypePointer Output %float
1540 %uint_1 = OpConstant %uint 1
1541 %uint_2 = OpConstant %uint 2
1542 %float_0_5 = OpConstant %float 0.5
1543 %61 = OpConstantComposite %v3float %float_0_5 %float_0_5 %float_0_5
1544 %main = OpFunction %void None %3
1545 %5 = OpLabel
1546 %55 = OpAccessChain %_ptr_Uniform_float %materialParams %int_0
1547 %56 = OpLoad %float %55
1548 %57 = OpCompositeConstruct %v3float %56 %56 %56
1549 %31 = OpFOrdGreaterThan %bool %56 %float_0
1550 OpSelectionMerge %33 None
1551 OpBranchConditional %31 %32 %33
1552 %32 = OpLabel
1553 %37 = OpFMul %v3float %57 %61
1554 OpBranch %33
1555 %33 = OpLabel
1556 %58 = OpPhi %v3float %57 %5 %37 %32
1557 %45 = OpAccessChain %_ptr_Output_float %output_color %uint_0
1558 %46 = OpCompositeExtract %float %58 0
1559 OpStore %45 %46
1560 %48 = OpAccessChain %_ptr_Output_float %output_color %uint_1
1561 %49 = OpCompositeExtract %float %58 1
1562 OpStore %48 %49
1563 %51 = OpAccessChain %_ptr_Output_float %output_color %uint_2
1564 %52 = OpCompositeExtract %float %58 2
1565 OpStore %51 %52
1566 OpReturn
1567 OpFunctionEnd
1568 )";
1569
1570 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1571 auto result = SinglePassRunAndMatch<ConvertToHalfPass>(test, true);
1572 EXPECT_EQ(Pass::Status::SuccessWithChange, std::get<1>(result));
1573 }
1574
1575 TEST_F(ConvertToHalfTest, DoNotReplaceStructMember) {
1576 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/4814
1577
1578 // This test is a case with a non-relaxed phi with a relaxed operand.
1579 // A convert must be inserted at the end of the block associated with
1580 // the operand.
1581 const std::string test =
1582 R"(OpCapability Shader
1583 OpMemoryModel Logical GLSL450
1584 OpEntryPoint Fragment %PSMain "PSMain" %out_var_SV_TARGET %MyConstants
1585 OpExecutionMode %PSMain OriginUpperLeft
1586 OpSource HLSL 600
1587 OpName %type_ConstantBuffer_myStruct "type.ConstantBuffer.myStruct"
1588 OpMemberName %type_ConstantBuffer_myStruct 0 "f"
1589 OpName %MyConstants "MyConstants"
1590 OpName %out_var_SV_TARGET "out.var.SV_TARGET"
1591 OpName %PSMain "PSMain"
1592 OpDecorate %out_var_SV_TARGET Location 0
1593 OpDecorate %MyConstants DescriptorSet 1
1594 OpDecorate %MyConstants Binding 2
1595 OpMemberDecorate %type_ConstantBuffer_myStruct 0 Offset 0
1596 OpDecorate %type_ConstantBuffer_myStruct Block
1597 %float = OpTypeFloat 32
1598 %type_ConstantBuffer_myStruct = OpTypeStruct %float
1599 %_ptr_Uniform_type_ConstantBuffer_myStruct = OpTypePointer Uniform %type_ConstantBuffer_myStruct
1600 %_ptr_Output_float = OpTypePointer Output %float
1601 %void = OpTypeVoid
1602 %9 = OpTypeFunction %void
1603 %MyConstants = OpVariable %_ptr_Uniform_type_ConstantBuffer_myStruct Uniform
1604 %out_var_SV_TARGET = OpVariable %_ptr_Output_float Output
1605 %PSMain = OpFunction %void None %9
1606 %10 = OpLabel
1607 %11 = OpLoad %type_ConstantBuffer_myStruct %MyConstants
1608 %12 = OpCompositeExtract %float %11 0
1609 OpStore %out_var_SV_TARGET %12
1610 OpReturn
1611 OpFunctionEnd
1612 )";
1613
1614 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1615 SinglePassRunAndCheck<ConvertToHalfPass>(test, test, true);
1616 }
1617
1618 TEST_F(ConvertToHalfTest, PreserveImageOperandPrecision) {
1619 // Ensure that a non-relaxed texture coordinate does not get relaxed nor
1620 // converted to half precision if the image instruction is marked relaxed.
1621 // Update - coordinates are not allowed to be 16-bit regardless
1622
1623 // Also ensure that a relaxed local variable does get converted to half
1624 // precision before being passed to an image opeartor.
1625
1626 // #version 310 es
1627 //
1628 // precision mediump float;
1629 //
1630 // layout(location = 10) in highp vec4 vertex_uv01;
1631 // layout(binding = 0, set = 3) uniform sampler2D materialParams_baseColorMap;
1632 //
1633 // layout(location = 0) out vec4 fragColor;
1634 //
1635 // void main() {
1636 // vec4 uv = vec4(2.0);
1637 // fragColor = texture(materialParams_baseColorMap, uv.xy);
1638 // fragColor = texture(materialParams_baseColorMap, vertex_uv01.xy);
1639 // }
1640 const std::string test = R"(
1641 OpCapability Shader
1642 OpCapability Float16
1643 %1 = OpExtInstImport "GLSL.std.450"
1644 OpMemoryModel Logical GLSL450
1645 OpEntryPoint Fragment %4 "main" %13 %26
1646 OpExecutionMode %4 OriginUpperLeft
1647 OpSource ESSL 310
1648 OpDecorate %9 RelaxedPrecision
1649 ;CHECK: OpDecorate [[uv:%\w+]] RelaxedPrecision
1650 OpDecorate %13 Location 0
1651 OpDecorate %17 DescriptorSet 3
1652 OpDecorate %17 Binding 0
1653 OpDecorate %18 RelaxedPrecision
1654 OpDecorate %24 RelaxedPrecision
1655 OpDecorate %26 Location 10
1656 %2 = OpTypeVoid
1657 %3 = OpTypeFunction %2
1658 %6 = OpTypeFloat 32
1659 ;CHECK: [[float32_t:%\w+]] = OpTypeFloat 32
1660 %7 = OpTypeVector %6 4
1661 ;CHECK: [[vec4_t:%\w+]] = OpTypeVector [[float32_t]] 4
1662 %8 = OpTypePointer Function %7
1663 %10 = OpConstant %6 2
1664 %11 = OpConstantComposite %7 %10 %10 %10 %10
1665 %12 = OpTypePointer Output %7
1666 ;CHECK: [[output_ptr_t:%\w+]] = OpTypePointer Output [[vec4_t]]
1667 %13 = OpVariable %12 Output
1668 ;CHECK: [[output:%\w+]] = OpVariable [[output_ptr_t]] Output
1669 %14 = OpTypeImage %6 2D 0 0 0 1 Unknown
1670 %15 = OpTypeSampledImage %14
1671 %16 = OpTypePointer UniformConstant %15
1672 %17 = OpVariable %16 UniformConstant
1673 %19 = OpTypeVector %6 2
1674 ;CHECK: [[vec2_t:%\w+]] = OpTypeVector [[float32_t]] 2
1675 %25 = OpTypePointer Input %7
1676 ;CHECK: [[input_ptr_t:%\w+]] = OpTypePointer Input [[vec4_t]]
1677 %26 = OpVariable %25 Input
1678 %30 = OpTypeFloat 16
1679 ;CHECK: [[float16_t:%\w+]] = OpTypeFloat 16
1680 %31 = OpTypeVector %30 4
1681 %34 = OpTypeVector %30 2
1682 ;CHECK: [[vec2_16b_t:%\w+]] = OpTypeVector [[float16_t]] 2
1683 %4 = OpFunction %2 None %3
1684 %5 = OpLabel
1685
1686 ; The only Function storage variable is marked as relaxed
1687 %9 = OpVariable %8 Function
1688 ;CHECK: [[uv]] = OpVariable {{%\w+}} Function
1689 OpStore %9 %11
1690 %18 = OpLoad %15 %17
1691 %20 = OpLoad %7 %9
1692 %32 = OpFConvert %31 %20
1693 %33 = OpFConvert %31 %20
1694
1695 ; The first sample op should get a 32b coordinate, even if relaxed
1696 %21 = OpVectorShuffle %34 %32 %33 0 1
1697 ;CHECK: [[uv_16b:%\w+]] = OpVectorShuffle [[vec2_16b_t]]
1698 %22 = OpFConvert %19 %21
1699 %23 = OpImageSampleImplicitLod %7 %18 %22
1700 ;CHECK: OpImageSampleImplicitLod [[vec4_t]] {{%\w+}} {{%\w+}}
1701
1702 OpStore %13 %23
1703 %24 = OpLoad %15 %17
1704 %27 = OpLoad %7 %26
1705
1706 ; The second sample op should get a 32b coordinate
1707 %28 = OpVectorShuffle %19 %27 %27 0 1
1708 ;CHECK: [[uv_32b:%\w+]] = OpVectorShuffle [[vec2_t]]
1709 %29 = OpImageSampleImplicitLod %7 %24 %28
1710 ;CHECK: OpImageSampleImplicitLod [[vec4_t]] {{%\w+}} [[uv_32b]]
1711
1712 OpStore %13 %29
1713 OpReturn
1714 OpFunctionEnd
1715 )";
1716
1717 SinglePassRunAndMatch<ConvertToHalfPass>(test, true);
1718 }
1719
1720 TEST_F(ConvertToHalfTest, DontRelaxDecoratedOpCompositeExtract) {
1721 // This test checks that a OpCompositeExtract with a Struct operand won't be
1722 // relaxed, even if it is explicitly decorated with RelaxedPrecision.
1723 const std::string test =
1724 R"(OpCapability Shader
1725 OpMemoryModel Logical GLSL450
1726 OpEntryPoint Fragment %1 "main"
1727 OpExecutionMode %1 OriginUpperLeft
1728 OpDecorate %9 RelaxedPrecision
1729 %void = OpTypeVoid
1730 %3 = OpTypeFunction %void
1731 %float = OpTypeFloat 32
1732 %v4float = OpTypeVector %float 4
1733 %_struct_6 = OpTypeStruct %v4float
1734 %7 = OpUndef %_struct_6
1735 %1 = OpFunction %void None %3
1736 %8 = OpLabel
1737 %9 = OpCompositeExtract %float %7 0 3
1738 OpReturn
1739 OpFunctionEnd
1740 )";
1741
1742 const std::string expected =
1743 R"(OpCapability Shader
1744 OpMemoryModel Logical GLSL450
1745 OpEntryPoint Fragment %1 "main"
1746 OpExecutionMode %1 OriginUpperLeft
1747 %void = OpTypeVoid
1748 %3 = OpTypeFunction %void
1749 %float = OpTypeFloat 32
1750 %v4float = OpTypeVector %float 4
1751 %_struct_6 = OpTypeStruct %v4float
1752 %7 = OpUndef %_struct_6
1753 %1 = OpFunction %void None %3
1754 %8 = OpLabel
1755 %9 = OpCompositeExtract %float %7 0 3
1756 OpReturn
1757 OpFunctionEnd
1758 )";
1759
1760 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1761 SinglePassRunAndCheck<ConvertToHalfPass>(test, expected, true);
1762 }
1763
1764 TEST_F(ConvertToHalfTest, DontRelaxOpCompositeExtract) {
1765 // This test checks that a OpCompositeExtract with a Struct operand won't be
1766 // relaxed, even if its result has no uses.
1767 const std::string test =
1768 R"(OpCapability Shader
1769 OpMemoryModel Logical GLSL450
1770 OpEntryPoint Fragment %1 "main"
1771 OpExecutionMode %1 OriginUpperLeft
1772 %void = OpTypeVoid
1773 %3 = OpTypeFunction %void
1774 %float = OpTypeFloat 32
1775 %v4float = OpTypeVector %float 4
1776 %_struct_6 = OpTypeStruct %v4float
1777 %7 = OpUndef %_struct_6
1778 %1 = OpFunction %void None %3
1779 %8 = OpLabel
1780 %9 = OpCompositeExtract %float %7 0 3
1781 OpReturn
1782 OpFunctionEnd
1783 )";
1784
1785 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1786 SinglePassRunAndCheck<ConvertToHalfPass>(test, test, true);
1787 }
1788
1789 } // namespace
1790 } // namespace opt
1791 } // namespace spvtools