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 "validate.h"
  16. #include <string>
  17. #include "diagnostic.h"
  18. #include "opcode.h"
  19. #include "val/instruction.h"
  20. #include "val/validation_state.h"
  21. namespace libspirv {
  22. // Validates correctness of primitive instructions.
  23. spv_result_t PrimitivesPass(ValidationState_t& _,
  24. const spv_parsed_instruction_t* inst) {
  25. const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
  26. switch (opcode) {
  27. case SpvOpEmitVertex:
  28. case SpvOpEndPrimitive:
  29. case SpvOpEmitStreamVertex:
  30. case SpvOpEndStreamPrimitive:
  31. _.current_function().RegisterExecutionModelLimitation(
  32. SpvExecutionModelGeometry,
  33. std::string(spvOpcodeString(opcode)) +
  34. " instructions require Geometry execution model");
  35. break;
  36. default:
  37. break;
  38. }
  39. switch (opcode) {
  40. case SpvOpEmitStreamVertex:
  41. case SpvOpEndStreamPrimitive: {
  42. const uint32_t stream_id = inst->words[1];
  43. const uint32_t stream_type = _.GetTypeId(stream_id);
  44. if (!_.IsIntScalarType(stream_type)) {
  45. return _.diag(SPV_ERROR_INVALID_DATA)
  46. << spvOpcodeString(opcode)
  47. << ": expected Stream to be int scalar";
  48. }
  49. const SpvOp stream_opcode = _.GetIdOpcode(stream_id);
  50. if (!spvOpcodeIsConstant(stream_opcode)) {
  51. return _.diag(SPV_ERROR_INVALID_DATA)
  52. << spvOpcodeString(opcode)
  53. << ": expected Stream to be constant instruction";
  54. }
  55. }
  56. default:
  57. break;
  58. }
  59. return SPV_SUCCESS;
  60. }
  61. } // namespace libspirv