desc_sroa_test.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 "gmock/gmock.h"
  16. #include "test/opt/assembly_builder.h"
  17. #include "test/opt/pass_fixture.h"
  18. #include "test/opt/pass_utils.h"
  19. namespace spvtools {
  20. namespace opt {
  21. namespace {
  22. using DescriptorScalarReplacementTest = PassTest<::testing::Test>;
  23. std::string GetStructureArrayTestSpirv() {
  24. // The SPIR-V for the following high-level shader:
  25. // Flattening structures and arrays should result in the following binding
  26. // numbers. Only the ones that are actually used in the shader should be in
  27. // the final SPIR-V.
  28. //
  29. // globalS[0][0].t[0] 0 (used)
  30. // globalS[0][0].t[1] 1
  31. // globalS[0][0].s[0] 2 (used)
  32. // globalS[0][0].s[1] 3
  33. // globalS[0][1].t[0] 4
  34. // globalS[0][1].t[1] 5
  35. // globalS[0][1].s[0] 6
  36. // globalS[0][1].s[1] 7
  37. // globalS[1][0].t[0] 8
  38. // globalS[1][0].t[1] 9
  39. // globalS[1][0].s[0] 10
  40. // globalS[1][0].s[1] 11
  41. // globalS[1][1].t[0] 12
  42. // globalS[1][1].t[1] 13 (used)
  43. // globalS[1][1].s[0] 14
  44. // globalS[1][1].s[1] 15 (used)
  45. /*
  46. struct S {
  47. Texture2D t[2];
  48. SamplerState s[2];
  49. };
  50. S globalS[2][2];
  51. float4 main() : SV_Target {
  52. return globalS[0][0].t[0].Sample(globalS[0][0].s[0], float2(0,0)) +
  53. globalS[1][1].t[1].Sample(globalS[1][1].s[1], float2(0,0));
  54. }
  55. */
  56. return R"(
  57. OpCapability Shader
  58. OpMemoryModel Logical GLSL450
  59. OpEntryPoint Fragment %main "main" %out_var_SV_Target
  60. OpExecutionMode %main OriginUpperLeft
  61. OpName %S "S"
  62. OpMemberName %S 0 "t"
  63. OpMemberName %S 1 "s"
  64. OpName %type_2d_image "type.2d.image"
  65. OpName %type_sampler "type.sampler"
  66. OpName %globalS "globalS"
  67. OpName %out_var_SV_Target "out.var.SV_Target"
  68. OpName %main "main"
  69. OpName %src_main "src.main"
  70. OpName %bb_entry "bb.entry"
  71. OpName %type_sampled_image "type.sampled.image"
  72. OpDecorate %out_var_SV_Target Location 0
  73. OpDecorate %globalS DescriptorSet 0
  74. OpDecorate %globalS Binding 0
  75. %int = OpTypeInt 32 1
  76. %int_0 = OpConstant %int 0
  77. %int_1 = OpConstant %int 1
  78. %float = OpTypeFloat 32
  79. %float_0 = OpConstant %float 0
  80. %v2float = OpTypeVector %float 2
  81. %10 = OpConstantComposite %v2float %float_0 %float_0
  82. %uint = OpTypeInt 32 0
  83. %uint_2 = OpConstant %uint 2
  84. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  85. %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
  86. %type_sampler = OpTypeSampler
  87. %_arr_type_sampler_uint_2 = OpTypeArray %type_sampler %uint_2
  88. %S = OpTypeStruct %_arr_type_2d_image_uint_2 %_arr_type_sampler_uint_2
  89. %_arr_S_uint_2 = OpTypeArray %S %uint_2
  90. %_arr__arr_S_uint_2_uint_2 = OpTypeArray %_arr_S_uint_2 %uint_2
  91. %_ptr_UniformConstant__arr__arr_S_uint_2_uint_2 = OpTypePointer UniformConstant %_arr__arr_S_uint_2_uint_2
  92. %v4float = OpTypeVector %float 4
  93. %_ptr_Output_v4float = OpTypePointer Output %v4float
  94. %void = OpTypeVoid
  95. %24 = OpTypeFunction %void
  96. %28 = OpTypeFunction %v4float
  97. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  98. %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
  99. %type_sampled_image = OpTypeSampledImage %type_2d_image
  100. %globalS = OpVariable %_ptr_UniformConstant__arr__arr_S_uint_2_uint_2 UniformConstant
  101. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  102. %main = OpFunction %void None %24
  103. %25 = OpLabel
  104. %26 = OpFunctionCall %v4float %src_main
  105. OpStore %out_var_SV_Target %26
  106. OpReturn
  107. OpFunctionEnd
  108. %src_main = OpFunction %v4float None %28
  109. %bb_entry = OpLabel
  110. %31 = OpAccessChain %_ptr_UniformConstant_type_2d_image %globalS %int_0 %int_0 %int_0 %int_0
  111. %32 = OpLoad %type_2d_image %31
  112. %34 = OpAccessChain %_ptr_UniformConstant_type_sampler %globalS %int_0 %int_0 %int_1 %int_0
  113. %35 = OpLoad %type_sampler %34
  114. %37 = OpSampledImage %type_sampled_image %32 %35
  115. %38 = OpImageSampleImplicitLod %v4float %37 %10 None
  116. %39 = OpAccessChain %_ptr_UniformConstant_type_2d_image %globalS %int_1 %int_1 %int_0 %int_1
  117. %40 = OpLoad %type_2d_image %39
  118. %41 = OpAccessChain %_ptr_UniformConstant_type_sampler %globalS %int_1 %int_1 %int_1 %int_1
  119. %42 = OpLoad %type_sampler %41
  120. %43 = OpSampledImage %type_sampled_image %40 %42
  121. %44 = OpImageSampleImplicitLod %v4float %43 %10 None
  122. %45 = OpFAdd %v4float %38 %44
  123. OpReturnValue %45
  124. OpFunctionEnd
  125. )";
  126. }
  127. TEST_F(DescriptorScalarReplacementTest, ExpandArrayOfTextures) {
  128. const std::string text = R"(
  129. ; CHECK: OpDecorate [[var1:%\w+]] DescriptorSet 0
  130. ; CHECK: OpDecorate [[var1]] Binding 0
  131. ; CHECK: OpDecorate [[var2:%\w+]] DescriptorSet 0
  132. ; CHECK: OpDecorate [[var2]] Binding 1
  133. ; CHECK: OpDecorate [[var3:%\w+]] DescriptorSet 0
  134. ; CHECK: OpDecorate [[var3]] Binding 2
  135. ; CHECK: OpDecorate [[var4:%\w+]] DescriptorSet 0
  136. ; CHECK: OpDecorate [[var4]] Binding 3
  137. ; CHECK: OpDecorate [[var5:%\w+]] DescriptorSet 0
  138. ; CHECK: OpDecorate [[var5]] Binding 4
  139. ; CHECK: [[image_type:%\w+]] = OpTypeImage
  140. ; CHECK: [[ptr_type:%\w+]] = OpTypePointer UniformConstant [[image_type]]
  141. ; CHECK: [[var1]] = OpVariable [[ptr_type]] UniformConstant
  142. ; CHECK: [[var2]] = OpVariable [[ptr_type]] UniformConstant
  143. ; CHECK: [[var3]] = OpVariable [[ptr_type]] UniformConstant
  144. ; CHECK: [[var4]] = OpVariable [[ptr_type]] UniformConstant
  145. ; CHECK: [[var5]] = OpVariable [[ptr_type]] UniformConstant
  146. ; CHECK: OpLoad [[image_type]] [[var1]]
  147. ; CHECK: OpLoad [[image_type]] [[var2]]
  148. ; CHECK: OpLoad [[image_type]] [[var3]]
  149. ; CHECK: OpLoad [[image_type]] [[var4]]
  150. ; CHECK: OpLoad [[image_type]] [[var5]]
  151. OpCapability Shader
  152. OpMemoryModel Logical GLSL450
  153. OpEntryPoint Fragment %main "main"
  154. OpExecutionMode %main OriginUpperLeft
  155. OpSource HLSL 600
  156. OpDecorate %MyTextures DescriptorSet 0
  157. OpDecorate %MyTextures Binding 0
  158. %int = OpTypeInt 32 1
  159. %int_0 = OpConstant %int 0
  160. %int_1 = OpConstant %int 1
  161. %int_2 = OpConstant %int 2
  162. %int_3 = OpConstant %int 3
  163. %int_4 = OpConstant %int 4
  164. %uint = OpTypeInt 32 0
  165. %uint_5 = OpConstant %uint 5
  166. %float = OpTypeFloat 32
  167. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  168. %_arr_type_2d_image_uint_5 = OpTypeArray %type_2d_image %uint_5
  169. %_ptr_UniformConstant__arr_type_2d_image_uint_5 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_5
  170. %v2float = OpTypeVector %float 2
  171. %void = OpTypeVoid
  172. %26 = OpTypeFunction %void
  173. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  174. %MyTextures = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_5 UniformConstant
  175. %main = OpFunction %void None %26
  176. %28 = OpLabel
  177. %29 = OpUndef %v2float
  178. %30 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_0
  179. %31 = OpLoad %type_2d_image %30
  180. %35 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_1
  181. %36 = OpLoad %type_2d_image %35
  182. %40 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_2
  183. %41 = OpLoad %type_2d_image %40
  184. %45 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_3
  185. %46 = OpLoad %type_2d_image %45
  186. %50 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_4
  187. %51 = OpLoad %type_2d_image %50
  188. OpReturn
  189. OpFunctionEnd
  190. )";
  191. SinglePassRunAndMatch<DescriptorScalarReplacement>(text, true);
  192. }
  193. TEST_F(DescriptorScalarReplacementTest, ExpandArrayOfSamplers) {
  194. const std::string text = R"(
  195. ; CHECK: OpDecorate [[var1:%\w+]] DescriptorSet 0
  196. ; CHECK: OpDecorate [[var1]] Binding 1
  197. ; CHECK: OpDecorate [[var2:%\w+]] DescriptorSet 0
  198. ; CHECK: OpDecorate [[var2]] Binding 2
  199. ; CHECK: OpDecorate [[var3:%\w+]] DescriptorSet 0
  200. ; CHECK: OpDecorate [[var3]] Binding 3
  201. ; CHECK: [[sampler_type:%\w+]] = OpTypeSampler
  202. ; CHECK: [[ptr_type:%\w+]] = OpTypePointer UniformConstant [[sampler_type]]
  203. ; CHECK: [[var1]] = OpVariable [[ptr_type]] UniformConstant
  204. ; CHECK: [[var2]] = OpVariable [[ptr_type]] UniformConstant
  205. ; CHECK: [[var3]] = OpVariable [[ptr_type]] UniformConstant
  206. ; CHECK: OpLoad [[sampler_type]] [[var1]]
  207. ; CHECK: OpLoad [[sampler_type]] [[var2]]
  208. ; CHECK: OpLoad [[sampler_type]] [[var3]]
  209. OpCapability Shader
  210. OpMemoryModel Logical GLSL450
  211. OpEntryPoint Fragment %main "main"
  212. OpExecutionMode %main OriginUpperLeft
  213. OpSource HLSL 600
  214. OpDecorate %MySampler DescriptorSet 0
  215. OpDecorate %MySampler Binding 1
  216. %int = OpTypeInt 32 1
  217. %int_0 = OpConstant %int 0
  218. %int_1 = OpConstant %int 1
  219. %int_2 = OpConstant %int 2
  220. %uint = OpTypeInt 32 0
  221. %uint_3 = OpConstant %uint 3
  222. %type_sampler = OpTypeSampler
  223. %_arr_type_sampler_uint_3 = OpTypeArray %type_sampler %uint_3
  224. %_ptr_UniformConstant__arr_type_sampler_uint_3 = OpTypePointer UniformConstant %_arr_type_sampler_uint_3
  225. %void = OpTypeVoid
  226. %26 = OpTypeFunction %void
  227. %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
  228. %MySampler = OpVariable %_ptr_UniformConstant__arr_type_sampler_uint_3 UniformConstant
  229. %main = OpFunction %void None %26
  230. %28 = OpLabel
  231. %31 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_0
  232. %32 = OpLoad %type_sampler %31
  233. %35 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_1
  234. %36 = OpLoad %type_sampler %35
  235. %40 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_2
  236. %41 = OpLoad %type_sampler %40
  237. OpReturn
  238. OpFunctionEnd
  239. )";
  240. SinglePassRunAndMatch<DescriptorScalarReplacement>(text, 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>(text, true);
  298. }
  299. TEST_F(DescriptorScalarReplacementTest, NameNewVariables) {
  300. // Checks that if the original variable has a name, then the new variables
  301. // will have a name derived from that name.
  302. const std::string text = R"(
  303. ; CHECK: OpName [[var1:%\w+]] "SSBO[0]"
  304. ; CHECK: OpName [[var2:%\w+]] "SSBO[1]"
  305. ; CHECK: OpDecorate [[var1]] DescriptorSet 0
  306. ; CHECK: OpDecorate [[var1]] Binding 0
  307. ; CHECK: OpDecorate [[var2]] DescriptorSet 0
  308. ; CHECK: OpDecorate [[var2]] Binding 1
  309. ; CHECK: OpTypeStruct
  310. ; CHECK: [[struct_type:%\w+]] = OpTypeStruct
  311. ; CHECK: [[ptr_type:%\w+]] = OpTypePointer Uniform [[struct_type]]
  312. ; CHECK: [[var1]] = OpVariable [[ptr_type]] Uniform
  313. ; CHECK: [[var2]] = OpVariable [[ptr_type]] Uniform
  314. ; CHECK: [[ac1:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var1]] %uint_0 %uint_0 %uint_0
  315. ; CHECK: OpLoad %v4float [[ac1]]
  316. ; CHECK: [[ac2:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var2]] %uint_0 %uint_0 %uint_0
  317. ; CHECK: OpLoad %v4float [[ac2]]
  318. OpCapability Shader
  319. OpMemoryModel Logical GLSL450
  320. OpEntryPoint Fragment %main "main"
  321. OpExecutionMode %main OriginUpperLeft
  322. OpSource HLSL 600
  323. OpName %buffers "SSBO"
  324. OpDecorate %buffers DescriptorSet 0
  325. OpDecorate %buffers Binding 0
  326. OpMemberDecorate %S 0 Offset 0
  327. OpDecorate %_runtimearr_S ArrayStride 16
  328. OpMemberDecorate %type_StructuredBuffer_S 0 Offset 0
  329. OpMemberDecorate %type_StructuredBuffer_S 0 NonWritable
  330. OpDecorate %type_StructuredBuffer_S BufferBlock
  331. %uint = OpTypeInt 32 0
  332. %uint_0 = OpConstant %uint 0
  333. %uint_1 = OpConstant %uint 1
  334. %uint_2 = OpConstant %uint 2
  335. %float = OpTypeFloat 32
  336. %v4float = OpTypeVector %float 4
  337. %S = OpTypeStruct %v4float
  338. %_runtimearr_S = OpTypeRuntimeArray %S
  339. %type_StructuredBuffer_S = OpTypeStruct %_runtimearr_S
  340. %_arr_type_StructuredBuffer_S_uint_2 = OpTypeArray %type_StructuredBuffer_S %uint_2
  341. %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 = OpTypePointer Uniform %_arr_type_StructuredBuffer_S_uint_2
  342. %_ptr_Uniform_type_StructuredBuffer_S = OpTypePointer Uniform %type_StructuredBuffer_S
  343. %void = OpTypeVoid
  344. %19 = OpTypeFunction %void
  345. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  346. %buffers = OpVariable %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 Uniform
  347. %main = OpFunction %void None %19
  348. %21 = OpLabel
  349. %22 = OpAccessChain %_ptr_Uniform_v4float %buffers %uint_0 %uint_0 %uint_0 %uint_0
  350. %23 = OpLoad %v4float %22
  351. %24 = OpAccessChain %_ptr_Uniform_type_StructuredBuffer_S %buffers %uint_1
  352. %25 = OpAccessChain %_ptr_Uniform_v4float %24 %uint_0 %uint_0 %uint_0
  353. %26 = OpLoad %v4float %25
  354. OpReturn
  355. OpFunctionEnd
  356. )";
  357. SinglePassRunAndMatch<DescriptorScalarReplacement>(text, true);
  358. }
  359. TEST_F(DescriptorScalarReplacementTest, DontExpandCBuffers) {
  360. // Checks that constant buffers are not expanded.
  361. // Constant buffers are represented as global structures, but they should not
  362. // be replaced with new variables for their elements.
  363. /*
  364. cbuffer MyCbuffer : register(b1) {
  365. float2 a;
  366. float2 b;
  367. };
  368. float main() : A {
  369. return a.x + b.y;
  370. }
  371. */
  372. const std::string text = R"(
  373. ; CHECK: OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_0 %int_0
  374. ; CHECK: OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_1 %int_1
  375. OpCapability Shader
  376. OpMemoryModel Logical GLSL450
  377. OpEntryPoint Vertex %main "main" %out_var_A
  378. OpSource HLSL 600
  379. OpName %type_MyCbuffer "type.MyCbuffer"
  380. OpMemberName %type_MyCbuffer 0 "a"
  381. OpMemberName %type_MyCbuffer 1 "b"
  382. OpName %MyCbuffer "MyCbuffer"
  383. OpName %out_var_A "out.var.A"
  384. OpName %main "main"
  385. OpDecorate %out_var_A Location 0
  386. OpDecorate %MyCbuffer DescriptorSet 0
  387. OpDecorate %MyCbuffer Binding 1
  388. OpMemberDecorate %type_MyCbuffer 0 Offset 0
  389. OpMemberDecorate %type_MyCbuffer 1 Offset 8
  390. OpDecorate %type_MyCbuffer Block
  391. %int = OpTypeInt 32 1
  392. %int_0 = OpConstant %int 0
  393. %int_1 = OpConstant %int 1
  394. %float = OpTypeFloat 32
  395. %v2float = OpTypeVector %float 2
  396. %type_MyCbuffer = OpTypeStruct %v2float %v2float
  397. %_ptr_Uniform_type_MyCbuffer = OpTypePointer Uniform %type_MyCbuffer
  398. %_ptr_Output_float = OpTypePointer Output %float
  399. %void = OpTypeVoid
  400. %13 = OpTypeFunction %void
  401. %_ptr_Uniform_float = OpTypePointer Uniform %float
  402. %MyCbuffer = OpVariable %_ptr_Uniform_type_MyCbuffer Uniform
  403. %out_var_A = OpVariable %_ptr_Output_float Output
  404. %main = OpFunction %void None %13
  405. %15 = OpLabel
  406. %16 = OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_0 %int_0
  407. %17 = OpLoad %float %16
  408. %18 = OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_1 %int_1
  409. %19 = OpLoad %float %18
  410. %20 = OpFAdd %float %17 %19
  411. OpStore %out_var_A %20
  412. OpReturn
  413. OpFunctionEnd
  414. )";
  415. SinglePassRunAndMatch<DescriptorScalarReplacement>(text, true);
  416. }
  417. TEST_F(DescriptorScalarReplacementTest, DontExpandStructuredBuffers) {
  418. // Checks that structured buffers are not expanded.
  419. // Structured buffers are represented as global structures, that have one
  420. // member which is a runtime array.
  421. /*
  422. struct S {
  423. float2 a;
  424. float2 b;
  425. };
  426. RWStructuredBuffer<S> sb;
  427. float main() : A {
  428. return sb[0].a.x + sb[0].b.x;
  429. }
  430. */
  431. const std::string text = R"(
  432. ; CHECK: OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_0 %int_0
  433. ; CHECK: OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_1 %int_0
  434. OpCapability Shader
  435. OpMemoryModel Logical GLSL450
  436. OpEntryPoint Vertex %main "main" %out_var_A
  437. OpName %type_RWStructuredBuffer_S "type.RWStructuredBuffer.S"
  438. OpName %S "S"
  439. OpMemberName %S 0 "a"
  440. OpMemberName %S 1 "b"
  441. OpName %sb "sb"
  442. OpName %out_var_A "out.var.A"
  443. OpName %main "main"
  444. OpDecorate %out_var_A Location 0
  445. OpDecorate %sb DescriptorSet 0
  446. OpDecorate %sb Binding 0
  447. OpMemberDecorate %S 0 Offset 0
  448. OpMemberDecorate %S 1 Offset 8
  449. OpDecorate %_runtimearr_S ArrayStride 16
  450. OpMemberDecorate %type_RWStructuredBuffer_S 0 Offset 0
  451. OpDecorate %type_RWStructuredBuffer_S BufferBlock
  452. %int = OpTypeInt 32 1
  453. %int_0 = OpConstant %int 0
  454. %uint = OpTypeInt 32 0
  455. %uint_0 = OpConstant %uint 0
  456. %int_1 = OpConstant %int 1
  457. %float = OpTypeFloat 32
  458. %v2float = OpTypeVector %float 2
  459. %S = OpTypeStruct %v2float %v2float
  460. %_runtimearr_S = OpTypeRuntimeArray %S
  461. %type_RWStructuredBuffer_S = OpTypeStruct %_runtimearr_S
  462. %_ptr_Uniform_type_RWStructuredBuffer_S = OpTypePointer Uniform %type_RWStructuredBuffer_S
  463. %_ptr_Output_float = OpTypePointer Output %float
  464. %void = OpTypeVoid
  465. %17 = OpTypeFunction %void
  466. %_ptr_Uniform_float = OpTypePointer Uniform %float
  467. %sb = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_S Uniform
  468. %out_var_A = OpVariable %_ptr_Output_float Output
  469. %main = OpFunction %void None %17
  470. %19 = OpLabel
  471. %20 = OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_0 %int_0
  472. %21 = OpLoad %float %20
  473. %22 = OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_1 %int_0
  474. %23 = OpLoad %float %22
  475. %24 = OpFAdd %float %21 %23
  476. OpStore %out_var_A %24
  477. OpReturn
  478. OpFunctionEnd
  479. )";
  480. SinglePassRunAndMatch<DescriptorScalarReplacement>(text, true);
  481. }
  482. TEST_F(DescriptorScalarReplacementTest, StructureArrayNames) {
  483. // Checks that names are properly generated for multi-dimension arrays and
  484. // structure members.
  485. const std::string checks = R"(
  486. ; CHECK: OpName %globalS_0__0__t_0_ "globalS[0][0].t[0]"
  487. ; CHECK: OpName %globalS_0__0__s_0_ "globalS[0][0].s[0]"
  488. ; CHECK: OpName %globalS_1__1__t_1_ "globalS[1][1].t[1]"
  489. ; CHECK: OpName %globalS_1__1__s_1_ "globalS[1][1].s[1]"
  490. )";
  491. const std::string text = checks + GetStructureArrayTestSpirv();
  492. SinglePassRunAndMatch<DescriptorScalarReplacement>(text, true);
  493. }
  494. TEST_F(DescriptorScalarReplacementTest, StructureArrayBindings) {
  495. // Checks that flattening structures and arrays results in correct binding
  496. // numbers.
  497. const std::string checks = R"(
  498. ; CHECK: OpDecorate %globalS_0__0__t_0_ Binding 0
  499. ; CHECK: OpDecorate %globalS_0__0__s_0_ Binding 2
  500. ; CHECK: OpDecorate %globalS_1__1__t_1_ Binding 13
  501. ; CHECK: OpDecorate %globalS_1__1__s_1_ Binding 15
  502. )";
  503. const std::string text = checks + GetStructureArrayTestSpirv();
  504. SinglePassRunAndMatch<DescriptorScalarReplacement>(text, true);
  505. }
  506. TEST_F(DescriptorScalarReplacementTest, StructureArrayReplacements) {
  507. // Checks that all access chains indexing into structures and/or arrays are
  508. // replaced with direct access to replacement variables.
  509. const std::string checks = R"(
  510. ; CHECK-NOT: OpAccessChain
  511. ; CHECK: OpLoad %type_2d_image %globalS_0__0__t_0_
  512. ; CHECK: OpLoad %type_sampler %globalS_0__0__s_0_
  513. ; CHECK: OpLoad %type_2d_image %globalS_1__1__t_1_
  514. ; CHECK: OpLoad %type_sampler %globalS_1__1__s_1_
  515. )";
  516. const std::string text = checks + GetStructureArrayTestSpirv();
  517. SinglePassRunAndMatch<DescriptorScalarReplacement>(text, true);
  518. }
  519. TEST_F(DescriptorScalarReplacementTest, ResourceStructAsFunctionParam) {
  520. // Checks that a mix of OpAccessChain, OpLoad, and OpCompositeExtract patterns
  521. // can be properly replaced with replacement variables.
  522. // This pattern can be seen when a global structure of resources is passed to
  523. // a function.
  524. /* High-level source:
  525. // globalS[0].t[0] binding: 0 (used)
  526. // globalS[0].t[1] binding: 1 (used)
  527. // globalS[0].tt[0].s[0] binding: 2
  528. // globalS[0].tt[0].s[1] binding: 3 (used)
  529. // globalS[0].tt[0].s[2] binding: 4
  530. // globalS[0].tt[1].s[0] binding: 5
  531. // globalS[0].tt[1].s[1] binding: 6
  532. // globalS[0].tt[1].s[2] binding: 7 (used)
  533. // globalS[1].t[0] binding: 8 (used)
  534. // globalS[1].t[1] binding: 9 (used)
  535. // globalS[1].tt[0].s[0] binding: 10
  536. // globalS[1].tt[0].s[1] binding: 11 (used)
  537. // globalS[1].tt[0].s[2] binding: 12
  538. // globalS[1].tt[1].s[0] binding: 13
  539. // globalS[1].tt[1].s[1] binding: 14
  540. // globalS[1].tt[1].s[2] binding: 15 (used)
  541. struct T {
  542. SamplerState s[3];
  543. };
  544. struct S {
  545. Texture2D t[2];
  546. T tt[2];
  547. };
  548. float4 tex2D(S x, float2 v) {
  549. return x.t[0].Sample(x.tt[0].s[1], v) + x.t[1].Sample(x.tt[1].s[2], v);
  550. }
  551. S globalS[2];
  552. float4 main() : SV_Target {
  553. return tex2D(globalS[0], float2(0,0)) + tex2D(globalS[1], float2(0,0)) ;
  554. }
  555. */
  556. const std::string shader = R"(
  557. OpCapability Shader
  558. OpMemoryModel Logical GLSL450
  559. OpEntryPoint Fragment %main "main" %out_var_SV_Target
  560. OpExecutionMode %main OriginUpperLeft
  561. OpName %S "S"
  562. OpMemberName %S 0 "t"
  563. OpMemberName %S 1 "tt"
  564. OpName %type_2d_image "type.2d.image"
  565. OpName %T "T"
  566. OpMemberName %T 0 "s"
  567. OpName %type_sampler "type.sampler"
  568. OpName %globalS "globalS"
  569. OpName %out_var_SV_Target "out.var.SV_Target"
  570. OpName %main "main"
  571. OpName %type_sampled_image "type.sampled.image"
  572. OpDecorate %out_var_SV_Target Location 0
  573. OpDecorate %globalS DescriptorSet 0
  574. OpDecorate %globalS Binding 0
  575. %int = OpTypeInt 32 1
  576. %int_0 = OpConstant %int 0
  577. %float = OpTypeFloat 32
  578. %float_0 = OpConstant %float 0
  579. %v2float = OpTypeVector %float 2
  580. %14 = OpConstantComposite %v2float %float_0 %float_0
  581. %int_1 = OpConstant %int 1
  582. %uint = OpTypeInt 32 0
  583. %uint_2 = OpConstant %uint 2
  584. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  585. %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
  586. %uint_3 = OpConstant %uint 3
  587. %type_sampler = OpTypeSampler
  588. %_arr_type_sampler_uint_3 = OpTypeArray %type_sampler %uint_3
  589. %T = OpTypeStruct %_arr_type_sampler_uint_3
  590. %_arr_T_uint_2 = OpTypeArray %T %uint_2
  591. %S = OpTypeStruct %_arr_type_2d_image_uint_2 %_arr_T_uint_2
  592. %_arr_S_uint_2 = OpTypeArray %S %uint_2
  593. %_ptr_UniformConstant__arr_S_uint_2 = OpTypePointer UniformConstant %_arr_S_uint_2
  594. %v4float = OpTypeVector %float 4
  595. %_ptr_Output_v4float = OpTypePointer Output %v4float
  596. %void = OpTypeVoid
  597. %27 = OpTypeFunction %void
  598. %_ptr_UniformConstant_S = OpTypePointer UniformConstant %S
  599. %type_sampled_image = OpTypeSampledImage %type_2d_image
  600. %globalS = OpVariable %_ptr_UniformConstant__arr_S_uint_2 UniformConstant
  601. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  602. %main = OpFunction %void None %27
  603. %29 = OpLabel
  604. %30 = OpAccessChain %_ptr_UniformConstant_S %globalS %int_0
  605. %31 = OpLoad %S %30
  606. %32 = OpCompositeExtract %_arr_type_2d_image_uint_2 %31 0
  607. %33 = OpCompositeExtract %type_2d_image %32 0
  608. %34 = OpCompositeExtract %type_2d_image %32 1
  609. %35 = OpCompositeExtract %_arr_T_uint_2 %31 1
  610. %36 = OpCompositeExtract %T %35 0
  611. %37 = OpCompositeExtract %_arr_type_sampler_uint_3 %36 0
  612. %38 = OpCompositeExtract %type_sampler %37 1
  613. %39 = OpCompositeExtract %T %35 1
  614. %40 = OpCompositeExtract %_arr_type_sampler_uint_3 %39 0
  615. %41 = OpCompositeExtract %type_sampler %40 2
  616. %42 = OpSampledImage %type_sampled_image %33 %38
  617. %43 = OpImageSampleImplicitLod %v4float %42 %14 None
  618. %44 = OpSampledImage %type_sampled_image %34 %41
  619. %45 = OpImageSampleImplicitLod %v4float %44 %14 None
  620. %46 = OpFAdd %v4float %43 %45
  621. %47 = OpAccessChain %_ptr_UniformConstant_S %globalS %int_1
  622. %48 = OpLoad %S %47
  623. %49 = OpCompositeExtract %_arr_type_2d_image_uint_2 %48 0
  624. %50 = OpCompositeExtract %type_2d_image %49 0
  625. %51 = OpCompositeExtract %type_2d_image %49 1
  626. %52 = OpCompositeExtract %_arr_T_uint_2 %48 1
  627. %53 = OpCompositeExtract %T %52 0
  628. %54 = OpCompositeExtract %_arr_type_sampler_uint_3 %53 0
  629. %55 = OpCompositeExtract %type_sampler %54 1
  630. %56 = OpCompositeExtract %T %52 1
  631. %57 = OpCompositeExtract %_arr_type_sampler_uint_3 %56 0
  632. %58 = OpCompositeExtract %type_sampler %57 2
  633. %59 = OpSampledImage %type_sampled_image %50 %55
  634. %60 = OpImageSampleImplicitLod %v4float %59 %14 None
  635. %61 = OpSampledImage %type_sampled_image %51 %58
  636. %62 = OpImageSampleImplicitLod %v4float %61 %14 None
  637. %63 = OpFAdd %v4float %60 %62
  638. %64 = OpFAdd %v4float %46 %63
  639. OpStore %out_var_SV_Target %64
  640. OpReturn
  641. OpFunctionEnd
  642. )";
  643. const std::string checks = R"(
  644. ; CHECK: OpName %globalS_0__t_0_ "globalS[0].t[0]"
  645. ; CHECK: OpName %globalS_0__t_1_ "globalS[0].t[1]"
  646. ; CHECK: OpName %globalS_1__t_0_ "globalS[1].t[0]"
  647. ; CHECK: OpName %globalS_1__t_1_ "globalS[1].t[1]"
  648. ; CHECK: OpName %globalS_0__tt_0__s_1_ "globalS[0].tt[0].s[1]"
  649. ; CHECK: OpName %globalS_0__tt_1__s_2_ "globalS[0].tt[1].s[2]"
  650. ; CHECK: OpName %globalS_1__tt_0__s_1_ "globalS[1].tt[0].s[1]"
  651. ; CHECK: OpName %globalS_1__tt_1__s_2_ "globalS[1].tt[1].s[2]"
  652. ; CHECK: OpDecorate %globalS_0__t_0_ Binding 0
  653. ; CHECK: OpDecorate %globalS_0__t_1_ Binding 1
  654. ; CHECK: OpDecorate %globalS_1__t_0_ Binding 8
  655. ; CHECK: OpDecorate %globalS_1__t_1_ Binding 9
  656. ; CHECK: OpDecorate %globalS_0__tt_0__s_1_ Binding 3
  657. ; CHECK: OpDecorate %globalS_0__tt_1__s_2_ Binding 7
  658. ; CHECK: OpDecorate %globalS_1__tt_0__s_1_ Binding 11
  659. ; CHECK: OpDecorate %globalS_1__tt_1__s_2_ Binding 15
  660. ; CHECK: %globalS_0__t_0_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  661. ; CHECK: %globalS_0__t_1_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  662. ; CHECK: %globalS_1__t_0_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  663. ; CHECK: %globalS_1__t_1_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  664. ; CHECK: %globalS_0__tt_0__s_1_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  665. ; CHECK: %globalS_0__tt_1__s_2_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  666. ; CHECK: %globalS_1__tt_0__s_1_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  667. ; CHECK: %globalS_1__tt_1__s_2_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  668. ; CHECK: [[img_1:%\w+]] = OpLoad %type_2d_image %globalS_0__t_0_
  669. ; CHECK: [[img_2:%\w+]] = OpLoad %type_2d_image %globalS_0__t_1_
  670. ; CHECK: [[sampler_1:%\w+]] = OpLoad %type_sampler %globalS_0__tt_0__s_1_
  671. ; CHECK: [[sampler_2:%\w+]] = OpLoad %type_sampler %globalS_0__tt_1__s_2_
  672. ; CHECK: [[sampled_img_1:%\w+]] = OpSampledImage %type_sampled_image [[img_1]] [[sampler_1]]
  673. ; CHECK: [[sample_1:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_1]]
  674. ; CHECK: [[sampled_img_2:%\w+]] = OpSampledImage %type_sampled_image [[img_2]] [[sampler_2]]
  675. ; CHECK: [[sample_2:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_2]]
  676. ; CHECK: OpFAdd %v4float [[sample_1]] [[sample_2]]
  677. ; CHECK: [[img_3:%\w+]] = OpLoad %type_2d_image %globalS_1__t_0_
  678. ; CHECK: [[img_4:%\w+]] = OpLoad %type_2d_image %globalS_1__t_1_
  679. ; CHECK: [[sampler_3:%\w+]] = OpLoad %type_sampler %globalS_1__tt_0__s_1_
  680. ; CHECK: [[sampler_4:%\w+]] = OpLoad %type_sampler %globalS_1__tt_1__s_2_
  681. ; CHECK: [[sampled_img_3:%\w+]] = OpSampledImage %type_sampled_image [[img_3]] [[sampler_3]]
  682. ; CHECK: [[sample_3:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_3]]
  683. ; CHECK: [[sampled_img_4:%\w+]] = OpSampledImage %type_sampled_image [[img_4]] [[sampler_4]]
  684. ; CHECK: [[sample_4:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_4]]
  685. ; CHECK: OpFAdd %v4float [[sample_3]] [[sample_4]]
  686. )";
  687. SinglePassRunAndMatch<DescriptorScalarReplacement>(checks + shader, true);
  688. }
  689. TEST_F(DescriptorScalarReplacementTest, BindingForResourceArrayOfStructs) {
  690. // Check that correct binding numbers are given to an array of descriptors
  691. // to structs.
  692. const std::string shader = R"(
  693. ; CHECK: OpDecorate {{%\w+}} Binding 0
  694. ; CHECK: OpDecorate {{%\w+}} Binding 1
  695. OpCapability Shader
  696. %1 = OpExtInstImport "GLSL.std.450"
  697. OpMemoryModel Logical GLSL450
  698. OpEntryPoint Fragment %2 "psmain"
  699. OpExecutionMode %2 OriginUpperLeft
  700. OpDecorate %5 DescriptorSet 0
  701. OpDecorate %5 Binding 0
  702. OpMemberDecorate %_struct_4 0 Offset 0
  703. OpMemberDecorate %_struct_4 1 Offset 4
  704. OpDecorate %_struct_4 Block
  705. %float = OpTypeFloat 32
  706. %int = OpTypeInt 32 1
  707. %int_0 = OpConstant %int 0
  708. %int_1 = OpConstant %int 1
  709. %uint = OpTypeInt 32 0
  710. %uint_2 = OpConstant %uint 2
  711. %_struct_4 = OpTypeStruct %float %int
  712. %_arr__struct_4_uint_2 = OpTypeArray %_struct_4 %uint_2
  713. %_ptr_Uniform__arr__struct_4_uint_2 = OpTypePointer Uniform %_arr__struct_4_uint_2
  714. %void = OpTypeVoid
  715. %25 = OpTypeFunction %void
  716. %_ptr_Uniform_int = OpTypePointer Uniform %int
  717. %5 = OpVariable %_ptr_Uniform__arr__struct_4_uint_2 Uniform
  718. %2 = OpFunction %void None %25
  719. %29 = OpLabel
  720. %40 = OpAccessChain %_ptr_Uniform_int %5 %int_0 %int_1
  721. %41 = OpAccessChain %_ptr_Uniform_int %5 %int_1 %int_1
  722. OpReturn
  723. OpFunctionEnd
  724. )";
  725. SinglePassRunAndMatch<DescriptorScalarReplacement>(shader, true);
  726. }
  727. TEST_F(DescriptorScalarReplacementTest, MemberDecorationForResourceStruct) {
  728. // Check that an OpMemberDecorate instruction is correctly converted to a
  729. // OpDecorate instruction.
  730. const std::string shader = R"(
  731. ; CHECK: OpDecorate [[t:%\w+]] DescriptorSet 0
  732. ; CHECK: OpDecorate [[t]] Binding 0
  733. ; CHECK: OpDecorate [[t]] RelaxedPrecision
  734. ; CHECK: OpDecorate [[s:%\w+]] DescriptorSet 0
  735. ; CHECK: OpDecorate [[s]] Binding 1
  736. OpCapability Shader
  737. OpMemoryModel Logical GLSL450
  738. OpEntryPoint Fragment %PSMain "PSMain" %in_var_TEXCOORD %out_var_SV_Target
  739. OpExecutionMode %PSMain OriginUpperLeft
  740. OpSource HLSL 600
  741. OpName %sampler2D_h "sampler2D_h"
  742. OpMemberName %sampler2D_h 0 "t"
  743. OpMemberName %sampler2D_h 1 "s"
  744. OpName %type_2d_image "type.2d.image"
  745. OpName %type_sampler "type.sampler"
  746. OpName %_MainTex "_MainTex"
  747. OpName %in_var_TEXCOORD "in.var.TEXCOORD"
  748. OpName %out_var_SV_Target "out.var.SV_Target"
  749. OpName %PSMain "PSMain"
  750. OpName %type_sampled_image "type.sampled.image"
  751. OpDecorate %in_var_TEXCOORD Location 0
  752. OpDecorate %out_var_SV_Target Location 0
  753. OpDecorate %_MainTex DescriptorSet 0
  754. OpDecorate %_MainTex Binding 0
  755. OpMemberDecorate %sampler2D_h 0 RelaxedPrecision
  756. OpDecorate %out_var_SV_Target RelaxedPrecision
  757. OpDecorate %69 RelaxedPrecision
  758. %float = OpTypeFloat 32
  759. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  760. %type_sampler = OpTypeSampler
  761. %sampler2D_h = OpTypeStruct %type_2d_image %type_sampler
  762. %_ptr_UniformConstant_sampler2D_h = OpTypePointer UniformConstant %sampler2D_h
  763. %v2float = OpTypeVector %float 2
  764. %_ptr_Input_v2float = OpTypePointer Input %v2float
  765. %v4float = OpTypeVector %float 4
  766. %_ptr_Output_v4float = OpTypePointer Output %v4float
  767. %void = OpTypeVoid
  768. %35 = OpTypeFunction %void
  769. %type_sampled_image = OpTypeSampledImage %type_2d_image
  770. %_MainTex = OpVariable %_ptr_UniformConstant_sampler2D_h UniformConstant
  771. %in_var_TEXCOORD = OpVariable %_ptr_Input_v2float Input
  772. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  773. %PSMain = OpFunction %void None %35
  774. %43 = OpLabel
  775. %44 = OpLoad %v2float %in_var_TEXCOORD
  776. %57 = OpLoad %sampler2D_h %_MainTex
  777. %72 = OpCompositeExtract %type_2d_image %57 0
  778. %73 = OpCompositeExtract %type_sampler %57 1
  779. %68 = OpSampledImage %type_sampled_image %72 %73
  780. %69 = OpImageSampleImplicitLod %v4float %68 %44 None
  781. OpStore %out_var_SV_Target %69
  782. OpReturn
  783. OpFunctionEnd
  784. )";
  785. SinglePassRunAndMatch<DescriptorScalarReplacement>(shader, true);
  786. }
  787. TEST_F(DescriptorScalarReplacementTest, DecorateStringForReflect) {
  788. // Check that an OpDecorateString instruction is correctly cloned to new
  789. // variable.
  790. const std::string shader = R"(
  791. ; CHECK: OpName %g_testTextures_0_ "g_testTextures[0]"
  792. ; CHECK: OpDecorate %g_testTextures_0_ DescriptorSet 0
  793. ; CHECK: OpDecorate %g_testTextures_0_ Binding 0
  794. ; CHECK: OpDecorateString %g_testTextures_0_ UserTypeGOOGLE "texture2d"
  795. OpCapability Shader
  796. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  797. OpExtension "SPV_GOOGLE_user_type"
  798. OpMemoryModel Logical GLSL450
  799. OpEntryPoint Fragment %main "main" %gl_FragCoord %out_var_SV_Target
  800. OpExecutionMode %main OriginUpperLeft
  801. OpSource HLSL 600
  802. OpName %type_2d_image "type.2d.image"
  803. OpName %g_testTextures "g_testTextures"
  804. OpName %out_var_SV_Target "out.var.SV_Target"
  805. OpName %main "main"
  806. OpName %param_var_vPixelPos "param.var.vPixelPos"
  807. OpName %src_main "src.main"
  808. OpName %vPixelPos "vPixelPos"
  809. OpName %bb_entry "bb.entry"
  810. OpDecorate %gl_FragCoord BuiltIn FragCoord
  811. OpDecorateString %gl_FragCoord UserSemantic "SV_Position"
  812. OpDecorateString %out_var_SV_Target UserSemantic "SV_Target"
  813. OpDecorate %out_var_SV_Target Location 0
  814. OpDecorate %g_testTextures DescriptorSet 0
  815. OpDecorate %g_testTextures Binding 0
  816. OpDecorateString %g_testTextures UserTypeGOOGLE "texture2d"
  817. %uint = OpTypeInt 32 0
  818. %uint_0 = OpConstant %uint 0
  819. %int = OpTypeInt 32 1
  820. %int_0 = OpConstant %int 0
  821. %uint_2 = OpConstant %uint 2
  822. %float = OpTypeFloat 32
  823. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  824. %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
  825. %_ptr_UniformConstant__arr_type_2d_image_uint_2 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_2
  826. %v4float = OpTypeVector %float 4
  827. %_ptr_Input_v4float = OpTypePointer Input %v4float
  828. %_ptr_Output_v4float = OpTypePointer Output %v4float
  829. %void = OpTypeVoid
  830. %18 = OpTypeFunction %void
  831. %_ptr_Function_v4float = OpTypePointer Function %v4float
  832. %25 = OpTypeFunction %v4float %_ptr_Function_v4float
  833. %v2float = OpTypeVector %float 2
  834. %v3uint = OpTypeVector %uint 3
  835. %v3int = OpTypeVector %int 3
  836. %v2int = OpTypeVector %int 2
  837. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  838. %g_testTextures = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_2 UniformConstant
  839. %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  840. %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
  841. %main = OpFunction %void None %18
  842. %19 = OpLabel
  843. %param_var_vPixelPos = OpVariable %_ptr_Function_v4float Function
  844. %22 = OpLoad %v4float %gl_FragCoord
  845. OpStore %param_var_vPixelPos %22
  846. %23 = OpFunctionCall %v4float %src_main %param_var_vPixelPos
  847. OpStore %out_var_SV_Target %23
  848. OpReturn
  849. OpFunctionEnd
  850. %src_main = OpFunction %v4float None %25
  851. %vPixelPos = OpFunctionParameter %_ptr_Function_v4float
  852. %bb_entry = OpLabel
  853. %28 = OpLoad %v4float %vPixelPos
  854. %30 = OpVectorShuffle %v2float %28 %28 0 1
  855. %31 = OpCompositeExtract %float %30 0
  856. %32 = OpCompositeExtract %float %30 1
  857. %33 = OpConvertFToU %uint %31
  858. %34 = OpConvertFToU %uint %32
  859. %36 = OpCompositeConstruct %v3uint %33 %34 %uint_0
  860. %38 = OpBitcast %v3int %36
  861. %40 = OpVectorShuffle %v2int %38 %38 0 1
  862. %41 = OpCompositeExtract %int %38 2
  863. %43 = OpAccessChain %_ptr_UniformConstant_type_2d_image %g_testTextures %int_0
  864. %44 = OpLoad %type_2d_image %43
  865. %45 = OpImageFetch %v4float %44 %40 Lod %41
  866. OpReturnValue %45
  867. OpFunctionEnd
  868. )";
  869. SinglePassRunAndMatch<DescriptorScalarReplacement>(shader, true);
  870. }
  871. } // namespace
  872. } // namespace opt
  873. } // namespace spvtools