1 //===- DXILResource.h - Tools to translate DXIL resources -------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_TRANSFORMS_UTILS_DXILRESOURCE_H 10 #define LLVM_TRANSFORMS_UTILS_DXILRESOURCE_H 11 12 #include "llvm/IR/Metadata.h" 13 #include "llvm/IR/Value.h" 14 #include "llvm/Support/DXILABI.h" 15 16 namespace llvm { 17 namespace dxil { 18 19 struct ResourceBinding { 20 uint32_t Space; 21 uint32_t LowerBound; 22 uint32_t Size; 23 24 bool operator==(const ResourceBinding &RHS) const { 25 return std::tie(Space, LowerBound, Size) == 26 std::tie(RHS.Space, RHS.LowerBound, RHS.Size); 27 } 28 bool operator!=(const ResourceBinding &RHS) const { return !(*this == RHS); } 29 }; 30 31 class ResourceInfo { 32 struct UAVInfo { 33 bool GloballyCoherent; 34 bool HasCounter; 35 bool IsROV; 36 37 bool operator==(const UAVInfo &RHS) const { 38 return std::tie(GloballyCoherent, HasCounter, IsROV) == 39 std::tie(RHS.GloballyCoherent, RHS.HasCounter, RHS.IsROV); 40 } 41 bool operator!=(const UAVInfo &RHS) const { return !(*this == RHS); } 42 }; 43 44 struct StructInfo { 45 uint32_t Stride; 46 Align Alignment; 47 48 bool operator==(const StructInfo &RHS) const { 49 return std::tie(Stride, Alignment) == std::tie(RHS.Stride, RHS.Alignment); 50 } 51 bool operator!=(const StructInfo &RHS) const { return !(*this == RHS); } 52 }; 53 54 struct TypedInfo { 55 dxil::ElementType ElementTy; 56 uint32_t ElementCount; 57 58 bool operator==(const TypedInfo &RHS) const { 59 return std::tie(ElementTy, ElementCount) == 60 std::tie(RHS.ElementTy, RHS.ElementCount); 61 } 62 bool operator!=(const TypedInfo &RHS) const { return !(*this == RHS); } 63 }; 64 65 struct MSInfo { 66 uint32_t Count; 67 68 bool operator==(const MSInfo &RHS) const { return Count == RHS.Count; } 69 bool operator!=(const MSInfo &RHS) const { return !(*this == RHS); } 70 }; 71 72 struct FeedbackInfo { 73 dxil::SamplerFeedbackType Type; 74 75 bool operator==(const FeedbackInfo &RHS) const { return Type == RHS.Type; } 76 bool operator!=(const FeedbackInfo &RHS) const { return !(*this == RHS); } 77 }; 78 79 // Universal properties. 80 Value *Symbol; 81 StringRef Name; 82 83 ResourceBinding Binding; 84 uint32_t UniqueID; 85 86 dxil::ResourceClass RC; 87 dxil::ResourceKind Kind; 88 89 // Resource class dependent properties. 90 // CBuffer, Sampler, and RawBuffer end here. 91 union { 92 UAVInfo UAVFlags; // UAV 93 uint32_t CBufferSize; // CBuffer 94 dxil::SamplerType SamplerTy; // Sampler 95 }; 96 97 // Resource kind dependent properties. 98 union { 99 StructInfo Struct; // StructuredBuffer 100 TypedInfo Typed; // All SRV/UAV except Raw/StructuredBuffer 101 FeedbackInfo Feedback; // FeedbackTexture 102 }; 103 104 MSInfo MultiSample; 105 106 // Conditions to check before accessing union members. 107 bool isUAV() const; 108 bool isCBuffer() const; 109 bool isSampler() const; 110 bool isStruct() const; 111 bool isTyped() const; 112 bool isFeedback() const; 113 bool isMultiSample() const; 114 ResourceInfo(dxil::ResourceClass RC,dxil::ResourceKind Kind,Value * Symbol,StringRef Name,ResourceBinding Binding,uint32_t UniqueID)115 ResourceInfo(dxil::ResourceClass RC, dxil::ResourceKind Kind, Value *Symbol, 116 StringRef Name, ResourceBinding Binding, uint32_t UniqueID) 117 : Symbol(Symbol), Name(Name), Binding(Binding), UniqueID(UniqueID), 118 RC(RC), Kind(Kind) {} 119 120 public: 121 static ResourceInfo SRV(Value *Symbol, StringRef Name, 122 ResourceBinding Binding, uint32_t UniqueID, 123 dxil::ElementType ElementTy, uint32_t ElementCount, 124 dxil::ResourceKind Kind); 125 static ResourceInfo RawBuffer(Value *Symbol, StringRef Name, 126 ResourceBinding Binding, uint32_t UniqueID); 127 static ResourceInfo StructuredBuffer(Value *Symbol, StringRef Name, 128 ResourceBinding Binding, 129 uint32_t UniqueID, uint32_t Stride, 130 Align Alignment); 131 static ResourceInfo Texture2DMS(Value *Symbol, StringRef Name, 132 ResourceBinding Binding, uint32_t UniqueID, 133 dxil::ElementType ElementTy, 134 uint32_t ElementCount, uint32_t SampleCount); 135 static ResourceInfo 136 Texture2DMSArray(Value *Symbol, StringRef Name, ResourceBinding Binding, 137 uint32_t UniqueID, dxil::ElementType ElementTy, 138 uint32_t ElementCount, uint32_t SampleCount); 139 140 static ResourceInfo UAV(Value *Symbol, StringRef Name, 141 ResourceBinding Binding, uint32_t UniqueID, 142 dxil::ElementType ElementTy, uint32_t ElementCount, 143 bool GloballyCoherent, bool IsROV, 144 dxil::ResourceKind Kind); 145 static ResourceInfo RWRawBuffer(Value *Symbol, StringRef Name, 146 ResourceBinding Binding, uint32_t UniqueID, 147 bool GloballyCoherent, bool IsROV); 148 static ResourceInfo RWStructuredBuffer(Value *Symbol, StringRef Name, 149 ResourceBinding Binding, 150 uint32_t UniqueID, uint32_t Stride, 151 Align Alignment, bool GloballyCoherent, 152 bool IsROV, bool HasCounter); 153 static ResourceInfo RWTexture2DMS(Value *Symbol, StringRef Name, 154 ResourceBinding Binding, uint32_t UniqueID, 155 dxil::ElementType ElementTy, 156 uint32_t ElementCount, uint32_t SampleCount, 157 bool GloballyCoherent); 158 static ResourceInfo 159 RWTexture2DMSArray(Value *Symbol, StringRef Name, ResourceBinding Binding, 160 uint32_t UniqueID, dxil::ElementType ElementTy, 161 uint32_t ElementCount, uint32_t SampleCount, 162 bool GloballyCoherent); 163 static ResourceInfo FeedbackTexture2D(Value *Symbol, StringRef Name, 164 ResourceBinding Binding, 165 uint32_t UniqueID, 166 dxil::SamplerFeedbackType FeedbackTy); 167 static ResourceInfo 168 FeedbackTexture2DArray(Value *Symbol, StringRef Name, ResourceBinding Binding, 169 uint32_t UniqueID, 170 dxil::SamplerFeedbackType FeedbackTy); 171 172 static ResourceInfo CBuffer(Value *Symbol, StringRef Name, 173 ResourceBinding Binding, uint32_t UniqueID, 174 uint32_t Size); 175 176 static ResourceInfo Sampler(Value *Symbol, StringRef Name, 177 ResourceBinding Binding, uint32_t UniqueID, 178 dxil::SamplerType SamplerTy); 179 180 bool operator==(const ResourceInfo &RHS) const; 181 182 MDTuple *getAsMetadata(LLVMContext &Ctx) const; 183 getBinding()184 ResourceBinding getBinding() const { return Binding; } 185 std::pair<uint32_t, uint32_t> getAnnotateProps() const; 186 }; 187 188 } // namespace dxil 189 } // namespace llvm 190 191 #endif // LLVM_TRANSFORMS_UTILS_DXILRESOURCE_H 192