val_ray_tracing_reorder_test.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. // Copyright (c) 2022 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Tests instructions from SPV_NV_shader_invocation_reorder.
  15. #include <sstream>
  16. #include <string>
  17. #include "gmock/gmock.h"
  18. #include "test/val/val_fixtures.h"
  19. namespace spvtools {
  20. namespace val {
  21. namespace {
  22. using ::testing::HasSubstr;
  23. using ::testing::Values;
  24. using ValidateRayTracingReorderNV = spvtest::ValidateBase<bool>;
  25. std::string GenerateReorderThreadCode(const std::string& body = "",
  26. const std::string& declarations = "",
  27. const std::string& extensions = "",
  28. const std::string& capabilities = "") {
  29. std::ostringstream ss;
  30. ss << R"(
  31. OpCapability RayTracingKHR
  32. OpCapability ShaderInvocationReorderNV
  33. )";
  34. ss << capabilities;
  35. ss << R"(
  36. OpExtension "SPV_KHR_ray_tracing"
  37. OpExtension "SPV_NV_shader_invocation_reorder"
  38. )";
  39. ss << extensions;
  40. ss << R"(
  41. %1 = OpExtInstImport "GLSL.std.450"
  42. OpMemoryModel Logical GLSL450
  43. OpEntryPoint RayGenerationNV %main "main" %hObj
  44. OpSourceExtension "GL_EXT_ray_tracing"
  45. OpSourceExtension "GL_NV_shader_invocation_reorder"
  46. OpName %main "main"
  47. %void = OpTypeVoid
  48. %3 = OpTypeFunction %void
  49. %6 = OpTypeHitObjectNV
  50. %_ptr_Private_6 = OpTypePointer Private %6
  51. %hObj = OpVariable %_ptr_Private_6 Private
  52. )";
  53. ss << declarations;
  54. ss << R"(
  55. %main = OpFunction %void None %3
  56. %5 = OpLabel
  57. )";
  58. ss << body;
  59. ss << R"(
  60. OpReturn
  61. OpFunctionEnd
  62. )";
  63. return ss.str();
  64. }
  65. TEST_F(ValidateRayTracingReorderNV, ReorderThreadWithHintNV) {
  66. const std::string declarations = R"(
  67. %uint = OpTypeInt 32 0
  68. %uint_4 = OpConstant %uint 4
  69. )";
  70. const std::string body = R"(
  71. OpReorderThreadWithHintNV %uint_4 %uint_4
  72. )";
  73. CompileSuccessfully(GenerateReorderThreadCode(body, declarations).c_str(),
  74. SPV_ENV_VULKAN_1_2);
  75. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  76. }
  77. TEST_F(ValidateRayTracingReorderNV, ReorderThreadWithHitObjectNV) {
  78. const std::string declarations = R"(
  79. %uint = OpTypeInt 32 0
  80. %uint_4 = OpConstant %uint 4
  81. %uint_2 = OpConstant %uint 2
  82. )";
  83. const std::string body = R"(
  84. OpReorderThreadWithHitObjectNV %hObj
  85. OpReorderThreadWithHitObjectNV %hObj %uint_4 %uint_2
  86. )";
  87. CompileSuccessfully(GenerateReorderThreadCode(body, declarations).c_str(),
  88. SPV_ENV_VULKAN_1_2);
  89. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  90. }
  91. std::string GenerateReorderShaderCode(
  92. const std::string& body = "", const std::string& declarations = "",
  93. const std::string execution_model = "RayGenerationKHR") {
  94. std::ostringstream ss;
  95. ss << R"(
  96. OpCapability RayTracingKHR
  97. OpCapability ShaderInvocationReorderNV
  98. OpExtension "SPV_KHR_ray_tracing"
  99. OpExtension "SPV_NV_shader_invocation_reorder"
  100. %1 = OpExtInstImport "GLSL.std.450"
  101. OpMemoryModel Logical GLSL450
  102. OpEntryPoint )"
  103. << execution_model
  104. << R"( %main "main" %attr %_ %hObj %payload %__0 %as %__1
  105. OpSource GLSL 460
  106. OpSourceExtension "GL_EXT_ray_tracing"
  107. OpSourceExtension "GL_NV_shader_invocation_reorder"
  108. OpName %main "main"
  109. OpName %attr "attr"
  110. OpName %hBlock "hBlock"
  111. OpMemberName %hBlock 0 "attrval"
  112. OpName %_ ""
  113. OpName %hObj "hObj"
  114. OpName %payload "payload"
  115. OpName %pBlock "pBlock"
  116. OpMemberName %pBlock 0 "val1"
  117. OpMemberName %pBlock 1 "val2"
  118. OpName %__0 ""
  119. OpName %as "as"
  120. OpName %block "block"
  121. OpMemberName %block 0 "op"
  122. OpName %__1 ""
  123. OpDecorate %hBlock Block
  124. OpDecorate %pBlock Block
  125. OpDecorate %as DescriptorSet 0
  126. OpDecorate %as Binding 0
  127. OpMemberDecorate %block 0 Offset 0
  128. OpDecorate %block Block
  129. OpDecorate %__1 DescriptorSet 0
  130. OpDecorate %__1 Binding 1
  131. %void = OpTypeVoid
  132. %3 = OpTypeFunction %void
  133. %float = OpTypeFloat 32
  134. %v2float = OpTypeVector %float 2
  135. %_ptr_HitObjectAttributeNV_v2float = OpTypePointer HitObjectAttributeNV %v2float
  136. %attr = OpVariable %_ptr_HitObjectAttributeNV_v2float HitObjectAttributeNV
  137. %float_1 = OpConstant %float 1
  138. %11 = OpConstantComposite %v2float %float_1 %float_1
  139. %hBlock = OpTypeStruct %float
  140. %_ptr_HitObjectAttributeNV_hBlock = OpTypePointer HitObjectAttributeNV %hBlock
  141. %_ = OpVariable %_ptr_HitObjectAttributeNV_hBlock HitObjectAttributeNV
  142. %int = OpTypeInt 32 1
  143. %int_0 = OpConstant %int 0
  144. %float_2 = OpConstant %float 2
  145. %_ptr_HitObjectAttributeNV_float = OpTypePointer HitObjectAttributeNV %float
  146. %20 = OpTypeHitObjectNV
  147. %_ptr_Private_20 = OpTypePointer Private %20
  148. %hObj = OpVariable %_ptr_Private_20 Private
  149. %23 = OpTypeAccelerationStructureKHR
  150. %_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
  151. %as = OpVariable %_ptr_UniformConstant_23 UniformConstant
  152. %v4float = OpTypeVector %float 4
  153. %_ptr_RayPayloadNV_v4float = OpTypePointer RayPayloadNV %v4float
  154. %payload = OpVariable %_ptr_RayPayloadNV_v4float RayPayloadNV
  155. %pBlock = OpTypeStruct %v2float %v2float
  156. %_ptr_RayPayloadNV_pBlock = OpTypePointer RayPayloadNV %pBlock
  157. %__0 = OpVariable %_ptr_RayPayloadNV_pBlock RayPayloadNV
  158. %block = OpTypeStruct %float
  159. %_ptr_StorageBuffer_block = OpTypePointer StorageBuffer %block
  160. %__1 = OpVariable %_ptr_StorageBuffer_block StorageBuffer
  161. )";
  162. ss << declarations;
  163. ss << R"(
  164. %main = OpFunction %void None %3
  165. %5 = OpLabel
  166. )";
  167. ss << body;
  168. ss << R"(
  169. OpReturn
  170. OpFunctionEnd)";
  171. return ss.str();
  172. }
  173. TEST_F(ValidateRayTracingReorderNV, HitObjectTraceRayNV) {
  174. const std::string declarations = R"(
  175. %uint = OpTypeInt 32 0
  176. %uint_1 = OpConstant %uint 1
  177. %v3float = OpTypeVector %float 3
  178. %float_0_5 = OpConstant %float 0.5
  179. %31 = OpConstantComposite %v3float %float_0_5 %float_0_5 %float_0_5
  180. %32 = OpConstantComposite %v3float %float_1 %float_1 %float_1
  181. %int_1 = OpConstant %int 1
  182. )";
  183. const std::string body = R"(
  184. OpStore %attr %11
  185. %26 = OpLoad %23 %as
  186. OpHitObjectTraceRayNV %hObj %26 %uint_1 %uint_1 %uint_1 %uint_1 %uint_1 %31 %float_0_5 %32 %float_1 %payload
  187. )";
  188. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  189. SPV_ENV_VULKAN_1_2);
  190. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  191. }
  192. TEST_F(ValidateRayTracingReorderNV, HitObjectTraceRayMotionNV) {
  193. const std::string declarations = R"(
  194. %uint = OpTypeInt 32 0
  195. %uint_1 = OpConstant %uint 1
  196. %v3float = OpTypeVector %float 3
  197. %float_0_5 = OpConstant %float 0.5
  198. %31 = OpConstantComposite %v3float %float_0_5 %float_0_5 %float_0_5
  199. %32 = OpConstantComposite %v3float %float_1 %float_1 %float_1
  200. %float_10 = OpConstant %float 10
  201. %int_2 = OpConstant %int 2
  202. )";
  203. const std::string body = R"(
  204. OpStore %attr %11
  205. %26 = OpLoad %23 %as
  206. OpHitObjectTraceRayMotionNV %hObj %26 %uint_1 %uint_1 %uint_1 %uint_1 %uint_1 %31 %float_0_5 %32 %float_1 %float_10 %__0
  207. )";
  208. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  209. SPV_ENV_VULKAN_1_2);
  210. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  211. }
  212. TEST_F(ValidateRayTracingReorderNV, HitObjectRecordHitNV) {
  213. const std::string declarations = R"(
  214. %int_1 = OpConstant %int 1
  215. %uint = OpTypeInt 32 0
  216. %uint_2 = OpConstant %uint 2
  217. %v3float = OpTypeVector %float 3
  218. %float_0_5 = OpConstant %float 0.5
  219. %31 = OpConstantComposite %v3float %float_0_5 %float_0_5 %float_0_5
  220. %32 = OpConstantComposite %v3float %float_1 %float_1 %float_1
  221. %float_10 = OpConstant %float 10
  222. %int_2 = OpConstant %int 2
  223. )";
  224. const std::string body = R"(
  225. OpStore %attr %11
  226. %26 = OpLoad %23 %as
  227. OpHitObjectRecordHitNV %hObj %26 %int_1 %int_1 %int_1 %uint_2 %uint_2 %uint_2 %31 %float_1 %32 %float_2 %attr
  228. )";
  229. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  230. SPV_ENV_VULKAN_1_2);
  231. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  232. }
  233. TEST_F(ValidateRayTracingReorderNV, HitObjectRecordHitWithIndexNV) {
  234. const std::string declarations = R"(
  235. %int_1 = OpConstant %int 1
  236. %uint = OpTypeInt 32 0
  237. %uint_2 = OpConstant %uint 2
  238. %v3float = OpTypeVector %float 3
  239. %float_0_5 = OpConstant %float 0.5
  240. %31 = OpConstantComposite %v3float %float_0_5 %float_0_5 %float_0_5
  241. %32 = OpConstantComposite %v3float %float_1 %float_1 %float_1
  242. %float_10 = OpConstant %float 10
  243. %int_2 = OpConstant %int 2
  244. )";
  245. const std::string body = R"(
  246. OpStore %attr %11
  247. %26 = OpLoad %23 %as
  248. OpHitObjectRecordHitWithIndexNV %hObj %26 %int_1 %int_1 %int_1 %uint_2 %uint_2 %31 %float_1 %32 %float_2 %_
  249. )";
  250. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  251. SPV_ENV_VULKAN_1_2);
  252. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  253. }
  254. TEST_F(ValidateRayTracingReorderNV, HitObjectRecordEmptyNV) {
  255. const std::string body = R"(
  256. OpHitObjectRecordEmptyNV %hObj
  257. )";
  258. CompileSuccessfully(GenerateReorderShaderCode(body).c_str(),
  259. SPV_ENV_VULKAN_1_2);
  260. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  261. }
  262. TEST_F(ValidateRayTracingReorderNV, HitObjectRecordMissNV) {
  263. const std::string declarations = R"(
  264. %uint = OpTypeInt 32 0
  265. %uint_1 = OpConstant %uint 1
  266. %v3float = OpTypeVector %float 3
  267. %float_0_5 = OpConstant %float 0.5
  268. %29 = OpConstantComposite %v3float %float_0_5 %float_0_5 %float_0_5
  269. %float_1_5 = OpConstant %float 1.5
  270. %31 = OpConstantComposite %v3float %float_1_5 %float_1_5 %float_1_5
  271. %float_5 = OpConstant %float 5
  272. )";
  273. const std::string body = R"(
  274. OpHitObjectRecordMissNV %hObj %uint_1 %29 %float_2 %31 %float_5
  275. )";
  276. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  277. SPV_ENV_VULKAN_1_2);
  278. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  279. }
  280. TEST_F(ValidateRayTracingReorderNV, HitObjectIsHitNV) {
  281. const std::string declarations = R"(
  282. %bool = OpTypeBool
  283. %_ptr_StorageBuffer_float = OpTypePointer StorageBuffer %float
  284. )";
  285. const std::string body = R"(
  286. %26 = OpHitObjectIsHitNV %bool %hObj
  287. OpSelectionMerge %28 None
  288. OpBranchConditional %26 %27 %28
  289. %27 = OpLabel
  290. %33 = OpAccessChain %_ptr_StorageBuffer_float %__1 %int_0
  291. OpStore %33 %float_1
  292. OpBranch %28
  293. %28 = OpLabel
  294. )";
  295. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  296. SPV_ENV_VULKAN_1_2);
  297. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  298. }
  299. TEST_F(ValidateRayTracingReorderNV, HitObjectGetRayTMaxNV) {
  300. const std::string declarations = R"(
  301. %_ptr_Function_float = OpTypePointer Function %float
  302. )";
  303. const std::string body = R"(
  304. %tmin = OpVariable %_ptr_Function_float Function
  305. %12 = OpHitObjectGetRayTMaxNV %float %hObj
  306. OpStore %tmin %12
  307. )";
  308. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  309. SPV_ENV_VULKAN_1_2);
  310. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  311. }
  312. TEST_F(ValidateRayTracingReorderNV, HitObjectGetRayTMinNV) {
  313. const std::string declarations = R"(
  314. %_ptr_Function_float = OpTypePointer Function %float
  315. )";
  316. const std::string body = R"(
  317. %tmin = OpVariable %_ptr_Function_float Function
  318. %12 = OpHitObjectGetRayTMinNV %float %hObj
  319. OpStore %tmin %12
  320. )";
  321. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  322. SPV_ENV_VULKAN_1_2);
  323. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  324. }
  325. TEST_F(ValidateRayTracingReorderNV, HitObjectGetWorldRayOriginNV) {
  326. const std::string declarations = R"(
  327. %v3float = OpTypeVector %float 3
  328. %_ptr_Function_v3float = OpTypePointer Function %v3float
  329. )";
  330. const std::string body = R"(
  331. %orig = OpVariable %_ptr_Function_v3float Function
  332. %13 = OpHitObjectGetWorldRayOriginNV %v3float %hObj
  333. OpStore %orig %13
  334. )";
  335. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  336. SPV_ENV_VULKAN_1_2);
  337. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  338. }
  339. TEST_F(ValidateRayTracingReorderNV, HitObjectGetObjectRayOriginNV) {
  340. const std::string declarations = R"(
  341. %v3float = OpTypeVector %float 3
  342. %_ptr_Function_v3float = OpTypePointer Function %v3float
  343. )";
  344. const std::string body = R"(
  345. %oorig = OpVariable %_ptr_Function_v3float Function
  346. %13 = OpHitObjectGetObjectRayOriginNV %v3float %hObj
  347. OpStore %oorig %13
  348. )";
  349. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  350. SPV_ENV_VULKAN_1_2);
  351. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  352. }
  353. TEST_F(ValidateRayTracingReorderNV, HitObjectGetWorldRayDirectionNV) {
  354. const std::string declarations = R"(
  355. %v3float = OpTypeVector %float 3
  356. %_ptr_Function_v3float = OpTypePointer Function %v3float
  357. )";
  358. const std::string body = R"(
  359. %dir = OpVariable %_ptr_Function_v3float Function
  360. %13 = OpHitObjectGetWorldRayDirectionNV %v3float %hObj
  361. OpStore %dir %13
  362. )";
  363. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  364. SPV_ENV_VULKAN_1_2);
  365. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  366. }
  367. TEST_F(ValidateRayTracingReorderNV, HitObjectGetObjectRayDirectionNV) {
  368. const std::string declarations = R"(
  369. %v3float = OpTypeVector %float 3
  370. %_ptr_Function_v3float = OpTypePointer Function %v3float
  371. )";
  372. const std::string body = R"(
  373. %odir = OpVariable %_ptr_Function_v3float Function
  374. %13 = OpHitObjectGetObjectRayDirectionNV %v3float %hObj
  375. OpStore %odir %13
  376. )";
  377. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  378. SPV_ENV_VULKAN_1_2);
  379. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  380. }
  381. TEST_F(ValidateRayTracingReorderNV, HitObjectGetObjectToWorldNV) {
  382. const std::string declarations = R"(
  383. %v3float = OpTypeVector %float 3
  384. %mat4v3float = OpTypeMatrix %v3float 4
  385. %_ptr_Function_mat4v3float = OpTypePointer Function %mat4v3float
  386. )";
  387. const std::string body = R"(
  388. %otw = OpVariable %_ptr_Function_mat4v3float Function
  389. %14 = OpHitObjectGetObjectToWorldNV %mat4v3float %hObj
  390. OpStore %otw %14
  391. )";
  392. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  393. SPV_ENV_VULKAN_1_2);
  394. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  395. }
  396. TEST_F(ValidateRayTracingReorderNV, HitObjectGetWorldToObjectNV) {
  397. const std::string declarations = R"(
  398. %v3float = OpTypeVector %float 3
  399. %mat4v3float = OpTypeMatrix %v3float 4
  400. %_ptr_Function_mat4v3float = OpTypePointer Function %mat4v3float
  401. )";
  402. const std::string body = R"(
  403. %wto = OpVariable %_ptr_Function_mat4v3float Function
  404. %14 = OpHitObjectGetWorldToObjectNV %mat4v3float %hObj
  405. OpStore %wto %14
  406. )";
  407. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  408. SPV_ENV_VULKAN_1_2);
  409. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  410. }
  411. TEST_F(ValidateRayTracingReorderNV, HitObjectGetInstanceCustomIndexNV) {
  412. const std::string declarations = R"(
  413. %_ptr_Function_int = OpTypePointer Function %int
  414. )";
  415. const std::string body = R"(
  416. %id = OpVariable %_ptr_Function_int Function
  417. %12 = OpHitObjectGetInstanceCustomIndexNV %int %hObj
  418. OpStore %id %12
  419. )";
  420. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  421. SPV_ENV_VULKAN_1_2);
  422. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  423. }
  424. TEST_F(ValidateRayTracingReorderNV, HitObjectGetInstanceIdNV) {
  425. const std::string declarations = R"(
  426. %_ptr_Function_int = OpTypePointer Function %int
  427. )";
  428. const std::string body = R"(
  429. %id = OpVariable %_ptr_Function_int Function
  430. %12 = OpHitObjectGetInstanceIdNV %int %hObj
  431. OpStore %id %12
  432. )";
  433. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  434. SPV_ENV_VULKAN_1_2);
  435. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  436. }
  437. TEST_F(ValidateRayTracingReorderNV, HitObjectGetPrimitiveIndexNV) {
  438. const std::string declarations = R"(
  439. %_ptr_Function_int = OpTypePointer Function %int
  440. )";
  441. const std::string body = R"(
  442. %id = OpVariable %_ptr_Function_int Function
  443. %12 = OpHitObjectGetPrimitiveIndexNV %int %hObj
  444. OpStore %id %12
  445. )";
  446. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  447. SPV_ENV_VULKAN_1_2);
  448. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  449. }
  450. TEST_F(ValidateRayTracingReorderNV, HitObjectGetGeometryIndexNV) {
  451. const std::string declarations = R"(
  452. %_ptr_Function_int = OpTypePointer Function %int
  453. )";
  454. const std::string body = R"(
  455. %id = OpVariable %_ptr_Function_int Function
  456. %12 = OpHitObjectGetGeometryIndexNV %int %hObj
  457. OpStore %id %12
  458. )";
  459. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  460. SPV_ENV_VULKAN_1_2);
  461. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  462. }
  463. TEST_F(ValidateRayTracingReorderNV, HitObjectGetHitKindNV) {
  464. const std::string declarations = R"(
  465. %uint = OpTypeInt 32 0
  466. %_ptr_Function_uint = OpTypePointer Function %uint
  467. )";
  468. const std::string body = R"(
  469. %uid = OpVariable %_ptr_Function_uint Function
  470. %12 = OpHitObjectGetHitKindNV %uint %hObj
  471. OpStore %uid %12
  472. )";
  473. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  474. SPV_ENV_VULKAN_1_2);
  475. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  476. }
  477. TEST_F(ValidateRayTracingReorderNV, HitObjectGetAttributesNV) {
  478. const std::string body = R"(
  479. OpHitObjectGetAttributesNV %hObj %attr
  480. )";
  481. CompileSuccessfully(GenerateReorderShaderCode(body).c_str(),
  482. SPV_ENV_VULKAN_1_2);
  483. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  484. }
  485. TEST_F(ValidateRayTracingReorderNV, HitObjectGetShaderRecordBufferHandleNV) {
  486. const std::string declarations = R"(
  487. %uint = OpTypeInt 32 0
  488. %v2uint = OpTypeVector %uint 2
  489. %_ptr_Function_v2uint = OpTypePointer Function %v2uint
  490. )";
  491. const std::string body = R"(
  492. %handle = OpVariable %_ptr_Function_v2uint Function
  493. %13 = OpHitObjectGetShaderRecordBufferHandleNV %v2uint %hObj
  494. OpStore %handle %13
  495. )";
  496. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  497. SPV_ENV_VULKAN_1_2);
  498. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  499. }
  500. TEST_F(ValidateRayTracingReorderNV,
  501. HitObjectGetShaderBindingTableRecordIndexNV) {
  502. const std::string declarations = R"(
  503. %uint = OpTypeInt 32 0
  504. %_ptr_Function_uint = OpTypePointer Function %uint
  505. )";
  506. const std::string body = R"(
  507. %rid = OpVariable %_ptr_Function_uint Function
  508. %12 = OpHitObjectGetShaderBindingTableRecordIndexNV %uint %hObj
  509. OpStore %rid %12
  510. )";
  511. CompileSuccessfully(GenerateReorderShaderCode(body, declarations).c_str(),
  512. SPV_ENV_VULKAN_1_2);
  513. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  514. }
  515. TEST_F(ValidateRayTracingReorderNV, ClusterASNV) {
  516. const std::string cap = R"(
  517. OpCapability RayTracingClusterAccelerationStructureNV
  518. )";
  519. const std::string ext = R"(
  520. OpExtension "SPV_NV_cluster_acceleration_structure"
  521. )";
  522. const std::string declarations = R"(
  523. %int = OpTypeInt 32 1
  524. %_ptr_Function_int = OpTypePointer Function %int
  525. )";
  526. const std::string body = R"(
  527. %id = OpVariable %_ptr_Function_int Function
  528. %12 = OpHitObjectGetClusterIdNV %int %hObj
  529. OpStore %id %12
  530. )";
  531. CompileSuccessfully(
  532. GenerateReorderThreadCode(body, declarations, ext, cap).c_str(),
  533. SPV_ENV_VULKAN_1_2);
  534. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  535. }
  536. TEST_F(ValidateRayTracingReorderNV, LSSGetSpherePositionNV) {
  537. const std::string cap = R"(
  538. OpCapability RayTracingSpheresGeometryNV
  539. )";
  540. const std::string ext = R"(
  541. OpExtension "SPV_NV_linear_swept_spheres"
  542. )";
  543. const std::string declarations = R"(
  544. %float = OpTypeFloat 32
  545. %v3float = OpTypeVector %float 3
  546. %_ptr_Function_v3float = OpTypePointer Function %v3float
  547. )";
  548. const std::string body = R"(
  549. %pos = OpVariable %_ptr_Function_v3float Function
  550. %result = OpHitObjectGetSpherePositionNV %v3float %hObj
  551. OpStore %pos %result
  552. )";
  553. CompileSuccessfully(
  554. GenerateReorderThreadCode(body, declarations, ext, cap).c_str(),
  555. SPV_ENV_VULKAN_1_2);
  556. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  557. }
  558. TEST_F(ValidateRayTracingReorderNV, LSSGetLSSPositionsNV) {
  559. const std::string cap = R"(
  560. OpCapability RayTracingSpheresGeometryNV
  561. OpCapability RayTracingLinearSweptSpheresGeometryNV
  562. )";
  563. const std::string ext = R"(
  564. OpExtension "SPV_NV_linear_swept_spheres"
  565. )";
  566. const std::string declarations = R"(
  567. %float = OpTypeFloat 32
  568. %uint = OpTypeInt 32 0
  569. %v3float = OpTypeVector %float 3
  570. %uint_2 = OpConstant %uint 2
  571. %arr = OpTypeArray %v3float %uint_2
  572. %_ptr_Function_v3float = OpTypePointer Function %arr
  573. )";
  574. const std::string body = R"(
  575. %lsspos = OpVariable %_ptr_Function_v3float Function
  576. %result = OpHitObjectGetLSSPositionsNV %arr %hObj
  577. OpStore %lsspos %result
  578. )";
  579. CompileSuccessfully(
  580. GenerateReorderThreadCode(body, declarations, ext, cap).c_str(),
  581. SPV_ENV_VULKAN_1_2);
  582. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  583. }
  584. TEST_F(ValidateRayTracingReorderNV, LSSGetSphereRadiusNV) {
  585. const std::string cap = R"(
  586. OpCapability RayTracingSpheresGeometryNV
  587. )";
  588. const std::string ext = R"(
  589. OpExtension "SPV_NV_linear_swept_spheres"
  590. )";
  591. const std::string declarations = R"(
  592. %float = OpTypeFloat 32
  593. %_ptr_Function_float = OpTypePointer Function %float
  594. )";
  595. const std::string body = R"(
  596. %rad = OpVariable %_ptr_Function_float Function
  597. %result = OpHitObjectGetSphereRadiusNV %float %hObj
  598. OpStore %rad %result
  599. )";
  600. CompileSuccessfully(
  601. GenerateReorderThreadCode(body, declarations, ext, cap).c_str(),
  602. SPV_ENV_VULKAN_1_2);
  603. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  604. }
  605. TEST_F(ValidateRayTracingReorderNV, LSSGetLSSRadiiNV) {
  606. const std::string cap = R"(
  607. OpCapability RayTracingLinearSweptSpheresGeometryNV
  608. )";
  609. const std::string ext = R"(
  610. OpExtension "SPV_NV_linear_swept_spheres"
  611. )";
  612. const std::string declarations = R"(
  613. %float = OpTypeFloat 32
  614. %uint = OpTypeInt 32 0
  615. %uint_2 = OpConstant %uint 2
  616. %arr = OpTypeArray %float %uint_2
  617. %_ptr_Function_float = OpTypePointer Function %arr
  618. )";
  619. const std::string body = R"(
  620. %rad = OpVariable %_ptr_Function_float Function
  621. %result = OpHitObjectGetLSSRadiiNV %arr %hObj
  622. OpStore %rad %result
  623. )";
  624. CompileSuccessfully(
  625. GenerateReorderThreadCode(body, declarations, ext, cap).c_str(),
  626. SPV_ENV_VULKAN_1_2);
  627. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  628. }
  629. TEST_F(ValidateRayTracingReorderNV, LSSIsSphereHitNV) {
  630. const std::string cap = R"(
  631. OpCapability RayTracingSpheresGeometryNV
  632. )";
  633. const std::string ext = R"(
  634. OpExtension "SPV_NV_linear_swept_spheres"
  635. )";
  636. const std::string declarations = R"(
  637. %bool = OpTypeBool
  638. %_ptr_Function_bool = OpTypePointer Function %bool
  639. )";
  640. const std::string body = R"(
  641. %ishit = OpVariable %_ptr_Function_bool Function
  642. %result = OpHitObjectIsSphereHitNV %bool %hObj
  643. OpStore %ishit %result
  644. )";
  645. CompileSuccessfully(
  646. GenerateReorderThreadCode(body, declarations, ext, cap).c_str(),
  647. SPV_ENV_VULKAN_1_2);
  648. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  649. }
  650. TEST_F(ValidateRayTracingReorderNV, LSSIsLSSHitNV) {
  651. const std::string cap = R"(
  652. OpCapability RayTracingLinearSweptSpheresGeometryNV
  653. )";
  654. const std::string ext = R"(
  655. OpExtension "SPV_NV_linear_swept_spheres"
  656. )";
  657. const std::string declarations = R"(
  658. %bool = OpTypeBool
  659. %_ptr_Function_bool = OpTypePointer Function %bool
  660. )";
  661. const std::string body = R"(
  662. %ishit = OpVariable %_ptr_Function_bool Function
  663. %result = OpHitObjectIsLSSHitNV %bool %hObj
  664. OpStore %ishit %result
  665. )";
  666. CompileSuccessfully(
  667. GenerateReorderThreadCode(body, declarations, ext, cap).c_str(),
  668. SPV_ENV_VULKAN_1_2);
  669. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
  670. }
  671. } // namespace
  672. } // namespace val
  673. } // namespace spvtools