1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
//===- DXILResource.cpp - Tools to translate DXIL resources ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/DXILResource.h"
#include "llvm/ADT/APInt.h"
#include "llvm/IR/DerivedTypes.h"
using namespace llvm;
using namespace dxil;
bool ResourceInfo::isUAV() const { return RC == ResourceClass::UAV; }
bool ResourceInfo::isCBuffer() const { return RC == ResourceClass::CBuffer; }
bool ResourceInfo::isSampler() const { return RC == ResourceClass::Sampler; }
bool ResourceInfo::isStruct() const {
return Kind == ResourceKind::StructuredBuffer;
}
bool ResourceInfo::isTyped() const {
switch (Kind) {
case ResourceKind::Texture1D:
case ResourceKind::Texture2D:
case ResourceKind::Texture2DMS:
case ResourceKind::Texture3D:
case ResourceKind::TextureCube:
case ResourceKind::Texture1DArray:
case ResourceKind::Texture2DArray:
case ResourceKind::Texture2DMSArray:
case ResourceKind::TextureCubeArray:
case ResourceKind::TypedBuffer:
return true;
case ResourceKind::RawBuffer:
case ResourceKind::StructuredBuffer:
case ResourceKind::FeedbackTexture2D:
case ResourceKind::FeedbackTexture2DArray:
case ResourceKind::CBuffer:
case ResourceKind::Sampler:
case ResourceKind::TBuffer:
case ResourceKind::RTAccelerationStructure:
return false;
case ResourceKind::Invalid:
case ResourceKind::NumEntries:
llvm_unreachable("Invalid resource kind");
}
llvm_unreachable("Unhandled ResourceKind enum");
}
bool ResourceInfo::isFeedback() const {
return Kind == ResourceKind::FeedbackTexture2D ||
Kind == ResourceKind::FeedbackTexture2DArray;
}
bool ResourceInfo::isMultiSample() const {
return Kind == ResourceKind::Texture2DMS ||
Kind == ResourceKind::Texture2DMSArray;
}
ResourceInfo ResourceInfo::SRV(Value *Symbol, StringRef Name,
ResourceBinding Binding, uint32_t UniqueID,
ElementType ElementTy, uint32_t ElementCount,
ResourceKind Kind) {
ResourceInfo RI(ResourceClass::SRV, Kind, Symbol, Name, Binding, UniqueID);
assert(RI.isTyped() && !(RI.isStruct() || RI.isMultiSample()) &&
"Invalid ResourceKind for SRV constructor.");
RI.Typed.ElementTy = ElementTy;
RI.Typed.ElementCount = ElementCount;
return RI;
}
ResourceInfo ResourceInfo::RawBuffer(Value *Symbol, StringRef Name,
ResourceBinding Binding,
uint32_t UniqueID) {
ResourceInfo RI(ResourceClass::SRV, ResourceKind::RawBuffer, Symbol, Name,
Binding, UniqueID);
return RI;
}
ResourceInfo ResourceInfo::StructuredBuffer(Value *Symbol, StringRef Name,
ResourceBinding Binding,
uint32_t UniqueID, uint32_t Stride,
Align Alignment) {
ResourceInfo RI(ResourceClass::SRV, ResourceKind::StructuredBuffer, Symbol,
Name, Binding, UniqueID);
RI.Struct.Stride = Stride;
RI.Struct.Alignment = Alignment;
return RI;
}
ResourceInfo ResourceInfo::Texture2DMS(Value *Symbol, StringRef Name,
ResourceBinding Binding,
uint32_t UniqueID, ElementType ElementTy,
uint32_t ElementCount,
uint32_t SampleCount) {
ResourceInfo RI(ResourceClass::SRV, ResourceKind::Texture2DMS, Symbol, Name,
Binding, UniqueID);
RI.Typed.ElementTy = ElementTy;
RI.Typed.ElementCount = ElementCount;
RI.MultiSample.Count = SampleCount;
return RI;
}
ResourceInfo ResourceInfo::Texture2DMSArray(
Value *Symbol, StringRef Name, ResourceBinding Binding, uint32_t UniqueID,
ElementType ElementTy, uint32_t ElementCount, uint32_t SampleCount) {
ResourceInfo RI(ResourceClass::SRV, ResourceKind::Texture2DMSArray, Symbol,
Name, Binding, UniqueID);
RI.Typed.ElementTy = ElementTy;
RI.Typed.ElementCount = ElementCount;
RI.MultiSample.Count = SampleCount;
return RI;
}
ResourceInfo ResourceInfo::UAV(Value *Symbol, StringRef Name,
ResourceBinding Binding, uint32_t UniqueID,
ElementType ElementTy, uint32_t ElementCount,
bool GloballyCoherent, bool IsROV,
ResourceKind Kind) {
ResourceInfo RI(ResourceClass::UAV, Kind, Symbol, Name, Binding, UniqueID);
assert(RI.isTyped() && !(RI.isStruct() || RI.isMultiSample()) &&
"Invalid ResourceKind for UAV constructor.");
RI.Typed.ElementTy = ElementTy;
RI.Typed.ElementCount = ElementCount;
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
RI.UAVFlags.IsROV = IsROV;
RI.UAVFlags.HasCounter = false;
return RI;
}
ResourceInfo ResourceInfo::RWRawBuffer(Value *Symbol, StringRef Name,
ResourceBinding Binding,
uint32_t UniqueID, bool GloballyCoherent,
bool IsROV) {
ResourceInfo RI(ResourceClass::UAV, ResourceKind::RawBuffer, Symbol, Name,
Binding, UniqueID);
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
RI.UAVFlags.IsROV = IsROV;
RI.UAVFlags.HasCounter = false;
return RI;
}
ResourceInfo ResourceInfo::RWStructuredBuffer(Value *Symbol, StringRef Name,
ResourceBinding Binding,
uint32_t UniqueID,
uint32_t Stride, Align Alignment,
bool GloballyCoherent, bool IsROV,
bool HasCounter) {
ResourceInfo RI(ResourceClass::UAV, ResourceKind::StructuredBuffer, Symbol,
Name, Binding, UniqueID);
RI.Struct.Stride = Stride;
RI.Struct.Alignment = Alignment;
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
RI.UAVFlags.IsROV = IsROV;
RI.UAVFlags.HasCounter = HasCounter;
return RI;
}
ResourceInfo
ResourceInfo::RWTexture2DMS(Value *Symbol, StringRef Name,
ResourceBinding Binding, uint32_t UniqueID,
ElementType ElementTy, uint32_t ElementCount,
uint32_t SampleCount, bool GloballyCoherent) {
ResourceInfo RI(ResourceClass::UAV, ResourceKind::Texture2DMS, Symbol, Name,
Binding, UniqueID);
RI.Typed.ElementTy = ElementTy;
RI.Typed.ElementCount = ElementCount;
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
RI.UAVFlags.IsROV = false;
RI.UAVFlags.HasCounter = false;
RI.MultiSample.Count = SampleCount;
return RI;
}
ResourceInfo
ResourceInfo::RWTexture2DMSArray(Value *Symbol, StringRef Name,
ResourceBinding Binding, uint32_t UniqueID,
ElementType ElementTy, uint32_t ElementCount,
uint32_t SampleCount, bool GloballyCoherent) {
ResourceInfo RI(ResourceClass::UAV, ResourceKind::Texture2DMSArray, Symbol,
Name, Binding, UniqueID);
RI.Typed.ElementTy = ElementTy;
RI.Typed.ElementCount = ElementCount;
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
RI.UAVFlags.IsROV = false;
RI.UAVFlags.HasCounter = false;
RI.MultiSample.Count = SampleCount;
return RI;
}
ResourceInfo ResourceInfo::FeedbackTexture2D(Value *Symbol, StringRef Name,
ResourceBinding Binding,
uint32_t UniqueID,
SamplerFeedbackType FeedbackTy) {
ResourceInfo RI(ResourceClass::UAV, ResourceKind::FeedbackTexture2D, Symbol,
Name, Binding, UniqueID);
RI.UAVFlags.GloballyCoherent = false;
RI.UAVFlags.IsROV = false;
RI.UAVFlags.HasCounter = false;
RI.Feedback.Type = FeedbackTy;
return RI;
}
ResourceInfo
ResourceInfo::FeedbackTexture2DArray(Value *Symbol, StringRef Name,
ResourceBinding Binding, uint32_t UniqueID,
SamplerFeedbackType FeedbackTy) {
ResourceInfo RI(ResourceClass::UAV, ResourceKind::FeedbackTexture2DArray,
Symbol, Name, Binding, UniqueID);
RI.UAVFlags.GloballyCoherent = false;
RI.UAVFlags.IsROV = false;
RI.UAVFlags.HasCounter = false;
RI.Feedback.Type = FeedbackTy;
return RI;
}
ResourceInfo ResourceInfo::CBuffer(Value *Symbol, StringRef Name,
ResourceBinding Binding, uint32_t UniqueID,
uint32_t Size) {
ResourceInfo RI(ResourceClass::CBuffer, ResourceKind::CBuffer, Symbol, Name,
Binding, UniqueID);
RI.CBufferSize = Size;
return RI;
}
ResourceInfo ResourceInfo::Sampler(Value *Symbol, StringRef Name,
ResourceBinding Binding, uint32_t UniqueID,
SamplerType SamplerTy) {
ResourceInfo RI(ResourceClass::Sampler, ResourceKind::Sampler, Symbol, Name,
Binding, UniqueID);
RI.SamplerTy = SamplerTy;
return RI;
}
bool ResourceInfo::operator==(const ResourceInfo &RHS) const {
if (std::tie(Symbol, Name, Binding, UniqueID, RC, Kind) !=
std::tie(RHS.Symbol, RHS.Name, RHS.Binding, RHS.UniqueID, RHS.RC,
RHS.Kind))
return false;
if (isCBuffer())
return CBufferSize == RHS.CBufferSize;
if (isSampler())
return SamplerTy == RHS.SamplerTy;
if (isUAV() && UAVFlags != RHS.UAVFlags)
return false;
if (isStruct())
return Struct == RHS.Struct;
if (isFeedback())
return Feedback == RHS.Feedback;
if (isTyped() && Typed != RHS.Typed)
return false;
if (isMultiSample())
return MultiSample == RHS.MultiSample;
assert((Kind == ResourceKind::RawBuffer) && "Unhandled resource kind");
return true;
}
MDTuple *ResourceInfo::getAsMetadata(LLVMContext &Ctx) const {
SmallVector<Metadata *, 11> MDVals;
Type *I32Ty = Type::getInt32Ty(Ctx);
Type *I1Ty = Type::getInt1Ty(Ctx);
auto getIntMD = [&I32Ty](uint32_t V) {
return ConstantAsMetadata::get(
Constant::getIntegerValue(I32Ty, APInt(32, V)));
};
auto getBoolMD = [&I1Ty](uint32_t V) {
return ConstantAsMetadata::get(
Constant::getIntegerValue(I1Ty, APInt(1, V)));
};
MDVals.push_back(getIntMD(UniqueID));
MDVals.push_back(ValueAsMetadata::get(Symbol));
MDVals.push_back(MDString::get(Ctx, Name));
MDVals.push_back(getIntMD(Binding.Space));
MDVals.push_back(getIntMD(Binding.LowerBound));
MDVals.push_back(getIntMD(Binding.Size));
if (isCBuffer()) {
MDVals.push_back(getIntMD(CBufferSize));
MDVals.push_back(nullptr);
} else if (isSampler()) {
MDVals.push_back(getIntMD(llvm::to_underlying(SamplerTy)));
MDVals.push_back(nullptr);
} else {
MDVals.push_back(getIntMD(llvm::to_underlying(Kind)));
if (isUAV()) {
MDVals.push_back(getBoolMD(UAVFlags.GloballyCoherent));
MDVals.push_back(getBoolMD(UAVFlags.HasCounter));
MDVals.push_back(getBoolMD(UAVFlags.IsROV));
} else {
// All SRVs include sample count in the metadata, but it's only meaningful
// for multi-sampled textured. Also, UAVs can be multisampled in SM6.7+,
// but this just isn't reflected in the metadata at all.
uint32_t SampleCount = isMultiSample() ? MultiSample.Count : 0;
MDVals.push_back(getIntMD(SampleCount));
}
// Further properties are attached to a metadata list of tag-value pairs.
SmallVector<Metadata *> Tags;
if (isStruct()) {
Tags.push_back(
getIntMD(llvm::to_underlying(ExtPropTags::StructuredBufferStride)));
Tags.push_back(getIntMD(Struct.Stride));
} else if (isTyped()) {
Tags.push_back(getIntMD(llvm::to_underlying(ExtPropTags::ElementType)));
Tags.push_back(getIntMD(llvm::to_underlying(Typed.ElementTy)));
} else if (isFeedback()) {
Tags.push_back(
getIntMD(llvm::to_underlying(ExtPropTags::SamplerFeedbackKind)));
Tags.push_back(getIntMD(llvm::to_underlying(Feedback.Type)));
}
MDVals.push_back(Tags.empty() ? nullptr : MDNode::get(Ctx, Tags));
}
return MDNode::get(Ctx, MDVals);
}
std::pair<uint32_t, uint32_t> ResourceInfo::getAnnotateProps() const {
uint32_t ResourceKind = llvm::to_underlying(Kind);
uint32_t AlignLog2 = isStruct() ? Log2(Struct.Alignment) : 0;
bool IsUAV = isUAV();
bool IsROV = IsUAV && UAVFlags.IsROV;
bool IsGloballyCoherent = IsUAV && UAVFlags.GloballyCoherent;
uint8_t SamplerCmpOrHasCounter = 0;
if (IsUAV)
SamplerCmpOrHasCounter = UAVFlags.HasCounter;
else if (isSampler())
SamplerCmpOrHasCounter = SamplerTy == SamplerType::Comparison;
// TODO: Document this format. Currently the only reference is the
// implementation of dxc's DxilResourceProperties struct.
uint32_t Word0 = 0;
Word0 |= ResourceKind & 0xFF;
Word0 |= (AlignLog2 & 0xF) << 8;
Word0 |= (IsUAV & 1) << 12;
Word0 |= (IsROV & 1) << 13;
Word0 |= (IsGloballyCoherent & 1) << 14;
Word0 |= (SamplerCmpOrHasCounter & 1) << 15;
uint32_t Word1 = 0;
if (isStruct())
Word1 = Struct.Stride;
else if (isCBuffer())
Word1 = CBufferSize;
else if (isFeedback())
Word1 = llvm::to_underlying(Feedback.Type);
else if (isTyped()) {
uint32_t CompType = llvm::to_underlying(Typed.ElementTy);
uint32_t CompCount = Typed.ElementCount;
uint32_t SampleCount = isMultiSample() ? MultiSample.Count : 0;
Word1 |= (CompType & 0xFF) << 0;
Word1 |= (CompCount & 0xFF) << 8;
Word1 |= (SampleCount & 0xFF) << 16;
}
return {Word0, Word1};
}
#define DEBUG_TYPE "dxil-resource"
|