decoration.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef SOURCE_VAL_DECORATION_H_
  15. #define SOURCE_VAL_DECORATION_H_
  16. #include <cassert>
  17. #include <cstdint>
  18. #include <unordered_map>
  19. #include <vector>
  20. #include "source/latest_version_spirv_header.h"
  21. namespace spvtools {
  22. namespace val {
  23. // An object of this class represents a specific decoration including its
  24. // parameters (if any). Decorations are used by OpDecorate and OpMemberDecorate,
  25. // and they describe certain properties that can be assigned to one or several
  26. // <id>s.
  27. //
  28. // A Decoration object contains the decoration type (an enum), associated
  29. // literal parameters, and struct member index. If the decoration does not apply
  30. // to a struct member, then the index is kInvalidIndex. A Decoration object does
  31. // not store the target Id, i.e. the Id to which it applies. It is
  32. // possible for the same decoration to be applied to several <id>s (and they
  33. // might be assigned using separate SPIR-V instructions, possibly using an
  34. // assignment through GroupDecorate).
  35. //
  36. // Example 1: Decoration for an object<id> with no parameters:
  37. // OpDecorate %obj Flat
  38. // dec_type_ = spv::Decoration::Flat
  39. // params_ = empty vector
  40. // struct_member_index_ = kInvalidMember
  41. //
  42. // Example 2: Decoration for an object<id> with two parameters:
  43. // OpDecorate %obj LinkageAttributes "link" Import
  44. // dec_type_ = spv::Decoration::LinkageAttributes
  45. // params_ = vector { link, Import }
  46. // struct_member_index_ = kInvalidMember
  47. //
  48. // Example 3: Decoration for a member of a structure with one parameter:
  49. // OpMemberDecorate %struct 2 Offset 2
  50. // dec_type_ = spv::Decoration::Offset
  51. // params_ = vector { 2 }
  52. // struct_member_index_ = 2
  53. //
  54. // Example 4: Decoration for a Builtin:
  55. // OpDecorate %var BuiltIn FragDepth
  56. // dec_type_ = spv::Decoration::BuiltIn
  57. // params_ = vector { FragDepth }
  58. // struct_member_index_ = kInvalidMember
  59. //
  60. class Decoration {
  61. public:
  62. enum { kInvalidMember = -1 };
  63. Decoration(spv::Decoration t,
  64. const std::vector<uint32_t>& parameters = std::vector<uint32_t>(),
  65. uint32_t member_index = kInvalidMember)
  66. : dec_type_(t), params_(parameters), struct_member_index_(member_index) {}
  67. void set_struct_member_index(uint32_t index) { struct_member_index_ = index; }
  68. int struct_member_index() const { return struct_member_index_; }
  69. spv::Decoration dec_type() const { return dec_type_; }
  70. std::vector<uint32_t>& params() { return params_; }
  71. const std::vector<uint32_t>& params() const { return params_; }
  72. spv::BuiltIn builtin() const {
  73. assert(dec_type_ == spv::Decoration::BuiltIn);
  74. return spv::BuiltIn(params_[0]);
  75. }
  76. inline bool operator<(const Decoration& rhs) const {
  77. // Note: Sort by struct_member_index_ first, then type, so look up can be
  78. // efficient using lower_bound() and upper_bound().
  79. if (struct_member_index_ < rhs.struct_member_index_) return true;
  80. if (rhs.struct_member_index_ < struct_member_index_) return false;
  81. if (dec_type_ < rhs.dec_type_) return true;
  82. if (rhs.dec_type_ < dec_type_) return false;
  83. return params_ < rhs.params_;
  84. }
  85. inline bool operator==(const Decoration& rhs) const {
  86. return (dec_type_ == rhs.dec_type_ && params_ == rhs.params_ &&
  87. struct_member_index_ == rhs.struct_member_index_);
  88. }
  89. private:
  90. spv::Decoration dec_type_;
  91. std::vector<uint32_t> params_;
  92. // If the decoration applies to a member of a structure type, then the index
  93. // of the member is stored here. Otherwise, this is kInvalidIndex.
  94. int struct_member_index_;
  95. };
  96. } // namespace val
  97. } // namespace spvtools
  98. #endif // SOURCE_VAL_DECORATION_H_