process_lines_test.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // Copyright (c) 2017 Valve Corporation
  2. // Copyright (c) 2017 LunarG Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "test/opt/pass_fixture.h"
  19. #include "test/opt/pass_utils.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using ProcessLinesTest = PassTest<::testing::Test>;
  24. TEST_F(ProcessLinesTest, SimplePropagation) {
  25. // Texture2D g_tColor[128];
  26. //
  27. // layout(push_constant) cbuffer PerViewConstantBuffer_t
  28. // {
  29. // uint g_nDataIdx;
  30. // uint g_nDataIdx2;
  31. // bool g_B;
  32. // };
  33. //
  34. // SamplerState g_sAniso;
  35. //
  36. // struct PS_INPUT
  37. // {
  38. // float2 vTextureCoords : TEXCOORD2;
  39. // };
  40. //
  41. // struct PS_OUTPUT
  42. // {
  43. // float4 vColor : SV_Target0;
  44. // };
  45. //
  46. // PS_OUTPUT MainPs(PS_INPUT i)
  47. // {
  48. // PS_OUTPUT ps_output;
  49. //
  50. // uint u;
  51. // if (g_B)
  52. // u = g_nDataIdx;
  53. // else
  54. // u = g_nDataIdx2;
  55. // ps_output.vColor = g_tColor[u].Sample(g_sAniso, i.vTextureCoords.xy);
  56. // return ps_output;
  57. // }
  58. const std::string predefs =
  59. R"(OpCapability Shader
  60. %1 = OpExtInstImport "GLSL.std.450"
  61. OpMemoryModel Logical GLSL450
  62. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  63. OpExecutionMode %MainPs OriginUpperLeft
  64. %5 = OpString "foo.frag"
  65. OpSource HLSL 500
  66. OpName %MainPs "MainPs"
  67. OpName %PS_INPUT "PS_INPUT"
  68. OpMemberName %PS_INPUT 0 "vTextureCoords"
  69. OpName %PS_OUTPUT "PS_OUTPUT"
  70. OpMemberName %PS_OUTPUT 0 "vColor"
  71. OpName %_MainPs_struct_PS_INPUT_vf21_ "@MainPs(struct-PS_INPUT-vf21;"
  72. OpName %i "i"
  73. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  74. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  75. OpMemberName %PerViewConstantBuffer_t 1 "g_nDataIdx2"
  76. OpMemberName %PerViewConstantBuffer_t 2 "g_B"
  77. OpName %_ ""
  78. OpName %u "u"
  79. OpName %ps_output "ps_output"
  80. OpName %g_tColor "g_tColor"
  81. OpName %g_sAniso "g_sAniso"
  82. OpName %i_0 "i"
  83. OpName %i_vTextureCoords "i.vTextureCoords"
  84. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  85. OpName %param "param"
  86. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  87. OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 4
  88. OpMemberDecorate %PerViewConstantBuffer_t 2 Offset 8
  89. OpDecorate %PerViewConstantBuffer_t Block
  90. OpDecorate %g_tColor DescriptorSet 0
  91. OpDecorate %g_sAniso DescriptorSet 0
  92. OpDecorate %i_vTextureCoords Location 0
  93. OpDecorate %_entryPointOutput_vColor Location 0
  94. )";
  95. const std::string before =
  96. R"(%void = OpTypeVoid
  97. %19 = OpTypeFunction %void
  98. %float = OpTypeFloat 32
  99. %v2float = OpTypeVector %float 2
  100. %PS_INPUT = OpTypeStruct %v2float
  101. %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
  102. %v4float = OpTypeVector %float 4
  103. %PS_OUTPUT = OpTypeStruct %v4float
  104. %24 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
  105. %uint = OpTypeInt 32 0
  106. %PerViewConstantBuffer_t = OpTypeStruct %uint %uint %uint
  107. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  108. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  109. %int = OpTypeInt 32 1
  110. %int_2 = OpConstant %int 2
  111. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  112. %bool = OpTypeBool
  113. %uint_0 = OpConstant %uint 0
  114. %_ptr_Function_uint = OpTypePointer Function %uint
  115. %int_0 = OpConstant %int 0
  116. %int_1 = OpConstant %int 1
  117. %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
  118. %36 = OpTypeImage %float 2D 0 0 0 1 Unknown
  119. %uint_128 = OpConstant %uint 128
  120. %_arr_36_uint_128 = OpTypeArray %36 %uint_128
  121. %_ptr_UniformConstant__arr_36_uint_128 = OpTypePointer UniformConstant %_arr_36_uint_128
  122. %g_tColor = OpVariable %_ptr_UniformConstant__arr_36_uint_128 UniformConstant
  123. %_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
  124. %41 = OpTypeSampler
  125. %_ptr_UniformConstant_41 = OpTypePointer UniformConstant %41
  126. %g_sAniso = OpVariable %_ptr_UniformConstant_41 UniformConstant
  127. %43 = OpTypeSampledImage %36
  128. %_ptr_Function_v2float = OpTypePointer Function %v2float
  129. %_ptr_Function_v4float = OpTypePointer Function %v4float
  130. %_ptr_Input_v2float = OpTypePointer Input %v2float
  131. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  132. %_ptr_Output_v4float = OpTypePointer Output %v4float
  133. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  134. %MainPs = OpFunction %void None %19
  135. %48 = OpLabel
  136. %i_0 = OpVariable %_ptr_Function_PS_INPUT Function
  137. %param = OpVariable %_ptr_Function_PS_INPUT Function
  138. OpLine %5 23 0
  139. %49 = OpLoad %v2float %i_vTextureCoords
  140. %50 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
  141. OpStore %50 %49
  142. %51 = OpLoad %PS_INPUT %i_0
  143. OpStore %param %51
  144. %52 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
  145. %53 = OpCompositeExtract %v4float %52 0
  146. OpStore %_entryPointOutput_vColor %53
  147. OpReturn
  148. OpFunctionEnd
  149. %_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %24
  150. %i = OpFunctionParameter %_ptr_Function_PS_INPUT
  151. %54 = OpLabel
  152. %u = OpVariable %_ptr_Function_uint Function
  153. %ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
  154. OpLine %5 27 0
  155. %55 = OpAccessChain %_ptr_PushConstant_uint %_ %int_2
  156. %56 = OpLoad %uint %55
  157. %57 = OpINotEqual %bool %56 %uint_0
  158. OpSelectionMerge %58 None
  159. OpBranchConditional %57 %59 %60
  160. %59 = OpLabel
  161. OpLine %5 28 0
  162. %61 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  163. %62 = OpLoad %uint %61
  164. OpStore %u %62
  165. OpBranch %58
  166. %60 = OpLabel
  167. OpLine %5 30 0
  168. %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
  169. %64 = OpLoad %uint %63
  170. OpStore %u %64
  171. OpBranch %58
  172. %58 = OpLabel
  173. OpLine %5 31 0
  174. %65 = OpLoad %uint %u
  175. %66 = OpAccessChain %_ptr_UniformConstant_36 %g_tColor %65
  176. %67 = OpLoad %36 %66
  177. %68 = OpLoad %41 %g_sAniso
  178. %69 = OpSampledImage %43 %67 %68
  179. %70 = OpAccessChain %_ptr_Function_v2float %i %int_0
  180. %71 = OpLoad %v2float %70
  181. %72 = OpImageSampleImplicitLod %v4float %69 %71
  182. %73 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
  183. OpStore %73 %72
  184. OpLine %5 32 0
  185. %74 = OpLoad %PS_OUTPUT %ps_output
  186. OpReturnValue %74
  187. OpFunctionEnd
  188. )";
  189. const std::string after =
  190. R"(OpNoLine
  191. %void = OpTypeVoid
  192. OpNoLine
  193. %19 = OpTypeFunction %void
  194. OpNoLine
  195. %float = OpTypeFloat 32
  196. OpNoLine
  197. %v2float = OpTypeVector %float 2
  198. OpNoLine
  199. %PS_INPUT = OpTypeStruct %v2float
  200. OpNoLine
  201. %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
  202. OpNoLine
  203. %v4float = OpTypeVector %float 4
  204. OpNoLine
  205. %PS_OUTPUT = OpTypeStruct %v4float
  206. OpNoLine
  207. %24 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
  208. OpNoLine
  209. %uint = OpTypeInt 32 0
  210. OpNoLine
  211. %PerViewConstantBuffer_t = OpTypeStruct %uint %uint %uint
  212. OpNoLine
  213. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  214. OpNoLine
  215. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  216. OpNoLine
  217. %int = OpTypeInt 32 1
  218. OpNoLine
  219. %int_2 = OpConstant %int 2
  220. OpNoLine
  221. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  222. OpNoLine
  223. %bool = OpTypeBool
  224. OpNoLine
  225. %uint_0 = OpConstant %uint 0
  226. OpNoLine
  227. %_ptr_Function_uint = OpTypePointer Function %uint
  228. OpNoLine
  229. %int_0 = OpConstant %int 0
  230. OpNoLine
  231. %int_1 = OpConstant %int 1
  232. OpNoLine
  233. %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
  234. OpNoLine
  235. %36 = OpTypeImage %float 2D 0 0 0 1 Unknown
  236. OpNoLine
  237. %uint_128 = OpConstant %uint 128
  238. OpNoLine
  239. %_arr_36_uint_128 = OpTypeArray %36 %uint_128
  240. OpNoLine
  241. %_ptr_UniformConstant__arr_36_uint_128 = OpTypePointer UniformConstant %_arr_36_uint_128
  242. OpNoLine
  243. %g_tColor = OpVariable %_ptr_UniformConstant__arr_36_uint_128 UniformConstant
  244. OpNoLine
  245. %_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
  246. OpNoLine
  247. %41 = OpTypeSampler
  248. OpNoLine
  249. %_ptr_UniformConstant_41 = OpTypePointer UniformConstant %41
  250. OpNoLine
  251. %g_sAniso = OpVariable %_ptr_UniformConstant_41 UniformConstant
  252. OpNoLine
  253. %43 = OpTypeSampledImage %36
  254. OpNoLine
  255. %_ptr_Function_v2float = OpTypePointer Function %v2float
  256. OpNoLine
  257. %_ptr_Function_v4float = OpTypePointer Function %v4float
  258. OpNoLine
  259. %_ptr_Input_v2float = OpTypePointer Input %v2float
  260. OpNoLine
  261. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  262. OpNoLine
  263. %_ptr_Output_v4float = OpTypePointer Output %v4float
  264. OpNoLine
  265. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  266. OpNoLine
  267. %MainPs = OpFunction %void None %19
  268. OpNoLine
  269. %48 = OpLabel
  270. OpNoLine
  271. %i_0 = OpVariable %_ptr_Function_PS_INPUT Function
  272. OpNoLine
  273. %param = OpVariable %_ptr_Function_PS_INPUT Function
  274. OpLine %5 23 0
  275. %49 = OpLoad %v2float %i_vTextureCoords
  276. OpLine %5 23 0
  277. %50 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
  278. OpLine %5 23 0
  279. OpStore %50 %49
  280. OpLine %5 23 0
  281. %51 = OpLoad %PS_INPUT %i_0
  282. OpLine %5 23 0
  283. OpStore %param %51
  284. OpLine %5 23 0
  285. %52 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
  286. OpLine %5 23 0
  287. %53 = OpCompositeExtract %v4float %52 0
  288. OpLine %5 23 0
  289. OpStore %_entryPointOutput_vColor %53
  290. OpLine %5 23 0
  291. OpReturn
  292. OpNoLine
  293. OpFunctionEnd
  294. OpNoLine
  295. %_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %24
  296. OpNoLine
  297. %i = OpFunctionParameter %_ptr_Function_PS_INPUT
  298. OpNoLine
  299. %54 = OpLabel
  300. OpNoLine
  301. %u = OpVariable %_ptr_Function_uint Function
  302. OpNoLine
  303. %ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
  304. OpLine %5 27 0
  305. %55 = OpAccessChain %_ptr_PushConstant_uint %_ %int_2
  306. OpLine %5 27 0
  307. %56 = OpLoad %uint %55
  308. OpLine %5 27 0
  309. %57 = OpINotEqual %bool %56 %uint_0
  310. OpLine %5 27 0
  311. OpSelectionMerge %58 None
  312. OpBranchConditional %57 %59 %60
  313. OpNoLine
  314. %59 = OpLabel
  315. OpLine %5 28 0
  316. %61 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  317. OpLine %5 28 0
  318. %62 = OpLoad %uint %61
  319. OpLine %5 28 0
  320. OpStore %u %62
  321. OpLine %5 28 0
  322. OpBranch %58
  323. OpNoLine
  324. %60 = OpLabel
  325. OpLine %5 30 0
  326. %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
  327. OpLine %5 30 0
  328. %64 = OpLoad %uint %63
  329. OpLine %5 30 0
  330. OpStore %u %64
  331. OpLine %5 30 0
  332. OpBranch %58
  333. OpNoLine
  334. %58 = OpLabel
  335. OpLine %5 31 0
  336. %65 = OpLoad %uint %u
  337. OpLine %5 31 0
  338. %66 = OpAccessChain %_ptr_UniformConstant_36 %g_tColor %65
  339. OpLine %5 31 0
  340. %67 = OpLoad %36 %66
  341. OpLine %5 31 0
  342. %68 = OpLoad %41 %g_sAniso
  343. OpLine %5 31 0
  344. %69 = OpSampledImage %43 %67 %68
  345. OpLine %5 31 0
  346. %70 = OpAccessChain %_ptr_Function_v2float %i %int_0
  347. OpLine %5 31 0
  348. %71 = OpLoad %v2float %70
  349. OpLine %5 31 0
  350. %72 = OpImageSampleImplicitLod %v4float %69 %71
  351. OpLine %5 31 0
  352. %73 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
  353. OpLine %5 31 0
  354. OpStore %73 %72
  355. OpLine %5 32 0
  356. %74 = OpLoad %PS_OUTPUT %ps_output
  357. OpLine %5 32 0
  358. OpReturnValue %74
  359. OpNoLine
  360. OpFunctionEnd
  361. )";
  362. SinglePassRunAndCheck<ProcessLinesPass>(predefs + before, predefs + after,
  363. false, true, kLinesPropagateLines);
  364. }
  365. TEST_F(ProcessLinesTest, SimpleElimination) {
  366. // Previous test with before and after reversed
  367. const std::string predefs =
  368. R"(OpCapability Shader
  369. %1 = OpExtInstImport "GLSL.std.450"
  370. OpMemoryModel Logical GLSL450
  371. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  372. OpExecutionMode %MainPs OriginUpperLeft
  373. %5 = OpString "foo.frag"
  374. OpSource HLSL 500
  375. OpName %MainPs "MainPs"
  376. OpName %PS_INPUT "PS_INPUT"
  377. OpMemberName %PS_INPUT 0 "vTextureCoords"
  378. OpName %PS_OUTPUT "PS_OUTPUT"
  379. OpMemberName %PS_OUTPUT 0 "vColor"
  380. OpName %_MainPs_struct_PS_INPUT_vf21_ "@MainPs(struct-PS_INPUT-vf21;"
  381. OpName %i "i"
  382. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  383. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  384. OpMemberName %PerViewConstantBuffer_t 1 "g_nDataIdx2"
  385. OpMemberName %PerViewConstantBuffer_t 2 "g_B"
  386. OpName %_ ""
  387. OpName %u "u"
  388. OpName %ps_output "ps_output"
  389. OpName %g_tColor "g_tColor"
  390. OpName %g_sAniso "g_sAniso"
  391. OpName %i_0 "i"
  392. OpName %i_vTextureCoords "i.vTextureCoords"
  393. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  394. OpName %param "param"
  395. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  396. OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 4
  397. OpMemberDecorate %PerViewConstantBuffer_t 2 Offset 8
  398. OpDecorate %PerViewConstantBuffer_t Block
  399. OpDecorate %g_tColor DescriptorSet 0
  400. OpDecorate %g_sAniso DescriptorSet 0
  401. OpDecorate %i_vTextureCoords Location 0
  402. OpDecorate %_entryPointOutput_vColor Location 0
  403. )";
  404. const std::string before =
  405. R"(OpNoLine
  406. %void = OpTypeVoid
  407. OpNoLine
  408. %19 = OpTypeFunction %void
  409. OpNoLine
  410. %float = OpTypeFloat 32
  411. OpNoLine
  412. %v2float = OpTypeVector %float 2
  413. OpNoLine
  414. %PS_INPUT = OpTypeStruct %v2float
  415. OpNoLine
  416. %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
  417. OpNoLine
  418. %v4float = OpTypeVector %float 4
  419. OpNoLine
  420. %PS_OUTPUT = OpTypeStruct %v4float
  421. OpNoLine
  422. %24 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
  423. OpNoLine
  424. %uint = OpTypeInt 32 0
  425. OpNoLine
  426. %PerViewConstantBuffer_t = OpTypeStruct %uint %uint %uint
  427. OpNoLine
  428. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  429. OpNoLine
  430. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  431. OpNoLine
  432. %int = OpTypeInt 32 1
  433. OpNoLine
  434. %int_2 = OpConstant %int 2
  435. OpNoLine
  436. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  437. OpNoLine
  438. %bool = OpTypeBool
  439. OpNoLine
  440. %uint_0 = OpConstant %uint 0
  441. OpNoLine
  442. %_ptr_Function_uint = OpTypePointer Function %uint
  443. OpNoLine
  444. %int_0 = OpConstant %int 0
  445. OpNoLine
  446. %int_1 = OpConstant %int 1
  447. OpNoLine
  448. %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
  449. OpNoLine
  450. %36 = OpTypeImage %float 2D 0 0 0 1 Unknown
  451. OpNoLine
  452. %uint_128 = OpConstant %uint 128
  453. OpNoLine
  454. %_arr_36_uint_128 = OpTypeArray %36 %uint_128
  455. OpNoLine
  456. %_ptr_UniformConstant__arr_36_uint_128 = OpTypePointer UniformConstant %_arr_36_uint_128
  457. OpNoLine
  458. %g_tColor = OpVariable %_ptr_UniformConstant__arr_36_uint_128 UniformConstant
  459. OpNoLine
  460. %_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
  461. OpNoLine
  462. %41 = OpTypeSampler
  463. OpNoLine
  464. %_ptr_UniformConstant_41 = OpTypePointer UniformConstant %41
  465. OpNoLine
  466. %g_sAniso = OpVariable %_ptr_UniformConstant_41 UniformConstant
  467. OpNoLine
  468. %43 = OpTypeSampledImage %36
  469. OpNoLine
  470. %_ptr_Function_v2float = OpTypePointer Function %v2float
  471. OpNoLine
  472. %_ptr_Function_v4float = OpTypePointer Function %v4float
  473. OpNoLine
  474. %_ptr_Input_v2float = OpTypePointer Input %v2float
  475. OpNoLine
  476. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  477. OpNoLine
  478. %_ptr_Output_v4float = OpTypePointer Output %v4float
  479. OpNoLine
  480. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  481. OpNoLine
  482. %MainPs = OpFunction %void None %19
  483. OpNoLine
  484. %48 = OpLabel
  485. OpNoLine
  486. %i_0 = OpVariable %_ptr_Function_PS_INPUT Function
  487. OpNoLine
  488. %param = OpVariable %_ptr_Function_PS_INPUT Function
  489. OpLine %5 23 0
  490. %49 = OpLoad %v2float %i_vTextureCoords
  491. OpLine %5 23 0
  492. %50 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
  493. OpLine %5 23 0
  494. OpStore %50 %49
  495. OpLine %5 23 0
  496. %51 = OpLoad %PS_INPUT %i_0
  497. OpLine %5 23 0
  498. OpStore %param %51
  499. OpLine %5 23 0
  500. %52 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
  501. OpLine %5 23 0
  502. %53 = OpCompositeExtract %v4float %52 0
  503. OpLine %5 23 0
  504. OpStore %_entryPointOutput_vColor %53
  505. OpLine %5 23 0
  506. OpReturn
  507. OpNoLine
  508. OpFunctionEnd
  509. OpNoLine
  510. %_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %24
  511. OpNoLine
  512. %i = OpFunctionParameter %_ptr_Function_PS_INPUT
  513. OpNoLine
  514. %54 = OpLabel
  515. OpNoLine
  516. %u = OpVariable %_ptr_Function_uint Function
  517. OpNoLine
  518. %ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
  519. OpLine %5 27 0
  520. %55 = OpAccessChain %_ptr_PushConstant_uint %_ %int_2
  521. OpLine %5 27 0
  522. %56 = OpLoad %uint %55
  523. OpLine %5 27 0
  524. %57 = OpINotEqual %bool %56 %uint_0
  525. OpLine %5 27 0
  526. OpSelectionMerge %58 None
  527. OpBranchConditional %57 %59 %60
  528. OpNoLine
  529. %59 = OpLabel
  530. OpLine %5 28 0
  531. %61 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  532. OpLine %5 28 0
  533. %62 = OpLoad %uint %61
  534. OpLine %5 28 0
  535. OpStore %u %62
  536. OpLine %5 28 0
  537. OpBranch %58
  538. OpNoLine
  539. %60 = OpLabel
  540. OpLine %5 30 0
  541. %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
  542. OpLine %5 30 0
  543. %64 = OpLoad %uint %63
  544. OpLine %5 30 0
  545. OpStore %u %64
  546. OpLine %5 30 0
  547. OpBranch %58
  548. OpNoLine
  549. %58 = OpLabel
  550. OpLine %5 31 0
  551. %65 = OpLoad %uint %u
  552. OpLine %5 31 0
  553. %66 = OpAccessChain %_ptr_UniformConstant_36 %g_tColor %65
  554. OpLine %5 31 0
  555. %67 = OpLoad %36 %66
  556. OpLine %5 31 0
  557. %68 = OpLoad %41 %g_sAniso
  558. OpLine %5 31 0
  559. %69 = OpSampledImage %43 %67 %68
  560. OpLine %5 31 0
  561. %70 = OpAccessChain %_ptr_Function_v2float %i %int_0
  562. OpLine %5 31 0
  563. %71 = OpLoad %v2float %70
  564. OpLine %5 31 0
  565. %72 = OpImageSampleImplicitLod %v4float %69 %71
  566. OpLine %5 31 0
  567. %73 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
  568. OpLine %5 31 0
  569. OpStore %73 %72
  570. OpLine %5 32 0
  571. %74 = OpLoad %PS_OUTPUT %ps_output
  572. OpLine %5 32 0
  573. OpReturnValue %74
  574. OpNoLine
  575. OpFunctionEnd
  576. )";
  577. const std::string after =
  578. R"(%void = OpTypeVoid
  579. %19 = OpTypeFunction %void
  580. %float = OpTypeFloat 32
  581. %v2float = OpTypeVector %float 2
  582. %PS_INPUT = OpTypeStruct %v2float
  583. %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
  584. %v4float = OpTypeVector %float 4
  585. %PS_OUTPUT = OpTypeStruct %v4float
  586. %24 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
  587. %uint = OpTypeInt 32 0
  588. %PerViewConstantBuffer_t = OpTypeStruct %uint %uint %uint
  589. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  590. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  591. %int = OpTypeInt 32 1
  592. %int_2 = OpConstant %int 2
  593. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  594. %bool = OpTypeBool
  595. %uint_0 = OpConstant %uint 0
  596. %_ptr_Function_uint = OpTypePointer Function %uint
  597. %int_0 = OpConstant %int 0
  598. %int_1 = OpConstant %int 1
  599. %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
  600. %36 = OpTypeImage %float 2D 0 0 0 1 Unknown
  601. %uint_128 = OpConstant %uint 128
  602. %_arr_36_uint_128 = OpTypeArray %36 %uint_128
  603. %_ptr_UniformConstant__arr_36_uint_128 = OpTypePointer UniformConstant %_arr_36_uint_128
  604. %g_tColor = OpVariable %_ptr_UniformConstant__arr_36_uint_128 UniformConstant
  605. %_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
  606. %41 = OpTypeSampler
  607. %_ptr_UniformConstant_41 = OpTypePointer UniformConstant %41
  608. %g_sAniso = OpVariable %_ptr_UniformConstant_41 UniformConstant
  609. %43 = OpTypeSampledImage %36
  610. %_ptr_Function_v2float = OpTypePointer Function %v2float
  611. %_ptr_Function_v4float = OpTypePointer Function %v4float
  612. %_ptr_Input_v2float = OpTypePointer Input %v2float
  613. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  614. %_ptr_Output_v4float = OpTypePointer Output %v4float
  615. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  616. %MainPs = OpFunction %void None %19
  617. %48 = OpLabel
  618. %i_0 = OpVariable %_ptr_Function_PS_INPUT Function
  619. %param = OpVariable %_ptr_Function_PS_INPUT Function
  620. OpLine %5 23 0
  621. %49 = OpLoad %v2float %i_vTextureCoords
  622. %50 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
  623. OpStore %50 %49
  624. %51 = OpLoad %PS_INPUT %i_0
  625. OpStore %param %51
  626. %52 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
  627. %53 = OpCompositeExtract %v4float %52 0
  628. OpStore %_entryPointOutput_vColor %53
  629. OpReturn
  630. OpFunctionEnd
  631. %_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %24
  632. %i = OpFunctionParameter %_ptr_Function_PS_INPUT
  633. %54 = OpLabel
  634. %u = OpVariable %_ptr_Function_uint Function
  635. %ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
  636. OpLine %5 27 0
  637. %55 = OpAccessChain %_ptr_PushConstant_uint %_ %int_2
  638. %56 = OpLoad %uint %55
  639. %57 = OpINotEqual %bool %56 %uint_0
  640. OpSelectionMerge %58 None
  641. OpBranchConditional %57 %59 %60
  642. %59 = OpLabel
  643. OpLine %5 28 0
  644. %61 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  645. %62 = OpLoad %uint %61
  646. OpStore %u %62
  647. OpBranch %58
  648. %60 = OpLabel
  649. OpLine %5 30 0
  650. %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
  651. %64 = OpLoad %uint %63
  652. OpStore %u %64
  653. OpBranch %58
  654. %58 = OpLabel
  655. OpLine %5 31 0
  656. %65 = OpLoad %uint %u
  657. %66 = OpAccessChain %_ptr_UniformConstant_36 %g_tColor %65
  658. %67 = OpLoad %36 %66
  659. %68 = OpLoad %41 %g_sAniso
  660. %69 = OpSampledImage %43 %67 %68
  661. %70 = OpAccessChain %_ptr_Function_v2float %i %int_0
  662. %71 = OpLoad %v2float %70
  663. %72 = OpImageSampleImplicitLod %v4float %69 %71
  664. %73 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
  665. OpStore %73 %72
  666. OpLine %5 32 0
  667. %74 = OpLoad %PS_OUTPUT %ps_output
  668. OpReturnValue %74
  669. OpFunctionEnd
  670. )";
  671. SinglePassRunAndCheck<ProcessLinesPass>(
  672. predefs + before, predefs + after, false, true, kLinesEliminateDeadLines);
  673. }
  674. // TODO(greg-lunarg): Add tests to verify handling of these cases:
  675. //
  676. // TODO(greg-lunarg): Think about other tests :)
  677. } // namespace
  678. } // namespace opt
  679. } // namespace spvtools