validate_primitives.cpp 2.2 KB

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