• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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 static const std::string kOutputDecorations = R"(
30 ; CHECK: OpDecorate [[output_buffer_type:%inst_printf_OutputBuffer]] Block
31 ; CHECK: OpMemberDecorate [[output_buffer_type]] 0 Offset 0
32 ; CHECK: OpMemberDecorate [[output_buffer_type]] 1 Offset 4
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 %_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 %gl_FragCoord
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 ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
91 )" + kOutputDecorations + R"(
92 ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
93 )";
94 
95   const std::string globals =
96       R"(%void = OpTypeVoid
97 %9 = OpTypeFunction %void
98 %float = OpTypeFloat 32
99 %v2float = OpTypeVector %float 2
100 %v4float = OpTypeVector %float 4
101 %13 = OpTypeImage %float 2D 0 0 0 1 Unknown
102 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
103 %6 = OpVariable %_ptr_UniformConstant_13 UniformConstant
104 %15 = OpTypeSampler
105 %_ptr_UniformConstant_15 = OpTypePointer UniformConstant %15
106 %7 = OpVariable %_ptr_UniformConstant_15 UniformConstant
107 %17 = OpTypeSampledImage %13
108 %_ptr_Input_v2float = OpTypePointer Input %v2float
109 %3 = OpVariable %_ptr_Input_v2float Input
110 %_ptr_Output_v4float = OpTypePointer Output %v4float
111 %4 = OpVariable %_ptr_Output_v4float Output
112 ; CHECK: %uint = OpTypeInt 32 0
113 ; CHECK: %38 = OpTypeFunction %void %uint %uint %uint %uint %uint %uint
114 ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
115 )" + kOutputGlobals + R"(
116 ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
117 ; CHECK: %bool = OpTypeBool
118 ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
119 ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
120 ; CHECK: %v4uint = OpTypeVector %uint 4
121 )";
122   // clang-format on
123 
124   const std::string main =
125       R"(%2 = OpFunction %void None %9
126 %20 = OpLabel
127 %21 = OpLoad %v2float %3
128 %22 = OpLoad %13 %6
129 %23 = OpLoad %15 %7
130 %24 = OpSampledImage %17 %22 %23
131 %25 = OpImageSampleImplicitLod %v4float %24 %21
132 %26 = OpExtInst %void %1 1 %5 %25
133 ; CHECK-NOT: %26 = OpExtInst %void %1 1 %5 %25
134 ; CHECK: %29 = OpCompositeExtract %float %25 0
135 ; CHECK: %30 = OpBitcast %uint %29
136 ; CHECK: %31 = OpCompositeExtract %float %25 1
137 ; CHECK: %32 = OpBitcast %uint %31
138 ; CHECK: %33 = OpCompositeExtract %float %25 2
139 ; CHECK: %34 = OpBitcast %uint %33
140 ; CHECK: %35 = OpCompositeExtract %float %25 3
141 ; CHECK: %36 = OpBitcast %uint %35
142 ; CHECK: %101 = OpFunctionCall %void %inst_printf_stream_write_6 %uint_36 %uint_5 %30 %32 %34 %36
143 ; CHECK: OpBranch %102
144 ; CHECK: %102 = OpLabel
145 OpStore %4 %25
146 OpReturn
147 OpFunctionEnd
148 )";
149 
150   const std::string output_func = R"(
151 ; CHECK: %inst_printf_stream_write_6 = OpFunction %void None %38
152 ; CHECK: %39 = OpFunctionParameter %uint
153 ; CHECK: %40 = OpFunctionParameter %uint
154 ; CHECK: %41 = OpFunctionParameter %uint
155 ; CHECK: %42 = OpFunctionParameter %uint
156 ; CHECK: %43 = OpFunctionParameter %uint
157 ; CHECK: %44 = OpFunctionParameter %uint
158 ; CHECK: %45 = OpLabel
159 ; CHECK: %52 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_0
160 ; CHECK: %55 = OpAtomicIAdd %uint %52 %uint_4 %uint_0 %uint_12
161 ; CHECK: %56 = OpIAdd %uint %55 %uint_12
162 ; CHECK: %57 = OpArrayLength %uint %inst_printf_output_buffer 1
163 ; CHECK: %59 = OpULessThanEqual %bool %56 %57
164 ; CHECK: OpSelectionMerge %60 None
165 ; CHECK: OpBranchConditional %59 %61 %60
166 ; CHECK: %61 = OpLabel
167 ; CHECK: %62 = OpIAdd %uint %55 %uint_0
168 ; CHECK: %64 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %62
169 ; CHECK: OpStore %64 %uint_12
170 ; CHECK: %66 = OpIAdd %uint %55 %uint_1
171 ; CHECK: %67 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %66
172 ; CHECK: OpStore %67 %uint_23
173 ; CHECK: %69 = OpIAdd %uint %55 %uint_2
174 ; CHECK: %70 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %69
175 ; CHECK: OpStore %70 %39
176 ; CHECK: %72 = OpIAdd %uint %55 %uint_3
177 ; CHECK: %73 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %72
178 ; CHECK: OpStore %73 %uint_4
179 ; CHECK: %76 = OpLoad %v4float %gl_FragCoord
180 ; CHECK: %78 = OpBitcast %v4uint %76
181 ; CHECK: %79 = OpCompositeExtract %uint %78 0
182 ; CHECK: %80 = OpIAdd %uint %55 %uint_4
183 ; CHECK: %81 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %80
184 ; CHECK: OpStore %81 %79
185 ; CHECK: %82 = OpCompositeExtract %uint %78 1
186 ; CHECK: %83 = OpIAdd %uint %55 %uint_5
187 ; CHECK: %84 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %83
188 ; CHECK: OpStore %84 %82
189 ; CHECK: %86 = OpIAdd %uint %55 %uint_7
190 ; CHECK: %87 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %86
191 ; CHECK: OpStore %87 %40
192 ; CHECK: %89 = OpIAdd %uint %55 %uint_8
193 ; CHECK: %90 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %89
194 ; CHECK: OpStore %90 %41
195 ; CHECK: %92 = OpIAdd %uint %55 %uint_9
196 ; CHECK: %93 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %92
197 ; CHECK: OpStore %93 %42
198 ; CHECK: %95 = OpIAdd %uint %55 %uint_10
199 ; CHECK: %96 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %95
200 ; CHECK: OpStore %96 %43
201 ; CHECK: %98 = OpIAdd %uint %55 %uint_11
202 ; CHECK: %99 = OpAccessChain %_ptr_StorageBuffer_uint %inst_printf_output_buffer %uint_1 %98
203 ; CHECK: OpStore %99 %44
204 ; CHECK: OpBranch %60
205 ; CHECK: %60 = OpLabel
206 ; CHECK: OpReturn
207 ; CHECK: OpFunctionEnd
208 )";
209 
210   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
211   SinglePassRunAndMatch<InstDebugPrintfPass>(
212       defs + decorates + globals + main + output_func, true);
213 }
214 
215 // TODO(greg-lunarg): Add tests to verify handling of these cases:
216 //
217 //   Compute shader
218 //   Geometry shader
219 //   Tessellation control shader
220 //   Tessellation eval shader
221 //   Vertex shader
222 
223 }  // namespace
224 }  // namespace opt
225 }  // namespace spvtools
226