1616#include " llvm/ADT/StringExtras.h"
1717#include " llvm/ADT/StringRef.h"
1818#include " llvm/Analysis/DXILMetadataAnalysis.h"
19+ #include " llvm/Analysis/DXILResource.h"
1920#include " llvm/BinaryFormat/DXContainer.h"
2021#include " llvm/CodeGen/Passes.h"
2122#include " llvm/IR/Constants.h"
@@ -40,6 +41,7 @@ class DXContainerGlobals : public llvm::ModulePass {
4041 GlobalVariable *buildSignature (Module &M, Signature &Sig, StringRef Name,
4142 StringRef SectionName);
4243 void addSignature (Module &M, SmallVector<GlobalValue *> &Globals);
44+ void addResourcesForPSV (Module &M, PSVRuntimeInfo &PSV);
4345 void addPipelineStateValidationInfo (Module &M,
4446 SmallVector<GlobalValue *> &Globals);
4547
@@ -59,6 +61,7 @@ class DXContainerGlobals : public llvm::ModulePass {
5961 AU.setPreservesAll ();
6062 AU.addRequired <ShaderFlagsAnalysisWrapper>();
6163 AU.addRequired <DXILMetadataAnalysisWrapperPass>();
64+ AU.addRequired <DXILResourceWrapperPass>();
6265 }
6366};
6467
@@ -140,6 +143,56 @@ void DXContainerGlobals::addSignature(Module &M,
140143 Globals.emplace_back (buildSignature (M, OutputSig, " dx.osg1" , " OSG1" ));
141144}
142145
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+
143196void DXContainerGlobals::addPipelineStateValidationInfo (
144197 Module &M, SmallVector<GlobalValue *> &Globals) {
145198 SmallString<256 > Data;
@@ -155,6 +208,8 @@ void DXContainerGlobals::addPipelineStateValidationInfo(
155208 PSV.BaseData .ShaderStage =
156209 static_cast <uint8_t >(MMI.ShaderStage - Triple::Pixel);
157210
211+ addResourcesForPSV (M, PSV);
212+
158213 // Hardcoded values here to unblock loading the shader into D3D.
159214 //
160215 // TODO: Lots more stuff to do here!
@@ -185,6 +240,7 @@ INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
185240 " DXContainer Global Emitter" , false , true )
186241INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper)
187242INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
243+ INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass)
188244INITIALIZE_PASS_END(DXContainerGlobals, " dxil-globals" ,
189245 " DXContainer Global Emitter" , false , true )
190246
0 commit comments