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