From be80b832805677789c1c8bda4f361368c6dcabf3 Mon Sep 17 00:00:00 2001 From: "Bernhart, Bryan" Date: Wed, 31 Jan 2024 11:22:10 -0800 Subject: [PATCH] Eliminate DXGI adapter dependency Allows specifying another adapter type. Issue: #683 --- include/gpgmm_d3d12.h | 6 +- src/gpgmm/d3d12/CapsD3D12.cpp | 64 +++++++++++++--------- src/gpgmm/d3d12/CapsD3D12.h | 2 +- src/gpgmm/d3d12/ResidencyManagerD3D12.cpp | 9 ++- src/gpgmm/d3d12/ResidencyManagerD3D12.h | 2 +- src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp | 12 ++-- src/gpgmm/d3d12/ResourceAllocatorD3D12.h | 10 ++-- 7 files changed, 59 insertions(+), 46 deletions(-) diff --git a/include/gpgmm_d3d12.h b/include/gpgmm_d3d12.h index fc9b9434..7b7da71c 100644 --- a/include/gpgmm_d3d12.h +++ b/include/gpgmm_d3d12.h @@ -667,7 +667,7 @@ namespace gpgmm::d3d12 { */ GPGMM_EXPORT HRESULT CreateResidencyManager(const RESIDENCY_MANAGER_DESC& descriptor, ID3D12Device* pDevice, - IDXGIAdapter3* pAdapter, + IUnknown* pAdapter, IResidencyManager** ppResidencyManagerOut); /** \struct RESOURCE_ALLOCATION_INFO @@ -1430,7 +1430,7 @@ namespace gpgmm::d3d12 { */ GPGMM_EXPORT HRESULT CreateResourceAllocator(const RESOURCE_ALLOCATOR_DESC& allocatorDescriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, IResourceAllocator** ppResourceAllocatorOut, IResidencyManager** ppResidencyManagerOut); @@ -1450,7 +1450,7 @@ namespace gpgmm::d3d12 { */ GPGMM_EXPORT HRESULT CreateResourceAllocator(const RESOURCE_ALLOCATOR_DESC& allocatorDescriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, IResidencyManager* pResidencyManager, IResourceAllocator** ppResourceAllocatorOut); diff --git a/src/gpgmm/d3d12/CapsD3D12.cpp b/src/gpgmm/d3d12/CapsD3D12.cpp index b4d90666..81117bc4 100644 --- a/src/gpgmm/d3d12/CapsD3D12.cpp +++ b/src/gpgmm/d3d12/CapsD3D12.cpp @@ -25,85 +25,95 @@ namespace gpgmm::d3d12 { - HRESULT SetMaxResourceSize(ID3D12Device* device, uint64_t* sizeOut) { + HRESULT SetMaxResourceSize(ID3D12Device* pDevice, uint64_t* pSizeOut) { + ASSERT(pSizeOut); D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT feature = {}; GPGMM_RETURN_IF_FAILED( - device->CheckFeatureSupport(D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT, &feature, - sizeof(D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT)), - device); + pDevice->CheckFeatureSupport(D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT, &feature, + sizeof(D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT)), + pDevice); // Check for overflow. if (feature.MaxGPUVirtualAddressBitsPerResource == 0 || feature.MaxGPUVirtualAddressBitsPerResource > GetNumOfBits()) { return E_FAIL; } - *sizeOut = (1ull << (feature.MaxGPUVirtualAddressBitsPerResource - 1)) - 1; + *pSizeOut = (1ull << (feature.MaxGPUVirtualAddressBitsPerResource - 1)) - 1; return S_OK; } - HRESULT SetMaxResourceHeapSize(ID3D12Device* device, uint64_t* sizeOut) { + HRESULT SetMaxResourceHeapSize(ID3D12Device* pDevice, uint64_t* pSizeOut) { + ASSERT(pSizeOut); D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT feature = {}; GPGMM_RETURN_IF_FAILED( - device->CheckFeatureSupport(D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT, &feature, - sizeof(D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT)), - device); + pDevice->CheckFeatureSupport(D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT, &feature, + sizeof(D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT)), + pDevice); // Check for overflow. if (feature.MaxGPUVirtualAddressBitsPerProcess == 0 || feature.MaxGPUVirtualAddressBitsPerProcess > GetNumOfBits()) { return E_FAIL; } - *sizeOut = (1ull << (feature.MaxGPUVirtualAddressBitsPerProcess - 1)) - 1; + *pSizeOut = (1ull << (feature.MaxGPUVirtualAddressBitsPerProcess - 1)) - 1; return S_OK; } - HRESULT SetCreateHeapNotResidentSupported(ID3D12Device* device, + HRESULT SetCreateHeapNotResidentSupported(ID3D12Device* pDevice, bool* createHeapNotResidencySupported) { + ASSERT(createHeapNotResidencySupported); *createHeapNotResidencySupported = false; // Only Windows 10 Build 20348 and later support creating non-resident heaps. // ID3D12Device8 is required to be defined in Windows 10 Build 20348 or newer builds. #ifdef __ID3D12Device8_FWD_DEFINED__ D3D12_FEATURE_DATA_D3D12_OPTIONS7 options7 = {}; - if (SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS7, &options7, - sizeof(options7)))) { + if (SUCCEEDED(pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS7, &options7, + sizeof(options7)))) { *createHeapNotResidencySupported = true; } #endif return S_OK; } - HRESULT SetMaxResourceHeapTierSupported(ID3D12Device* device, + HRESULT SetMaxResourceHeapTierSupported(ID3D12Device* pDevice, D3D12_RESOURCE_HEAP_TIER* maxResourceHeapTierOut) { + ASSERT(maxResourceHeapTierOut); D3D12_FEATURE_DATA_D3D12_OPTIONS options = {}; GPGMM_RETURN_IF_FAILED( - device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options)), - device); + pDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options)), + pDevice); *maxResourceHeapTierOut = options.ResourceHeapTier; return S_OK; } // static - HRESULT Caps::CreateCaps(ID3D12Device* device, IDXGIAdapter* adapter, Caps** capsOut) { - GPGMM_RETURN_IF_NULL(device); + HRESULT Caps::CreateCaps(ID3D12Device* pDevice, IUnknown* pAdapter, Caps** ppCapsOut) { + ASSERT(pDevice); std::unique_ptr caps(new Caps()); - GPGMM_RETURN_IF_FAILED(SetMaxResourceSize(device, &caps->mMaxResourceSize)); - GPGMM_RETURN_IF_FAILED(SetMaxResourceHeapSize(device, &caps->mMaxResourceHeapSize)); + + // Load device-specific capabilities. + GPGMM_RETURN_IF_FAILED(SetMaxResourceSize(pDevice, &caps->mMaxResourceSize)); + GPGMM_RETURN_IF_FAILED(SetMaxResourceHeapSize(pDevice, &caps->mMaxResourceHeapSize)); GPGMM_RETURN_IF_FAILED( - SetMaxResourceHeapTierSupported(device, &caps->mMaxResourceHeapTier)); + SetMaxResourceHeapTierSupported(pDevice, &caps->mMaxResourceHeapTier)); GPGMM_RETURN_IF_FAILED( - SetCreateHeapNotResidentSupported(device, &caps->mIsCreateHeapNotResidentSupported)); + SetCreateHeapNotResidentSupported(pDevice, &caps->mIsCreateHeapNotResidentSupported)); + // Load adapter-specific capabilities. D3D12_FEATURE_DATA_ARCHITECTURE arch = {}; GPGMM_RETURN_IF_FAILED( - device->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE, &arch, sizeof(arch)), device); + pDevice->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE, &arch, sizeof(arch)), pDevice); caps->mIsAdapterUMA = arch.UMA; caps->mIsAdapterCacheCoherentUMA = arch.CacheCoherentUMA; - if (adapter != nullptr) { + if (pAdapter != nullptr) { + ComPtr dxgiAdapter; + GPGMM_RETURN_IF_FAILED(pAdapter->QueryInterface(IID_PPV_ARGS(&dxgiAdapter))); + DXGI_ADAPTER_DESC adapterDesc; - GPGMM_RETURN_IF_FAILED(adapter->GetDesc(&adapterDesc)); + GPGMM_RETURN_IF_FAILED(dxgiAdapter->GetDesc(&adapterDesc)); caps->mSharedSegmentSize = adapterDesc.SharedSystemMemory; caps->mDedicatedSegmentSize = adapterDesc.DedicatedVideoMemory; @@ -117,8 +127,8 @@ namespace gpgmm::d3d12 { << "Adapter was left unspecified. Device capabilities may not be fully detected."; } - if (capsOut != nullptr) { - *capsOut = caps.release(); + if (ppCapsOut != nullptr) { + *ppCapsOut = caps.release(); } return S_OK; diff --git a/src/gpgmm/d3d12/CapsD3D12.h b/src/gpgmm/d3d12/CapsD3D12.h index 3ca4beb2..d9dcaee6 100644 --- a/src/gpgmm/d3d12/CapsD3D12.h +++ b/src/gpgmm/d3d12/CapsD3D12.h @@ -25,7 +25,7 @@ namespace gpgmm::d3d12 { class Caps { public: - static HRESULT CreateCaps(ID3D12Device* device, IDXGIAdapter* adapter, Caps** capsOut); + static HRESULT CreateCaps(ID3D12Device* pDevice, IUnknown* pAdapter, Caps** ppCapsOut); // Largest resource size that this device can make available. uint64_t GetMaxResourceSize() const; diff --git a/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp b/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp index 780e7dc2..93f1cc39 100644 --- a/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp +++ b/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp @@ -42,7 +42,7 @@ namespace gpgmm::d3d12 { HRESULT CreateResidencyManager(const RESIDENCY_MANAGER_DESC& descriptor, ID3D12Device* pDevice, - IDXGIAdapter3* pAdapter, + IUnknown* pAdapter, IResidencyManager** ppResidencyManagerOut) { return ResidencyManager::CreateResidencyManager(descriptor, pDevice, pAdapter, ppResidencyManagerOut); @@ -51,7 +51,7 @@ namespace gpgmm::d3d12 { // static HRESULT ResidencyManager::CreateResidencyManager(const RESIDENCY_MANAGER_DESC& descriptor, ID3D12Device* pDevice, - IDXGIAdapter3* pAdapter, + IUnknown* pAdapter, IResidencyManager** ppResidencyManagerOut) { GPGMM_RETURN_IF_NULL(pAdapter); GPGMM_RETURN_IF_NULL(pDevice); @@ -86,8 +86,11 @@ namespace gpgmm::d3d12 { SetLogLevel(GetMessageSeverity(descriptor.MinLogLevel)); + ComPtr dxgiAdapter3; + GPGMM_RETURN_IF_FAILED(pAdapter->QueryInterface(IID_PPV_ARGS(&dxgiAdapter3))); + std::unique_ptr residencyManager = std::unique_ptr( - new ResidencyManager(descriptor, pDevice, pAdapter, std::move(caps))); + new ResidencyManager(descriptor, pDevice, dxgiAdapter3.Get(), std::move(caps))); // Enable automatic video memory budget updates. if (descriptor.Flags & RESIDENCY_MANAGER_FLAG_ALLOW_BACKGROUND_BUDGET_UPDATES) { diff --git a/src/gpgmm/d3d12/ResidencyManagerD3D12.h b/src/gpgmm/d3d12/ResidencyManagerD3D12.h index d6ce4069..dced2d43 100644 --- a/src/gpgmm/d3d12/ResidencyManagerD3D12.h +++ b/src/gpgmm/d3d12/ResidencyManagerD3D12.h @@ -44,7 +44,7 @@ namespace gpgmm::d3d12 { public: static HRESULT CreateResidencyManager(const RESIDENCY_MANAGER_DESC& descriptor, ID3D12Device* pDevice, - IDXGIAdapter3* pAdapter, + IUnknown* pAdapter, IResidencyManager** ppResidencyManagerOut); ~ResidencyManager() override; diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index fb400b2e..591e199f 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -377,7 +377,7 @@ namespace gpgmm::d3d12 { HRESULT CreateResourceAllocator(const RESOURCE_ALLOCATOR_DESC& allocatorDescriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, IResourceAllocator** ppResourceAllocatorOut, IResidencyManager** ppResidencyManagerOut = nullptr) { return ResourceAllocator::CreateResourceAllocator( @@ -386,7 +386,7 @@ namespace gpgmm::d3d12 { HRESULT CreateResourceAllocator(const RESOURCE_ALLOCATOR_DESC& allocatorDescriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, IResidencyManager* pResidencyManager, IResourceAllocator** ppResourceAllocatorOut) { return ResourceAllocator::CreateResourceAllocator(allocatorDescriptor, pDevice, pAdapter, @@ -407,7 +407,7 @@ namespace gpgmm::d3d12 { HRESULT ResourceAllocator::CreateResourceAllocator( const RESOURCE_ALLOCATOR_DESC& allocatorDescriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, IResourceAllocator** ppResourceAllocatorOut, IResidencyManager** ppResidencyManagerOut) { GPGMM_RETURN_IF_NULL(pDevice); @@ -451,7 +451,7 @@ namespace gpgmm::d3d12 { HRESULT ResourceAllocator::CreateResourceAllocator( const RESOURCE_ALLOCATOR_DESC& allocatorDescriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, IResidencyManager* pResidencyManager, IResourceAllocator* pResourceAllocator, IResourceAllocator** ppResourceAllocatorOut) { @@ -628,7 +628,7 @@ namespace gpgmm::d3d12 { ResourceAllocator::ResourceAllocator(const RESOURCE_ALLOCATOR_DESC& descriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, ResidencyManager* pResidencyManager, std::unique_ptr caps) : mDevice(pDevice), @@ -1878,7 +1878,7 @@ namespace gpgmm::d3d12 { return mDevice; } - IDXGIAdapter* ResourceAllocator::GetAdapter() const { + IUnknown* ResourceAllocator::GetAdapter() const { return mAdapter; } diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h index 8589904a..82e14de5 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h @@ -41,13 +41,13 @@ namespace gpgmm::d3d12 { public: static HRESULT CreateResourceAllocator(const RESOURCE_ALLOCATOR_DESC& allocatorDescriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, IResourceAllocator** ppResourceAllocatorOut, IResidencyManager** ppResidencyManagerOut); static HRESULT CreateResourceAllocator(const RESOURCE_ALLOCATOR_DESC& allocatorDescriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, IResidencyManager* pResidencyManager, IResourceAllocator* pResourceAllocator, IResourceAllocator** ppResourceAllocatorOut); @@ -80,7 +80,7 @@ namespace gpgmm::d3d12 { HRESULT SetDebugName(LPCWSTR Name) override; ID3D12Device* GetDevice() const; - IDXGIAdapter* GetAdapter() const; + IUnknown* GetAdapter() const; IResidencyManager* GetResidencyManager() const; private: @@ -89,7 +89,7 @@ namespace gpgmm::d3d12 { ResourceAllocator(const RESOURCE_ALLOCATOR_DESC& descriptor, ID3D12Device* pDevice, - IDXGIAdapter* pAdapter, + IUnknown* pAdapter, ResidencyManager* pResidencyManager, std::unique_ptr caps); @@ -167,7 +167,7 @@ namespace gpgmm::d3d12 { DEFINE_OBJECT_BASE_OVERRIDES(IResourceAllocator) ID3D12Device* mDevice = nullptr; - IDXGIAdapter* mAdapter = nullptr; + IUnknown* mAdapter = nullptr; ComPtr mResidencyManager; std::unique_ptr mCaps;