Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 123 additions & 123 deletions .github/workflows/.patches/dawn.diff

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ First create an allocator then create allocations from it:
```cpp
#include <gpgmm_d3d12.h>

// Required
gpgmm::d3d12::ALLOCATOR_DESC allocatorDesc = {};
allocatorDesc.Device = Device;
allocatorDesc.Adapter = Adapter;

ComPtr<gpgmm::d3d12::IResidencyManager> residency; // Optional
ComPtr<gpgmm::d3d12::IResourceAllocator> allocator;
gpgmm::d3d12::CreateResourceAllocator(desc, &allocator, &residency);
gpgmm::d3d12::CreateResourceAllocator(desc, device, adapter, &allocator, &residency);
```

```cpp
Expand Down
47 changes: 18 additions & 29 deletions include/gpgmm_d3d12.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,6 @@ namespace gpgmm::d3d12 {
Specify parameters when creating a residency manager.
*/
struct RESIDENCY_DESC {
/** \brief Specifies the device used by this residency manager.
Required parameter. Use CreateDevice get the device.
*/
ID3D12Device* Device;

/** \brief Specifies the adapter used by this residency manager.

Requires DXGI 1.4 due to IDXGIAdapter3::QueryVideoMemoryInfo.

Required parameter. Use EnumAdapters to get the adapter.
*/
IDXGIAdapter3* Adapter;

/** \brief Specifies residency options.

Optional parameter. By default, no flags are specified or RESIDENCY_FLAG_NONE.
Expand Down Expand Up @@ -536,12 +523,18 @@ namespace gpgmm::d3d12 {

@param descriptor A reference to RESIDENCY_DESC structure that describes the residency
manager.
@param pDevice device used by this allocator. Required parameter. Use CreateDevice get the
device.
@param pAdapter DXGI adapter used to create the device. Requires DXGI 1.4 due to
IDXGIAdapter3::QueryVideoMemoryInfo. Use EnumAdapters to get the adapter.
@param[out] ppResidencyManagerOut Pointer to a memory block that receives a pointer to the
residency manager. Pass NULL to test if residency Manager creation would succeed, but not
actually create the residency Manager. If NULL is passed and residency manager creating
would succeed, S_FALSE is returned.
*/
GPGMM_EXPORT HRESULT CreateResidencyManager(const RESIDENCY_DESC& descriptor,
ID3D12Device* pDevice,
IDXGIAdapter3* pAdapter,
IResidencyManager** ppResidencyManagerOut);

/** \struct RESOURCE_ALLOCATION_INFO
Expand Down Expand Up @@ -784,22 +777,6 @@ namespace gpgmm::d3d12 {
Specify parameters for creating allocators.
*/
struct ALLOCATOR_DESC {
/** \brief Specifies the device used by this allocator.

Required parameter. Use CreateDevice get the device.
*/
ID3D12Device* Device;

/** \brief Specifies the adapter used by this allocator.

The adapter is used to detect for additional device capabilities (by GPU vendor).
If the adapter is left unspecified, the capabiltities will not be detected and disabled by
CheckFeatureSupport.

Optional parameter. Use EnumAdapters to get the adapter.
*/
IDXGIAdapter* Adapter;

/** \brief Specifies allocator options.

For example, whether the allocator can reuse memory, or resources should be resident upon
Expand Down Expand Up @@ -1220,6 +1197,10 @@ namespace gpgmm::d3d12 {

@param allocatorDescriptor A reference to ALLOCATOR_DESC structure that describes the
allocator.
@param pDevice device used by this allocator. Required parameter. Use CreateDevice get the
device.
@param pAdapter DXGI adapter used to create the device. Used to detect for additional device
capabilities (by GPU vendor). Optional parameter. Use EnumAdapters to get the adapter.
@param[out] ppResourceAllocatorOut Pointer to a memory block that receives a pointer to the
resource allocator. Pass NULL to test if allocator creation would succeed, but not actually
create the allocator.
Expand All @@ -1228,20 +1209,28 @@ namespace gpgmm::d3d12 {
residency.
*/
GPGMM_EXPORT HRESULT CreateResourceAllocator(const ALLOCATOR_DESC& allocatorDescriptor,
ID3D12Device* pDevice,
IDXGIAdapter* pAdapter,
IResourceAllocator** ppResourceAllocatorOut,
IResidencyManager** ppResidencyManagerOut);

/** \brief Create a resource allocator using a specified residency manager.

@param allocatorDescriptor A reference to ALLOCATOR_DESC structure that describes the
allocator.
@param pDevice device used by this allocator. Required parameter. Use CreateDevice get the
device.
@param pAdapter DXGI adapter used to create the device. Used to detect for additional device
capabilities (by GPU vendor). Optional parameter. Use EnumAdapters to get the adapter.
@param pResidencyManager Pointer to a memory block that receives a pointer to the
residency manager.
@param[out] ppResourceAllocatorOut Pointer to a memory block that receives a pointer to the
resource allocator. Pass NULL to test if allocator creation would succeed, but not actually
create the allocator.
*/
GPGMM_EXPORT HRESULT CreateResourceAllocator(const ALLOCATOR_DESC& allocatorDescriptor,
ID3D12Device* pDevice,
IDXGIAdapter* pAdapter,
IResidencyManager* pResidencyManager,
IResourceAllocator** ppResourceAllocatorOut);

Expand Down
6 changes: 1 addition & 5 deletions src/fuzzers/D3D12Fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ HRESULT CreateResourceAllocatorDesc(gpgmm::d3d12::ALLOCATOR_DESC* pAllocatorDesc
return E_FAIL;
}

allocatorDescOut.Device = *ppDeviceOut;

// Populate the adapter
LUID adapterLUID = allocatorDescOut.Device->GetAdapterLuid();
LUID adapterLUID = (*ppDeviceOut)->GetAdapterLuid();
ComPtr<IDXGIFactory1> dxgiFactory;
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)))) {
return E_FAIL;
Expand All @@ -48,8 +46,6 @@ HRESULT CreateResourceAllocatorDesc(gpgmm::d3d12::ALLOCATOR_DESC* pAllocatorDesc
return E_FAIL;
}

allocatorDescOut.Adapter = *ppAdapterOut;

// Configure options
allocatorDescOut.MinLogLevel = D3D12_MESSAGE_SEVERITY_MESSAGE;

Expand Down
18 changes: 8 additions & 10 deletions src/fuzzers/D3D12ResidencyManagerFuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,36 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {

allocatorDesc.Flags |= gpgmm::d3d12::ALLOCATOR_FLAG_ALWAYS_IN_BUDGET;

gpgmm::d3d12::RESIDENCY_DESC residencyDesc = {};

ComPtr<IDXGIAdapter3> adapter3;
if (FAILED(allocatorDesc.Adapter->QueryInterface(IID_PPV_ARGS(&adapter3)))) {
if (FAILED(gAdapter->QueryInterface(IID_PPV_ARGS(&adapter3)))) {
return 0;
}

residencyDesc.Adapter = adapter3.Get();
residencyDesc.Device = allocatorDesc.Device;
gpgmm::d3d12::RESIDENCY_DESC residencyDesc = {};
residencyDesc.MinLogLevel = D3D12_MESSAGE_SEVERITY_MESSAGE;

// Create ResidencyManager
if (FAILED(gpgmm::d3d12::CreateResidencyManager(residencyDesc, &gResidencyManager))) {
if (FAILED(gpgmm::d3d12::CreateResidencyManager(residencyDesc, gDevice.Get(), adapter3.Get(),
&gResidencyManager))) {
return 0;
}

if (FAILED(gpgmm::d3d12::CreateResourceAllocator(allocatorDesc, gResidencyManager.Get(),
if (FAILED(gpgmm::d3d12::CreateResourceAllocator(allocatorDesc, gDevice.Get(), gAdapter.Get(),
gResidencyManager.Get(),
&gResourceAllocator))) {
return 0;
}

D3D12_FEATURE_DATA_ARCHITECTURE arch = {};
if (FAILED(residencyDesc.Device->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE, &arch,
sizeof(arch)))) {
if (FAILED(gDevice->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE, &arch, sizeof(arch)))) {
return 0;
}

gpgmm::d3d12::ALLOCATION_DESC allocationDesc = {};
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;

D3D12_HEAP_PROPERTIES heapProperties =
residencyDesc.Device->GetCustomHeapProperties(0, allocationDesc.HeapType);
gDevice->GetCustomHeapProperties(0, allocationDesc.HeapType);

const DXGI_MEMORY_SEGMENT_GROUP bufferMemorySegment =
gpgmm::d3d12::GetMemorySegmentGroup(heapProperties.MemoryPoolPreference, arch.UMA);
Expand Down
4 changes: 2 additions & 2 deletions src/fuzzers/D3D12ResourceAllocatorFuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
return 0;
}

if (FAILED(
gpgmm::d3d12::CreateResourceAllocator(allocatorDesc, &gResourceAllocator, nullptr))) {
if (FAILED(gpgmm::d3d12::CreateResourceAllocator(allocatorDesc, gDevice.Get(), gAdapter.Get(),
&gResourceAllocator, nullptr))) {
return 0;
}

Expand Down
26 changes: 17 additions & 9 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,25 @@ namespace gpgmm::d3d12 {
};

HRESULT CreateResidencyManager(const RESIDENCY_DESC& descriptor,
ID3D12Device* pDevice,
IDXGIAdapter3* pAdapter,
IResidencyManager** ppResidencyManagerOut) {
return ResidencyManager::CreateResidencyManager(descriptor, ppResidencyManagerOut);
return ResidencyManager::CreateResidencyManager(descriptor, pDevice, pAdapter,
ppResidencyManagerOut);
}

// static
HRESULT ResidencyManager::CreateResidencyManager(const RESIDENCY_DESC& descriptor,
ID3D12Device* pDevice,
IDXGIAdapter3* pAdapter,
IResidencyManager** ppResidencyManagerOut) {
ReturnIfNullptr(descriptor.Adapter);
ReturnIfNullptr(descriptor.Device);
ReturnIfNullptr(pAdapter);
ReturnIfNullptr(pDevice);

std::unique_ptr<Caps> caps;
{
Caps* ptr = nullptr;
ReturnIfFailed(Caps::CreateCaps(descriptor.Device, descriptor.Adapter, &ptr));
ReturnIfFailed(Caps::CreateCaps(pDevice, pAdapter, &ptr));
caps.reset(ptr);
}

Expand All @@ -200,8 +205,8 @@ namespace gpgmm::d3d12 {

SetLogLevel(GetMessageSeverity(descriptor.MinLogLevel));

std::unique_ptr<ResidencyManager> residencyManager =
std::unique_ptr<ResidencyManager>(new ResidencyManager(descriptor, std::move(caps)));
std::unique_ptr<ResidencyManager> residencyManager = std::unique_ptr<ResidencyManager>(
new ResidencyManager(descriptor, pDevice, pAdapter, std::move(caps)));

// Require automatic video memory budget updates.
if (!(descriptor.Flags & RESIDENCY_FLAG_NEVER_UPDATE_BUDGET_ON_WORKER_THREAD)) {
Expand Down Expand Up @@ -264,9 +269,12 @@ namespace gpgmm::d3d12 {
return S_OK;
}

ResidencyManager::ResidencyManager(const RESIDENCY_DESC& descriptor, std::unique_ptr<Caps> caps)
: mDevice(descriptor.Device),
mAdapter(descriptor.Adapter),
ResidencyManager::ResidencyManager(const RESIDENCY_DESC& descriptor,
ID3D12Device* pDevice,
IDXGIAdapter3* pAdapter,
std::unique_ptr<Caps> caps)
: mDevice(pDevice),
mAdapter(pAdapter),
mMaxPctOfVideoMemoryToBudget(descriptor.MaxPctOfVideoMemoryToBudget == 0
? kDefaultMaxPctOfVideoMemoryToBudget
: descriptor.MaxPctOfVideoMemoryToBudget),
Expand Down
7 changes: 6 additions & 1 deletion src/gpgmm/d3d12/ResidencyManagerD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace gpgmm::d3d12 {
class ResidencyManager final : public DebugObject, public IResidencyManager, public ObjectBase {
public:
static HRESULT CreateResidencyManager(const RESIDENCY_DESC& descriptor,
ID3D12Device* pDevice,
IDXGIAdapter3* pAdapter,
IResidencyManager** ppResidencyManagerOut);

~ResidencyManager() override;
Expand Down Expand Up @@ -72,7 +74,10 @@ namespace gpgmm::d3d12 {
friend ResourceAllocator;
friend ResourceHeapAllocator;

ResidencyManager(const RESIDENCY_DESC& descriptor, std::unique_ptr<Caps> caps);
ResidencyManager(const RESIDENCY_DESC& descriptor,
ID3D12Device* pDevice,
IDXGIAdapter3* pAdapter,
std::unique_ptr<Caps> caps);

HRESULT EnsureInBudget(uint64_t bytesToEvict,
const DXGI_MEMORY_SEGMENT_GROUP& memorySegmentGroup);
Expand Down
45 changes: 26 additions & 19 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,49 +405,53 @@ namespace gpgmm::d3d12 {
} // namespace

HRESULT CreateResourceAllocator(const ALLOCATOR_DESC& allocatorDescriptor,
ID3D12Device* pDevice,
IDXGIAdapter* pAdapter,
IResourceAllocator** ppResourceAllocatorOut,
IResidencyManager** ppResidencyManagerOut = nullptr) {
return ResourceAllocator::CreateResourceAllocator(
allocatorDescriptor, ppResourceAllocatorOut, ppResidencyManagerOut);
allocatorDescriptor, pDevice, pAdapter, ppResourceAllocatorOut, ppResidencyManagerOut);
}

HRESULT CreateResourceAllocator(const ALLOCATOR_DESC& allocatorDescriptor,
ID3D12Device* pDevice,
IDXGIAdapter* pAdapter,
IResidencyManager* pResidencyManager,
IResourceAllocator** ppResourceAllocatorOut) {
return ResourceAllocator::CreateResourceAllocator(allocatorDescriptor, pResidencyManager,
ppResourceAllocatorOut);
return ResourceAllocator::CreateResourceAllocator(
allocatorDescriptor, pDevice, pAdapter, pResidencyManager, ppResourceAllocatorOut);
}

// static
HRESULT ResourceAllocator::CreateResourceAllocator(const ALLOCATOR_DESC& allocatorDescriptor,
ID3D12Device* pDevice,
IDXGIAdapter* pAdapter,
IResourceAllocator** ppResourceAllocatorOut,
IResidencyManager** ppResidencyManagerOut) {
ReturnIfNullptr(allocatorDescriptor.Device);
ReturnIfNullptr(pDevice);

ComPtr<IResidencyManager> residencyManager;
if (ppResidencyManagerOut != nullptr) {
RESIDENCY_DESC residencyDesc = {};
residencyDesc.Device = allocatorDescriptor.Device;

if (allocatorDescriptor.Adapter != nullptr) {
ReturnIfFailed(allocatorDescriptor.Adapter->QueryInterface(
IID_PPV_ARGS(&residencyDesc.Adapter)));
ComPtr<IDXGIAdapter3> adapter3;
if (pAdapter != nullptr) {
ReturnIfFailed(pAdapter->QueryInterface(IID_PPV_ARGS(&adapter3)));
}

RESIDENCY_DESC residencyDesc = {};
residencyDesc.MinLogLevel = allocatorDescriptor.MinLogLevel;
residencyDesc.RecordOptions = allocatorDescriptor.RecordOptions;

if (allocatorDescriptor.Flags & ALLOCATOR_FLAG_ALWAYS_IN_BUDGET) {
residencyDesc.Flags |= RESIDENCY_FLAG_ALWAYS_IN_BUDGET;
}

ReturnIfFailed(
ResidencyManager::CreateResidencyManager(residencyDesc, &residencyManager));
ReturnIfFailed(ResidencyManager::CreateResidencyManager(
residencyDesc, pDevice, adapter3.Get(), &residencyManager));
}

ComPtr<IResourceAllocator> resourceAllocator;
ReturnIfFailed(CreateResourceAllocator(allocatorDescriptor, residencyManager.Get(),
&resourceAllocator));
ReturnIfFailed(CreateResourceAllocator(allocatorDescriptor, pDevice, pAdapter,
residencyManager.Get(), &resourceAllocator));

if (ppResourceAllocatorOut != nullptr) {
*ppResourceAllocatorOut = resourceAllocator.Detach();
Expand All @@ -463,15 +467,16 @@ namespace gpgmm::d3d12 {
// static
HRESULT ResourceAllocator::CreateResourceAllocator(
const ALLOCATOR_DESC& allocatorDescriptor,
ID3D12Device* pDevice,
IDXGIAdapter* pAdapter,
IResidencyManager* pResidencyManager,
IResourceAllocator** ppResourceAllocatorOut) {
ReturnIfNullptr(allocatorDescriptor.Device);
ReturnIfNullptr(pDevice);

std::unique_ptr<Caps> caps;
{
Caps* ptr = nullptr;
ReturnIfFailed(
Caps::CreateCaps(allocatorDescriptor.Device, allocatorDescriptor.Adapter, &ptr));
ReturnIfFailed(Caps::CreateCaps(pDevice, pAdapter, &ptr));
caps.reset(ptr);
}

Expand Down Expand Up @@ -585,7 +590,8 @@ namespace gpgmm::d3d12 {

std::unique_ptr<ResourceAllocator> resourceAllocator =
std::unique_ptr<ResourceAllocator>(new ResourceAllocator(
newDescriptor, static_cast<ResidencyManager*>(pResidencyManager), std::move(caps)));
newDescriptor, pDevice, static_cast<ResidencyManager*>(pResidencyManager),
std::move(caps)));

GPGMM_TRACE_EVENT_OBJECT_SNAPSHOT(resourceAllocator.get(), newDescriptor);

Expand All @@ -597,9 +603,10 @@ namespace gpgmm::d3d12 {
}

ResourceAllocator::ResourceAllocator(const ALLOCATOR_DESC& descriptor,
ID3D12Device* pDevice,
ResidencyManager* pResidencyManager,
std::unique_ptr<Caps> caps)
: mDevice(std::move(descriptor.Device)),
: mDevice(pDevice),
mResidencyManager(pResidencyManager),
mCaps(std::move(caps)),
mResourceHeapTier(descriptor.ResourceHeapTier),
Expand Down
Loading