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