2
0

desc_sroa_test.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. // Copyright (c) 2019 Google LLC
  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. #include <string>
  15. #include "test/opt/pass_fixture.h"
  16. #include "test/opt/pass_utils.h"
  17. namespace spvtools {
  18. namespace opt {
  19. namespace {
  20. using DescriptorScalarReplacementTest = PassTest<::testing::Test>;
  21. std::string GetStructureArrayTestSpirv() {
  22. // The SPIR-V for the following high-level shader:
  23. // Flattening structures and arrays should result in the following binding
  24. // numbers. Only the ones that are actually used in the shader should be in
  25. // the final SPIR-V.
  26. //
  27. // globalS[0][0].t[0] 0 (used)
  28. // globalS[0][0].t[1] 1
  29. // globalS[0][0].s[0] 2 (used)
  30. // globalS[0][0].s[1] 3
  31. // globalS[0][1].t[0] 4
  32. // globalS[0][1].t[1] 5
  33. // globalS[0][1].s[0] 6
  34. // globalS[0][1].s[1] 7
  35. // globalS[1][0].t[0] 8
  36. // globalS[1][0].t[1] 9
  37. // globalS[1][0].s[0] 10
  38. // globalS[1][0].s[1] 11
  39. // globalS[1][1].t[0] 12
  40. // globalS[1][1].t[1] 13 (used)
  41. // globalS[1][1].s[0] 14
  42. // globalS[1][1].s[1] 15 (used)
  43. /*
  44. struct S {
  45. Texture2D t[2];
  46. SamplerState s[2];
  47. };
  48. S globalS[2][2];
  49. float4 main() : SV_Target {
  50. return globalS[0][0].t[0].Sample(globalS[0][0].s[0], float2(0,0)) +
  51. globalS[1][1].t[1].Sample(globalS[1][1].s[1], float2(0,0));
  52. }
  53. */
  54. return R"(
  55. OpCapability Shader
  56. OpMemoryModel Logical GLSL450
  57. OpEntryPoint Fragment %main "main" %out_var_SV_Target
  58. OpExecutionMode %main OriginUpperLeft
  59. OpName %S "S"
  60. OpMemberName %S 0 "t"
  61. OpMemberName %S 1 "s"
  62. OpName %type_2d_image "type.2d.image"
  63. OpName %type_sampler "type.sampler"
  64. OpName %globalS "globalS"
  65. OpName %out_var_SV_Target "out.var.SV_Target"
  66. OpName %main "main"
  67. OpName %src_main "src.main"
  68. OpName %bb_entry "bb.entry"
  69. OpName %type_sampled_image "type.sampled.image"
  70. OpDecorate %out_var_SV_Target Location 0
  71. OpDecorate %globalS DescriptorSet 0
  72. OpDecorate %globalS Binding 0
  73. %int = OpTypeInt 32 1
  74. %int_0 = OpConstant %int 0
  75. %int_1 = OpConstant %int 1
  76. %float = OpTypeFloat 32
  77. %float_0 = OpConstant %float 0
  78. %v2float = OpTypeVector %float 2
  79. %10 = OpConstantComposite %v2float %float_0 %float_0
  80. %uint = OpTypeInt 32 0
  81. %uint_2 = OpConstant %uint 2
  82. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  83. %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
  84. %type_sampler = OpTypeSampler
  85. %_arr_type_sampler_uint_2 = OpTypeArray %type_sampler %uint_2
  86. %S = OpTypeStruct %_arr_type_2d_image_uint_2 %_arr_type_sampler_uint_2
  87. %_arr_S_uint_2 = OpTypeArray %S %uint_2
  88. %_arr__arr_S_uint_2_uint_2 = OpTypeArray %_arr_S_uint_2 %uint_2
  89. %_ptr_UniformConstant__arr__arr_S_uint_2_uint_2 = OpTypePointer UniformConstant %_arr__arr_S_uint_2_uint_2
  90. %v4float = OpTypeVector %float 4
  91. %_ptr_Output_v4float = OpTypePointer Output %v4float
  92. %void = OpTypeVoid
  93. %24 = OpTypeFunction %void
  94. %28 = OpTypeFunction %v4float
  95. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  96. %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
  97. %type_sampled_image = OpTypeSampledImage %type_2d_image
  98. %globalS = OpVariable %_ptr_UniformConstant__arr__arr_S_uint_2_uint_2 UniformConstant
  99. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  100. %main = OpFunction %void None %24
  101. %25 = OpLabel
  102. %26 = OpFunctionCall %v4float %src_main
  103. OpStore %out_var_SV_Target %26
  104. OpReturn
  105. OpFunctionEnd
  106. %src_main = OpFunction %v4float None %28
  107. %bb_entry = OpLabel
  108. %31 = OpAccessChain %_ptr_UniformConstant_type_2d_image %globalS %int_0 %int_0 %int_0 %int_0
  109. %32 = OpLoad %type_2d_image %31
  110. %34 = OpAccessChain %_ptr_UniformConstant_type_sampler %globalS %int_0 %int_0 %int_1 %int_0
  111. %35 = OpLoad %type_sampler %34
  112. %37 = OpSampledImage %type_sampled_image %32 %35
  113. %38 = OpImageSampleImplicitLod %v4float %37 %10 None
  114. %39 = OpAccessChain %_ptr_UniformConstant_type_2d_image %globalS %int_1 %int_1 %int_0 %int_1
  115. %40 = OpLoad %type_2d_image %39
  116. %41 = OpAccessChain %_ptr_UniformConstant_type_sampler %globalS %int_1 %int_1 %int_1 %int_1
  117. %42 = OpLoad %type_sampler %41
  118. %43 = OpSampledImage %type_sampled_image %40 %42
  119. %44 = OpImageSampleImplicitLod %v4float %43 %10 None
  120. %45 = OpFAdd %v4float %38 %44
  121. OpReturnValue %45
  122. OpFunctionEnd
  123. )";
  124. }
  125. TEST_F(DescriptorScalarReplacementTest, ExpandArrayOfTextures) {
  126. const std::string text = R"(
  127. ; CHECK: OpDecorate [[var1:%\w+]] DescriptorSet 0
  128. ; CHECK: OpDecorate [[var1]] Binding 0
  129. ; CHECK: OpDecorate [[var2:%\w+]] DescriptorSet 0
  130. ; CHECK: OpDecorate [[var2]] Binding 1
  131. ; CHECK: OpDecorate [[var3:%\w+]] DescriptorSet 0
  132. ; CHECK: OpDecorate [[var3]] Binding 2
  133. ; CHECK: OpDecorate [[var4:%\w+]] DescriptorSet 0
  134. ; CHECK: OpDecorate [[var4]] Binding 3
  135. ; CHECK: OpDecorate [[var5:%\w+]] DescriptorSet 0
  136. ; CHECK: OpDecorate [[var5]] Binding 4
  137. ; CHECK: [[image_type:%\w+]] = OpTypeImage
  138. ; CHECK: [[ptr_type:%\w+]] = OpTypePointer UniformConstant [[image_type]]
  139. ; CHECK: [[var1]] = OpVariable [[ptr_type]] UniformConstant
  140. ; CHECK: [[var2]] = OpVariable [[ptr_type]] UniformConstant
  141. ; CHECK: [[var3]] = OpVariable [[ptr_type]] UniformConstant
  142. ; CHECK: [[var4]] = OpVariable [[ptr_type]] UniformConstant
  143. ; CHECK: [[var5]] = OpVariable [[ptr_type]] UniformConstant
  144. ; CHECK: OpLoad [[image_type]] [[var1]]
  145. ; CHECK: OpLoad [[image_type]] [[var2]]
  146. ; CHECK: OpLoad [[image_type]] [[var3]]
  147. ; CHECK: OpLoad [[image_type]] [[var4]]
  148. ; CHECK: OpLoad [[image_type]] [[var5]]
  149. OpCapability Shader
  150. OpMemoryModel Logical GLSL450
  151. OpEntryPoint Fragment %main "main"
  152. OpExecutionMode %main OriginUpperLeft
  153. OpSource HLSL 600
  154. OpDecorate %MyTextures DescriptorSet 0
  155. OpDecorate %MyTextures Binding 0
  156. %int = OpTypeInt 32 1
  157. %int_0 = OpConstant %int 0
  158. %int_1 = OpConstant %int 1
  159. %int_2 = OpConstant %int 2
  160. %int_3 = OpConstant %int 3
  161. %int_4 = OpConstant %int 4
  162. %uint = OpTypeInt 32 0
  163. %uint_5 = OpConstant %uint 5
  164. %float = OpTypeFloat 32
  165. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  166. %_arr_type_2d_image_uint_5 = OpTypeArray %type_2d_image %uint_5
  167. %_ptr_UniformConstant__arr_type_2d_image_uint_5 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_5
  168. %v2float = OpTypeVector %float 2
  169. %void = OpTypeVoid
  170. %26 = OpTypeFunction %void
  171. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  172. %MyTextures = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_5 UniformConstant
  173. %main = OpFunction %void None %26
  174. %28 = OpLabel
  175. %29 = OpUndef %v2float
  176. %30 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_0
  177. %31 = OpLoad %type_2d_image %30
  178. %35 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_1
  179. %36 = OpLoad %type_2d_image %35
  180. %40 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_2
  181. %41 = OpLoad %type_2d_image %40
  182. %45 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_3
  183. %46 = OpLoad %type_2d_image %45
  184. %50 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_4
  185. %51 = OpLoad %type_2d_image %50
  186. OpReturn
  187. OpFunctionEnd
  188. )";
  189. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  190. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  191. }
  192. TEST_F(DescriptorScalarReplacementTest, ExpandArrayOfSamplers) {
  193. const std::string text = R"(
  194. ; CHECK: OpDecorate [[var1:%\w+]] DescriptorSet 0
  195. ; CHECK: OpDecorate [[var1]] Binding 1
  196. ; CHECK: OpDecorate [[var2:%\w+]] DescriptorSet 0
  197. ; CHECK: OpDecorate [[var2]] Binding 2
  198. ; CHECK: OpDecorate [[var3:%\w+]] DescriptorSet 0
  199. ; CHECK: OpDecorate [[var3]] Binding 3
  200. ; CHECK: [[sampler_type:%\w+]] = OpTypeSampler
  201. ; CHECK: [[ptr_type:%\w+]] = OpTypePointer UniformConstant [[sampler_type]]
  202. ; CHECK: [[var1]] = OpVariable [[ptr_type]] UniformConstant
  203. ; CHECK: [[var2]] = OpVariable [[ptr_type]] UniformConstant
  204. ; CHECK: [[var3]] = OpVariable [[ptr_type]] UniformConstant
  205. ; CHECK: OpLoad [[sampler_type]] [[var1]]
  206. ; CHECK: OpLoad [[sampler_type]] [[var2]]
  207. ; CHECK: OpLoad [[sampler_type]] [[var3]]
  208. OpCapability Shader
  209. OpMemoryModel Logical GLSL450
  210. OpEntryPoint Fragment %main "main"
  211. OpExecutionMode %main OriginUpperLeft
  212. OpSource HLSL 600
  213. OpDecorate %MySampler DescriptorSet 0
  214. OpDecorate %MySampler Binding 1
  215. %int = OpTypeInt 32 1
  216. %int_0 = OpConstant %int 0
  217. %int_1 = OpConstant %int 1
  218. %int_2 = OpConstant %int 2
  219. %uint = OpTypeInt 32 0
  220. %uint_3 = OpConstant %uint 3
  221. %type_sampler = OpTypeSampler
  222. %_arr_type_sampler_uint_3 = OpTypeArray %type_sampler %uint_3
  223. %_ptr_UniformConstant__arr_type_sampler_uint_3 = OpTypePointer UniformConstant %_arr_type_sampler_uint_3
  224. %void = OpTypeVoid
  225. %26 = OpTypeFunction %void
  226. %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
  227. %MySampler = OpVariable %_ptr_UniformConstant__arr_type_sampler_uint_3 UniformConstant
  228. %main = OpFunction %void None %26
  229. %28 = OpLabel
  230. %31 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_0
  231. %32 = OpLoad %type_sampler %31
  232. %35 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_1
  233. %36 = OpLoad %type_sampler %35
  234. %40 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_2
  235. %41 = OpLoad %type_sampler %40
  236. OpReturn
  237. OpFunctionEnd
  238. )";
  239. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  240. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  241. }
  242. TEST_F(DescriptorScalarReplacementTest, ExpandArrayOfSSBOs) {
  243. // Tests the expansion of an SSBO. Also check that an access chain with more
  244. // than 1 index is correctly handled.
  245. const std::string text = R"(
  246. ; CHECK: OpDecorate [[var1:%\w+]] DescriptorSet 0
  247. ; CHECK: OpDecorate [[var1]] Binding 0
  248. ; CHECK: OpDecorate [[var2:%\w+]] DescriptorSet 0
  249. ; CHECK: OpDecorate [[var2]] Binding 1
  250. ; CHECK: OpTypeStruct
  251. ; CHECK: [[struct_type:%\w+]] = OpTypeStruct
  252. ; CHECK: [[ptr_type:%\w+]] = OpTypePointer Uniform [[struct_type]]
  253. ; CHECK: [[var1]] = OpVariable [[ptr_type]] Uniform
  254. ; CHECK: [[var2]] = OpVariable [[ptr_type]] Uniform
  255. ; CHECK: [[ac1:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var1]] %uint_0 %uint_0 %uint_0
  256. ; CHECK: OpLoad %v4float [[ac1]]
  257. ; CHECK: [[ac2:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var2]] %uint_0 %uint_0 %uint_0
  258. ; CHECK: OpLoad %v4float [[ac2]]
  259. OpCapability Shader
  260. OpMemoryModel Logical GLSL450
  261. OpEntryPoint Fragment %main "main"
  262. OpExecutionMode %main OriginUpperLeft
  263. OpSource HLSL 600
  264. OpDecorate %buffers DescriptorSet 0
  265. OpDecorate %buffers Binding 0
  266. OpMemberDecorate %S 0 Offset 0
  267. OpDecorate %_runtimearr_S ArrayStride 16
  268. OpMemberDecorate %type_StructuredBuffer_S 0 Offset 0
  269. OpMemberDecorate %type_StructuredBuffer_S 0 NonWritable
  270. OpDecorate %type_StructuredBuffer_S BufferBlock
  271. %uint = OpTypeInt 32 0
  272. %uint_0 = OpConstant %uint 0
  273. %uint_1 = OpConstant %uint 1
  274. %uint_2 = OpConstant %uint 2
  275. %float = OpTypeFloat 32
  276. %v4float = OpTypeVector %float 4
  277. %S = OpTypeStruct %v4float
  278. %_runtimearr_S = OpTypeRuntimeArray %S
  279. %type_StructuredBuffer_S = OpTypeStruct %_runtimearr_S
  280. %_arr_type_StructuredBuffer_S_uint_2 = OpTypeArray %type_StructuredBuffer_S %uint_2
  281. %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 = OpTypePointer Uniform %_arr_type_StructuredBuffer_S_uint_2
  282. %_ptr_Uniform_type_StructuredBuffer_S = OpTypePointer Uniform %type_StructuredBuffer_S
  283. %void = OpTypeVoid
  284. %19 = OpTypeFunction %void
  285. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  286. %buffers = OpVariable %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 Uniform
  287. %main = OpFunction %void None %19
  288. %21 = OpLabel
  289. %22 = OpAccessChain %_ptr_Uniform_v4float %buffers %uint_0 %uint_0 %uint_0 %uint_0
  290. %23 = OpLoad %v4float %22
  291. %24 = OpAccessChain %_ptr_Uniform_type_StructuredBuffer_S %buffers %uint_1
  292. %25 = OpAccessChain %_ptr_Uniform_v4float %24 %uint_0 %uint_0 %uint_0
  293. %26 = OpLoad %v4float %25
  294. OpReturn
  295. OpFunctionEnd
  296. )";
  297. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  298. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  299. }
  300. TEST_F(DescriptorScalarReplacementTest, NameNewVariables) {
  301. // Checks that if the original variable has a name, then the new variables
  302. // will have a name derived from that name.
  303. const std::string text = R"(
  304. ; CHECK: OpName [[var1:%\w+]] "SSBO[0]"
  305. ; CHECK: OpName [[var2:%\w+]] "SSBO[1]"
  306. ; CHECK: OpDecorate [[var1]] DescriptorSet 0
  307. ; CHECK: OpDecorate [[var1]] Binding 0
  308. ; CHECK: OpDecorate [[var2]] DescriptorSet 0
  309. ; CHECK: OpDecorate [[var2]] Binding 1
  310. ; CHECK: OpTypeStruct
  311. ; CHECK: [[struct_type:%\w+]] = OpTypeStruct
  312. ; CHECK: [[ptr_type:%\w+]] = OpTypePointer Uniform [[struct_type]]
  313. ; CHECK: [[var1]] = OpVariable [[ptr_type]] Uniform
  314. ; CHECK: [[var2]] = OpVariable [[ptr_type]] Uniform
  315. ; CHECK: [[ac1:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var1]] %uint_0 %uint_0 %uint_0
  316. ; CHECK: OpLoad %v4float [[ac1]]
  317. ; CHECK: [[ac2:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var2]] %uint_0 %uint_0 %uint_0
  318. ; CHECK: OpLoad %v4float [[ac2]]
  319. OpCapability Shader
  320. OpMemoryModel Logical GLSL450
  321. OpEntryPoint Fragment %main "main"
  322. OpExecutionMode %main OriginUpperLeft
  323. OpSource HLSL 600
  324. OpName %buffers "SSBO"
  325. OpDecorate %buffers DescriptorSet 0
  326. OpDecorate %buffers Binding 0
  327. OpMemberDecorate %S 0 Offset 0
  328. OpDecorate %_runtimearr_S ArrayStride 16
  329. OpMemberDecorate %type_StructuredBuffer_S 0 Offset 0
  330. OpMemberDecorate %type_StructuredBuffer_S 0 NonWritable
  331. OpDecorate %type_StructuredBuffer_S BufferBlock
  332. %uint = OpTypeInt 32 0
  333. %uint_0 = OpConstant %uint 0
  334. %uint_1 = OpConstant %uint 1
  335. %uint_2 = OpConstant %uint 2
  336. %float = OpTypeFloat 32
  337. %v4float = OpTypeVector %float 4
  338. %S = OpTypeStruct %v4float
  339. %_runtimearr_S = OpTypeRuntimeArray %S
  340. %type_StructuredBuffer_S = OpTypeStruct %_runtimearr_S
  341. %_arr_type_StructuredBuffer_S_uint_2 = OpTypeArray %type_StructuredBuffer_S %uint_2
  342. %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 = OpTypePointer Uniform %_arr_type_StructuredBuffer_S_uint_2
  343. %_ptr_Uniform_type_StructuredBuffer_S = OpTypePointer Uniform %type_StructuredBuffer_S
  344. %void = OpTypeVoid
  345. %19 = OpTypeFunction %void
  346. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  347. %buffers = OpVariable %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 Uniform
  348. %main = OpFunction %void None %19
  349. %21 = OpLabel
  350. %22 = OpAccessChain %_ptr_Uniform_v4float %buffers %uint_0 %uint_0 %uint_0 %uint_0
  351. %23 = OpLoad %v4float %22
  352. %24 = OpAccessChain %_ptr_Uniform_type_StructuredBuffer_S %buffers %uint_1
  353. %25 = OpAccessChain %_ptr_Uniform_v4float %24 %uint_0 %uint_0 %uint_0
  354. %26 = OpLoad %v4float %25
  355. OpReturn
  356. OpFunctionEnd
  357. )";
  358. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  359. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  360. }
  361. TEST_F(DescriptorScalarReplacementTest, DontExpandCBuffers) {
  362. // Checks that constant buffers are not expanded.
  363. // Constant buffers are represented as global structures, but they should not
  364. // be replaced with new variables for their elements.
  365. /*
  366. cbuffer MyCbuffer : register(b1) {
  367. float2 a;
  368. float2 b;
  369. };
  370. float main() : A {
  371. return a.x + b.y;
  372. }
  373. */
  374. const std::string text = R"(
  375. ; CHECK: OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_0 %int_0
  376. ; CHECK: OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_1 %int_1
  377. OpCapability Shader
  378. OpMemoryModel Logical GLSL450
  379. OpEntryPoint Vertex %main "main" %out_var_A
  380. OpSource HLSL 600
  381. OpName %type_MyCbuffer "type.MyCbuffer"
  382. OpMemberName %type_MyCbuffer 0 "a"
  383. OpMemberName %type_MyCbuffer 1 "b"
  384. OpName %MyCbuffer "MyCbuffer"
  385. OpName %out_var_A "out.var.A"
  386. OpName %main "main"
  387. OpDecorate %out_var_A Location 0
  388. OpDecorate %MyCbuffer DescriptorSet 0
  389. OpDecorate %MyCbuffer Binding 1
  390. OpMemberDecorate %type_MyCbuffer 0 Offset 0
  391. OpMemberDecorate %type_MyCbuffer 1 Offset 8
  392. OpDecorate %type_MyCbuffer Block
  393. %int = OpTypeInt 32 1
  394. %int_0 = OpConstant %int 0
  395. %int_1 = OpConstant %int 1
  396. %float = OpTypeFloat 32
  397. %v2float = OpTypeVector %float 2
  398. %type_MyCbuffer = OpTypeStruct %v2float %v2float
  399. %_ptr_Uniform_type_MyCbuffer = OpTypePointer Uniform %type_MyCbuffer
  400. %_ptr_Output_float = OpTypePointer Output %float
  401. %void = OpTypeVoid
  402. %13 = OpTypeFunction %void
  403. %_ptr_Uniform_float = OpTypePointer Uniform %float
  404. %MyCbuffer = OpVariable %_ptr_Uniform_type_MyCbuffer Uniform
  405. %out_var_A = OpVariable %_ptr_Output_float Output
  406. %main = OpFunction %void None %13
  407. %15 = OpLabel
  408. %16 = OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_0 %int_0
  409. %17 = OpLoad %float %16
  410. %18 = OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_1 %int_1
  411. %19 = OpLoad %float %18
  412. %20 = OpFAdd %float %17 %19
  413. OpStore %out_var_A %20
  414. OpReturn
  415. OpFunctionEnd
  416. )";
  417. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  418. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  419. }
  420. TEST_F(DescriptorScalarReplacementTest, DontExpandStructuredBuffers) {
  421. // Checks that structured buffers are not expanded.
  422. // Structured buffers are represented as global structures, that have one
  423. // member which is a runtime array.
  424. /*
  425. struct S {
  426. float2 a;
  427. float2 b;
  428. };
  429. RWStructuredBuffer<S> sb;
  430. float main() : A {
  431. return sb[0].a.x + sb[0].b.x;
  432. }
  433. */
  434. const std::string text = R"(
  435. ; CHECK: OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_0 %int_0
  436. ; CHECK: OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_1 %int_0
  437. OpCapability Shader
  438. OpMemoryModel Logical GLSL450
  439. OpEntryPoint Vertex %main "main" %out_var_A
  440. OpName %type_RWStructuredBuffer_S "type.RWStructuredBuffer.S"
  441. OpName %S "S"
  442. OpMemberName %S 0 "a"
  443. OpMemberName %S 1 "b"
  444. OpName %sb "sb"
  445. OpName %out_var_A "out.var.A"
  446. OpName %main "main"
  447. OpDecorate %out_var_A Location 0
  448. OpDecorate %sb DescriptorSet 0
  449. OpDecorate %sb Binding 0
  450. OpMemberDecorate %S 0 Offset 0
  451. OpMemberDecorate %S 1 Offset 8
  452. OpDecorate %_runtimearr_S ArrayStride 16
  453. OpMemberDecorate %type_RWStructuredBuffer_S 0 Offset 0
  454. OpDecorate %type_RWStructuredBuffer_S BufferBlock
  455. %int = OpTypeInt 32 1
  456. %int_0 = OpConstant %int 0
  457. %uint = OpTypeInt 32 0
  458. %uint_0 = OpConstant %uint 0
  459. %int_1 = OpConstant %int 1
  460. %float = OpTypeFloat 32
  461. %v2float = OpTypeVector %float 2
  462. %S = OpTypeStruct %v2float %v2float
  463. %_runtimearr_S = OpTypeRuntimeArray %S
  464. %type_RWStructuredBuffer_S = OpTypeStruct %_runtimearr_S
  465. %_ptr_Uniform_type_RWStructuredBuffer_S = OpTypePointer Uniform %type_RWStructuredBuffer_S
  466. %_ptr_Output_float = OpTypePointer Output %float
  467. %void = OpTypeVoid
  468. %17 = OpTypeFunction %void
  469. %_ptr_Uniform_float = OpTypePointer Uniform %float
  470. %sb = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_S Uniform
  471. %out_var_A = OpVariable %_ptr_Output_float Output
  472. %main = OpFunction %void None %17
  473. %19 = OpLabel
  474. %20 = OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_0 %int_0
  475. %21 = OpLoad %float %20
  476. %22 = OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_1 %int_0
  477. %23 = OpLoad %float %22
  478. %24 = OpFAdd %float %21 %23
  479. OpStore %out_var_A %24
  480. OpReturn
  481. OpFunctionEnd
  482. )";
  483. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  484. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  485. }
  486. TEST_F(DescriptorScalarReplacementTest, StructureArrayNames) {
  487. // Checks that names are properly generated for multi-dimension arrays and
  488. // structure members.
  489. const std::string checks = R"(
  490. ; CHECK: OpName %globalS_0__0__t_0_ "globalS[0][0].t[0]"
  491. ; CHECK: OpName %globalS_0__0__s_0_ "globalS[0][0].s[0]"
  492. ; CHECK: OpName %globalS_1__1__t_1_ "globalS[1][1].t[1]"
  493. ; CHECK: OpName %globalS_1__1__s_1_ "globalS[1][1].s[1]"
  494. )";
  495. const std::string text = checks + GetStructureArrayTestSpirv();
  496. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  497. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  498. }
  499. TEST_F(DescriptorScalarReplacementTest,
  500. FlattensArraysOfStructsButNoResourceArrays) {
  501. // Check that only the composite array is flattenned, but internal resource
  502. // arrays are left as-is.
  503. const std::string checks = R"(
  504. ; CHECK: OpName %globalS_0__0__t "globalS[0][0].t"
  505. ; CHECK: OpName %globalS_0__0__s "globalS[0][0].s"
  506. ; CHECK: OpName %globalS_1__1__t "globalS[1][1].t"
  507. ; CHECK: OpName %globalS_1__1__s "globalS[1][1].s"
  508. ; CHECK-NOT: OpName %globalS_1__1__t_0_
  509. ; CHECK-NOT: OpName %globalS_1__1__s_0_
  510. )";
  511. const std::string text = checks + GetStructureArrayTestSpirv();
  512. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  513. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/false);
  514. }
  515. TEST_F(DescriptorScalarReplacementTest, FlattenNothingIfAskedTo) {
  516. // Not useful, but checks what happens if both are set to false.
  517. // In such case, nothing happens.
  518. const std::string checks = R"(
  519. ; CHECK: OpName %globalS
  520. ; CHECK-NOT: OpName %globalS_
  521. )";
  522. const std::string text = checks + GetStructureArrayTestSpirv();
  523. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  524. text, true, /* flatten_composites=*/false, /* flatten_arrays=*/false);
  525. }
  526. TEST_F(DescriptorScalarReplacementTest, StructureArrayBindings) {
  527. // Checks that flattening structures and arrays results in correct binding
  528. // numbers.
  529. const std::string checks = R"(
  530. ; CHECK: OpDecorate %globalS_0__0__t_0_ Binding 0
  531. ; CHECK: OpDecorate %globalS_0__0__s_0_ Binding 2
  532. ; CHECK: OpDecorate %globalS_1__1__t_1_ Binding 13
  533. ; CHECK: OpDecorate %globalS_1__1__s_1_ Binding 15
  534. )";
  535. const std::string text = checks + GetStructureArrayTestSpirv();
  536. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  537. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  538. }
  539. TEST_F(DescriptorScalarReplacementTest, StructureArrayReplacements) {
  540. // Checks that all access chains indexing into structures and/or arrays are
  541. // replaced with direct access to replacement variables.
  542. const std::string checks = R"(
  543. ; CHECK-NOT: OpAccessChain
  544. ; CHECK: OpLoad %type_2d_image %globalS_0__0__t_0_
  545. ; CHECK: OpLoad %type_sampler %globalS_0__0__s_0_
  546. ; CHECK: OpLoad %type_2d_image %globalS_1__1__t_1_
  547. ; CHECK: OpLoad %type_sampler %globalS_1__1__s_1_
  548. )";
  549. const std::string text = checks + GetStructureArrayTestSpirv();
  550. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  551. text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  552. }
  553. TEST_F(DescriptorScalarReplacementTest, ResourceStructAsFunctionParam) {
  554. // Checks that a mix of OpAccessChain, OpLoad, and OpCompositeExtract patterns
  555. // can be properly replaced with replacement variables.
  556. // This pattern can be seen when a global structure of resources is passed to
  557. // a function.
  558. /* High-level source:
  559. // globalS[0].t[0] binding: 0 (used)
  560. // globalS[0].t[1] binding: 1 (used)
  561. // globalS[0].tt[0].s[0] binding: 2
  562. // globalS[0].tt[0].s[1] binding: 3 (used)
  563. // globalS[0].tt[0].s[2] binding: 4
  564. // globalS[0].tt[1].s[0] binding: 5
  565. // globalS[0].tt[1].s[1] binding: 6
  566. // globalS[0].tt[1].s[2] binding: 7 (used)
  567. // globalS[1].t[0] binding: 8 (used)
  568. // globalS[1].t[1] binding: 9 (used)
  569. // globalS[1].tt[0].s[0] binding: 10
  570. // globalS[1].tt[0].s[1] binding: 11 (used)
  571. // globalS[1].tt[0].s[2] binding: 12
  572. // globalS[1].tt[1].s[0] binding: 13
  573. // globalS[1].tt[1].s[1] binding: 14
  574. // globalS[1].tt[1].s[2] binding: 15 (used)
  575. struct T {
  576. SamplerState s[3];
  577. };
  578. struct S {
  579. Texture2D t[2];
  580. T tt[2];
  581. };
  582. float4 tex2D(S x, float2 v) {
  583. return x.t[0].Sample(x.tt[0].s[1], v) + x.t[1].Sample(x.tt[1].s[2], v);
  584. }
  585. S globalS[2];
  586. float4 main() : SV_Target {
  587. return tex2D(globalS[0], float2(0,0)) + tex2D(globalS[1], float2(0,0)) ;
  588. }
  589. */
  590. const std::string shader = R"(
  591. OpCapability Shader
  592. OpMemoryModel Logical GLSL450
  593. OpEntryPoint Fragment %main "main" %out_var_SV_Target
  594. OpExecutionMode %main OriginUpperLeft
  595. OpName %S "S"
  596. OpMemberName %S 0 "t"
  597. OpMemberName %S 1 "tt"
  598. OpName %type_2d_image "type.2d.image"
  599. OpName %T "T"
  600. OpMemberName %T 0 "s"
  601. OpName %type_sampler "type.sampler"
  602. OpName %globalS "globalS"
  603. OpName %out_var_SV_Target "out.var.SV_Target"
  604. OpName %main "main"
  605. OpName %type_sampled_image "type.sampled.image"
  606. OpDecorate %out_var_SV_Target Location 0
  607. OpDecorate %globalS DescriptorSet 0
  608. OpDecorate %globalS Binding 0
  609. %int = OpTypeInt 32 1
  610. %int_0 = OpConstant %int 0
  611. %float = OpTypeFloat 32
  612. %float_0 = OpConstant %float 0
  613. %v2float = OpTypeVector %float 2
  614. %14 = OpConstantComposite %v2float %float_0 %float_0
  615. %int_1 = OpConstant %int 1
  616. %uint = OpTypeInt 32 0
  617. %uint_2 = OpConstant %uint 2
  618. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  619. %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
  620. %uint_3 = OpConstant %uint 3
  621. %type_sampler = OpTypeSampler
  622. %_arr_type_sampler_uint_3 = OpTypeArray %type_sampler %uint_3
  623. %T = OpTypeStruct %_arr_type_sampler_uint_3
  624. %_arr_T_uint_2 = OpTypeArray %T %uint_2
  625. %S = OpTypeStruct %_arr_type_2d_image_uint_2 %_arr_T_uint_2
  626. %_arr_S_uint_2 = OpTypeArray %S %uint_2
  627. %_ptr_UniformConstant__arr_S_uint_2 = OpTypePointer UniformConstant %_arr_S_uint_2
  628. %v4float = OpTypeVector %float 4
  629. %_ptr_Output_v4float = OpTypePointer Output %v4float
  630. %void = OpTypeVoid
  631. %27 = OpTypeFunction %void
  632. %_ptr_UniformConstant_S = OpTypePointer UniformConstant %S
  633. %type_sampled_image = OpTypeSampledImage %type_2d_image
  634. %globalS = OpVariable %_ptr_UniformConstant__arr_S_uint_2 UniformConstant
  635. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  636. %main = OpFunction %void None %27
  637. %29 = OpLabel
  638. %30 = OpAccessChain %_ptr_UniformConstant_S %globalS %int_0
  639. %31 = OpLoad %S %30
  640. %32 = OpCompositeExtract %_arr_type_2d_image_uint_2 %31 0
  641. %33 = OpCompositeExtract %type_2d_image %32 0
  642. %34 = OpCompositeExtract %type_2d_image %32 1
  643. %35 = OpCompositeExtract %_arr_T_uint_2 %31 1
  644. %36 = OpCompositeExtract %T %35 0
  645. %37 = OpCompositeExtract %_arr_type_sampler_uint_3 %36 0
  646. %38 = OpCompositeExtract %type_sampler %37 1
  647. %39 = OpCompositeExtract %T %35 1
  648. %40 = OpCompositeExtract %_arr_type_sampler_uint_3 %39 0
  649. %41 = OpCompositeExtract %type_sampler %40 2
  650. %42 = OpSampledImage %type_sampled_image %33 %38
  651. %43 = OpImageSampleImplicitLod %v4float %42 %14 None
  652. %44 = OpSampledImage %type_sampled_image %34 %41
  653. %45 = OpImageSampleImplicitLod %v4float %44 %14 None
  654. %46 = OpFAdd %v4float %43 %45
  655. %47 = OpAccessChain %_ptr_UniformConstant_S %globalS %int_1
  656. %48 = OpLoad %S %47
  657. %49 = OpCompositeExtract %_arr_type_2d_image_uint_2 %48 0
  658. %50 = OpCompositeExtract %type_2d_image %49 0
  659. %51 = OpCompositeExtract %type_2d_image %49 1
  660. %52 = OpCompositeExtract %_arr_T_uint_2 %48 1
  661. %53 = OpCompositeExtract %T %52 0
  662. %54 = OpCompositeExtract %_arr_type_sampler_uint_3 %53 0
  663. %55 = OpCompositeExtract %type_sampler %54 1
  664. %56 = OpCompositeExtract %T %52 1
  665. %57 = OpCompositeExtract %_arr_type_sampler_uint_3 %56 0
  666. %58 = OpCompositeExtract %type_sampler %57 2
  667. %59 = OpSampledImage %type_sampled_image %50 %55
  668. %60 = OpImageSampleImplicitLod %v4float %59 %14 None
  669. %61 = OpSampledImage %type_sampled_image %51 %58
  670. %62 = OpImageSampleImplicitLod %v4float %61 %14 None
  671. %63 = OpFAdd %v4float %60 %62
  672. %64 = OpFAdd %v4float %46 %63
  673. OpStore %out_var_SV_Target %64
  674. OpReturn
  675. OpFunctionEnd
  676. )";
  677. const std::string checks = R"(
  678. ; CHECK: OpName %globalS_0__t_0_ "globalS[0].t[0]"
  679. ; CHECK: OpName %globalS_0__t_1_ "globalS[0].t[1]"
  680. ; CHECK: OpName %globalS_1__t_0_ "globalS[1].t[0]"
  681. ; CHECK: OpName %globalS_1__t_1_ "globalS[1].t[1]"
  682. ; CHECK: OpName %globalS_0__tt_0__s_1_ "globalS[0].tt[0].s[1]"
  683. ; CHECK: OpName %globalS_0__tt_1__s_2_ "globalS[0].tt[1].s[2]"
  684. ; CHECK: OpName %globalS_1__tt_0__s_1_ "globalS[1].tt[0].s[1]"
  685. ; CHECK: OpName %globalS_1__tt_1__s_2_ "globalS[1].tt[1].s[2]"
  686. ; CHECK: OpDecorate %globalS_0__t_0_ Binding 0
  687. ; CHECK: OpDecorate %globalS_0__t_1_ Binding 1
  688. ; CHECK: OpDecorate %globalS_1__t_0_ Binding 8
  689. ; CHECK: OpDecorate %globalS_1__t_1_ Binding 9
  690. ; CHECK: OpDecorate %globalS_0__tt_0__s_1_ Binding 3
  691. ; CHECK: OpDecorate %globalS_0__tt_1__s_2_ Binding 7
  692. ; CHECK: OpDecorate %globalS_1__tt_0__s_1_ Binding 11
  693. ; CHECK: OpDecorate %globalS_1__tt_1__s_2_ Binding 15
  694. ; CHECK: %globalS_0__t_0_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  695. ; CHECK: %globalS_0__t_1_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  696. ; CHECK: %globalS_1__t_0_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  697. ; CHECK: %globalS_1__t_1_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  698. ; CHECK: %globalS_0__tt_0__s_1_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  699. ; CHECK: %globalS_0__tt_1__s_2_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  700. ; CHECK: %globalS_1__tt_0__s_1_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  701. ; CHECK: %globalS_1__tt_1__s_2_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  702. ; CHECK: [[img_1:%\w+]] = OpLoad %type_2d_image %globalS_0__t_0_
  703. ; CHECK: [[img_2:%\w+]] = OpLoad %type_2d_image %globalS_0__t_1_
  704. ; CHECK: [[sampler_1:%\w+]] = OpLoad %type_sampler %globalS_0__tt_0__s_1_
  705. ; CHECK: [[sampler_2:%\w+]] = OpLoad %type_sampler %globalS_0__tt_1__s_2_
  706. ; CHECK: [[sampled_img_1:%\w+]] = OpSampledImage %type_sampled_image [[img_1]] [[sampler_1]]
  707. ; CHECK: [[sample_1:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_1]]
  708. ; CHECK: [[sampled_img_2:%\w+]] = OpSampledImage %type_sampled_image [[img_2]] [[sampler_2]]
  709. ; CHECK: [[sample_2:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_2]]
  710. ; CHECK: OpFAdd %v4float [[sample_1]] [[sample_2]]
  711. ; CHECK: [[img_3:%\w+]] = OpLoad %type_2d_image %globalS_1__t_0_
  712. ; CHECK: [[img_4:%\w+]] = OpLoad %type_2d_image %globalS_1__t_1_
  713. ; CHECK: [[sampler_3:%\w+]] = OpLoad %type_sampler %globalS_1__tt_0__s_1_
  714. ; CHECK: [[sampler_4:%\w+]] = OpLoad %type_sampler %globalS_1__tt_1__s_2_
  715. ; CHECK: [[sampled_img_3:%\w+]] = OpSampledImage %type_sampled_image [[img_3]] [[sampler_3]]
  716. ; CHECK: [[sample_3:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_3]]
  717. ; CHECK: [[sampled_img_4:%\w+]] = OpSampledImage %type_sampled_image [[img_4]] [[sampler_4]]
  718. ; CHECK: [[sample_4:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_4]]
  719. ; CHECK: OpFAdd %v4float [[sample_3]] [[sample_4]]
  720. )";
  721. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  722. checks + shader, true, /* flatten_composites=*/true,
  723. /* flatten_arrays=*/true);
  724. }
  725. TEST_F(DescriptorScalarReplacementTest, BindingForResourceArrayOfStructs) {
  726. // Check that correct binding numbers are given to an array of descriptors
  727. // to structs.
  728. const std::string shader = R"(
  729. ; CHECK: OpDecorate {{%\w+}} Binding 0
  730. ; CHECK: OpDecorate {{%\w+}} Binding 1
  731. OpCapability Shader
  732. %1 = OpExtInstImport "GLSL.std.450"
  733. OpMemoryModel Logical GLSL450
  734. OpEntryPoint Fragment %2 "psmain"
  735. OpExecutionMode %2 OriginUpperLeft
  736. OpDecorate %5 DescriptorSet 0
  737. OpDecorate %5 Binding 0
  738. OpMemberDecorate %_struct_4 0 Offset 0
  739. OpMemberDecorate %_struct_4 1 Offset 4
  740. OpDecorate %_struct_4 Block
  741. %float = OpTypeFloat 32
  742. %int = OpTypeInt 32 1
  743. %int_0 = OpConstant %int 0
  744. %int_1 = OpConstant %int 1
  745. %uint = OpTypeInt 32 0
  746. %uint_2 = OpConstant %uint 2
  747. %_struct_4 = OpTypeStruct %float %int
  748. %_arr__struct_4_uint_2 = OpTypeArray %_struct_4 %uint_2
  749. %_ptr_Uniform__arr__struct_4_uint_2 = OpTypePointer Uniform %_arr__struct_4_uint_2
  750. %void = OpTypeVoid
  751. %25 = OpTypeFunction %void
  752. %_ptr_Uniform_int = OpTypePointer Uniform %int
  753. %5 = OpVariable %_ptr_Uniform__arr__struct_4_uint_2 Uniform
  754. %2 = OpFunction %void None %25
  755. %29 = OpLabel
  756. %40 = OpAccessChain %_ptr_Uniform_int %5 %int_0 %int_1
  757. %41 = OpAccessChain %_ptr_Uniform_int %5 %int_1 %int_1
  758. OpReturn
  759. OpFunctionEnd
  760. )";
  761. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  762. shader, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  763. }
  764. TEST_F(DescriptorScalarReplacementTest, MemberDecorationForResourceStruct) {
  765. // Check that an OpMemberDecorate instruction is correctly converted to a
  766. // OpDecorate instruction.
  767. const std::string shader = R"(
  768. ; CHECK: OpDecorate [[t:%\w+]] DescriptorSet 0
  769. ; CHECK: OpDecorate [[t]] Binding 0
  770. ; CHECK: OpDecorate [[t]] RelaxedPrecision
  771. ; CHECK: OpDecorate [[s:%\w+]] DescriptorSet 0
  772. ; CHECK: OpDecorate [[s]] Binding 1
  773. OpCapability Shader
  774. OpMemoryModel Logical GLSL450
  775. OpEntryPoint Fragment %PSMain "PSMain" %in_var_TEXCOORD %out_var_SV_Target
  776. OpExecutionMode %PSMain OriginUpperLeft
  777. OpSource HLSL 600
  778. OpName %sampler2D_h "sampler2D_h"
  779. OpMemberName %sampler2D_h 0 "t"
  780. OpMemberName %sampler2D_h 1 "s"
  781. OpName %type_2d_image "type.2d.image"
  782. OpName %type_sampler "type.sampler"
  783. OpName %_MainTex "_MainTex"
  784. OpName %in_var_TEXCOORD "in.var.TEXCOORD"
  785. OpName %out_var_SV_Target "out.var.SV_Target"
  786. OpName %PSMain "PSMain"
  787. OpName %type_sampled_image "type.sampled.image"
  788. OpDecorate %in_var_TEXCOORD Location 0
  789. OpDecorate %out_var_SV_Target Location 0
  790. OpDecorate %_MainTex DescriptorSet 0
  791. OpDecorate %_MainTex Binding 0
  792. OpMemberDecorate %sampler2D_h 0 RelaxedPrecision
  793. OpDecorate %out_var_SV_Target RelaxedPrecision
  794. OpDecorate %69 RelaxedPrecision
  795. %float = OpTypeFloat 32
  796. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  797. %type_sampler = OpTypeSampler
  798. %sampler2D_h = OpTypeStruct %type_2d_image %type_sampler
  799. %_ptr_UniformConstant_sampler2D_h = OpTypePointer UniformConstant %sampler2D_h
  800. %v2float = OpTypeVector %float 2
  801. %_ptr_Input_v2float = OpTypePointer Input %v2float
  802. %v4float = OpTypeVector %float 4
  803. %_ptr_Output_v4float = OpTypePointer Output %v4float
  804. %void = OpTypeVoid
  805. %35 = OpTypeFunction %void
  806. %type_sampled_image = OpTypeSampledImage %type_2d_image
  807. %_MainTex = OpVariable %_ptr_UniformConstant_sampler2D_h UniformConstant
  808. %in_var_TEXCOORD = OpVariable %_ptr_Input_v2float Input
  809. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  810. %PSMain = OpFunction %void None %35
  811. %43 = OpLabel
  812. %44 = OpLoad %v2float %in_var_TEXCOORD
  813. %57 = OpLoad %sampler2D_h %_MainTex
  814. %72 = OpCompositeExtract %type_2d_image %57 0
  815. %73 = OpCompositeExtract %type_sampler %57 1
  816. %68 = OpSampledImage %type_sampled_image %72 %73
  817. %69 = OpImageSampleImplicitLod %v4float %68 %44 None
  818. OpStore %out_var_SV_Target %69
  819. OpReturn
  820. OpFunctionEnd
  821. )";
  822. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  823. shader, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  824. }
  825. TEST_F(DescriptorScalarReplacementTest, DecorateStringForReflect) {
  826. // Check that an OpDecorateString instruction is correctly cloned to new
  827. // variable.
  828. const std::string shader = R"(
  829. ; CHECK: OpName %g_testTextures_0_ "g_testTextures[0]"
  830. ; CHECK: OpDecorate %g_testTextures_0_ DescriptorSet 0
  831. ; CHECK: OpDecorate %g_testTextures_0_ Binding 0
  832. ; CHECK: OpDecorateString %g_testTextures_0_ UserTypeGOOGLE "texture2d"
  833. OpCapability Shader
  834. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  835. OpExtension "SPV_GOOGLE_user_type"
  836. OpMemoryModel Logical GLSL450
  837. OpEntryPoint Fragment %main "main" %gl_FragCoord %out_var_SV_Target
  838. OpExecutionMode %main OriginUpperLeft
  839. OpSource HLSL 600
  840. OpName %type_2d_image "type.2d.image"
  841. OpName %g_testTextures "g_testTextures"
  842. OpName %out_var_SV_Target "out.var.SV_Target"
  843. OpName %main "main"
  844. OpName %param_var_vPixelPos "param.var.vPixelPos"
  845. OpName %src_main "src.main"
  846. OpName %vPixelPos "vPixelPos"
  847. OpName %bb_entry "bb.entry"
  848. OpDecorate %gl_FragCoord BuiltIn FragCoord
  849. OpDecorateString %gl_FragCoord UserSemantic "SV_Position"
  850. OpDecorateString %out_var_SV_Target UserSemantic "SV_Target"
  851. OpDecorate %out_var_SV_Target Location 0
  852. OpDecorate %g_testTextures DescriptorSet 0
  853. OpDecorate %g_testTextures Binding 0
  854. OpDecorateString %g_testTextures UserTypeGOOGLE "texture2d"
  855. %uint = OpTypeInt 32 0
  856. %uint_0 = OpConstant %uint 0
  857. %int = OpTypeInt 32 1
  858. %int_0 = OpConstant %int 0
  859. %uint_2 = OpConstant %uint 2
  860. %float = OpTypeFloat 32
  861. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  862. %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
  863. %_ptr_UniformConstant__arr_type_2d_image_uint_2 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_2
  864. %v4float = OpTypeVector %float 4
  865. %_ptr_Input_v4float = OpTypePointer Input %v4float
  866. %_ptr_Output_v4float = OpTypePointer Output %v4float
  867. %void = OpTypeVoid
  868. %18 = OpTypeFunction %void
  869. %_ptr_Function_v4float = OpTypePointer Function %v4float
  870. %25 = OpTypeFunction %v4float %_ptr_Function_v4float
  871. %v2float = OpTypeVector %float 2
  872. %v3uint = OpTypeVector %uint 3
  873. %v3int = OpTypeVector %int 3
  874. %v2int = OpTypeVector %int 2
  875. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  876. %g_testTextures = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_2 UniformConstant
  877. %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  878. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  879. %main = OpFunction %void None %18
  880. %19 = OpLabel
  881. %param_var_vPixelPos = OpVariable %_ptr_Function_v4float Function
  882. %22 = OpLoad %v4float %gl_FragCoord
  883. OpStore %param_var_vPixelPos %22
  884. %23 = OpFunctionCall %v4float %src_main %param_var_vPixelPos
  885. OpStore %out_var_SV_Target %23
  886. OpReturn
  887. OpFunctionEnd
  888. %src_main = OpFunction %v4float None %25
  889. %vPixelPos = OpFunctionParameter %_ptr_Function_v4float
  890. %bb_entry = OpLabel
  891. %28 = OpLoad %v4float %vPixelPos
  892. %30 = OpVectorShuffle %v2float %28 %28 0 1
  893. %31 = OpCompositeExtract %float %30 0
  894. %32 = OpCompositeExtract %float %30 1
  895. %33 = OpConvertFToU %uint %31
  896. %34 = OpConvertFToU %uint %32
  897. %36 = OpCompositeConstruct %v3uint %33 %34 %uint_0
  898. %38 = OpBitcast %v3int %36
  899. %40 = OpVectorShuffle %v2int %38 %38 0 1
  900. %41 = OpCompositeExtract %int %38 2
  901. %43 = OpAccessChain %_ptr_UniformConstant_type_2d_image %g_testTextures %int_0
  902. %44 = OpLoad %type_2d_image %43
  903. %45 = OpImageFetch %v4float %44 %40 Lod %41
  904. OpReturnValue %45
  905. OpFunctionEnd
  906. )";
  907. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  908. shader, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  909. }
  910. TEST_F(DescriptorScalarReplacementTest, ExpandArrayInOpEntryPoint) {
  911. const std::string text = R"(; SPIR-V
  912. ; Version: 1.6
  913. ; Bound: 31
  914. ; Schema: 0
  915. OpCapability Shader
  916. OpMemoryModel Logical GLSL450
  917. ; CHECK: OpEntryPoint GLCompute %main "main" %output_0_ %output_1_
  918. OpEntryPoint GLCompute %main "main" %output
  919. OpExecutionMode %main LocalSize 1 1 1
  920. OpSource HLSL 670
  921. OpName %type_RWByteAddressBuffer "type.RWByteAddressBuffer"
  922. OpName %output "output"
  923. OpName %main "main"
  924. OpName %src_main "src.main"
  925. OpName %bb_entry "bb.entry"
  926. ; CHECK: OpDecorate %output_1_ DescriptorSet 0
  927. ; CHECK: OpDecorate %output_1_ Binding 1
  928. ; CHECK: OpDecorate %output_0_ DescriptorSet 0
  929. ; CHECK: OpDecorate %output_0_ Binding 0
  930. OpDecorate %output DescriptorSet 0
  931. OpDecorate %output Binding 0
  932. OpDecorate %_runtimearr_uint ArrayStride 4
  933. OpMemberDecorate %type_RWByteAddressBuffer 0 Offset 0
  934. OpDecorate %type_RWByteAddressBuffer Block
  935. %int = OpTypeInt 32 1
  936. %int_1 = OpConstant %int 1
  937. %uint = OpTypeInt 32 0
  938. %uint_0 = OpConstant %uint 0
  939. %uint_2 = OpConstant %uint 2
  940. %uint_32 = OpConstant %uint 32
  941. %_runtimearr_uint = OpTypeRuntimeArray %uint
  942. %type_RWByteAddressBuffer = OpTypeStruct %_runtimearr_uint
  943. %_arr_type_RWByteAddressBuffer_uint_2 = OpTypeArray %type_RWByteAddressBuffer %uint_2
  944. %_ptr_StorageBuffer__arr_type_RWByteAddressBuffer_uint_2 = OpTypePointer StorageBuffer %_arr_type_RWByteAddressBuffer_uint_2
  945. %void = OpTypeVoid
  946. %23 = OpTypeFunction %void
  947. %_ptr_StorageBuffer_type_RWByteAddressBuffer = OpTypePointer StorageBuffer %type_RWByteAddressBuffer
  948. %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  949. ; CHECK: %output_1_ = OpVariable %_ptr_StorageBuffer_type_RWByteAddressBuffer StorageBuffer
  950. ; CHECK: %output_0_ = OpVariable %_ptr_StorageBuffer_type_RWByteAddressBuffer StorageBuffer
  951. %output = OpVariable %_ptr_StorageBuffer__arr_type_RWByteAddressBuffer_uint_2 StorageBuffer
  952. %main = OpFunction %void None %23
  953. %26 = OpLabel
  954. %27 = OpFunctionCall %void %src_main
  955. OpReturn
  956. OpFunctionEnd
  957. %src_main = OpFunction %void None %23
  958. %bb_entry = OpLabel
  959. %28 = OpAccessChain %_ptr_StorageBuffer_type_RWByteAddressBuffer %output %int_1
  960. %29 = OpShiftRightLogical %uint %uint_0 %uint_2
  961. %30 = OpAccessChain %_ptr_StorageBuffer_uint %28 %uint_0 %29
  962. OpStore %30 %uint_32
  963. OpReturn
  964. OpFunctionEnd
  965. )";
  966. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  967. text, false, /* flatten_composites=*/true, /* flatten_arrays=*/true);
  968. }
  969. TEST_F(DescriptorScalarReplacementTest,
  970. ExpandArrayWhenCompositeExpensionIsOff) {
  971. const std::string text = R"(; SPIR-V
  972. ; Version: 1.6
  973. ; Bound: 31
  974. ; Schema: 0
  975. OpCapability Shader
  976. OpMemoryModel Logical GLSL450
  977. ; CHECK: OpEntryPoint GLCompute %main "main" %output_0_ %output_1_
  978. OpEntryPoint GLCompute %main "main" %output
  979. OpExecutionMode %main LocalSize 1 1 1
  980. OpSource HLSL 670
  981. OpName %type_RWByteAddressBuffer "type.RWByteAddressBuffer"
  982. OpName %output "output"
  983. OpName %main "main"
  984. OpName %src_main "src.main"
  985. OpName %bb_entry "bb.entry"
  986. ; CHECK: OpDecorate %output_1_ DescriptorSet 0
  987. ; CHECK: OpDecorate %output_1_ Binding 1
  988. ; CHECK: OpDecorate %output_0_ DescriptorSet 0
  989. ; CHECK: OpDecorate %output_0_ Binding 0
  990. OpDecorate %output DescriptorSet 0
  991. OpDecorate %output Binding 0
  992. OpDecorate %_runtimearr_uint ArrayStride 4
  993. OpMemberDecorate %type_RWByteAddressBuffer 0 Offset 0
  994. OpDecorate %type_RWByteAddressBuffer Block
  995. %int = OpTypeInt 32 1
  996. %int_1 = OpConstant %int 1
  997. %uint = OpTypeInt 32 0
  998. %uint_0 = OpConstant %uint 0
  999. %uint_2 = OpConstant %uint 2
  1000. %uint_32 = OpConstant %uint 32
  1001. %_runtimearr_uint = OpTypeRuntimeArray %uint
  1002. %type_RWByteAddressBuffer = OpTypeStruct %_runtimearr_uint
  1003. %_arr_type_RWByteAddressBuffer_uint_2 = OpTypeArray %type_RWByteAddressBuffer %uint_2
  1004. %_ptr_StorageBuffer__arr_type_RWByteAddressBuffer_uint_2 = OpTypePointer StorageBuffer %_arr_type_RWByteAddressBuffer_uint_2
  1005. %void = OpTypeVoid
  1006. %23 = OpTypeFunction %void
  1007. %_ptr_StorageBuffer_type_RWByteAddressBuffer = OpTypePointer StorageBuffer %type_RWByteAddressBuffer
  1008. %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  1009. ; CHECK: %output_1_ = OpVariable %_ptr_StorageBuffer_type_RWByteAddressBuffer StorageBuffer
  1010. ; CHECK: %output_0_ = OpVariable %_ptr_StorageBuffer_type_RWByteAddressBuffer StorageBuffer
  1011. %output = OpVariable %_ptr_StorageBuffer__arr_type_RWByteAddressBuffer_uint_2 StorageBuffer
  1012. %main = OpFunction %void None %23
  1013. %26 = OpLabel
  1014. %27 = OpFunctionCall %void %src_main
  1015. OpReturn
  1016. OpFunctionEnd
  1017. %src_main = OpFunction %void None %23
  1018. %bb_entry = OpLabel
  1019. %28 = OpAccessChain %_ptr_StorageBuffer_type_RWByteAddressBuffer %output %int_1
  1020. %29 = OpShiftRightLogical %uint %uint_0 %uint_2
  1021. %30 = OpAccessChain %_ptr_StorageBuffer_uint %28 %uint_0 %29
  1022. OpStore %30 %uint_32
  1023. OpReturn
  1024. OpFunctionEnd
  1025. )";
  1026. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  1027. text, false, /* flatten_composites=*/false, /* flatten_arrays=*/true);
  1028. }
  1029. TEST_F(DescriptorScalarReplacementTest, ExpandStructButNotArray) {
  1030. const std::string text = R"(; SPIR-V
  1031. ; Version: 1.6
  1032. ; Generator: Khronos SPIR-V Tools Assembler; 0
  1033. ; Bound: 41
  1034. ; Schema: 0
  1035. OpCapability Shader
  1036. OpMemoryModel Logical GLSL450
  1037. OpEntryPoint Fragment %main "main" %out_var_SV_Target
  1038. OpExecutionMode %main OriginUpperLeft
  1039. OpSource HLSL 660
  1040. OpName %type_2d_image "type.2d.image"
  1041. OpName %Textures "Textures"
  1042. OpName %type_sampler "type.sampler"
  1043. OpName %out_var_SV_Target "out.var.SV_Target"
  1044. OpName %main "main"
  1045. OpName %type_sampled_image "type.sampled.image"
  1046. OpName %TheStruct "TheStruct"
  1047. OpMemberName %StructOfResources 0 "Texture"
  1048. OpMemberName %StructOfResources 1 "Sampler"
  1049. ; CHECK: OpName %TheStruct_Sampler "TheStruct.Sampler"
  1050. ; CHECK: OpName %TheStruct_Texture "TheStruct.Texture"
  1051. OpDecorate %out_var_SV_Target Location 0
  1052. OpDecorate %Textures DescriptorSet 0
  1053. OpDecorate %Textures Binding 0
  1054. OpDecorate %TheStruct DescriptorSet 0
  1055. OpDecorate %TheStruct Binding 10
  1056. ; CHECK: OpDecorate %TheStruct_Sampler DescriptorSet 0
  1057. ; CHECK: OpDecorate %TheStruct_Sampler Binding 11
  1058. ; CHECK: OpDecorate %TheStruct_Texture DescriptorSet 0
  1059. ; CHECK: OpDecorate %TheStruct_Texture Binding 10
  1060. %int = OpTypeInt 32 1
  1061. %int_0 = OpConstant %int 0
  1062. %int_1 = OpConstant %int 1
  1063. %float = OpTypeFloat 32
  1064. %float_0 = OpConstant %float 0
  1065. %v2float = OpTypeVector %float 2
  1066. %13 = OpConstantComposite %v2float %float_0 %float_0
  1067. %uint = OpTypeInt 32 0
  1068. %uint_10 = OpConstant %uint 10
  1069. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  1070. %_arr_type_2d_image_uint_10 = OpTypeArray %type_2d_image %uint_10
  1071. %_ptr_UniformConstant__arr_type_2d_image_uint_10 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_10
  1072. %type_sampler = OpTypeSampler
  1073. %StructOfResources = OpTypeStruct %type_2d_image %type_sampler
  1074. %_ptr_UniformConstant__struct_18 = OpTypePointer UniformConstant %StructOfResources
  1075. %v4float = OpTypeVector %float 4
  1076. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1077. %void = OpTypeVoid
  1078. %23 = OpTypeFunction %void
  1079. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  1080. %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
  1081. %type_sampled_image = OpTypeSampledImage %type_2d_image
  1082. %Textures = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_10 UniformConstant
  1083. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  1084. %TheStruct = OpVariable %_ptr_UniformConstant__struct_18 UniformConstant
  1085. %main = OpFunction %void None %23
  1086. %26 = OpLabel
  1087. %27 = OpAccessChain %_ptr_UniformConstant_type_2d_image %Textures %int_0
  1088. %28 = OpLoad %type_2d_image %27
  1089. %29 = OpAccessChain %_ptr_UniformConstant_type_sampler %TheStruct %int_1
  1090. %31 = OpLoad %type_sampler %29
  1091. ; CHECK: %31 = OpLoad %type_sampler %TheStruct_Sampler
  1092. %32 = OpSampledImage %type_sampled_image %28 %31
  1093. %33 = OpImageSampleImplicitLod %v4float %32 %13 None
  1094. %34 = OpAccessChain %_ptr_UniformConstant_type_2d_image %TheStruct %int_0
  1095. %35 = OpLoad %type_2d_image %34
  1096. ; CHECK: %35 = OpLoad %type_2d_image %TheStruct_Texture
  1097. %36 = OpAccessChain %_ptr_UniformConstant_type_sampler %TheStruct %int_1
  1098. %37 = OpLoad %type_sampler %36
  1099. ; CHECK: %37 = OpLoad %type_sampler %TheStruct_Sampler
  1100. %38 = OpSampledImage %type_sampled_image %35 %37
  1101. %39 = OpImageSampleImplicitLod %v4float %38 %13 None
  1102. %40 = OpFAdd %v4float %33 %39
  1103. OpStore %out_var_SV_Target %40
  1104. OpReturn
  1105. OpFunctionEnd
  1106. )";
  1107. SinglePassRunAndMatch<DescriptorScalarReplacement>(
  1108. text, false, /* flatten_composites=*/true, /* flatten_arrays=*/false);
  1109. }
  1110. } // namespace
  1111. } // namespace opt
  1112. } // namespace spvtools