1 // Copyright (c) 2021 The Khronos Group Inc.
2 // Copyright (c) 2021 Valve Corporation
3 // Copyright (c) 2021 LunarG Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 #include <vector>
18
19 #include "gmock/gmock.h"
20 #include "test/opt/pass_fixture.h"
21 #include "test/opt/pass_utils.h"
22
23 namespace spvtools {
24 namespace opt {
25 namespace {
26
27 using InterpFixupTest = PassTest<::testing::Test>;
28
29 using ::testing::HasSubstr;
30
TEST_F(InterpFixupTest,FixInterpAtSample)31 TEST_F(InterpFixupTest, FixInterpAtSample) {
32 const std::string text = R"(
33 OpCapability Shader
34 OpCapability InterpolationFunction
35 %1 = OpExtInstImport "GLSL.std.450"
36 OpMemoryModel Logical GLSL450
37 OpEntryPoint Fragment %MainPs "MainPs" %i_vPositionOs %_entryPointOutput
38 OpExecutionMode %MainPs OriginUpperLeft
39 OpSource HLSL 500
40 OpName %MainPs "MainPs"
41 OpName %i_vPositionOs "i.vPositionOs"
42 OpName %_entryPointOutput "@entryPointOutput"
43 OpDecorate %i_vPositionOs Location 0
44 OpDecorate %_entryPointOutput Location 0
45 %void = OpTypeVoid
46 %6 = OpTypeFunction %void
47 %float = OpTypeFloat 32
48 %v4float = OpTypeVector %float 4
49 %float_0 = OpConstant %float 0
50 %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
51 %uint = OpTypeInt 32 0
52 %uint_0 = OpConstant %uint 0
53 %uint_4 = OpConstant %uint 4
54 %bool = OpTypeBool
55 %int = OpTypeInt 32 1
56 %int_1 = OpConstant %int 1
57 %_ptr_Input_v4float = OpTypePointer Input %v4float
58 %i_vPositionOs = OpVariable %_ptr_Input_v4float Input
59 %_ptr_Output_v4float = OpTypePointer Output %v4float
60 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
61 %MainPs = OpFunction %void None %6
62 %19 = OpLabel
63 %20 = OpLoad %v4float %i_vPositionOs
64 OpBranch %21
65 %21 = OpLabel
66 %22 = OpPhi %v4float %10 %19 %23 %24
67 %25 = OpPhi %uint %uint_0 %19 %26 %24
68 %27 = OpULessThan %bool %25 %uint_4
69 OpLoopMerge %28 %24 None
70 OpBranchConditional %27 %24 %28
71 %24 = OpLabel
72 %29 = OpExtInst %v4float %1 InterpolateAtSample %20 %25
73 ;CHECK: %29 = OpExtInst %v4float %1 InterpolateAtSample %i_vPositionOs %25
74 %30 = OpCompositeExtract %float %29 0
75 %31 = OpCompositeExtract %float %22 0
76 %32 = OpFAdd %float %31 %30
77 %23 = OpCompositeInsert %v4float %32 %22 0
78 %26 = OpIAdd %uint %25 %int_1
79 OpBranch %21
80 %28 = OpLabel
81 OpStore %_entryPointOutput %22
82 OpReturn
83 OpFunctionEnd
84 )";
85
86 SinglePassRunAndMatch<InterpFixupPass>(text, false);
87 }
88
TEST_F(InterpFixupTest,FixInterpAtCentroid)89 TEST_F(InterpFixupTest, FixInterpAtCentroid) {
90 const std::string text = R"(
91 OpCapability Shader
92 OpCapability InterpolationFunction
93 %1 = OpExtInstImport "GLSL.std.450"
94 OpMemoryModel Logical GLSL450
95 OpEntryPoint Fragment %MainPs "MainPs" %i_vPositionOs %_entryPointOutput
96 OpExecutionMode %MainPs OriginUpperLeft
97 OpSource HLSL 500
98 OpName %MainPs "MainPs"
99 OpName %i_vPositionOs "i.vPositionOs"
100 OpName %_entryPointOutput "@entryPointOutput"
101 OpDecorate %i_vPositionOs Location 0
102 OpDecorate %_entryPointOutput Location 0
103 %void = OpTypeVoid
104 %6 = OpTypeFunction %void
105 %float = OpTypeFloat 32
106 %v4float = OpTypeVector %float 4
107 %float_0 = OpConstant %float 0
108 %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
109 %_ptr_Input_v4float = OpTypePointer Input %v4float
110 %i_vPositionOs = OpVariable %_ptr_Input_v4float Input
111 %_ptr_Output_v4float = OpTypePointer Output %v4float
112 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
113 %MainPs = OpFunction %void None %6
114 %13 = OpLabel
115 %14 = OpLoad %v4float %i_vPositionOs
116 %15 = OpExtInst %v4float %1 InterpolateAtCentroid %14
117 ;CHECK: %15 = OpExtInst %v4float %1 InterpolateAtCentroid %i_vPositionOs
118 %16 = OpCompositeExtract %float %15 0
119 %17 = OpCompositeInsert %v4float %16 %10 0
120 OpStore %_entryPointOutput %17
121 OpReturn
122 OpFunctionEnd
123 )";
124
125 SinglePassRunAndMatch<InterpFixupPass>(text, false);
126 }
127
TEST_F(InterpFixupTest,FixInterpAtOffset)128 TEST_F(InterpFixupTest, FixInterpAtOffset) {
129 const std::string text = R"(
130 OpCapability Shader
131 OpCapability InterpolationFunction
132 %1 = OpExtInstImport "GLSL.std.450"
133 OpMemoryModel Logical GLSL450
134 OpEntryPoint Fragment %MainPs "MainPs" %i_vPositionOs %_entryPointOutput
135 OpExecutionMode %MainPs OriginUpperLeft
136 OpSource HLSL 500
137 OpName %MainPs "MainPs"
138 OpName %i_vPositionOs "i.vPositionOs"
139 OpName %_entryPointOutput "@entryPointOutput"
140 OpDecorate %i_vPositionOs Location 0
141 OpDecorate %_entryPointOutput Location 0
142 %void = OpTypeVoid
143 %6 = OpTypeFunction %void
144 %float = OpTypeFloat 32
145 %v4float = OpTypeVector %float 4
146 %float_0 = OpConstant %float 0
147 %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
148 %v2float = OpTypeVector %float 2
149 %float_0_0625 = OpConstant %float 0.0625
150 %13 = OpConstantComposite %v2float %float_0_0625 %float_0_0625
151 %_ptr_Input_v4float = OpTypePointer Input %v4float
152 %i_vPositionOs = OpVariable %_ptr_Input_v4float Input
153 %_ptr_Output_v4float = OpTypePointer Output %v4float
154 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
155 %MainPs = OpFunction %void None %6
156 %16 = OpLabel
157 %17 = OpLoad %v4float %i_vPositionOs
158 %18 = OpExtInst %v4float %1 InterpolateAtOffset %17 %13
159 ;CHECK: %18 = OpExtInst %v4float %1 InterpolateAtOffset %i_vPositionOs %13
160 %19 = OpCompositeExtract %float %18 0
161 %20 = OpCompositeInsert %v4float %19 %10 0
162 OpStore %_entryPointOutput %20
163 OpReturn
164 OpFunctionEnd
165 )";
166
167 SinglePassRunAndMatch<InterpFixupPass>(text, false);
168 }
169
170 } // namespace
171 } // namespace opt
172 } // namespace spvtools
173