1fd4e5da5Sopenharmony_ci// Copyright (c) 2019 Valve Corporation
2fd4e5da5Sopenharmony_ci// Copyright (c) 2019 LunarG Inc.
3fd4e5da5Sopenharmony_ci//
4fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
5fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
6fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
7fd4e5da5Sopenharmony_ci//
8fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
9fd4e5da5Sopenharmony_ci//
10fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
11fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
12fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
14fd4e5da5Sopenharmony_ci// limitations under the License.
15fd4e5da5Sopenharmony_ci
16fd4e5da5Sopenharmony_ci// Relax float ops tests
17fd4e5da5Sopenharmony_ci
18fd4e5da5Sopenharmony_ci#include <string>
19fd4e5da5Sopenharmony_ci#include <vector>
20fd4e5da5Sopenharmony_ci
21fd4e5da5Sopenharmony_ci#include "test/opt/pass_fixture.h"
22fd4e5da5Sopenharmony_ci#include "test/opt/pass_utils.h"
23fd4e5da5Sopenharmony_ci
24fd4e5da5Sopenharmony_cinamespace spvtools {
25fd4e5da5Sopenharmony_cinamespace opt {
26fd4e5da5Sopenharmony_cinamespace {
27fd4e5da5Sopenharmony_ci
28fd4e5da5Sopenharmony_ciusing RelaxFloatOpsTest = PassTest<::testing::Test>;
29fd4e5da5Sopenharmony_ci
30fd4e5da5Sopenharmony_ciTEST_F(RelaxFloatOpsTest, RelaxFloatOpsBasic) {
31fd4e5da5Sopenharmony_ci  // All float result instructions in functions should be relaxed
32fd4e5da5Sopenharmony_ci  // clang-format off
33fd4e5da5Sopenharmony_ci  //
34fd4e5da5Sopenharmony_ci  // SamplerState       g_sSamp : register(s0);
35fd4e5da5Sopenharmony_ci  // uniform Texture1D <float4> g_tTex1df4 : register(t0);
36fd4e5da5Sopenharmony_ci  //
37fd4e5da5Sopenharmony_ci  // struct PS_INPUT
38fd4e5da5Sopenharmony_ci  // {
39fd4e5da5Sopenharmony_ci  //   float Tex0 : TEXCOORD0;
40fd4e5da5Sopenharmony_ci  //   float Tex1 : TEXCOORD1;
41fd4e5da5Sopenharmony_ci  // };
42fd4e5da5Sopenharmony_ci  //
43fd4e5da5Sopenharmony_ci  // struct PS_OUTPUT
44fd4e5da5Sopenharmony_ci  // {
45fd4e5da5Sopenharmony_ci  //   float4 Color : SV_Target0;
46fd4e5da5Sopenharmony_ci  // };
47fd4e5da5Sopenharmony_ci  //
48fd4e5da5Sopenharmony_ci  // PS_OUTPUT main(PS_INPUT i)
49fd4e5da5Sopenharmony_ci  // {
50fd4e5da5Sopenharmony_ci  //   PS_OUTPUT psout;
51fd4e5da5Sopenharmony_ci  //   float4 txval10 = g_tTex1df4.Sample(g_sSamp, i.Tex0);
52fd4e5da5Sopenharmony_ci  //   float4 txval11 = g_tTex1df4.Sample(g_sSamp, i.Tex1);
53fd4e5da5Sopenharmony_ci  //   float4 t = txval10 + txval11;
54fd4e5da5Sopenharmony_ci  //   float4 t2 = t / 2.0;
55fd4e5da5Sopenharmony_ci  //   psout.Color = t2;
56fd4e5da5Sopenharmony_ci  //   return psout;
57fd4e5da5Sopenharmony_ci  // }
58fd4e5da5Sopenharmony_ci  // clang-format on
59fd4e5da5Sopenharmony_ci
60fd4e5da5Sopenharmony_ci  const std::string defs0 =
61fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
62fd4e5da5Sopenharmony_ciOpCapability Sampled1D
63fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
64fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
65fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %i_Tex0 %i_Tex1 %_entryPointOutput_Color
66fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
67fd4e5da5Sopenharmony_ciOpSource HLSL 500
68fd4e5da5Sopenharmony_ciOpName %main "main"
69fd4e5da5Sopenharmony_ciOpName %g_tTex1df4 "g_tTex1df4"
70fd4e5da5Sopenharmony_ciOpName %g_sSamp "g_sSamp"
71fd4e5da5Sopenharmony_ciOpName %i_Tex0 "i.Tex0"
72fd4e5da5Sopenharmony_ciOpName %i_Tex1 "i.Tex1"
73fd4e5da5Sopenharmony_ciOpName %_entryPointOutput_Color "@entryPointOutput.Color"
74fd4e5da5Sopenharmony_ciOpDecorate %g_tTex1df4 DescriptorSet 0
75fd4e5da5Sopenharmony_ciOpDecorate %g_tTex1df4 Binding 0
76fd4e5da5Sopenharmony_ciOpDecorate %g_sSamp DescriptorSet 0
77fd4e5da5Sopenharmony_ciOpDecorate %g_sSamp Binding 0
78fd4e5da5Sopenharmony_ciOpDecorate %i_Tex0 Location 0
79fd4e5da5Sopenharmony_ciOpDecorate %i_Tex1 Location 1
80fd4e5da5Sopenharmony_ciOpDecorate %_entryPointOutput_Color Location 0
81fd4e5da5Sopenharmony_ci)";
82fd4e5da5Sopenharmony_ci
83fd4e5da5Sopenharmony_ci  const std::string defs1 =
84fd4e5da5Sopenharmony_ci      R"(%void = OpTypeVoid
85fd4e5da5Sopenharmony_ci%3 = OpTypeFunction %void
86fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
87fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
88fd4e5da5Sopenharmony_ci%17 = OpTypeImage %float 1D 0 0 0 1 Unknown
89fd4e5da5Sopenharmony_ci%_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
90fd4e5da5Sopenharmony_ci%g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
91fd4e5da5Sopenharmony_ci%21 = OpTypeSampler
92fd4e5da5Sopenharmony_ci%_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
93fd4e5da5Sopenharmony_ci%g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
94fd4e5da5Sopenharmony_ci%25 = OpTypeSampledImage %17
95fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
96fd4e5da5Sopenharmony_ci%i_Tex0 = OpVariable %_ptr_Input_float Input
97fd4e5da5Sopenharmony_ci%i_Tex1 = OpVariable %_ptr_Input_float Input
98fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
99fd4e5da5Sopenharmony_ci%_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
100fd4e5da5Sopenharmony_ci%float_0_5 = OpConstant %float 0.5
101fd4e5da5Sopenharmony_ci%116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
102fd4e5da5Sopenharmony_ci)";
103fd4e5da5Sopenharmony_ci
104fd4e5da5Sopenharmony_ci  const std::string relax_decos =
105fd4e5da5Sopenharmony_ci      R"(OpDecorate %60 RelaxedPrecision
106fd4e5da5Sopenharmony_ciOpDecorate %63 RelaxedPrecision
107fd4e5da5Sopenharmony_ciOpDecorate %82 RelaxedPrecision
108fd4e5da5Sopenharmony_ciOpDecorate %88 RelaxedPrecision
109fd4e5da5Sopenharmony_ciOpDecorate %91 RelaxedPrecision
110fd4e5da5Sopenharmony_ciOpDecorate %94 RelaxedPrecision
111fd4e5da5Sopenharmony_ci)";
112fd4e5da5Sopenharmony_ci
113fd4e5da5Sopenharmony_ci  const std::string func_orig =
114fd4e5da5Sopenharmony_ci      R"(%main = OpFunction %void None %3
115fd4e5da5Sopenharmony_ci%5 = OpLabel
116fd4e5da5Sopenharmony_ci%60 = OpLoad %float %i_Tex0
117fd4e5da5Sopenharmony_ci%63 = OpLoad %float %i_Tex1
118fd4e5da5Sopenharmony_ci%77 = OpLoad %17 %g_tTex1df4
119fd4e5da5Sopenharmony_ci%78 = OpLoad %21 %g_sSamp
120fd4e5da5Sopenharmony_ci%79 = OpSampledImage %25 %77 %78
121fd4e5da5Sopenharmony_ci%82 = OpImageSampleImplicitLod %v4float %79 %60
122fd4e5da5Sopenharmony_ci%83 = OpLoad %17 %g_tTex1df4
123fd4e5da5Sopenharmony_ci%84 = OpLoad %21 %g_sSamp
124fd4e5da5Sopenharmony_ci%85 = OpSampledImage %25 %83 %84
125fd4e5da5Sopenharmony_ci%88 = OpImageSampleImplicitLod %v4float %85 %63
126fd4e5da5Sopenharmony_ci%91 = OpFAdd %v4float %82 %88
127fd4e5da5Sopenharmony_ci%94 = OpFMul %v4float %91 %116
128fd4e5da5Sopenharmony_ciOpStore %_entryPointOutput_Color %94
129fd4e5da5Sopenharmony_ciOpReturn
130fd4e5da5Sopenharmony_ciOpFunctionEnd
131fd4e5da5Sopenharmony_ci)";
132fd4e5da5Sopenharmony_ci
133fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
134fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<RelaxFloatOpsPass>(
135fd4e5da5Sopenharmony_ci      defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
136fd4e5da5Sopenharmony_ci      true);
137fd4e5da5Sopenharmony_ci}
138fd4e5da5Sopenharmony_ci
139fd4e5da5Sopenharmony_ciTEST_F(RelaxFloatOpsTest, RelaxFloatOpsForLinkage) {
140fd4e5da5Sopenharmony_ci  const std::string defs0 =
141fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
142fd4e5da5Sopenharmony_ciOpCapability Linkage
143fd4e5da5Sopenharmony_ciOpCapability Sampled1D
144fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
145fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
146fd4e5da5Sopenharmony_ciOpSource HLSL 630
147fd4e5da5Sopenharmony_ciOpName %main "main"
148fd4e5da5Sopenharmony_ciOpName %g_tTex1df4 "g_tTex1df4"
149fd4e5da5Sopenharmony_ciOpName %g_sSamp "g_sSamp"
150fd4e5da5Sopenharmony_ciOpName %i_Tex0 "i.Tex0"
151fd4e5da5Sopenharmony_ciOpName %i_Tex1 "i.Tex1"
152fd4e5da5Sopenharmony_ciOpName %_entryPointOutput_Color "@entryPointOutput.Color"
153fd4e5da5Sopenharmony_ciOpDecorate %main LinkageAttributes "main" Export
154fd4e5da5Sopenharmony_ciOpDecorate %g_tTex1df4 DescriptorSet 0
155fd4e5da5Sopenharmony_ciOpDecorate %g_tTex1df4 Binding 0
156fd4e5da5Sopenharmony_ciOpDecorate %g_sSamp DescriptorSet 0
157fd4e5da5Sopenharmony_ciOpDecorate %g_sSamp Binding 0
158fd4e5da5Sopenharmony_ciOpDecorate %i_Tex0 Location 0
159fd4e5da5Sopenharmony_ciOpDecorate %i_Tex1 Location 1
160fd4e5da5Sopenharmony_ciOpDecorate %_entryPointOutput_Color Location 0
161fd4e5da5Sopenharmony_ci)";
162fd4e5da5Sopenharmony_ci
163fd4e5da5Sopenharmony_ci  const std::string defs1 =
164fd4e5da5Sopenharmony_ci      R"(%void = OpTypeVoid
165fd4e5da5Sopenharmony_ci%3 = OpTypeFunction %void
166fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
167fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
168fd4e5da5Sopenharmony_ci%17 = OpTypeImage %float 1D 0 0 0 1 Unknown
169fd4e5da5Sopenharmony_ci%_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
170fd4e5da5Sopenharmony_ci%g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
171fd4e5da5Sopenharmony_ci%21 = OpTypeSampler
172fd4e5da5Sopenharmony_ci%_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
173fd4e5da5Sopenharmony_ci%g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
174fd4e5da5Sopenharmony_ci%25 = OpTypeSampledImage %17
175fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
176fd4e5da5Sopenharmony_ci%i_Tex0 = OpVariable %_ptr_Input_float Input
177fd4e5da5Sopenharmony_ci%i_Tex1 = OpVariable %_ptr_Input_float Input
178fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
179fd4e5da5Sopenharmony_ci%_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
180fd4e5da5Sopenharmony_ci%float_0_5 = OpConstant %float 0.5
181fd4e5da5Sopenharmony_ci%116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
182fd4e5da5Sopenharmony_ci)";
183fd4e5da5Sopenharmony_ci
184fd4e5da5Sopenharmony_ci  const std::string relax_decos =
185fd4e5da5Sopenharmony_ci      R"(OpDecorate %60 RelaxedPrecision
186fd4e5da5Sopenharmony_ciOpDecorate %63 RelaxedPrecision
187fd4e5da5Sopenharmony_ciOpDecorate %82 RelaxedPrecision
188fd4e5da5Sopenharmony_ciOpDecorate %88 RelaxedPrecision
189fd4e5da5Sopenharmony_ciOpDecorate %91 RelaxedPrecision
190fd4e5da5Sopenharmony_ciOpDecorate %94 RelaxedPrecision
191fd4e5da5Sopenharmony_ci)";
192fd4e5da5Sopenharmony_ci
193fd4e5da5Sopenharmony_ci  const std::string func_orig =
194fd4e5da5Sopenharmony_ci      R"(%main = OpFunction %void None %3
195fd4e5da5Sopenharmony_ci%5 = OpLabel
196fd4e5da5Sopenharmony_ci%60 = OpLoad %float %i_Tex0
197fd4e5da5Sopenharmony_ci%63 = OpLoad %float %i_Tex1
198fd4e5da5Sopenharmony_ci%77 = OpLoad %17 %g_tTex1df4
199fd4e5da5Sopenharmony_ci%78 = OpLoad %21 %g_sSamp
200fd4e5da5Sopenharmony_ci%79 = OpSampledImage %25 %77 %78
201fd4e5da5Sopenharmony_ci%82 = OpImageSampleImplicitLod %v4float %79 %60
202fd4e5da5Sopenharmony_ci%83 = OpLoad %17 %g_tTex1df4
203fd4e5da5Sopenharmony_ci%84 = OpLoad %21 %g_sSamp
204fd4e5da5Sopenharmony_ci%85 = OpSampledImage %25 %83 %84
205fd4e5da5Sopenharmony_ci%88 = OpImageSampleImplicitLod %v4float %85 %63
206fd4e5da5Sopenharmony_ci%91 = OpFAdd %v4float %82 %88
207fd4e5da5Sopenharmony_ci%94 = OpFMul %v4float %91 %116
208fd4e5da5Sopenharmony_ciOpStore %_entryPointOutput_Color %94
209fd4e5da5Sopenharmony_ciOpReturn
210fd4e5da5Sopenharmony_ciOpFunctionEnd
211fd4e5da5Sopenharmony_ci)";
212fd4e5da5Sopenharmony_ci
213fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
214fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<RelaxFloatOpsPass>(
215fd4e5da5Sopenharmony_ci      defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
216fd4e5da5Sopenharmony_ci      true);
217fd4e5da5Sopenharmony_ci}
218fd4e5da5Sopenharmony_ci
219fd4e5da5Sopenharmony_ci}  // namespace
220fd4e5da5Sopenharmony_ci}  // namespace opt
221fd4e5da5Sopenharmony_ci}  // namespace spvtools
222