decoration.h 3.3 KB

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