analyze_live_input_test.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. // Copyright (c) 2022 The Khronos Group Inc.
  2. // Copyright (c) 2022 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 <unordered_set>
  16. #include "gmock/gmock.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 AnalyzeLiveInputTest = PassTest<::testing::Test>;
  23. TEST_F(AnalyzeLiveInputTest, FragMultipleLocations) {
  24. // Should report locations {2, 5}
  25. //
  26. // #version 450
  27. //
  28. // layout(location = 2) in Vertex
  29. // {
  30. // vec4 color0;
  31. // vec4 color1;
  32. // vec4 color2[3];
  33. // } iVert;
  34. //
  35. // layout(location = 0) out vec4 oFragColor;
  36. //
  37. // void main()
  38. // {
  39. // oFragColor = iVert.color0 + iVert.color2[1];
  40. // }
  41. const std::string text = R"(
  42. OpCapability Shader
  43. %1 = OpExtInstImport "GLSL.std.450"
  44. OpMemoryModel Logical GLSL450
  45. OpEntryPoint Fragment %main "main" %oFragColor %iVert
  46. OpExecutionMode %main OriginUpperLeft
  47. OpSource GLSL 450
  48. OpName %main "main"
  49. OpName %oFragColor "oFragColor"
  50. OpName %Vertex "Vertex"
  51. OpMemberName %Vertex 0 "color0"
  52. OpMemberName %Vertex 1 "color1"
  53. OpMemberName %Vertex 2 "color2"
  54. OpName %iVert "iVert"
  55. OpDecorate %oFragColor Location 0
  56. OpDecorate %Vertex Block
  57. OpDecorate %iVert Location 2
  58. %void = OpTypeVoid
  59. %3 = OpTypeFunction %void
  60. %float = OpTypeFloat 32
  61. %v4float = OpTypeVector %float 4
  62. %_ptr_Output_v4float = OpTypePointer Output %v4float
  63. %oFragColor = OpVariable %_ptr_Output_v4float Output
  64. %uint = OpTypeInt 32 0
  65. %uint_3 = OpConstant %uint 3
  66. %_arr_v4float_uint_3 = OpTypeArray %v4float %uint_3
  67. %Vertex = OpTypeStruct %v4float %v4float %_arr_v4float_uint_3
  68. %_ptr_Input_Vertex = OpTypePointer Input %Vertex
  69. %iVert = OpVariable %_ptr_Input_Vertex Input
  70. %int = OpTypeInt 32 1
  71. %int_0 = OpConstant %int 0
  72. %_ptr_Input_v4float = OpTypePointer Input %v4float
  73. %int_2 = OpConstant %int 2
  74. %int_1 = OpConstant %int 1
  75. %main = OpFunction %void None %3
  76. %5 = OpLabel
  77. %19 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
  78. %20 = OpLoad %v4float %19
  79. %23 = OpAccessChain %_ptr_Input_v4float %iVert %int_2 %int_1
  80. %24 = OpLoad %v4float %23
  81. %25 = OpFAdd %v4float %20 %24
  82. OpStore %oFragColor %25
  83. OpReturn
  84. OpFunctionEnd
  85. )";
  86. SetTargetEnv(SPV_ENV_VULKAN_1_3);
  87. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  88. std::unordered_set<uint32_t> live_inputs;
  89. std::unordered_set<uint32_t> live_builtins;
  90. auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
  91. text, true, &live_inputs, &live_builtins);
  92. auto itr0 = live_inputs.find(0);
  93. auto itr1 = live_inputs.find(1);
  94. auto itr2 = live_inputs.find(2);
  95. auto itr3 = live_inputs.find(3);
  96. auto itr4 = live_inputs.find(4);
  97. auto itr5 = live_inputs.find(5);
  98. auto itr6 = live_inputs.find(6);
  99. // Expect live_inputs == {2, 5}
  100. EXPECT_TRUE(itr0 == live_inputs.end());
  101. EXPECT_TRUE(itr1 == live_inputs.end());
  102. EXPECT_TRUE(itr2 != live_inputs.end());
  103. EXPECT_TRUE(itr3 == live_inputs.end());
  104. EXPECT_TRUE(itr4 == live_inputs.end());
  105. EXPECT_TRUE(itr5 != live_inputs.end());
  106. EXPECT_TRUE(itr6 == live_inputs.end());
  107. }
  108. TEST_F(AnalyzeLiveInputTest, FragMatrix) {
  109. // Should report locations {2, 8, 9, 10, 11}
  110. //
  111. // #version 450
  112. //
  113. // uniform ui_name {
  114. // int i;
  115. // } ui_inst;
  116. //
  117. // layout(location = 2) in Vertex
  118. // {
  119. // vec4 color0;
  120. // vec4 color1;
  121. // mat4 color2;
  122. // mat4 color3;
  123. // mat4 color4;
  124. // } iVert;
  125. //
  126. // // Output variable for the color
  127. // layout(location = 0) out vec4 oFragColor;
  128. //
  129. // void main()
  130. // {
  131. // oFragColor = iVert.color0 + iVert.color3[ui_inst.i];
  132. // }
  133. const std::string text = R"(
  134. OpCapability Shader
  135. %1 = OpExtInstImport "GLSL.std.450"
  136. OpMemoryModel Logical GLSL450
  137. OpEntryPoint Fragment %main "main" %oFragColor %iVert %ui_inst
  138. OpExecutionMode %main OriginUpperLeft
  139. OpSource GLSL 450
  140. OpName %main "main"
  141. OpName %oFragColor "oFragColor"
  142. OpName %Vertex "Vertex"
  143. OpMemberName %Vertex 0 "color0"
  144. OpMemberName %Vertex 1 "color1"
  145. OpMemberName %Vertex 2 "color2"
  146. OpMemberName %Vertex 3 "color3"
  147. OpMemberName %Vertex 4 "color4"
  148. OpName %iVert "iVert"
  149. OpName %ui_name "ui_name"
  150. OpMemberName %ui_name 0 "i"
  151. OpName %ui_inst "ui_inst"
  152. OpDecorate %oFragColor Location 0
  153. OpDecorate %Vertex Block
  154. OpDecorate %iVert Location 2
  155. OpMemberDecorate %ui_name 0 Offset 0
  156. OpDecorate %ui_name Block
  157. OpDecorate %ui_inst DescriptorSet 0
  158. OpDecorate %ui_inst Binding 0
  159. %void = OpTypeVoid
  160. %3 = OpTypeFunction %void
  161. %float = OpTypeFloat 32
  162. %v4float = OpTypeVector %float 4
  163. %_ptr_Output_v4float = OpTypePointer Output %v4float
  164. %oFragColor = OpVariable %_ptr_Output_v4float Output
  165. %mat4v4float = OpTypeMatrix %v4float 4
  166. %Vertex = OpTypeStruct %v4float %v4float %mat4v4float %mat4v4float %mat4v4float
  167. %_ptr_Input_Vertex = OpTypePointer Input %Vertex
  168. %iVert = OpVariable %_ptr_Input_Vertex Input
  169. %int = OpTypeInt 32 1
  170. %int_0 = OpConstant %int 0
  171. %_ptr_Input_v4float = OpTypePointer Input %v4float
  172. %int_3 = OpConstant %int 3
  173. %ui_name = OpTypeStruct %int
  174. %_ptr_Uniform_ui_name = OpTypePointer Uniform %ui_name
  175. %ui_inst = OpVariable %_ptr_Uniform_ui_name Uniform
  176. %_ptr_Uniform_int = OpTypePointer Uniform %int
  177. %main = OpFunction %void None %3
  178. %5 = OpLabel
  179. %17 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
  180. %18 = OpLoad %v4float %17
  181. %24 = OpAccessChain %_ptr_Uniform_int %ui_inst %int_0
  182. %25 = OpLoad %int %24
  183. %26 = OpAccessChain %_ptr_Input_v4float %iVert %int_3 %25
  184. %27 = OpLoad %v4float %26
  185. %28 = OpFAdd %v4float %18 %27
  186. OpStore %oFragColor %28
  187. OpReturn
  188. OpFunctionEnd
  189. )";
  190. SetTargetEnv(SPV_ENV_VULKAN_1_3);
  191. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  192. std::unordered_set<uint32_t> live_inputs;
  193. std::unordered_set<uint32_t> live_builtins;
  194. auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
  195. text, true, &live_inputs, &live_builtins);
  196. auto itr0 = live_inputs.find(0);
  197. auto itr1 = live_inputs.find(1);
  198. auto itr2 = live_inputs.find(2);
  199. auto itr3 = live_inputs.find(3);
  200. auto itr4 = live_inputs.find(4);
  201. auto itr5 = live_inputs.find(5);
  202. auto itr6 = live_inputs.find(6);
  203. auto itr7 = live_inputs.find(7);
  204. auto itr8 = live_inputs.find(8);
  205. auto itr9 = live_inputs.find(9);
  206. auto itr10 = live_inputs.find(10);
  207. auto itr11 = live_inputs.find(11);
  208. auto itr12 = live_inputs.find(12);
  209. auto itr13 = live_inputs.find(13);
  210. auto itr14 = live_inputs.find(14);
  211. auto itr15 = live_inputs.find(15);
  212. // Expect live_inputs == {2, 8, 9, 10, 11}
  213. EXPECT_TRUE(itr0 == live_inputs.end());
  214. EXPECT_TRUE(itr1 == live_inputs.end());
  215. EXPECT_TRUE(itr2 != live_inputs.end());
  216. EXPECT_TRUE(itr3 == live_inputs.end());
  217. EXPECT_TRUE(itr4 == live_inputs.end());
  218. EXPECT_TRUE(itr5 == live_inputs.end());
  219. EXPECT_TRUE(itr6 == live_inputs.end());
  220. EXPECT_TRUE(itr7 == live_inputs.end());
  221. EXPECT_TRUE(itr8 != live_inputs.end());
  222. EXPECT_TRUE(itr9 != live_inputs.end());
  223. EXPECT_TRUE(itr10 != live_inputs.end());
  224. EXPECT_TRUE(itr11 != live_inputs.end());
  225. EXPECT_TRUE(itr12 == live_inputs.end());
  226. EXPECT_TRUE(itr13 == live_inputs.end());
  227. EXPECT_TRUE(itr14 == live_inputs.end());
  228. EXPECT_TRUE(itr15 == live_inputs.end());
  229. }
  230. TEST_F(AnalyzeLiveInputTest, FragMemberLocs) {
  231. // Should report location {1}
  232. //
  233. // #version 450
  234. //
  235. // in Vertex
  236. // {
  237. // layout (location = 1) vec4 Cd;
  238. // layout (location = 0) vec2 uv;
  239. // } iVert;
  240. //
  241. // layout (location = 0) out vec4 fragColor;
  242. //
  243. // void main()
  244. // {
  245. // vec4 color = vec4(iVert.Cd);
  246. // fragColor = color;
  247. // }
  248. const std::string text = R"(
  249. OpCapability Shader
  250. %1 = OpExtInstImport "GLSL.std.450"
  251. OpMemoryModel Logical GLSL450
  252. OpEntryPoint Fragment %main "main" %iVert %fragColor
  253. OpExecutionMode %main OriginUpperLeft
  254. OpSource GLSL 450
  255. OpName %main "main"
  256. OpName %color "color"
  257. OpName %Vertex "Vertex"
  258. OpMemberName %Vertex 0 "Cd"
  259. OpMemberName %Vertex 1 "uv"
  260. OpName %iVert "iVert"
  261. OpName %fragColor "fragColor"
  262. OpMemberDecorate %Vertex 0 Location 1
  263. OpMemberDecorate %Vertex 1 Location 0
  264. OpDecorate %Vertex Block
  265. OpDecorate %fragColor Location 0
  266. OpDecorate %_struct_27 Block
  267. OpMemberDecorate %_struct_27 0 Location 1
  268. %void = OpTypeVoid
  269. %3 = OpTypeFunction %void
  270. %float = OpTypeFloat 32
  271. %v4float = OpTypeVector %float 4
  272. %_ptr_Function_v4float = OpTypePointer Function %v4float
  273. %v2float = OpTypeVector %float 2
  274. %Vertex = OpTypeStruct %v4float %v2float
  275. %_ptr_Input_Vertex = OpTypePointer Input %Vertex
  276. %int = OpTypeInt 32 1
  277. %int_0 = OpConstant %int 0
  278. %_ptr_Input_v4float = OpTypePointer Input %v4float
  279. %_ptr_Output_v4float = OpTypePointer Output %v4float
  280. %fragColor = OpVariable %_ptr_Output_v4float Output
  281. %_struct_27 = OpTypeStruct %v4float
  282. %_ptr_Input__struct_27 = OpTypePointer Input %_struct_27
  283. %iVert = OpVariable %_ptr_Input__struct_27 Input
  284. %main = OpFunction %void None %3
  285. %5 = OpLabel
  286. %color = OpVariable %_ptr_Function_v4float Function
  287. %17 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
  288. %18 = OpLoad %v4float %17
  289. %19 = OpCompositeExtract %float %18 0
  290. %20 = OpCompositeExtract %float %18 1
  291. %21 = OpCompositeExtract %float %18 2
  292. %22 = OpCompositeExtract %float %18 3
  293. %23 = OpCompositeConstruct %v4float %19 %20 %21 %22
  294. OpStore %color %23
  295. %26 = OpLoad %v4float %color
  296. OpStore %fragColor %26
  297. OpReturn
  298. OpFunctionEnd
  299. )";
  300. SetTargetEnv(SPV_ENV_VULKAN_1_3);
  301. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  302. std::unordered_set<uint32_t> live_inputs;
  303. std::unordered_set<uint32_t> live_builtins;
  304. auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
  305. text, true, &live_inputs, &live_builtins);
  306. auto itr0 = live_inputs.find(0);
  307. auto itr1 = live_inputs.find(1);
  308. // Expect live_inputs == {2, 5}
  309. EXPECT_TRUE(itr0 == live_inputs.end());
  310. EXPECT_TRUE(itr1 != live_inputs.end());
  311. }
  312. TEST_F(AnalyzeLiveInputTest, ArrayedInput) {
  313. // Tests handling of arrayed input seen in Tesc, Tese and Geom shaders.
  314. //
  315. // Should report location {1, 10}.
  316. //
  317. // #version 450
  318. //
  319. // layout (vertices = 4) out;
  320. //
  321. // layout (location = 1) in Vertex
  322. // {
  323. // vec4 p;
  324. // vec3 n;
  325. // vec4 f[100];
  326. // } iVert[];
  327. //
  328. // layout (location = 0) out vec4 position[4];
  329. //
  330. // void main()
  331. // {
  332. // vec4 pos = iVert[gl_InvocationID].p *
  333. // iVert[gl_InvocationID].f[7];
  334. // position[gl_InvocationID] = pos;
  335. // }
  336. const std::string text = R"(
  337. OpCapability Tessellation
  338. %1 = OpExtInstImport "GLSL.std.450"
  339. OpMemoryModel Logical GLSL450
  340. OpEntryPoint TessellationControl %main "main" %iVert %gl_InvocationID %position
  341. OpExecutionMode %main OutputVertices 4
  342. OpSource GLSL 450
  343. OpName %main "main"
  344. OpName %Vertex "Vertex"
  345. OpMemberName %Vertex 0 "p"
  346. OpMemberName %Vertex 1 "n"
  347. OpMemberName %Vertex 2 "f"
  348. OpName %iVert "iVert"
  349. OpName %gl_InvocationID "gl_InvocationID"
  350. OpName %position "position"
  351. OpDecorate %Vertex Block
  352. OpDecorate %iVert Location 1
  353. OpDecorate %gl_InvocationID BuiltIn InvocationId
  354. OpDecorate %position Location 0
  355. %void = OpTypeVoid
  356. %3 = OpTypeFunction %void
  357. %float = OpTypeFloat 32
  358. %v4float = OpTypeVector %float 4
  359. %v3float = OpTypeVector %float 3
  360. %uint = OpTypeInt 32 0
  361. %uint_100 = OpConstant %uint 100
  362. %_arr_v4float_uint_100 = OpTypeArray %v4float %uint_100
  363. %Vertex = OpTypeStruct %v4float %v3float %_arr_v4float_uint_100
  364. %uint_32 = OpConstant %uint 32
  365. %_arr_Vertex_uint_32 = OpTypeArray %Vertex %uint_32
  366. %_ptr_Input__arr_Vertex_uint_32 = OpTypePointer Input %_arr_Vertex_uint_32
  367. %iVert = OpVariable %_ptr_Input__arr_Vertex_uint_32 Input
  368. %int = OpTypeInt 32 1
  369. %_ptr_Input_int = OpTypePointer Input %int
  370. %gl_InvocationID = OpVariable %_ptr_Input_int Input
  371. %int_0 = OpConstant %int 0
  372. %_ptr_Input_v4float = OpTypePointer Input %v4float
  373. %int_2 = OpConstant %int 2
  374. %int_7 = OpConstant %int 7
  375. %uint_4 = OpConstant %uint 4
  376. %_arr_v4float_uint_4 = OpTypeArray %v4float %uint_4
  377. %_ptr_Output__arr_v4float_uint_4 = OpTypePointer Output %_arr_v4float_uint_4
  378. %position = OpVariable %_ptr_Output__arr_v4float_uint_4 Output
  379. %_ptr_Output_v4float = OpTypePointer Output %v4float
  380. %main = OpFunction %void None %3
  381. %5 = OpLabel
  382. %22 = OpLoad %int %gl_InvocationID
  383. %25 = OpAccessChain %_ptr_Input_v4float %iVert %22 %int_0
  384. %26 = OpLoad %v4float %25
  385. %30 = OpAccessChain %_ptr_Input_v4float %iVert %22 %int_2 %int_7
  386. %31 = OpLoad %v4float %30
  387. %32 = OpFMul %v4float %26 %31
  388. %40 = OpAccessChain %_ptr_Output_v4float %position %22
  389. OpStore %40 %32
  390. OpReturn
  391. OpFunctionEnd
  392. )";
  393. SetTargetEnv(SPV_ENV_VULKAN_1_3);
  394. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  395. std::unordered_set<uint32_t> live_inputs;
  396. std::unordered_set<uint32_t> live_builtins;
  397. auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
  398. text, true, &live_inputs, &live_builtins);
  399. auto itr0 = live_inputs.find(0);
  400. auto itr1 = live_inputs.find(1);
  401. auto itr2 = live_inputs.find(2);
  402. auto itr3 = live_inputs.find(3);
  403. auto itr4 = live_inputs.find(4);
  404. auto itr5 = live_inputs.find(5);
  405. auto itr6 = live_inputs.find(6);
  406. auto itr7 = live_inputs.find(7);
  407. auto itr8 = live_inputs.find(8);
  408. auto itr9 = live_inputs.find(9);
  409. auto itr10 = live_inputs.find(10);
  410. auto itr11 = live_inputs.find(11);
  411. // Expect live_inputs == {1, 10}
  412. EXPECT_TRUE(itr0 == live_inputs.end());
  413. EXPECT_TRUE(itr1 != live_inputs.end());
  414. EXPECT_TRUE(itr2 == live_inputs.end());
  415. EXPECT_TRUE(itr3 == live_inputs.end());
  416. EXPECT_TRUE(itr4 == live_inputs.end());
  417. EXPECT_TRUE(itr5 == live_inputs.end());
  418. EXPECT_TRUE(itr6 == live_inputs.end());
  419. EXPECT_TRUE(itr7 == live_inputs.end());
  420. EXPECT_TRUE(itr8 == live_inputs.end());
  421. EXPECT_TRUE(itr9 == live_inputs.end());
  422. EXPECT_TRUE(itr10 != live_inputs.end());
  423. EXPECT_TRUE(itr11 == live_inputs.end());
  424. }
  425. TEST_F(AnalyzeLiveInputTest, ArrayedInputMemberLocs) {
  426. // Tests handling of member locs with arrayed input seen in Tesc, Tese
  427. // and Geom shaders.
  428. //
  429. // Should report location {1, 12}.
  430. //
  431. // #version 450
  432. //
  433. // layout (vertices = 4) out;
  434. //
  435. // in Vertex
  436. // {
  437. // layout (location = 1) vec4 p;
  438. // layout (location = 3) vec3 n;
  439. // layout (location = 5) vec4 f[100];
  440. // } iVert[];
  441. //
  442. // layout (location = 0) out vec4 position[4];
  443. //
  444. // void main()
  445. // {
  446. // vec4 pos = iVert[gl_InvocationID].p *
  447. // iVert[gl_InvocationID].f[7];
  448. // position[gl_InvocationID] = pos;
  449. // }
  450. const std::string text = R"(
  451. OpCapability Tessellation
  452. %1 = OpExtInstImport "GLSL.std.450"
  453. OpMemoryModel Logical GLSL450
  454. OpEntryPoint TessellationControl %main "main" %iVert %gl_InvocationID %position
  455. OpExecutionMode %main OutputVertices 4
  456. OpSource GLSL 450
  457. OpName %main "main"
  458. OpName %Vertex "Vertex"
  459. OpMemberName %Vertex 0 "p"
  460. OpMemberName %Vertex 1 "n"
  461. OpMemberName %Vertex 2 "f"
  462. OpName %iVert "iVert"
  463. OpName %gl_InvocationID "gl_InvocationID"
  464. OpName %position "position"
  465. OpMemberDecorate %Vertex 0 Location 1
  466. OpMemberDecorate %Vertex 1 Location 3
  467. OpMemberDecorate %Vertex 2 Location 5
  468. OpDecorate %Vertex Block
  469. OpDecorate %gl_InvocationID BuiltIn InvocationId
  470. OpDecorate %position Location 0
  471. %void = OpTypeVoid
  472. %3 = OpTypeFunction %void
  473. %float = OpTypeFloat 32
  474. %v4float = OpTypeVector %float 4
  475. %v3float = OpTypeVector %float 3
  476. %uint = OpTypeInt 32 0
  477. %uint_100 = OpConstant %uint 100
  478. %_arr_v4float_uint_100 = OpTypeArray %v4float %uint_100
  479. %Vertex = OpTypeStruct %v4float %v3float %_arr_v4float_uint_100
  480. %uint_32 = OpConstant %uint 32
  481. %_arr_Vertex_uint_32 = OpTypeArray %Vertex %uint_32
  482. %_ptr_Input__arr_Vertex_uint_32 = OpTypePointer Input %_arr_Vertex_uint_32
  483. %iVert = OpVariable %_ptr_Input__arr_Vertex_uint_32 Input
  484. %int = OpTypeInt 32 1
  485. %_ptr_Input_int = OpTypePointer Input %int
  486. %gl_InvocationID = OpVariable %_ptr_Input_int Input
  487. %int_0 = OpConstant %int 0
  488. %_ptr_Input_v4float = OpTypePointer Input %v4float
  489. %int_2 = OpConstant %int 2
  490. %int_7 = OpConstant %int 7
  491. %uint_4 = OpConstant %uint 4
  492. %_arr_v4float_uint_4 = OpTypeArray %v4float %uint_4
  493. %_ptr_Output__arr_v4float_uint_4 = OpTypePointer Output %_arr_v4float_uint_4
  494. %position = OpVariable %_ptr_Output__arr_v4float_uint_4 Output
  495. %_ptr_Output_v4float = OpTypePointer Output %v4float
  496. %main = OpFunction %void None %3
  497. %5 = OpLabel
  498. %22 = OpLoad %int %gl_InvocationID
  499. %25 = OpAccessChain %_ptr_Input_v4float %iVert %22 %int_0
  500. %26 = OpLoad %v4float %25
  501. %30 = OpAccessChain %_ptr_Input_v4float %iVert %22 %int_2 %int_7
  502. %31 = OpLoad %v4float %30
  503. %32 = OpFMul %v4float %26 %31
  504. %40 = OpAccessChain %_ptr_Output_v4float %position %22
  505. OpStore %40 %32
  506. OpReturn
  507. OpFunctionEnd
  508. )";
  509. SetTargetEnv(SPV_ENV_VULKAN_1_3);
  510. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  511. std::unordered_set<uint32_t> live_inputs;
  512. std::unordered_set<uint32_t> live_builtins;
  513. auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
  514. text, true, &live_inputs, &live_builtins);
  515. auto itr0 = live_inputs.find(0);
  516. auto itr1 = live_inputs.find(1);
  517. auto itr2 = live_inputs.find(2);
  518. auto itr3 = live_inputs.find(3);
  519. auto itr4 = live_inputs.find(4);
  520. auto itr5 = live_inputs.find(5);
  521. auto itr6 = live_inputs.find(6);
  522. auto itr7 = live_inputs.find(7);
  523. auto itr8 = live_inputs.find(8);
  524. auto itr9 = live_inputs.find(9);
  525. auto itr10 = live_inputs.find(10);
  526. auto itr11 = live_inputs.find(11);
  527. auto itr12 = live_inputs.find(12);
  528. auto itr13 = live_inputs.find(13);
  529. // Expect live_inputs == {1, 12}
  530. EXPECT_TRUE(itr0 == live_inputs.end());
  531. EXPECT_TRUE(itr1 != live_inputs.end());
  532. EXPECT_TRUE(itr2 == live_inputs.end());
  533. EXPECT_TRUE(itr3 == live_inputs.end());
  534. EXPECT_TRUE(itr4 == live_inputs.end());
  535. EXPECT_TRUE(itr5 == live_inputs.end());
  536. EXPECT_TRUE(itr6 == live_inputs.end());
  537. EXPECT_TRUE(itr7 == live_inputs.end());
  538. EXPECT_TRUE(itr8 == live_inputs.end());
  539. EXPECT_TRUE(itr9 == live_inputs.end());
  540. EXPECT_TRUE(itr10 == live_inputs.end());
  541. EXPECT_TRUE(itr11 == live_inputs.end());
  542. EXPECT_TRUE(itr12 != live_inputs.end());
  543. EXPECT_TRUE(itr13 == live_inputs.end());
  544. }
  545. TEST_F(AnalyzeLiveInputTest, Builtins) {
  546. // Tests handling of builtin input seen in Tesc, Tese and Geom shaders.
  547. //
  548. // Should report builtin gl_PointSize only.
  549. //
  550. // #version 460
  551. //
  552. // layout(triangle_strip, max_vertices = 3) out;
  553. // layout(triangles) in;
  554. //
  555. // void main()
  556. // {
  557. // for (int i = 0; i < 3; i++)
  558. // {
  559. // gl_Position = gl_in[i].gl_Position;
  560. // gl_PointSize = gl_in[i].gl_PointSize;
  561. //
  562. // EmitVertex();
  563. // }
  564. //
  565. // EndPrimitive();
  566. // }
  567. const std::string text = R"(
  568. OpCapability Geometry
  569. OpCapability GeometryPointSize
  570. %1 = OpExtInstImport "GLSL.std.450"
  571. OpMemoryModel Logical GLSL450
  572. OpEntryPoint Geometry %main "main" %_ %gl_in
  573. OpExecutionMode %main Triangles
  574. OpExecutionMode %main Invocations 1
  575. OpExecutionMode %main OutputTriangleStrip
  576. OpExecutionMode %main OutputVertices 3
  577. OpSource GLSL 460
  578. OpName %main "main"
  579. OpName %i "i"
  580. OpName %gl_PerVertex "gl_PerVertex"
  581. OpMemberName %gl_PerVertex 0 "gl_Position"
  582. OpMemberName %gl_PerVertex 1 "gl_PointSize"
  583. OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
  584. OpMemberName %gl_PerVertex 3 "gl_CullDistance"
  585. OpName %_ ""
  586. OpName %gl_PerVertex_0 "gl_PerVertex"
  587. OpMemberName %gl_PerVertex_0 0 "gl_Position"
  588. OpMemberName %gl_PerVertex_0 1 "gl_PointSize"
  589. OpMemberName %gl_PerVertex_0 2 "gl_ClipDistance"
  590. OpMemberName %gl_PerVertex_0 3 "gl_CullDistance"
  591. OpName %gl_in "gl_in"
  592. OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
  593. OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
  594. OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
  595. OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
  596. OpDecorate %gl_PerVertex Block
  597. OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
  598. OpMemberDecorate %gl_PerVertex_0 1 BuiltIn PointSize
  599. OpDecorate %gl_PerVertex_0 Block
  600. %void = OpTypeVoid
  601. %3 = OpTypeFunction %void
  602. %int = OpTypeInt 32 1
  603. %_ptr_Function_int = OpTypePointer Function %int
  604. %int_0 = OpConstant %int 0
  605. %int_3 = OpConstant %int 3
  606. %bool = OpTypeBool
  607. %float = OpTypeFloat 32
  608. %v4float = OpTypeVector %float 4
  609. %uint = OpTypeInt 32 0
  610. %uint_1 = OpConstant %uint 1
  611. %_arr_float_uint_1 = OpTypeArray %float %uint_1
  612. %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
  613. %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
  614. %_ = OpVariable %_ptr_Output_gl_PerVertex Output
  615. %gl_PerVertex_0 = OpTypeStruct %v4float %float
  616. %uint_3 = OpConstant %uint 3
  617. %_arr_gl_PerVertex_0_uint_3 = OpTypeArray %gl_PerVertex_0 %uint_3
  618. %_ptr_Input__arr_gl_PerVertex_0_uint_3 = OpTypePointer Input %_arr_gl_PerVertex_0_uint_3
  619. %gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_0_uint_3 Input
  620. %_ptr_Input_v4float = OpTypePointer Input %v4float
  621. %_ptr_Output_v4float = OpTypePointer Output %v4float
  622. %int_1 = OpConstant %int 1
  623. %_ptr_Input_float = OpTypePointer Input %float
  624. %_ptr_Output_float = OpTypePointer Output %float
  625. %main = OpFunction %void None %3
  626. %5 = OpLabel
  627. %i = OpVariable %_ptr_Function_int Function
  628. OpStore %i %int_0
  629. OpBranch %10
  630. %10 = OpLabel
  631. OpLoopMerge %12 %13 None
  632. OpBranch %14
  633. %14 = OpLabel
  634. %15 = OpLoad %int %i
  635. %18 = OpSLessThan %bool %15 %int_3
  636. OpBranchConditional %18 %11 %12
  637. %11 = OpLabel
  638. %32 = OpLoad %int %i
  639. %34 = OpAccessChain %_ptr_Input_v4float %gl_in %32 %int_0
  640. %35 = OpLoad %v4float %34
  641. %37 = OpAccessChain %_ptr_Output_v4float %_ %int_0
  642. OpStore %37 %35
  643. %39 = OpLoad %int %i
  644. %41 = OpAccessChain %_ptr_Input_float %gl_in %39 %int_1
  645. %42 = OpLoad %float %41
  646. %44 = OpAccessChain %_ptr_Output_float %_ %int_1
  647. OpStore %44 %42
  648. OpEmitVertex
  649. OpBranch %13
  650. %13 = OpLabel
  651. %45 = OpLoad %int %i
  652. %46 = OpIAdd %int %45 %int_1
  653. OpStore %i %46
  654. OpBranch %10
  655. %12 = OpLabel
  656. OpEndPrimitive
  657. OpReturn
  658. OpFunctionEnd
  659. )";
  660. SetTargetEnv(SPV_ENV_VULKAN_1_3);
  661. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  662. std::unordered_set<uint32_t> live_inputs;
  663. std::unordered_set<uint32_t> live_builtins;
  664. auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
  665. text, true, &live_inputs, &live_builtins);
  666. auto itr0 = live_builtins.find((uint32_t)spv::BuiltIn::PointSize);
  667. auto itr1 = live_builtins.find((uint32_t)spv::BuiltIn::ClipDistance);
  668. auto itr2 = live_builtins.find((uint32_t)spv::BuiltIn::CullDistance);
  669. // Expect live_builtins == { spv::BuiltIn::PointSize }
  670. EXPECT_TRUE(itr0 != live_builtins.end());
  671. EXPECT_TRUE(itr1 == live_builtins.end());
  672. EXPECT_TRUE(itr2 == live_builtins.end());
  673. }
  674. TEST_F(AnalyzeLiveInputTest, ArrayedInputPatchLocs) {
  675. // Tests handling of locs with arrayed input patch seen in Tese
  676. //
  677. // Should report location {3}.
  678. //
  679. // #version 450 core
  680. //
  681. // layout(triangles, ccw) in;
  682. //
  683. // layout(fractional_odd_spacing) in;
  684. //
  685. // layout(point_mode) in;
  686. //
  687. // layout(location=2) patch in float patchIn1[2];
  688. //
  689. // void main()
  690. // {
  691. // vec4 p = gl_in[1].gl_Position;
  692. // gl_Position = p * patchIn1[1];
  693. // }
  694. const std::string text = R"(
  695. OpCapability Tessellation
  696. %1 = OpExtInstImport "GLSL.std.450"
  697. OpMemoryModel Logical GLSL450
  698. OpEntryPoint TessellationEvaluation %main "main" %gl_in %_ %patchIn1
  699. OpExecutionMode %main Triangles
  700. OpExecutionMode %main SpacingFractionalOdd
  701. OpExecutionMode %main VertexOrderCcw
  702. OpExecutionMode %main PointMode
  703. OpSource GLSL 450
  704. OpName %main "main"
  705. OpName %p "p"
  706. OpName %gl_PerVertex "gl_PerVertex"
  707. OpMemberName %gl_PerVertex 0 "gl_Position"
  708. OpName %gl_in "gl_in"
  709. OpName %gl_PerVertex_0 "gl_PerVertex"
  710. OpMemberName %gl_PerVertex_0 0 "gl_Position"
  711. OpName %_ ""
  712. OpName %patchIn1 "patchIn1"
  713. OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
  714. OpDecorate %gl_PerVertex Block
  715. OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
  716. OpDecorate %gl_PerVertex_0 Block
  717. OpDecorate %patchIn1 Patch
  718. OpDecorate %patchIn1 Location 2
  719. %void = OpTypeVoid
  720. %3 = OpTypeFunction %void
  721. %float = OpTypeFloat 32
  722. %v4float = OpTypeVector %float 4
  723. %_ptr_Function_v4float = OpTypePointer Function %v4float
  724. %uint = OpTypeInt 32 0
  725. %uint_1 = OpConstant %uint 1
  726. %_arr_float_uint_1 = OpTypeArray %float %uint_1
  727. %gl_PerVertex = OpTypeStruct %v4float
  728. %uint_32 = OpConstant %uint 32
  729. %_arr_gl_PerVertex_uint_32 = OpTypeArray %gl_PerVertex %uint_32
  730. %_ptr_Input__arr_gl_PerVertex_uint_32 = OpTypePointer Input %_arr_gl_PerVertex_uint_32
  731. %gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_uint_32 Input
  732. %int = OpTypeInt 32 1
  733. %int_1 = OpConstant %int 1
  734. %int_0 = OpConstant %int 0
  735. %_ptr_Input_v4float = OpTypePointer Input %v4float
  736. %gl_PerVertex_0 = OpTypeStruct %v4float
  737. %_ptr_Output_gl_PerVertex_0 = OpTypePointer Output %gl_PerVertex_0
  738. %_ = OpVariable %_ptr_Output_gl_PerVertex_0 Output
  739. %uint_2 = OpConstant %uint 2
  740. %_arr_float_uint_2 = OpTypeArray %float %uint_2
  741. %_ptr_Input__arr_float_uint_2 = OpTypePointer Input %_arr_float_uint_2
  742. %patchIn1 = OpVariable %_ptr_Input__arr_float_uint_2 Input
  743. %_ptr_Input_float = OpTypePointer Input %float
  744. %_ptr_Output_v4float = OpTypePointer Output %v4float
  745. %main = OpFunction %void None %3
  746. %5 = OpLabel
  747. %p = OpVariable %_ptr_Function_v4float Function
  748. %22 = OpAccessChain %_ptr_Input_v4float %gl_in %int_1 %int_0
  749. %23 = OpLoad %v4float %22
  750. OpStore %p %23
  751. %27 = OpLoad %v4float %p
  752. %33 = OpAccessChain %_ptr_Input_float %patchIn1 %int_1
  753. %34 = OpLoad %float %33
  754. %35 = OpVectorTimesScalar %v4float %27 %34
  755. %37 = OpAccessChain %_ptr_Output_v4float %_ %int_0
  756. OpStore %37 %35
  757. OpReturn
  758. OpFunctionEnd
  759. )";
  760. SetTargetEnv(SPV_ENV_VULKAN_1_3);
  761. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  762. std::unordered_set<uint32_t> live_inputs;
  763. std::unordered_set<uint32_t> live_builtins;
  764. auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
  765. text, true, &live_inputs, &live_builtins);
  766. auto itr0 = live_inputs.find(0);
  767. auto itr1 = live_inputs.find(1);
  768. auto itr2 = live_inputs.find(2);
  769. auto itr3 = live_inputs.find(3);
  770. // Expect live_inputs == {3}
  771. EXPECT_TRUE(itr0 == live_inputs.end());
  772. EXPECT_TRUE(itr1 == live_inputs.end());
  773. EXPECT_TRUE(itr2 == live_inputs.end());
  774. EXPECT_TRUE(itr3 != live_inputs.end());
  775. }
  776. TEST_F(AnalyzeLiveInputTest, FragMultipleLocationsF16) {
  777. // Should report locations {2, 5}
  778. //
  779. // #version 450
  780. //
  781. // layout(location = 2) in Vertex
  782. // {
  783. // f16vec4 color0;
  784. // f16vec4 color1;
  785. // f16vec4 color2[3];
  786. // } iVert;
  787. //
  788. // layout(location = 0) out f16vec4 oFragColor;
  789. //
  790. // void main()
  791. // {
  792. // oFragColor = iVert.color0 + iVert.color2[1];
  793. // }
  794. const std::string text = R"(
  795. OpCapability Shader
  796. OpCapability Float16
  797. OpCapability StorageInputOutput16
  798. %1 = OpExtInstImport "GLSL.std.450"
  799. OpMemoryModel Logical GLSL450
  800. OpEntryPoint Fragment %main "main" %oFragColor %iVert
  801. OpExecutionMode %main OriginUpperLeft
  802. OpSource GLSL 450
  803. OpName %main "main"
  804. OpName %oFragColor "oFragColor"
  805. OpName %Vertex "Vertex"
  806. OpMemberName %Vertex 0 "color0"
  807. OpMemberName %Vertex 1 "color1"
  808. OpMemberName %Vertex 2 "color2"
  809. OpName %iVert "iVert"
  810. OpDecorate %oFragColor Location 0
  811. OpDecorate %Vertex Block
  812. OpDecorate %iVert Location 2
  813. %void = OpTypeVoid
  814. %3 = OpTypeFunction %void
  815. %half = OpTypeFloat 16
  816. %v4half = OpTypeVector %half 4
  817. %_ptr_Output_v4half = OpTypePointer Output %v4half
  818. %oFragColor = OpVariable %_ptr_Output_v4half Output
  819. %uint = OpTypeInt 32 0
  820. %uint_3 = OpConstant %uint 3
  821. %_arr_v4half_uint_3 = OpTypeArray %v4half %uint_3
  822. %Vertex = OpTypeStruct %v4half %v4half %_arr_v4half_uint_3
  823. %_ptr_Input_Vertex = OpTypePointer Input %Vertex
  824. %iVert = OpVariable %_ptr_Input_Vertex Input
  825. %int = OpTypeInt 32 1
  826. %int_0 = OpConstant %int 0
  827. %_ptr_Input_v4half = OpTypePointer Input %v4half
  828. %int_2 = OpConstant %int 2
  829. %int_1 = OpConstant %int 1
  830. %main = OpFunction %void None %3
  831. %5 = OpLabel
  832. %19 = OpAccessChain %_ptr_Input_v4half %iVert %int_0
  833. %20 = OpLoad %v4half %19
  834. %23 = OpAccessChain %_ptr_Input_v4half %iVert %int_2 %int_1
  835. %24 = OpLoad %v4half %23
  836. %25 = OpFAdd %v4half %20 %24
  837. OpStore %oFragColor %25
  838. OpReturn
  839. OpFunctionEnd
  840. )";
  841. SetTargetEnv(SPV_ENV_VULKAN_1_3);
  842. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  843. std::unordered_set<uint32_t> live_inputs;
  844. std::unordered_set<uint32_t> live_builtins;
  845. auto result = SinglePassRunToBinary<AnalyzeLiveInputPass>(
  846. text, true, &live_inputs, &live_builtins);
  847. auto itr0 = live_inputs.find(0);
  848. auto itr1 = live_inputs.find(1);
  849. auto itr2 = live_inputs.find(2);
  850. auto itr3 = live_inputs.find(3);
  851. auto itr4 = live_inputs.find(4);
  852. auto itr5 = live_inputs.find(5);
  853. auto itr6 = live_inputs.find(6);
  854. // Expect live_inputs == {2, 5}
  855. EXPECT_TRUE(itr0 == live_inputs.end());
  856. EXPECT_TRUE(itr1 == live_inputs.end());
  857. EXPECT_TRUE(itr2 != live_inputs.end());
  858. EXPECT_TRUE(itr3 == live_inputs.end());
  859. EXPECT_TRUE(itr4 == live_inputs.end());
  860. EXPECT_TRUE(itr5 != live_inputs.end());
  861. EXPECT_TRUE(itr6 == live_inputs.end());
  862. }
  863. } // namespace
  864. } // namespace opt
  865. } // namespace spvtools