validate_mesh_shading.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/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 MeshShadingPass(ValidationState_t& _, const Instruction* inst) {
  22. const spv::Op opcode = inst->opcode();
  23. switch (opcode) {
  24. case spv::Op::OpEmitMeshTasksEXT: {
  25. _.function(inst->function()->id())
  26. ->RegisterExecutionModelLimitation(
  27. [](spv::ExecutionModel model, std::string* message) {
  28. if (model != spv::ExecutionModel::TaskEXT) {
  29. if (message) {
  30. *message =
  31. "OpEmitMeshTasksEXT requires TaskEXT execution model";
  32. }
  33. return false;
  34. }
  35. return true;
  36. });
  37. const uint32_t group_count_x = _.GetOperandTypeId(inst, 0);
  38. if (!_.IsUnsignedIntScalarType(group_count_x) ||
  39. _.GetBitWidth(group_count_x) != 32) {
  40. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  41. << "Group Count X must be a 32-bit unsigned int scalar";
  42. }
  43. const uint32_t group_count_y = _.GetOperandTypeId(inst, 1);
  44. if (!_.IsUnsignedIntScalarType(group_count_y) ||
  45. _.GetBitWidth(group_count_y) != 32) {
  46. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  47. << "Group Count Y must be a 32-bit unsigned int scalar";
  48. }
  49. const uint32_t group_count_z = _.GetOperandTypeId(inst, 2);
  50. if (!_.IsUnsignedIntScalarType(group_count_z) ||
  51. _.GetBitWidth(group_count_z) != 32) {
  52. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  53. << "Group Count Z must be a 32-bit unsigned int scalar";
  54. }
  55. if (inst->operands().size() == 4) {
  56. const auto payload = _.FindDef(inst->GetOperandAs<uint32_t>(3));
  57. if (payload->opcode() != spv::Op::OpVariable) {
  58. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  59. << "Payload must be the result of a OpVariable";
  60. }
  61. if (payload->GetOperandAs<spv::StorageClass>(2) !=
  62. spv::StorageClass::TaskPayloadWorkgroupEXT) {
  63. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  64. << "Payload OpVariable must have a storage class of "
  65. "TaskPayloadWorkgroupEXT";
  66. }
  67. }
  68. break;
  69. }
  70. case spv::Op::OpSetMeshOutputsEXT: {
  71. _.function(inst->function()->id())
  72. ->RegisterExecutionModelLimitation(
  73. [](spv::ExecutionModel model, std::string* message) {
  74. if (model != spv::ExecutionModel::MeshEXT) {
  75. if (message) {
  76. *message =
  77. "OpSetMeshOutputsEXT requires MeshEXT execution model";
  78. }
  79. return false;
  80. }
  81. return true;
  82. });
  83. const uint32_t vertex_count = _.GetOperandTypeId(inst, 0);
  84. if (!_.IsUnsignedIntScalarType(vertex_count) ||
  85. _.GetBitWidth(vertex_count) != 32) {
  86. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  87. << "Vertex Count must be a 32-bit unsigned int scalar";
  88. }
  89. const uint32_t primitive_count = _.GetOperandTypeId(inst, 1);
  90. if (!_.IsUnsignedIntScalarType(primitive_count) ||
  91. _.GetBitWidth(primitive_count) != 32) {
  92. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  93. << "Primitive Count must be a 32-bit unsigned int scalar";
  94. }
  95. break;
  96. }
  97. case spv::Op::OpWritePackedPrimitiveIndices4x8NV: {
  98. // No validation rules (for the moment).
  99. break;
  100. }
  101. default:
  102. break;
  103. }
  104. return SPV_SUCCESS;
  105. }
  106. } // namespace val
  107. } // namespace spvtools