switch_descriptorset_test.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (c) 2023 LunarG Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Bindless Check Instrumentation Tests.
  15. // Tests ending with V2 use version 2 record format.
  16. #include <string>
  17. #include <vector>
  18. #include "test/opt/pass_fixture.h"
  19. #include "test/opt/pass_utils.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using SwitchDescriptorSetTest = PassTest<::testing::Test>;
  24. TEST_F(SwitchDescriptorSetTest, Basic) {
  25. // #version 450
  26. // #extension GL_EXT_buffer_reference : enable
  27. //
  28. // layout(buffer_reference, buffer_reference_align = 16) buffer bufStruct;
  29. //
  30. // layout(set = 7, binding = 7) uniform ufoo {
  31. // bufStruct data;
  32. // uint offset;
  33. // } u_info;
  34. //
  35. // layout(buffer_reference, std140) buffer bufStruct {
  36. // layout(offset = 0) int a[2];
  37. // layout(offset = 32) int b;
  38. // };
  39. //
  40. // void main() {
  41. // u_info.data.b = 0xca7;
  42. // }
  43. const std::string spirv = R"(
  44. OpCapability Shader
  45. OpCapability PhysicalStorageBufferAddresses
  46. OpExtension "SPV_EXT_physical_storage_buffer"
  47. %1 = OpExtInstImport "GLSL.std.450"
  48. OpMemoryModel PhysicalStorageBuffer64 GLSL450
  49. OpEntryPoint GLCompute %main "main"
  50. OpExecutionMode %main LocalSize 1 1 1
  51. OpSource GLSL 450
  52. OpSourceExtension "GL_EXT_buffer_reference"
  53. OpName %main "main"
  54. OpName %ufoo "ufoo"
  55. OpMemberName %ufoo 0 "data"
  56. OpMemberName %ufoo 1 "offset"
  57. OpName %bufStruct "bufStruct"
  58. OpMemberName %bufStruct 0 "a"
  59. OpMemberName %bufStruct 1 "b"
  60. OpName %u_info "u_info"
  61. OpMemberDecorate %ufoo 0 Offset 0
  62. OpMemberDecorate %ufoo 1 Offset 8
  63. OpDecorate %ufoo Block
  64. OpDecorate %_arr_int_uint_2 ArrayStride 16
  65. OpMemberDecorate %bufStruct 0 Offset 0
  66. OpMemberDecorate %bufStruct 1 Offset 32
  67. OpDecorate %bufStruct Block
  68. OpDecorate %u_info DescriptorSet 7
  69. ;CHECK: OpDecorate %u_info DescriptorSet 31
  70. OpDecorate %u_info Binding 7
  71. ;CHECK: OpDecorate %u_info Binding 7
  72. %void = OpTypeVoid
  73. %3 = OpTypeFunction %void
  74. OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_bufStruct PhysicalStorageBuffer
  75. %uint = OpTypeInt 32 0
  76. %ufoo = OpTypeStruct %_ptr_PhysicalStorageBuffer_bufStruct %uint
  77. %int = OpTypeInt 32 1
  78. %uint_2 = OpConstant %uint 2
  79. %_arr_int_uint_2 = OpTypeArray %int %uint_2
  80. %bufStruct = OpTypeStruct %_arr_int_uint_2 %int
  81. %_ptr_PhysicalStorageBuffer_bufStruct = OpTypePointer PhysicalStorageBuffer %bufStruct
  82. %_ptr_Uniform_ufoo = OpTypePointer Uniform %ufoo
  83. %u_info = OpVariable %_ptr_Uniform_ufoo Uniform
  84. %int_0 = OpConstant %int 0
  85. %_ptr_Uniform__ptr_PhysicalStorageBuffer_bufStruct = OpTypePointer Uniform %_ptr_PhysicalStorageBuffer_bufStruct
  86. %int_1 = OpConstant %int 1
  87. %int_3239 = OpConstant %int 3239
  88. %_ptr_PhysicalStorageBuffer_int = OpTypePointer PhysicalStorageBuffer %int
  89. %main = OpFunction %void None %3
  90. %5 = OpLabel
  91. %17 = OpAccessChain %_ptr_Uniform__ptr_PhysicalStorageBuffer_bufStruct %u_info %int_0
  92. %18 = OpLoad %_ptr_PhysicalStorageBuffer_bufStruct %17
  93. %22 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %18 %int_1
  94. OpReturn
  95. OpFunctionEnd
  96. )";
  97. // clang-format off
  98. SinglePassRunAndMatch<SwitchDescriptorSetPass>(spirv, true, 7, 31);
  99. }
  100. // Make sure DescriptorSet decorations that don't match the requested number
  101. // are left unchanged.
  102. TEST_F(SwitchDescriptorSetTest, Unchanged) {
  103. // #version 450
  104. // #extension GL_EXT_buffer_reference : enable
  105. //
  106. // layout(buffer_reference, buffer_reference_align = 16) buffer bufStruct;
  107. //
  108. // layout(set = 11, binding = 7) uniform ufoo {
  109. // bufStruct data;
  110. // uint offset;
  111. // } u_info;
  112. //
  113. // layout(buffer_reference, std140) buffer bufStruct {
  114. // layout(offset = 0) int a[2];
  115. // layout(offset = 32) int b;
  116. // };
  117. //
  118. // void main() {
  119. // u_info.data.b = 0xca7;
  120. // }
  121. const std::string spirv = R"(
  122. OpCapability Shader
  123. OpCapability PhysicalStorageBufferAddresses
  124. OpExtension "SPV_EXT_physical_storage_buffer"
  125. %1 = OpExtInstImport "GLSL.std.450"
  126. OpMemoryModel PhysicalStorageBuffer64 GLSL450
  127. OpEntryPoint GLCompute %main "main"
  128. OpExecutionMode %main LocalSize 1 1 1
  129. OpSource GLSL 450
  130. OpSourceExtension "GL_EXT_buffer_reference"
  131. OpName %main "main"
  132. OpName %ufoo "ufoo"
  133. OpMemberName %ufoo 0 "data"
  134. OpMemberName %ufoo 1 "offset"
  135. OpName %bufStruct "bufStruct"
  136. OpMemberName %bufStruct 0 "a"
  137. OpMemberName %bufStruct 1 "b"
  138. OpName %u_info "u_info"
  139. OpMemberDecorate %ufoo 0 Offset 0
  140. OpMemberDecorate %ufoo 1 Offset 8
  141. OpDecorate %ufoo Block
  142. OpDecorate %_arr_int_uint_2 ArrayStride 16
  143. OpMemberDecorate %bufStruct 0 Offset 0
  144. OpMemberDecorate %bufStruct 1 Offset 32
  145. OpDecorate %bufStruct Block
  146. OpDecorate %u_info DescriptorSet 11
  147. ;CHECK: OpDecorate %u_info DescriptorSet 11
  148. OpDecorate %u_info Binding 7
  149. ;CHECK: OpDecorate %u_info Binding 7
  150. %void = OpTypeVoid
  151. %3 = OpTypeFunction %void
  152. OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_bufStruct PhysicalStorageBuffer
  153. %uint = OpTypeInt 32 0
  154. %ufoo = OpTypeStruct %_ptr_PhysicalStorageBuffer_bufStruct %uint
  155. %int = OpTypeInt 32 1
  156. %uint_2 = OpConstant %uint 2
  157. %_arr_int_uint_2 = OpTypeArray %int %uint_2
  158. %bufStruct = OpTypeStruct %_arr_int_uint_2 %int
  159. %_ptr_PhysicalStorageBuffer_bufStruct = OpTypePointer PhysicalStorageBuffer %bufStruct
  160. %_ptr_Uniform_ufoo = OpTypePointer Uniform %ufoo
  161. %u_info = OpVariable %_ptr_Uniform_ufoo Uniform
  162. %int_0 = OpConstant %int 0
  163. %_ptr_Uniform__ptr_PhysicalStorageBuffer_bufStruct = OpTypePointer Uniform %_ptr_PhysicalStorageBuffer_bufStruct
  164. %int_1 = OpConstant %int 1
  165. %int_3239 = OpConstant %int 3239
  166. %_ptr_PhysicalStorageBuffer_int = OpTypePointer PhysicalStorageBuffer %int
  167. %main = OpFunction %void None %3
  168. %5 = OpLabel
  169. %17 = OpAccessChain %_ptr_Uniform__ptr_PhysicalStorageBuffer_bufStruct %u_info %int_0
  170. %18 = OpLoad %_ptr_PhysicalStorageBuffer_bufStruct %17
  171. %22 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %18 %int_1
  172. OpReturn
  173. OpFunctionEnd
  174. )";
  175. // clang-format off
  176. SinglePassRunAndMatch<SwitchDescriptorSetPass>(spirv, true, 7, 31);
  177. }
  178. } // namespace
  179. } // namespace opt
  180. } // namespace spvtools