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 // Relax float ops tests
17
18 #include <string>
19 #include <vector>
20
21 #include "test/opt/pass_fixture.h"
22 #include "test/opt/pass_utils.h"
23
24 namespace spvtools {
25 namespace opt {
26 namespace {
27
28 using RelaxFloatOpsTest = PassTest<::testing::Test>;
29
TEST_F(RelaxFloatOpsTest,RelaxFloatOpsBasic)30 TEST_F(RelaxFloatOpsTest, RelaxFloatOpsBasic) {
31 // All float result instructions in functions should be relaxed
32 // clang-format off
33 //
34 // SamplerState g_sSamp : register(s0);
35 // uniform Texture1D <float4> g_tTex1df4 : register(t0);
36 //
37 // struct PS_INPUT
38 // {
39 // float Tex0 : TEXCOORD0;
40 // float Tex1 : TEXCOORD1;
41 // };
42 //
43 // struct PS_OUTPUT
44 // {
45 // float4 Color : SV_Target0;
46 // };
47 //
48 // PS_OUTPUT main(PS_INPUT i)
49 // {
50 // PS_OUTPUT psout;
51 // float4 txval10 = g_tTex1df4.Sample(g_sSamp, i.Tex0);
52 // float4 txval11 = g_tTex1df4.Sample(g_sSamp, i.Tex1);
53 // float4 t = txval10 + txval11;
54 // float4 t2 = t / 2.0;
55 // psout.Color = t2;
56 // return psout;
57 // }
58 // clang-format on
59
60 const std::string defs0 =
61 R"(OpCapability Shader
62 OpCapability Sampled1D
63 %1 = OpExtInstImport "GLSL.std.450"
64 OpMemoryModel Logical GLSL450
65 OpEntryPoint Fragment %main "main" %i_Tex0 %i_Tex1 %_entryPointOutput_Color
66 OpExecutionMode %main OriginUpperLeft
67 OpSource HLSL 500
68 OpName %main "main"
69 OpName %g_tTex1df4 "g_tTex1df4"
70 OpName %g_sSamp "g_sSamp"
71 OpName %i_Tex0 "i.Tex0"
72 OpName %i_Tex1 "i.Tex1"
73 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
74 OpDecorate %g_tTex1df4 DescriptorSet 0
75 OpDecorate %g_tTex1df4 Binding 0
76 OpDecorate %g_sSamp DescriptorSet 0
77 OpDecorate %g_sSamp Binding 0
78 OpDecorate %i_Tex0 Location 0
79 OpDecorate %i_Tex1 Location 1
80 OpDecorate %_entryPointOutput_Color Location 0
81 )";
82
83 const std::string defs1 =
84 R"(%void = OpTypeVoid
85 %3 = OpTypeFunction %void
86 %float = OpTypeFloat 32
87 %v4float = OpTypeVector %float 4
88 %17 = OpTypeImage %float 1D 0 0 0 1 Unknown
89 %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
90 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
91 %21 = OpTypeSampler
92 %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
93 %g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
94 %25 = OpTypeSampledImage %17
95 %_ptr_Input_float = OpTypePointer Input %float
96 %i_Tex0 = OpVariable %_ptr_Input_float Input
97 %i_Tex1 = OpVariable %_ptr_Input_float Input
98 %_ptr_Output_v4float = OpTypePointer Output %v4float
99 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
100 %float_0_5 = OpConstant %float 0.5
101 %116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
102 )";
103
104 const std::string relax_decos =
105 R"(OpDecorate %60 RelaxedPrecision
106 OpDecorate %63 RelaxedPrecision
107 OpDecorate %82 RelaxedPrecision
108 OpDecorate %88 RelaxedPrecision
109 OpDecorate %91 RelaxedPrecision
110 OpDecorate %94 RelaxedPrecision
111 )";
112
113 const std::string func_orig =
114 R"(%main = OpFunction %void None %3
115 %5 = OpLabel
116 %60 = OpLoad %float %i_Tex0
117 %63 = OpLoad %float %i_Tex1
118 %77 = OpLoad %17 %g_tTex1df4
119 %78 = OpLoad %21 %g_sSamp
120 %79 = OpSampledImage %25 %77 %78
121 %82 = OpImageSampleImplicitLod %v4float %79 %60
122 %83 = OpLoad %17 %g_tTex1df4
123 %84 = OpLoad %21 %g_sSamp
124 %85 = OpSampledImage %25 %83 %84
125 %88 = OpImageSampleImplicitLod %v4float %85 %63
126 %91 = OpFAdd %v4float %82 %88
127 %94 = OpFMul %v4float %91 %116
128 OpStore %_entryPointOutput_Color %94
129 OpReturn
130 OpFunctionEnd
131 )";
132
133 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
134 SinglePassRunAndCheck<RelaxFloatOpsPass>(
135 defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
136 true);
137 }
138
TEST_F(RelaxFloatOpsTest,RelaxFloatOpsForLinkage)139 TEST_F(RelaxFloatOpsTest, RelaxFloatOpsForLinkage) {
140 const std::string defs0 =
141 R"(OpCapability Shader
142 OpCapability Linkage
143 OpCapability Sampled1D
144 %1 = OpExtInstImport "GLSL.std.450"
145 OpMemoryModel Logical GLSL450
146 OpSource HLSL 630
147 OpName %main "main"
148 OpName %g_tTex1df4 "g_tTex1df4"
149 OpName %g_sSamp "g_sSamp"
150 OpName %i_Tex0 "i.Tex0"
151 OpName %i_Tex1 "i.Tex1"
152 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
153 OpDecorate %main LinkageAttributes "main" Export
154 OpDecorate %g_tTex1df4 DescriptorSet 0
155 OpDecorate %g_tTex1df4 Binding 0
156 OpDecorate %g_sSamp DescriptorSet 0
157 OpDecorate %g_sSamp Binding 0
158 OpDecorate %i_Tex0 Location 0
159 OpDecorate %i_Tex1 Location 1
160 OpDecorate %_entryPointOutput_Color Location 0
161 )";
162
163 const std::string defs1 =
164 R"(%void = OpTypeVoid
165 %3 = OpTypeFunction %void
166 %float = OpTypeFloat 32
167 %v4float = OpTypeVector %float 4
168 %17 = OpTypeImage %float 1D 0 0 0 1 Unknown
169 %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
170 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
171 %21 = OpTypeSampler
172 %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
173 %g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
174 %25 = OpTypeSampledImage %17
175 %_ptr_Input_float = OpTypePointer Input %float
176 %i_Tex0 = OpVariable %_ptr_Input_float Input
177 %i_Tex1 = OpVariable %_ptr_Input_float Input
178 %_ptr_Output_v4float = OpTypePointer Output %v4float
179 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
180 %float_0_5 = OpConstant %float 0.5
181 %116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
182 )";
183
184 const std::string relax_decos =
185 R"(OpDecorate %60 RelaxedPrecision
186 OpDecorate %63 RelaxedPrecision
187 OpDecorate %82 RelaxedPrecision
188 OpDecorate %88 RelaxedPrecision
189 OpDecorate %91 RelaxedPrecision
190 OpDecorate %94 RelaxedPrecision
191 )";
192
193 const std::string func_orig =
194 R"(%main = OpFunction %void None %3
195 %5 = OpLabel
196 %60 = OpLoad %float %i_Tex0
197 %63 = OpLoad %float %i_Tex1
198 %77 = OpLoad %17 %g_tTex1df4
199 %78 = OpLoad %21 %g_sSamp
200 %79 = OpSampledImage %25 %77 %78
201 %82 = OpImageSampleImplicitLod %v4float %79 %60
202 %83 = OpLoad %17 %g_tTex1df4
203 %84 = OpLoad %21 %g_sSamp
204 %85 = OpSampledImage %25 %83 %84
205 %88 = OpImageSampleImplicitLod %v4float %85 %63
206 %91 = OpFAdd %v4float %82 %88
207 %94 = OpFMul %v4float %91 %116
208 OpStore %_entryPointOutput_Color %94
209 OpReturn
210 OpFunctionEnd
211 )";
212
213 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
214 SinglePassRunAndCheck<RelaxFloatOpsPass>(
215 defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
216 true);
217 }
218
219 } // namespace
220 } // namespace opt
221 } // namespace spvtools
222