validate_mesh_shading.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 query instructions from SPV_KHR_ray_query
  15. #include "source/opcode.h"
  16. #include "source/spirv_target_env.h"
  17. #include "source/val/instruction.h"
  18. #include "source/val/validate.h"
  19. #include "source/val/validation_state.h"
  20. namespace spvtools {
  21. namespace val {
  22. bool IsInterfaceVariable(ValidationState_t& _, const Instruction* inst,
  23. spv::ExecutionModel model) {
  24. bool foundInterface = false;
  25. for (auto entry_point : _.entry_points()) {
  26. const auto* models = _.GetExecutionModels(entry_point);
  27. if (models->find(model) == models->end()) return false;
  28. for (const auto& desc : _.entry_point_descriptions(entry_point)) {
  29. for (auto interface : desc.interfaces) {
  30. if (inst->id() == interface) {
  31. foundInterface = true;
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. return foundInterface;
  38. }
  39. spv_result_t MeshShadingPass(ValidationState_t& _, const Instruction* inst) {
  40. const spv::Op opcode = inst->opcode();
  41. switch (opcode) {
  42. case spv::Op::OpEmitMeshTasksEXT: {
  43. _.function(inst->function()->id())
  44. ->RegisterExecutionModelLimitation(
  45. [](spv::ExecutionModel model, std::string* message) {
  46. if (model != spv::ExecutionModel::TaskEXT) {
  47. if (message) {
  48. *message =
  49. "OpEmitMeshTasksEXT requires TaskEXT execution model";
  50. }
  51. return false;
  52. }
  53. return true;
  54. });
  55. const uint32_t group_count_x = _.GetOperandTypeId(inst, 0);
  56. if (!_.IsUnsignedIntScalarType(group_count_x) ||
  57. _.GetBitWidth(group_count_x) != 32) {
  58. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  59. << "Group Count X must be a 32-bit unsigned int scalar";
  60. }
  61. const uint32_t group_count_y = _.GetOperandTypeId(inst, 1);
  62. if (!_.IsUnsignedIntScalarType(group_count_y) ||
  63. _.GetBitWidth(group_count_y) != 32) {
  64. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  65. << "Group Count Y must be a 32-bit unsigned int scalar";
  66. }
  67. const uint32_t group_count_z = _.GetOperandTypeId(inst, 2);
  68. if (!_.IsUnsignedIntScalarType(group_count_z) ||
  69. _.GetBitWidth(group_count_z) != 32) {
  70. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  71. << "Group Count Z must be a 32-bit unsigned int scalar";
  72. }
  73. if (inst->operands().size() == 4) {
  74. const auto payload = _.FindDef(inst->GetOperandAs<uint32_t>(3));
  75. if (payload->opcode() != spv::Op::OpVariable) {
  76. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  77. << "Payload must be the result of a OpVariable";
  78. }
  79. if (payload->GetOperandAs<spv::StorageClass>(2) !=
  80. spv::StorageClass::TaskPayloadWorkgroupEXT) {
  81. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  82. << "Payload OpVariable must have a storage class of "
  83. "TaskPayloadWorkgroupEXT";
  84. }
  85. }
  86. break;
  87. }
  88. case spv::Op::OpSetMeshOutputsEXT: {
  89. _.function(inst->function()->id())
  90. ->RegisterExecutionModelLimitation(
  91. [](spv::ExecutionModel model, std::string* message) {
  92. if (model != spv::ExecutionModel::MeshEXT) {
  93. if (message) {
  94. *message =
  95. "OpSetMeshOutputsEXT requires MeshEXT execution model";
  96. }
  97. return false;
  98. }
  99. return true;
  100. });
  101. const uint32_t vertex_count = _.GetOperandTypeId(inst, 0);
  102. if (!_.IsUnsignedIntScalarType(vertex_count) ||
  103. _.GetBitWidth(vertex_count) != 32) {
  104. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  105. << "Vertex Count must be a 32-bit unsigned int scalar";
  106. }
  107. const uint32_t primitive_count = _.GetOperandTypeId(inst, 1);
  108. if (!_.IsUnsignedIntScalarType(primitive_count) ||
  109. _.GetBitWidth(primitive_count) != 32) {
  110. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  111. << "Primitive Count must be a 32-bit unsigned int scalar";
  112. }
  113. break;
  114. }
  115. case spv::Op::OpWritePackedPrimitiveIndices4x8NV: {
  116. // No validation rules (for the moment).
  117. break;
  118. }
  119. case spv::Op::OpVariable: {
  120. if (_.HasCapability(spv::Capability::MeshShadingEXT)) {
  121. bool meshInterfaceVar =
  122. IsInterfaceVariable(_, inst, spv::ExecutionModel::MeshEXT);
  123. bool fragInterfaceVar =
  124. IsInterfaceVariable(_, inst, spv::ExecutionModel::Fragment);
  125. const spv::StorageClass storage_class =
  126. inst->GetOperandAs<spv::StorageClass>(2);
  127. bool storage_output = (storage_class == spv::StorageClass::Output);
  128. bool storage_input = (storage_class == spv::StorageClass::Input);
  129. if (_.HasDecoration(inst->id(), spv::Decoration::PerPrimitiveEXT)) {
  130. if (fragInterfaceVar && !storage_input) {
  131. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  132. << "PerPrimitiveEXT decoration must be applied only to "
  133. "variables in the Input Storage Class in the Fragment "
  134. "Execution Model.";
  135. }
  136. if (meshInterfaceVar && !storage_output) {
  137. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  138. << _.VkErrorID(4336)
  139. << "PerPrimitiveEXT decoration must be applied only to "
  140. "variables in the Output Storage Class in the "
  141. "Storage Class in the MeshEXT Execution Model.";
  142. }
  143. }
  144. }
  145. break;
  146. }
  147. default:
  148. break;
  149. }
  150. return SPV_SUCCESS;
  151. }
  152. } // namespace val
  153. } // namespace spvtools