Skip to content

Commit

Permalink
Refactor DXGI impl. into separate cpps.
Browse files Browse the repository at this point in the history
In the future, DXGI will be conditionally built for projects that don't require DXGI.

Issue: #683
  • Loading branch information
bbernhar committed Feb 12, 2024
1 parent abb4109 commit e9ff9b6
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 154 deletions.
4 changes: 4 additions & 0 deletions src/gpgmm/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ source_set("gpgmm_sources") {
sources += [
"d3d12/BudgetUpdateD3D12.cpp",
"d3d12/BudgetUpdateD3D12.h",
"d3d12/BudgetUpdateDXGI.cpp",
"d3d12/BudgetUpdateDXGI.h",
"d3d12/BufferAllocatorD3D12.cpp",
"d3d12/BufferAllocatorD3D12.h",
"d3d12/CapsD3D12.cpp",
Expand All @@ -136,6 +138,8 @@ source_set("gpgmm_sources") {
"d3d12/ResidencyListD3D12.h",
"d3d12/ResidencyManagerD3D12.cpp",
"d3d12/ResidencyManagerD3D12.h",
"d3d12/ResidencyManagerDXGI.cpp",
"d3d12/ResidencyManagerDXGI.h",
"d3d12/ResourceAllocationD3D12.cpp",
"d3d12/ResourceAllocationD3D12.h",
"d3d12/ResourceAllocationTrackingAllocatorD3D12.cpp",
Expand Down
4 changes: 4 additions & 0 deletions src/gpgmm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ endif()

if (GPGMM_ENABLE_D3D12)
target_sources(gpgmm PRIVATE
"d3d12/BudgetUpdateDXGI.cpp"
"d3d12/BudgetUpdateDXGI.h"
"d3d12/BudgetUpdateD3D12.cpp"
"d3d12/BudgetUpdateD3D12.h"
"d3d12/BufferAllocatorD3D12.cpp"
Expand All @@ -79,6 +81,8 @@ if (GPGMM_ENABLE_D3D12)
"d3d12/ResidencyListD3D12.h"
"d3d12/ResidencyManagerD3D12.cpp"
"d3d12/ResidencyManagerD3D12.h"
"d3d12/ResidencyManagerDXGI.cpp"
"d3d12/ResidencyManagerDXGI.h"
"d3d12/ResourceAllocationD3D12.cpp"
"d3d12/ResourceAllocationD3D12.h"
"d3d12/ResourceAllocatorD3D12.cpp"
Expand Down
71 changes: 0 additions & 71 deletions src/gpgmm/d3d12/BudgetUpdateD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

#include "gpgmm/d3d12/BudgetUpdateD3D12.h"

#include "gpgmm/common/Message.h"
#include "gpgmm/common/TraceEvent.h"
#include "gpgmm/d3d12/ErrorD3D12.h"
#include "gpgmm/d3d12/LogD3D12.h"
#include "gpgmm/d3d12/ResidencyManagerD3D12.h"

namespace gpgmm::d3d12 {

// BudgetUpdateTask
Expand All @@ -38,71 +32,6 @@ namespace gpgmm::d3d12 {
mLastError = hr;
}

// BudgetUpdateTaskDXGI

BudgetUpdateTaskDXGI::BudgetUpdateTaskDXGI(ResidencyManagerDXGI* const residencyManager)
: mResidencyManager(residencyManager),
mBudgetNotificationUpdateEvent(CreateEventW(NULL, FALSE, FALSE, NULL)),
mUnregisterAndExitEvent(CreateEventW(NULL, FALSE, FALSE, NULL)) {
ASSERT(mResidencyManager != nullptr);
mLastError =
mResidencyManager->GetAdapter()->RegisterVideoMemoryBudgetChangeNotificationEvent(
mBudgetNotificationUpdateEvent, &mCookie);
}

BudgetUpdateTaskDXGI::~BudgetUpdateTaskDXGI() {
CloseHandle(mUnregisterAndExitEvent);
CloseHandle(mBudgetNotificationUpdateEvent);
}

MaybeError BudgetUpdateTaskDXGI::operator()() {
HRESULT hr = GetLastError();
bool isExiting = false;
while (!isExiting && SUCCEEDED(hr)) {
// Wait on two events: one to unblock for OS budget changes, and another to unblock
// for shutdown.
HANDLE hWaitEvents[2] = {mBudgetNotificationUpdateEvent, mUnregisterAndExitEvent};
const DWORD waitedEvent =
WaitForMultipleObjects(2, hWaitEvents, /*bWaitAll*/ false, INFINITE);
switch (waitedEvent) {
// mBudgetNotificationUpdateEvent
case (WAIT_OBJECT_0 + 0): {
hr = mResidencyManager->UpdateMemorySegments();
if (FAILED(hr)) {
break;
}

DebugLog(MessageId::kBudgetUpdated, mResidencyManager)
<< "Updated budget from OS notification.";
break;
}
// mUnregisterAndExitEvent
case (WAIT_OBJECT_0 + 1): {
isExiting = true;
break;
}
default: {
UNREACHABLE();
break;
}
}
}

if (FAILED(hr)) {
ErrorLog(ErrorCode::kBudgetInvalid, mResidencyManager)
<< "Unable to update budget: " +
GetErrorResultMessage(hr, mResidencyManager->GetDevice());
}

SetLastError(hr);
return GetErrorCode(hr);
}

bool BudgetUpdateTaskDXGI::UnregisterAndExit() {
mResidencyManager->GetAdapter()->UnregisterVideoMemoryBudgetChangeNotification(mCookie);
return SetEvent(mUnregisterAndExitEvent);
}

// BudgetUpdateEvent

BudgetUpdateEvent::BudgetUpdateEvent(std::shared_ptr<Event> event,
Expand Down
22 changes: 0 additions & 22 deletions src/gpgmm/d3d12/BudgetUpdateD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,6 @@ namespace gpgmm::d3d12 {
HRESULT mLastError = S_OK;
};

class ResidencyManagerDXGI;

class BudgetUpdateTaskDXGI final : public BudgetUpdateTask {
public:
BudgetUpdateTaskDXGI(ResidencyManagerDXGI* const residencyManager);
~BudgetUpdateTaskDXGI() override;

// VoidCallback interface
MaybeError operator()() override;

// BudgetUpdateTask interface
bool UnregisterAndExit() override;

private:
ResidencyManagerDXGI* const mResidencyManager;

HANDLE mBudgetNotificationUpdateEvent = INVALID_HANDLE_VALUE;
HANDLE mUnregisterAndExitEvent = INVALID_HANDLE_VALUE;

DWORD mCookie = 0; // Used to unregister from notifications.
};

class BudgetUpdateEvent final : public Event {
public:
BudgetUpdateEvent(std::shared_ptr<Event> event, std::shared_ptr<BudgetUpdateTask> task);
Expand Down
87 changes: 87 additions & 0 deletions src/gpgmm/d3d12/BudgetUpdateDXGI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2024 The GPGMM Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "gpgmm/d3d12/BudgetUpdateDXGI.h"

#include "gpgmm/common/Message.h"
#include "gpgmm/d3d12/ErrorD3D12.h"
#include "gpgmm/d3d12/LogD3D12.h"
#include "gpgmm/d3d12/ResidencyManagerDXGI.h"

namespace gpgmm::d3d12 {

BudgetUpdateTaskDXGI::BudgetUpdateTaskDXGI(ResidencyManagerDXGI* const residencyManager)
: mResidencyManager(residencyManager),
mBudgetNotificationUpdateEvent(CreateEventW(NULL, FALSE, FALSE, NULL)),
mUnregisterAndExitEvent(CreateEventW(NULL, FALSE, FALSE, NULL)) {
ASSERT(mResidencyManager != nullptr);
mLastError =
mResidencyManager->GetAdapter()->RegisterVideoMemoryBudgetChangeNotificationEvent(
mBudgetNotificationUpdateEvent, &mCookie);
}

BudgetUpdateTaskDXGI::~BudgetUpdateTaskDXGI() {
CloseHandle(mUnregisterAndExitEvent);
CloseHandle(mBudgetNotificationUpdateEvent);
}

MaybeError BudgetUpdateTaskDXGI::operator()() {
HRESULT hr = GetLastError();
bool isExiting = false;
while (!isExiting && SUCCEEDED(hr)) {
// Wait on two events: one to unblock for OS budget changes, and another to unblock
// for shutdown.
HANDLE hWaitEvents[2] = {mBudgetNotificationUpdateEvent, mUnregisterAndExitEvent};
const DWORD waitedEvent =
WaitForMultipleObjects(2, hWaitEvents, /*bWaitAll*/ false, INFINITE);
switch (waitedEvent) {
// mBudgetNotificationUpdateEvent
case (WAIT_OBJECT_0 + 0): {
hr = mResidencyManager->UpdateMemorySegments();
if (FAILED(hr)) {
break;
}

DebugLog(MessageId::kBudgetUpdated, mResidencyManager)
<< "Updated budget from OS notification.";
break;
}
// mUnregisterAndExitEvent
case (WAIT_OBJECT_0 + 1): {
isExiting = true;
break;
}
default: {
UNREACHABLE();
break;
}
}
}

if (FAILED(hr)) {
ErrorLog(ErrorCode::kBudgetInvalid, mResidencyManager)
<< "Unable to update budget: " +
GetErrorResultMessage(hr, mResidencyManager->GetDevice());
}

SetLastError(hr);
return GetErrorCode(hr);
}

bool BudgetUpdateTaskDXGI::UnregisterAndExit() {
mResidencyManager->GetAdapter()->UnregisterVideoMemoryBudgetChangeNotification(mCookie);
return SetEvent(mUnregisterAndExitEvent);
}

} // namespace gpgmm::d3d12
47 changes: 47 additions & 0 deletions src/gpgmm/d3d12/BudgetUpdateDXGI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2024 The GPGMM Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SRC_GPGMM_D3D12_BUDGETUPDATEDXGI_H_
#define SRC_GPGMM_D3D12_BUDGETUPDATEDXGI_H_

#include "gpgmm/d3d12/BudgetUpdateD3D12.h"

namespace gpgmm::d3d12 {

class ResidencyManagerDXGI;

// Implements a long running task that can continuously receives DXGI budget notifications.
class BudgetUpdateTaskDXGI final : public BudgetUpdateTask {
public:
BudgetUpdateTaskDXGI(ResidencyManagerDXGI* const residencyManager);
~BudgetUpdateTaskDXGI() override;

// VoidCallback interface
MaybeError operator()() override;

// BudgetUpdateTask interface
bool UnregisterAndExit() override;

private:
ResidencyManagerDXGI* const mResidencyManager;

HANDLE mBudgetNotificationUpdateEvent = INVALID_HANDLE_VALUE;
HANDLE mUnregisterAndExitEvent = INVALID_HANDLE_VALUE;

DWORD mCookie = 0; // Used to unregister from notifications.
};

} // namespace gpgmm::d3d12

#endif // SRC_GPGMM_D3D12_BUDGETUPDATEDXGI_H_
34 changes: 1 addition & 33 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "gpgmm/d3d12/LogD3D12.h"
#include "gpgmm/d3d12/ResidencyHeapD3D12.h"
#include "gpgmm/d3d12/ResidencyListD3D12.h"
#include "gpgmm/d3d12/ResidencyManagerDXGI.h"
#include "gpgmm/d3d12/UtilsD3D12.h"
#include "gpgmm/utils/Math.h"

Expand Down Expand Up @@ -923,37 +924,4 @@ namespace gpgmm::d3d12 {
return mDevice;
}

// ResidencyManagerDXGI

ResidencyManagerDXGI::ResidencyManagerDXGI(const RESIDENCY_MANAGER_DESC& descriptor,
ID3D12Device* pDevice,
IDXGIAdapter3* pAdapter,
std::unique_ptr<Caps> caps)
: ResidencyManager(descriptor, pDevice, std::move(caps)), mAdapter(pAdapter) {
ASSERT(mAdapter != nullptr);
}

ResidencyManagerDXGI::~ResidencyManagerDXGI() = default;

HRESULT ResidencyManagerDXGI::QueryMemoryInfoImpl(const RESIDENCY_HEAP_SEGMENT& heapSegment,
RESIDENCY_MEMORY_INFO* pMemoryInfoOut) {
// Residency heap segments are 1:1 with DXGI memory segment groups.
DXGI_QUERY_VIDEO_MEMORY_INFO queryVideoMemoryInfoOut;
GPGMM_RETURN_IF_FAILED(mAdapter->QueryVideoMemoryInfo(
0, static_cast<DXGI_MEMORY_SEGMENT_GROUP>(heapSegment), &queryVideoMemoryInfoOut));
pMemoryInfoOut->AvailableForReservation = queryVideoMemoryInfoOut.AvailableForReservation;
pMemoryInfoOut->Budget = queryVideoMemoryInfoOut.Budget;
pMemoryInfoOut->CurrentReservation = queryVideoMemoryInfoOut.CurrentReservation;
pMemoryInfoOut->CurrentUsage = queryVideoMemoryInfoOut.CurrentUsage;
return S_OK;
}

std::shared_ptr<BudgetUpdateTask> ResidencyManagerDXGI::CreateBudgetUpdateTask() {
return std::make_shared<BudgetUpdateTaskDXGI>(this);
}

IDXGIAdapter3* ResidencyManagerDXGI::GetAdapter() const {
return mAdapter;
}

} // namespace gpgmm::d3d12
22 changes: 0 additions & 22 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "gpgmm/utils/EnumFlags.h"
#include "gpgmm/utils/LinkedList.h"

#include <dxgi1_4.h>
#include <gpgmm_d3d12.h>

#include <memory>
Expand Down Expand Up @@ -159,27 +158,6 @@ namespace gpgmm::d3d12 {
std::shared_ptr<BudgetUpdateEvent> mBudgetNotificationUpdateEvent;
};

class ResidencyManagerDXGI final : public ResidencyManager {
public:
ResidencyManagerDXGI(const RESIDENCY_MANAGER_DESC& descriptor,
ID3D12Device* pDevice,
IDXGIAdapter3* pAdapter,
std::unique_ptr<Caps> caps);

~ResidencyManagerDXGI() override;

IDXGIAdapter3* GetAdapter() const;

private:
// ResidencyManager overloads
HRESULT QueryMemoryInfoImpl(const RESIDENCY_HEAP_SEGMENT& heapSegment,
RESIDENCY_MEMORY_INFO* pMemoryInfoOut) override;

std::shared_ptr<BudgetUpdateTask> CreateBudgetUpdateTask() override;

IDXGIAdapter3* mAdapter = nullptr;
};

} // namespace gpgmm::d3d12

#endif // SRC_GPGMM_D3D12_RESIDENCYMANAGERD3D12_H_
Loading

0 comments on commit e9ff9b6

Please sign in to comment.