validate_ray_tracing.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // Validates ray tracing instructions from SPV_KHR_ray_tracing
  15. #include "source/opcode.h"
  16. #include "source/val/instruction.h"
  17. #include "source/val/validate.h"
  18. #include "source/val/validation_state.h"
  19. namespace spvtools {
  20. namespace val {
  21. spv_result_t RayTracingPass(ValidationState_t& _, const Instruction* inst) {
  22. const spv::Op opcode = inst->opcode();
  23. const uint32_t result_type = inst->type_id();
  24. switch (opcode) {
  25. case spv::Op::OpTraceRayKHR: {
  26. _.function(inst->function()->id())
  27. ->RegisterExecutionModelLimitation(
  28. [](spv::ExecutionModel model, std::string* message) {
  29. if (model != spv::ExecutionModel::RayGenerationKHR &&
  30. model != spv::ExecutionModel::ClosestHitKHR &&
  31. model != spv::ExecutionModel::MissKHR) {
  32. if (message) {
  33. *message =
  34. "OpTraceRayKHR requires RayGenerationKHR, "
  35. "ClosestHitKHR and MissKHR execution models";
  36. }
  37. return false;
  38. }
  39. return true;
  40. });
  41. if (_.GetIdOpcode(_.GetOperandTypeId(inst, 0)) !=
  42. spv::Op::OpTypeAccelerationStructureKHR) {
  43. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  44. << "Expected Acceleration Structure to be of type "
  45. "OpTypeAccelerationStructureKHR";
  46. }
  47. const uint32_t ray_flags = _.GetOperandTypeId(inst, 1);
  48. if (!_.IsIntScalarType(ray_flags) || _.GetBitWidth(ray_flags) != 32) {
  49. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  50. << "Ray Flags must be a 32-bit int scalar";
  51. }
  52. const uint32_t cull_mask = _.GetOperandTypeId(inst, 2);
  53. if (!_.IsIntScalarType(cull_mask) || _.GetBitWidth(cull_mask) != 32) {
  54. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  55. << "Cull Mask must be a 32-bit int scalar";
  56. }
  57. const uint32_t sbt_offset = _.GetOperandTypeId(inst, 3);
  58. if (!_.IsIntScalarType(sbt_offset) || _.GetBitWidth(sbt_offset) != 32) {
  59. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  60. << "SBT Offset must be a 32-bit int scalar";
  61. }
  62. const uint32_t sbt_stride = _.GetOperandTypeId(inst, 4);
  63. if (!_.IsIntScalarType(sbt_stride) || _.GetBitWidth(sbt_stride) != 32) {
  64. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  65. << "SBT Stride must be a 32-bit int scalar";
  66. }
  67. const uint32_t miss_index = _.GetOperandTypeId(inst, 5);
  68. if (!_.IsIntScalarType(miss_index) || _.GetBitWidth(miss_index) != 32) {
  69. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  70. << "Miss Index must be a 32-bit int scalar";
  71. }
  72. const uint32_t ray_origin = _.GetOperandTypeId(inst, 6);
  73. if (!_.IsFloatVectorType(ray_origin) || _.GetDimension(ray_origin) != 3 ||
  74. _.GetBitWidth(ray_origin) != 32) {
  75. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  76. << "Ray Origin must be a 32-bit float 3-component vector";
  77. }
  78. const uint32_t ray_tmin = _.GetOperandTypeId(inst, 7);
  79. if (!_.IsFloatScalarType(ray_tmin) || _.GetBitWidth(ray_tmin) != 32) {
  80. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  81. << "Ray TMin must be a 32-bit float scalar";
  82. }
  83. const uint32_t ray_direction = _.GetOperandTypeId(inst, 8);
  84. if (!_.IsFloatVectorType(ray_direction) ||
  85. _.GetDimension(ray_direction) != 3 ||
  86. _.GetBitWidth(ray_direction) != 32) {
  87. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  88. << "Ray Direction must be a 32-bit float 3-component vector";
  89. }
  90. const uint32_t ray_tmax = _.GetOperandTypeId(inst, 9);
  91. if (!_.IsFloatScalarType(ray_tmax) || _.GetBitWidth(ray_tmax) != 32) {
  92. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  93. << "Ray TMax must be a 32-bit float scalar";
  94. }
  95. const Instruction* payload = _.FindDef(inst->GetOperandAs<uint32_t>(10));
  96. if (payload->opcode() != spv::Op::OpVariable) {
  97. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  98. << "Payload must be the result of a OpVariable";
  99. } else if (payload->GetOperandAs<spv::StorageClass>(2) !=
  100. spv::StorageClass::RayPayloadKHR &&
  101. payload->GetOperandAs<spv::StorageClass>(2) !=
  102. spv::StorageClass::IncomingRayPayloadKHR) {
  103. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  104. << "Payload must have storage class RayPayloadKHR or "
  105. "IncomingRayPayloadKHR";
  106. }
  107. break;
  108. }
  109. case spv::Op::OpReportIntersectionKHR: {
  110. _.function(inst->function()->id())
  111. ->RegisterExecutionModelLimitation(
  112. [](spv::ExecutionModel model, std::string* message) {
  113. if (model != spv::ExecutionModel::IntersectionKHR) {
  114. if (message) {
  115. *message =
  116. "OpReportIntersectionKHR requires IntersectionKHR "
  117. "execution model";
  118. }
  119. return false;
  120. }
  121. return true;
  122. });
  123. if (!_.IsBoolScalarType(result_type)) {
  124. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  125. << "expected Result Type to be bool scalar type";
  126. }
  127. const uint32_t hit = _.GetOperandTypeId(inst, 2);
  128. if (!_.IsFloatScalarType(hit) || _.GetBitWidth(hit) != 32) {
  129. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  130. << "Hit must be a 32-bit int scalar";
  131. }
  132. const uint32_t hit_kind = _.GetOperandTypeId(inst, 3);
  133. if (!_.IsUnsignedIntScalarType(hit_kind) ||
  134. _.GetBitWidth(hit_kind) != 32) {
  135. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  136. << "Hit Kind must be a 32-bit unsigned int scalar";
  137. }
  138. break;
  139. }
  140. case spv::Op::OpExecuteCallableKHR: {
  141. _.function(inst->function()->id())
  142. ->RegisterExecutionModelLimitation([](spv::ExecutionModel model,
  143. std::string* message) {
  144. if (model != spv::ExecutionModel::RayGenerationKHR &&
  145. model != spv::ExecutionModel::ClosestHitKHR &&
  146. model != spv::ExecutionModel::MissKHR &&
  147. model != spv::ExecutionModel::CallableKHR) {
  148. if (message) {
  149. *message =
  150. "OpExecuteCallableKHR requires RayGenerationKHR, "
  151. "ClosestHitKHR, MissKHR and CallableKHR execution models";
  152. }
  153. return false;
  154. }
  155. return true;
  156. });
  157. const uint32_t sbt_index = _.GetOperandTypeId(inst, 0);
  158. if (!_.IsUnsignedIntScalarType(sbt_index) ||
  159. _.GetBitWidth(sbt_index) != 32) {
  160. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  161. << "SBT Index must be a 32-bit unsigned int scalar";
  162. }
  163. const auto callable_data = _.FindDef(inst->GetOperandAs<uint32_t>(1));
  164. if (callable_data->opcode() != spv::Op::OpVariable) {
  165. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  166. << "Callable Data must be the result of a OpVariable";
  167. } else if (callable_data->GetOperandAs<spv::StorageClass>(2) !=
  168. spv::StorageClass::CallableDataKHR &&
  169. callable_data->GetOperandAs<spv::StorageClass>(2) !=
  170. spv::StorageClass::IncomingCallableDataKHR) {
  171. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  172. << "Callable Data must have storage class CallableDataKHR or "
  173. "IncomingCallableDataKHR";
  174. }
  175. break;
  176. }
  177. default:
  178. break;
  179. }
  180. return SPV_SUCCESS;
  181. }
  182. } // namespace val
  183. } // namespace spvtools