16
16
#include " llvm/ADT/StringExtras.h"
17
17
#include " llvm/ADT/StringRef.h"
18
18
#include " llvm/Analysis/DXILMetadataAnalysis.h"
19
+ #include " llvm/Analysis/DXILResource.h"
19
20
#include " llvm/BinaryFormat/DXContainer.h"
20
21
#include " llvm/CodeGen/Passes.h"
21
22
#include " llvm/IR/Constants.h"
@@ -40,6 +41,7 @@ class DXContainerGlobals : public llvm::ModulePass {
40
41
GlobalVariable *buildSignature (Module &M, Signature &Sig, StringRef Name,
41
42
StringRef SectionName);
42
43
void addSignature (Module &M, SmallVector<GlobalValue *> &Globals);
44
+ void addResourcesForPSV (Module &M, PSVRuntimeInfo &PSV);
43
45
void addPipelineStateValidationInfo (Module &M,
44
46
SmallVector<GlobalValue *> &Globals);
45
47
@@ -59,6 +61,7 @@ class DXContainerGlobals : public llvm::ModulePass {
59
61
AU.setPreservesAll ();
60
62
AU.addRequired <ShaderFlagsAnalysisWrapper>();
61
63
AU.addRequired <DXILMetadataAnalysisWrapperPass>();
64
+ AU.addRequired <DXILResourceWrapperPass>();
62
65
}
63
66
};
64
67
@@ -140,6 +143,56 @@ void DXContainerGlobals::addSignature(Module &M,
140
143
Globals.emplace_back (buildSignature (M, OutputSig, " dx.osg1" , " OSG1" ));
141
144
}
142
145
146
+ void DXContainerGlobals::addResourcesForPSV (Module &M, PSVRuntimeInfo &PSV) {
147
+ const DXILResourceMap &ResMap =
148
+ getAnalysis<DXILResourceWrapperPass>().getResourceMap ();
149
+
150
+ for (const dxil::ResourceInfo &ResInfo : ResMap) {
151
+ const dxil::ResourceInfo::ResourceBinding &Binding = ResInfo.getBinding ();
152
+ dxbc::PSV::v2::ResourceBindInfo BindInfo;
153
+ BindInfo.LowerBound = Binding.LowerBound ;
154
+ BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1 ;
155
+ BindInfo.Space = Binding.Space ;
156
+
157
+ dxbc::PSV::ResourceType ResType = dxbc::PSV::ResourceType::Invalid;
158
+ bool IsUAV = ResInfo.getResourceClass () == dxil::ResourceClass::UAV;
159
+ switch (ResInfo.getResourceKind ()) {
160
+ case dxil::ResourceKind::Sampler:
161
+ ResType = dxbc::PSV::ResourceType::Sampler;
162
+ break ;
163
+ case dxil::ResourceKind::CBuffer:
164
+ ResType = dxbc::PSV::ResourceType::CBV;
165
+ break ;
166
+ case dxil::ResourceKind::StructuredBuffer:
167
+ ResType = IsUAV ? dxbc::PSV::ResourceType::UAVStructured
168
+ : dxbc::PSV::ResourceType::SRVStructured;
169
+ if (IsUAV && ResInfo.getUAV ().HasCounter )
170
+ ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
171
+ break ;
172
+ case dxil::ResourceKind::RTAccelerationStructure:
173
+ ResType = dxbc::PSV::ResourceType::SRVRaw;
174
+ break ;
175
+ case dxil::ResourceKind::RawBuffer:
176
+ ResType = IsUAV ? dxbc::PSV::ResourceType::UAVRaw
177
+ : dxbc::PSV::ResourceType::SRVRaw;
178
+ break ;
179
+ default :
180
+ ResType = IsUAV ? dxbc::PSV::ResourceType::UAVTyped
181
+ : dxbc::PSV::ResourceType::SRVTyped;
182
+ break ;
183
+ }
184
+ BindInfo.Type = ResType ;
185
+
186
+ BindInfo.Kind =
187
+ static_cast <dxbc::PSV::ResourceKind>(ResInfo.getResourceKind ());
188
+ // TODO: Add support for dxbc::PSV::ResourceFlag::UsedByAtomic64, tracking
189
+ // with https://github.com/llvm/llvm-project/issues/104392
190
+ BindInfo.Flags .Flags = 0u ;
191
+
192
+ PSV.Resources .emplace_back (BindInfo);
193
+ }
194
+ }
195
+
143
196
void DXContainerGlobals::addPipelineStateValidationInfo (
144
197
Module &M, SmallVector<GlobalValue *> &Globals) {
145
198
SmallString<256 > Data;
@@ -155,6 +208,8 @@ void DXContainerGlobals::addPipelineStateValidationInfo(
155
208
PSV.BaseData .ShaderStage =
156
209
static_cast <uint8_t >(MMI.ShaderStage - Triple::Pixel);
157
210
211
+ addResourcesForPSV (M, PSV);
212
+
158
213
// Hardcoded values here to unblock loading the shader into D3D.
159
214
//
160
215
// TODO: Lots more stuff to do here!
@@ -185,6 +240,7 @@ INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
185
240
" DXContainer Global Emitter" , false , true )
186
241
INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper)
187
242
INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
243
+ INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass)
188
244
INITIALIZE_PASS_END(DXContainerGlobals, " dxil-globals" ,
189
245
" DXContainer Global Emitter" , false , true )
190
246
0 commit comments