Skip to content

Commit

Permalink
Eliminate DXGI adapter dependency
Browse files Browse the repository at this point in the history
Allows specifying another adapter type.

Issue: #683
  • Loading branch information
bbernhar committed Jan 31, 2024
1 parent 7ef8366 commit be80b83
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 46 deletions.
6 changes: 3 additions & 3 deletions include/gpgmm_d3d12.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
64 changes: 37 additions & 27 deletions src/gpgmm/d3d12/CapsD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>()) {
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<uint64_t>()) {
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> 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<IDXGIAdapter> 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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/CapsD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -86,8 +86,11 @@ namespace gpgmm::d3d12 {

SetLogLevel(GetMessageSeverity(descriptor.MinLogLevel));

ComPtr<IDXGIAdapter3> dxgiAdapter3;
GPGMM_RETURN_IF_FAILED(pAdapter->QueryInterface(IID_PPV_ARGS(&dxgiAdapter3)));

std::unique_ptr<ResidencyManager> residencyManager = std::unique_ptr<ResidencyManager>(
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/ResidencyManagerD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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> caps)
: mDevice(pDevice),
Expand Down Expand Up @@ -1878,7 +1878,7 @@ namespace gpgmm::d3d12 {
return mDevice;
}

IDXGIAdapter* ResourceAllocator::GetAdapter() const {
IUnknown* ResourceAllocator::GetAdapter() const {
return mAdapter;
}

Expand Down
10 changes: 5 additions & 5 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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> caps);

Expand Down Expand Up @@ -167,7 +167,7 @@ namespace gpgmm::d3d12 {
DEFINE_OBJECT_BASE_OVERRIDES(IResourceAllocator)

ID3D12Device* mDevice = nullptr;
IDXGIAdapter* mAdapter = nullptr;
IUnknown* mAdapter = nullptr;
ComPtr<ResidencyManager> mResidencyManager;

std::unique_ptr<Caps> mCaps;
Expand Down

0 comments on commit be80b83

Please sign in to comment.