validate_derivatives.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2017 Google 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 derivative 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 derivative instructions.
  23. spv_result_t DerivativesPass(ValidationState_t& _, const Instruction* inst) {
  24. const spv::Op opcode = inst->opcode();
  25. const uint32_t result_type = inst->type_id();
  26. switch (opcode) {
  27. case spv::Op::OpDPdx:
  28. case spv::Op::OpDPdy:
  29. case spv::Op::OpFwidth:
  30. case spv::Op::OpDPdxFine:
  31. case spv::Op::OpDPdyFine:
  32. case spv::Op::OpFwidthFine:
  33. case spv::Op::OpDPdxCoarse:
  34. case spv::Op::OpDPdyCoarse:
  35. case spv::Op::OpFwidthCoarse: {
  36. if (!_.IsFloatScalarOrVectorType(result_type)) {
  37. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  38. << "Expected Result Type to be float scalar or vector type: "
  39. << spvOpcodeString(opcode);
  40. }
  41. if (!_.ContainsSizedIntOrFloatType(result_type, spv::Op::OpTypeFloat,
  42. 32)) {
  43. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  44. << "Result type component width must be 32 bits";
  45. }
  46. const uint32_t p_type = _.GetOperandTypeId(inst, 2);
  47. if (p_type != result_type) {
  48. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  49. << "Expected P type and Result Type to be the same: "
  50. << spvOpcodeString(opcode);
  51. }
  52. _.function(inst->function()->id())
  53. ->RegisterExecutionModelLimitation([opcode](spv::ExecutionModel model,
  54. std::string* message) {
  55. if (model != spv::ExecutionModel::Fragment &&
  56. model != spv::ExecutionModel::GLCompute &&
  57. model != spv::ExecutionModel::MeshEXT &&
  58. model != spv::ExecutionModel::TaskEXT) {
  59. if (message) {
  60. *message =
  61. std::string(
  62. "Derivative instructions require Fragment, GLCompute, "
  63. "MeshEXT or TaskEXT execution model: ") +
  64. spvOpcodeString(opcode);
  65. }
  66. return false;
  67. }
  68. return true;
  69. });
  70. _.function(inst->function()->id())
  71. ->RegisterLimitation([opcode](const ValidationState_t& state,
  72. const Function* entry_point,
  73. std::string* message) {
  74. const auto* models = state.GetExecutionModels(entry_point->id());
  75. const auto* modes = state.GetExecutionModes(entry_point->id());
  76. if (models &&
  77. (models->find(spv::ExecutionModel::GLCompute) !=
  78. models->end() ||
  79. models->find(spv::ExecutionModel::MeshEXT) != models->end() ||
  80. models->find(spv::ExecutionModel::TaskEXT) != models->end()) &&
  81. (!modes ||
  82. (modes->find(spv::ExecutionMode::DerivativeGroupLinearKHR) ==
  83. modes->end() &&
  84. modes->find(spv::ExecutionMode::DerivativeGroupQuadsKHR) ==
  85. modes->end()))) {
  86. if (message) {
  87. *message =
  88. std::string(
  89. "Derivative instructions require "
  90. "DerivativeGroupQuadsKHR "
  91. "or DerivativeGroupLinearKHR execution mode for "
  92. "GLCompute, MeshEXT or TaskEXT execution model: ") +
  93. spvOpcodeString(opcode);
  94. }
  95. return false;
  96. }
  97. return true;
  98. });
  99. break;
  100. }
  101. default:
  102. break;
  103. }
  104. return SPV_SUCCESS;
  105. }
  106. } // namespace val
  107. } // namespace spvtools