validate_derivatives.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "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 derivative instructions.
  24. spv_result_t DerivativesPass(ValidationState_t& _, const Instruction* inst) {
  25. const SpvOp opcode = inst->opcode();
  26. const uint32_t result_type = inst->type_id();
  27. switch (opcode) {
  28. case SpvOpDPdx:
  29. case SpvOpDPdy:
  30. case SpvOpFwidth:
  31. case SpvOpDPdxFine:
  32. case SpvOpDPdyFine:
  33. case SpvOpFwidthFine:
  34. case SpvOpDPdxCoarse:
  35. case SpvOpDPdyCoarse:
  36. case SpvOpFwidthCoarse: {
  37. if (!_.IsFloatScalarOrVectorType(result_type)) {
  38. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  39. << "Expected Result Type to be float scalar or vector type: "
  40. << spvOpcodeString(opcode);
  41. }
  42. const uint32_t p_type = _.GetOperandTypeId(inst, 2);
  43. if (p_type != result_type) {
  44. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  45. << "Expected P type and Result Type to be the same: "
  46. << spvOpcodeString(opcode);
  47. }
  48. const spvtools::Extension compute_shader_derivatives_extension =
  49. kSPV_NV_compute_shader_derivatives;
  50. ExtensionSet exts(1, &compute_shader_derivatives_extension);
  51. if (_.HasAnyOfExtensions(exts)) {
  52. _.function(inst->function()->id())
  53. ->RegisterExecutionModelLimitation([opcode](SpvExecutionModel model,
  54. std::string* message) {
  55. if (model != SpvExecutionModelFragment &&
  56. model != SpvExecutionModelGLCompute) {
  57. if (message) {
  58. *message =
  59. std::string(
  60. "Derivative instructions require Fragment execution "
  61. "model: ") +
  62. spvOpcodeString(opcode);
  63. }
  64. return false;
  65. }
  66. return true;
  67. });
  68. } else {
  69. _.function(inst->function()->id())
  70. ->RegisterExecutionModelLimitation(
  71. SpvExecutionModelFragment,
  72. std::string(
  73. "Derivative instructions require Fragment execution "
  74. "model: ") +
  75. spvOpcodeString(opcode));
  76. }
  77. break;
  78. }
  79. default:
  80. break;
  81. }
  82. return SPV_SUCCESS;
  83. }
  84. } // namespace val
  85. } // namespace spvtools