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/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 RelaxFloatOpsTest = PassTest<::testing::Test>;
30
TEST_F(RelaxFloatOpsTest,RelaxFloatOpsBasic)31 TEST_F(RelaxFloatOpsTest, RelaxFloatOpsBasic) {
32 // All float result instructions in functions should be relaxed
33 // clang-format off
34 //
35 // SamplerState g_sSamp : register(s0);
36 // uniform Texture1D <float4> g_tTex1df4 : register(t0);
37 //
38 // struct PS_INPUT
39 // {
40 // float Tex0 : TEXCOORD0;
41 // float Tex1 : TEXCOORD1;
42 // };
43 //
44 // struct PS_OUTPUT
45 // {
46 // float4 Color : SV_Target0;
47 // };
48 //
49 // PS_OUTPUT main(PS_INPUT i)
50 // {
51 // PS_OUTPUT psout;
52 // float4 txval10 = g_tTex1df4.Sample(g_sSamp, i.Tex0);
53 // float4 txval11 = g_tTex1df4.Sample(g_sSamp, i.Tex1);
54 // float4 t = txval10 + txval11;
55 // float4 t2 = t / 2.0;
56 // psout.Color = t2;
57 // return psout;
58 // }
59 // clang-format on
60
61 const std::string defs0 =
62 R"(OpCapability Shader
63 OpCapability Sampled1D
64 %1 = OpExtInstImport "GLSL.std.450"
65 OpMemoryModel Logical GLSL450
66 OpEntryPoint Fragment %main "main" %i_Tex0 %i_Tex1 %_entryPointOutput_Color
67 OpExecutionMode %main OriginUpperLeft
68 OpSource HLSL 500
69 OpName %main "main"
70 OpName %g_tTex1df4 "g_tTex1df4"
71 OpName %g_sSamp "g_sSamp"
72 OpName %i_Tex0 "i.Tex0"
73 OpName %i_Tex1 "i.Tex1"
74 OpName %_entryPointOutput_Color "@entryPointOutput.Color"
75 OpDecorate %g_tTex1df4 DescriptorSet 0
76 OpDecorate %g_tTex1df4 Binding 0
77 OpDecorate %g_sSamp DescriptorSet 0
78 OpDecorate %g_sSamp Binding 0
79 OpDecorate %i_Tex0 Location 0
80 OpDecorate %i_Tex1 Location 1
81 OpDecorate %_entryPointOutput_Color Location 0
82 )";
83
84 const std::string defs1 =
85 R"(%void = OpTypeVoid
86 %3 = OpTypeFunction %void
87 %float = OpTypeFloat 32
88 %v4float = OpTypeVector %float 4
89 %17 = OpTypeImage %float 1D 0 0 0 1 Unknown
90 %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
91 %g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
92 %21 = OpTypeSampler
93 %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
94 %g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
95 %25 = OpTypeSampledImage %17
96 %_ptr_Input_float = OpTypePointer Input %float
97 %i_Tex0 = OpVariable %_ptr_Input_float Input
98 %i_Tex1 = OpVariable %_ptr_Input_float Input
99 %_ptr_Output_v4float = OpTypePointer Output %v4float
100 %_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
101 %float_0_5 = OpConstant %float 0.5
102 %116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
103 )";
104
105 const std::string relax_decos =
106 R"(OpDecorate %60 RelaxedPrecision
107 OpDecorate %63 RelaxedPrecision
108 OpDecorate %82 RelaxedPrecision
109 OpDecorate %88 RelaxedPrecision
110 OpDecorate %91 RelaxedPrecision
111 OpDecorate %94 RelaxedPrecision
112 )";
113
114 const std::string func_orig =
115 R"(%main = OpFunction %void None %3
116 %5 = OpLabel
117 %60 = OpLoad %float %i_Tex0
118 %63 = OpLoad %float %i_Tex1
119 %77 = OpLoad %17 %g_tTex1df4
120 %78 = OpLoad %21 %g_sSamp
121 %79 = OpSampledImage %25 %77 %78
122 %82 = OpImageSampleImplicitLod %v4float %79 %60
123 %83 = OpLoad %17 %g_tTex1df4
124 %84 = OpLoad %21 %g_sSamp
125 %85 = OpSampledImage %25 %83 %84
126 %88 = OpImageSampleImplicitLod %v4float %85 %63
127 %91 = OpFAdd %v4float %82 %88
128 %94 = OpFMul %v4float %91 %116
129 OpStore %_entryPointOutput_Color %94
130 OpReturn
131 OpFunctionEnd
132 )";
133
134 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
135 SinglePassRunAndCheck<RelaxFloatOpsPass>(
136 defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
137 true);
138 }
139
140 } // namespace
141 } // namespace opt
142 } // namespace spvtools
143