2
0

inst_debug_printf_test.cpp 7.3 KB

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