1 // Copyright (c) 2020-2022 Valve Corporation
2 // Copyright (c) 2020-2022 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 // Debug Printf Instrumentation 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 static const std::string kOutputDecorations = R"(
29 ; CHECK: OpDecorate [[output_buffer_type:%inst_printf_OutputBuffer]] Block
30 ; CHECK: OpMemberDecorate [[output_buffer_type]] 0 Offset 0
31 ; CHECK: OpMemberDecorate [[output_buffer_type]] 1 Offset 4
32 ; CHECK: OpMemberDecorate [[output_buffer_type]] 2 Offset 8
33 ; CHECK: OpDecorate [[output_buffer_var:%\w+]] DescriptorSet 7
34 ; CHECK: OpDecorate [[output_buffer_var]] Binding 3
35 )";
36
37 static const std::string kOutputGlobals = R"(
38 ; CHECK: [[output_buffer_type]] = OpTypeStruct %uint %uint %_runtimearr_uint
39 ; CHECK: [[output_ptr_type:%\w+]] = OpTypePointer StorageBuffer [[output_buffer_type]]
40 ; CHECK: [[output_buffer_var]] = OpVariable [[output_ptr_type]] StorageBuffer
41 )";
42
43 using InstDebugPrintfTest = PassTest<::testing::Test>;
44
TEST_F(InstDebugPrintfTest,V4Float32)45 TEST_F(InstDebugPrintfTest, V4Float32) {
46 // SamplerState g_sDefault;
47 // Texture2D g_tColor;
48 //
49 // struct PS_INPUT
50 // {
51 // float2 vBaseTexCoord : TEXCOORD0;
52 // };
53 //
54 // struct PS_OUTPUT
55 // {
56 // float4 vDiffuse : SV_Target0;
57 // };
58 //
59 // PS_OUTPUT MainPs(PS_INPUT i)
60 // {
61 // PS_OUTPUT o;
62 //
63 // o.vDiffuse.rgba = g_tColor.Sample(g_sDefault, (i.vBaseTexCoord.xy).xy);
64 // debugPrintfEXT("diffuse: %v4f", o.vDiffuse.rgba);
65 // return o;
66 // }
67
68 const std::string defs =
69 R"(OpCapability Shader
70 OpExtension "SPV_KHR_non_semantic_info"
71 %1 = OpExtInstImport "NonSemantic.DebugPrintf"
72 ; CHECK-NOT: OpExtension "SPV_KHR_non_semantic_info"
73 ; CHECK-NOT: %1 = OpExtInstImport "NonSemantic.DebugPrintf"
74 ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
75 OpMemoryModel Logical GLSL450
76 OpEntryPoint Fragment %2 "MainPs" %3 %4
77 ; CHECK: OpEntryPoint Fragment %2 "MainPs" %3 %4
78 OpExecutionMode %2 OriginUpperLeft
79 %5 = OpString "Color is %vn"
80 )";
81
82 // clang-format off
83 const std::string decorates =
84 R"(OpDecorate %6 DescriptorSet 0
85 OpDecorate %6 Binding 1
86 OpDecorate %7 DescriptorSet 0
87 OpDecorate %7 Binding 0
88 OpDecorate %3 Location 0
89 OpDecorate %4 Location 0
90 )" + kOutputDecorations;
91
92 const std::string globals =
93 R"(%void = OpTypeVoid
94 %9 = OpTypeFunction %void
95 %float = OpTypeFloat 32
96 %v2float = OpTypeVector %float 2
97 %v4float = OpTypeVector %float 4
98 %13 = OpTypeImage %float 2D 0 0 0 1 Unknown
99 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
100 %6 = OpVariable %_ptr_UniformConstant_13 UniformConstant
101 %15 = OpTypeSampler
102 %_ptr_UniformConstant_15 = OpTypePointer UniformConstant %15
103 %7 = OpVariable %_ptr_UniformConstant_15 UniformConstant
104 %17 = OpTypeSampledImage %13
105 %_ptr_Input_v2float = OpTypePointer Input %v2float
106 %3 = OpVariable %_ptr_Input_v2float Input
107 %_ptr_Output_v4float = OpTypePointer Output %v4float
108 %4 = OpVariable %_ptr_Output_v4float Output
109 ; CHECK: %uint = OpTypeInt 32 0
110 ; CHECK: [[func_type:%\w+]] = OpTypeFunction %void %uint %uint %uint %uint %uint %uint %uint
111 ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
112 )" + kOutputGlobals + R"(
113 ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
114 ; CHECK: %bool = OpTypeBool
115 )";
116 // clang-format on
117
118 const std::string main =
119 R"(%2 = OpFunction %void None %9
120 %20 = OpLabel
121 %21 = OpLoad %v2float %3
122 %22 = OpLoad %13 %6
123 %23 = OpLoad %15 %7
124 %24 = OpSampledImage %17 %22 %23
125 %25 = OpImageSampleImplicitLod %v4float %24 %21
126 %26 = OpExtInst %void %1 1 %5 %25
127 ; CHECK-NOT: %26 = OpExtInst %void %1 1 %5 %25
128 ; CHECK: {{%\w+}} = OpCompositeExtract %float %25 0
129 ; CHECK: {{%\w+}} = OpBitcast %uint {{%\w+}}
130 ; CHECK: {{%\w+}} = OpCompositeExtract %float %25 1
131 ; CHECK: {{%\w+}} = OpBitcast %uint {{%\w+}}
132 ; CHECK: {{%\w+}} = OpCompositeExtract %float %25 2
133 ; CHECK: {{%\w+}} = OpBitcast %uint {{%\w+}}
134 ; CHECK: {{%\w+}} = OpCompositeExtract %float %25 3
135 ; CHECK: {{%\w+}} = OpBitcast %uint {{%\w+}}
136 ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_printf_stream_write_5 %uint_23 %uint_36 %uint_5 {{%\w+}} {{%\w+}} {{%\w+}} {{%\w+}}
137 ; CHECK: OpBranch {{%\w+}}
138 ; CHECK: {{%\w+}} = OpLabel
139 OpStore %4 %25
140 OpReturn
141 OpFunctionEnd
142 )";
143
144 const std::string output_func = R"(
145 ; CHECK: %inst_printf_stream_write_5 = OpFunction %void None {{%\w+}}
146 ; CHECK: [[sw_shader_id:%\w+]] = OpFunctionParameter %uint
147 ; CHECK: [[sw_inst_idx:%\w+]] = OpFunctionParameter %uint
148 ; CHECK: [[sw_param_1:%\w+]] = OpFunctionParameter %uint
149 ; CHECK: [[sw_param_2:%\w+]] = OpFunctionParameter %uint
150 ; CHECK: [[sw_param_3:%\w+]] = OpFunctionParameter %uint
151 ; CHECK: [[sw_param_4:%\w+]] = OpFunctionParameter %uint
152 ; CHECK: [[sw_param_5:%\w+]] = OpFunctionParameter %uint
153 ; CHECK: {{%\w+}} = OpLabel
154 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_1
155 ; CHECK: {{%\w+}} = OpAtomicIAdd %uint {{%\w+}} %uint_4 %uint_0 %uint_8
156 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_8
157 ; CHECK: {{%\w+}} = OpArrayLength %uint [[output_buffer_var]] 2
158 ; CHECK: {{%\w+}} = OpULessThanEqual %bool {{%\w+}} {{%\w+}}
159 ; CHECK: OpSelectionMerge {{%\w+}} None
160 ; CHECK: OpBranchConditional {{%\w+}} {{%\w+}} {{%\w+}}
161 ; CHECK: {{%\w+}} = OpLabel
162 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_0
163 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
164 ; CHECK: OpStore {{%\w+}} %uint_8
165 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_1
166 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
167 ; CHECK: OpStore {{%\w+}} [[sw_shader_id]]
168 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_2
169 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
170 ; CHECK: OpStore {{%\w+}} [[sw_inst_idx]]
171 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_3
172 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
173 ; CHECK: OpStore {{%\w+}} [[sw_param_1]]
174 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_4
175 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
176 ; CHECK: OpStore {{%\w+}} [[sw_param_2]]
177 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_5
178 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
179 ; CHECK: OpStore {{%\w+}} [[sw_param_3]]
180 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_6
181 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
182 ; CHECK: OpStore {{%\w+}} [[sw_param_4]]
183 ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_7
184 ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
185 ; CHECK: OpStore {{%\w+}} [[sw_param_5]]
186 ; CHECK: OpBranch {{%\w+}}
187 ; CHECK: {{%\w+}} = OpLabel
188 ; CHECK: OpReturn
189 ; CHECK: OpFunctionEnd
190 )";
191
192 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
193 SinglePassRunAndMatch<InstDebugPrintfPass>(
194 defs + decorates + globals + main + output_func, true);
195 }
196
197 // TODO(greg-lunarg): Add tests to verify handling of these cases:
198 //
199 // Compute shader
200 // Geometry shader
201 // Tessellation control shader
202 // Tessellation eval shader
203 // Vertex shader
204
205 } // namespace
206 } // namespace opt
207 } // namespace spvtools
208