validate_primitives.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2017 LunarG 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 correctness of primitive SPIR-V instructions.
  15. #include <string>
  16. #include "source/opcode.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. // Validates correctness of primitive instructions.
  23. spv_result_t PrimitivesPass(ValidationState_t& _, const Instruction* inst) {
  24. const spv::Op opcode = inst->opcode();
  25. switch (opcode) {
  26. case spv::Op::OpEmitVertex:
  27. case spv::Op::OpEndPrimitive:
  28. case spv::Op::OpEmitStreamVertex:
  29. case spv::Op::OpEndStreamPrimitive:
  30. _.function(inst->function()->id())
  31. ->RegisterExecutionModelLimitation(
  32. spv::ExecutionModel::Geometry,
  33. std::string(spvOpcodeString(opcode)) +
  34. " instructions require Geometry execution model");
  35. break;
  36. default:
  37. break;
  38. }
  39. switch (opcode) {
  40. case spv::Op::OpEmitStreamVertex:
  41. case spv::Op::OpEndStreamPrimitive: {
  42. const uint32_t stream_id = inst->word(1);
  43. const uint32_t stream_type = _.GetTypeId(stream_id);
  44. if (!_.IsIntScalarType(stream_type)) {
  45. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  46. << spvOpcodeString(opcode)
  47. << ": expected Stream to be int scalar";
  48. }
  49. const spv::Op stream_opcode = _.GetIdOpcode(stream_id);
  50. if (!spvOpcodeIsConstant(stream_opcode)) {
  51. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  52. << spvOpcodeString(opcode)
  53. << ": expected Stream to be constant instruction";
  54. }
  55. }
  56. default:
  57. break;
  58. }
  59. return SPV_SUCCESS;
  60. }
  61. } // namespace val
  62. } // namespace spvtools