Skip to content

Commit

Permalink
Use RESIDENCY_MEMORY_INFO instead of DXGI type
Browse files Browse the repository at this point in the history
Issue: #683
  • Loading branch information
bbernhar committed Feb 1, 2024
1 parent 70fdedc commit 209e366
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
25 changes: 24 additions & 1 deletion include/gpgmm_d3d12.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,29 @@ namespace gpgmm::d3d12 {
RESOURCE_ALLOCATION_TYPE_SUBALLOCATED_WITHIN = 3,
};

/** \struct RESIDENCY_MEMORY_INFO
Describes the current memory budget.
*/
struct RESIDENCY_MEMORY_INFO {
/** \brief OS-provided memory budget, in bytes, that the application should target.
*/
UINT64 Budget;

/** \brief Application's current memory usage, in bytes.
*/
UINT64 CurrentUsage;

/** \brief Amount of memory, in bytes, that the application has available for reservation.
To reserve memory, the application should call IResidencyManager::SetVideoMemoryReservation.
*/
UINT64 AvailableForReservation;

/** \brief The amount of memory, in bytes, reserved by the application.
*/
UINT64 CurrentReservation;
};

/** \brief ResidencyManager tracks and maintains one or more heaps within a residency cache.
A heap is considered "resident" when it is accessible by the GPU. A heap can be made explicitly
Expand Down Expand Up @@ -639,7 +662,7 @@ namespace gpgmm::d3d12 {
of nullptr will update but not return the current info.
*/
virtual HRESULT QueryVideoMemoryInfo(const RESIDENCY_HEAP_SEGMENT& heapSegment,
DXGI_QUERY_VIDEO_MEMORY_INFO* pVideoMemoryInfoOut) = 0;
RESIDENCY_MEMORY_INFO* pVideoMemoryInfoOut) = 0;

/** \brief Update the residency status of a heap.
Expand Down
2 changes: 1 addition & 1 deletion src/fuzzers/D3D12ResidencyManagerFuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace {
if (residencyManager == nullptr) {
return 0;
}
DXGI_QUERY_VIDEO_MEMORY_INFO segment = {};
gpgmm::d3d12::RESIDENCY_MEMORY_INFO segment = {};
gResidencyManager->QueryVideoMemoryInfo(heapSegment, &segment);
return (segment.Budget > segment.CurrentUsage) ? (segment.Budget - segment.CurrentUsage)
: 0;
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/ResidencyHeapD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ namespace gpgmm::d3d12 {
descriptor.SizeInBytes, descriptor.HeapSegment, &bytesEvicted));

if (bytesEvicted < descriptor.SizeInBytes) {
DXGI_QUERY_VIDEO_MEMORY_INFO currentVideoInfo = {};
RESIDENCY_MEMORY_INFO currentVideoInfo = {};
if (SUCCEEDED(residencyManager->QueryVideoMemoryInfo(descriptor.HeapSegment,
&currentVideoInfo))) {
ErrorLog(ErrorCode::kSizeExceeded)
Expand Down
19 changes: 9 additions & 10 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ namespace gpgmm::d3d12 {
// Set the initial video memory limits.
GPGMM_RETURN_IF_FAILED(residencyManager->UpdateMemorySegments());

DXGI_QUERY_VIDEO_MEMORY_INFO* localVideoMemorySegmentInfo =
RESIDENCY_MEMORY_INFO* localVideoMemorySegmentInfo =
residencyManager->GetVideoMemoryInfo(RESIDENCY_HEAP_SEGMENT_LOCAL);

DXGI_QUERY_VIDEO_MEMORY_INFO* nonLocalVideoMemorySegmentInfo =
RESIDENCY_MEMORY_INFO* nonLocalVideoMemorySegmentInfo =
residencyManager->GetVideoMemoryInfo(RESIDENCY_HEAP_SEGMENT_NON_LOCAL);

// D3D12 has non-zero memory usage even before any resources have been created, and this
Expand Down Expand Up @@ -300,7 +300,7 @@ namespace gpgmm::d3d12 {
return S_OK;
}

DXGI_QUERY_VIDEO_MEMORY_INFO* ResidencyManager::GetVideoMemoryInfo(
RESIDENCY_MEMORY_INFO* ResidencyManager::GetVideoMemoryInfo(
const RESIDENCY_HEAP_SEGMENT& heapSegment) {
switch (heapSegment) {
case RESIDENCY_HEAP_SEGMENT_LOCAL:
Expand Down Expand Up @@ -337,7 +337,7 @@ namespace gpgmm::d3d12 {

std::lock_guard<std::mutex> lock(mMutex);

DXGI_QUERY_VIDEO_MEMORY_INFO* videoMemorySegmentInfo = GetVideoMemoryInfo(heapSegment);
RESIDENCY_MEMORY_INFO* videoMemorySegmentInfo = GetVideoMemoryInfo(heapSegment);

videoMemorySegmentInfo->AvailableForReservation = availableForReservation;

Expand Down Expand Up @@ -370,7 +370,7 @@ namespace gpgmm::d3d12 {
// system, and may be lower than expected in certain scenarios. Under memory pressure, we
// cap the external reservation to half the available budget, which prevents the external
// component from consuming a disproportionate share of memory and ensures forward progress.
DXGI_QUERY_VIDEO_MEMORY_INFO* pVideoMemoryInfo = GetVideoMemoryInfo(heapSegment);
RESIDENCY_MEMORY_INFO* pVideoMemoryInfo = GetVideoMemoryInfo(heapSegment);

pVideoMemoryInfo->CurrentReservation = std::min(
static_cast<uint64_t>(queryVideoMemoryInfoOut.Budget * mMinPctOfBudgetToReserve),
Expand Down Expand Up @@ -476,9 +476,8 @@ namespace gpgmm::d3d12 {
return S_OK;
}

HRESULT ResidencyManager::QueryVideoMemoryInfo(
const RESIDENCY_HEAP_SEGMENT& heapSegment,
DXGI_QUERY_VIDEO_MEMORY_INFO* pVideoMemoryInfoOut) {
HRESULT ResidencyManager::QueryVideoMemoryInfo(const RESIDENCY_HEAP_SEGMENT& heapSegment,
RESIDENCY_MEMORY_INFO* pVideoMemoryInfoOut) {
std::lock_guard<std::mutex> lock(mMutex);
if (IsBudgetNotificationUpdatesDisabled()) {
GPGMM_RETURN_IF_FAILED(UpdateMemorySegmentInternal(heapSegment));
Expand All @@ -498,7 +497,7 @@ namespace gpgmm::d3d12 {
uint64_t* bytesEvictedOut) {
GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "ResidencyManager.Evict");

DXGI_QUERY_VIDEO_MEMORY_INFO* pVideoMemoryInfo = GetVideoMemoryInfo(heapSegment);
RESIDENCY_MEMORY_INFO* pVideoMemoryInfo = GetVideoMemoryInfo(heapSegment);
if (IsBudgetNotificationUpdatesDisabled()) {
GPGMM_RETURN_IF_FAILED(UpdateMemorySegmentInternal(heapSegment));
}
Expand Down Expand Up @@ -862,7 +861,7 @@ namespace gpgmm::d3d12 {
}

void ResidencyManager::ReportSegmentInfoForTesting(RESIDENCY_HEAP_SEGMENT segmentGroup) {
DXGI_QUERY_VIDEO_MEMORY_INFO* info = GetVideoMemoryInfo(segmentGroup);
RESIDENCY_MEMORY_INFO* info = GetVideoMemoryInfo(segmentGroup);
ASSERT(info != nullptr);

DebugLog(MessageId::kBudgetUpdated, this)
Expand Down
6 changes: 3 additions & 3 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace gpgmm::d3d12 {
uint64_t* pCurrentReservationOut = nullptr) override;

HRESULT QueryVideoMemoryInfo(const RESIDENCY_HEAP_SEGMENT& heapSegment,
DXGI_QUERY_VIDEO_MEMORY_INFO* pVideoMemoryInfoOut) override;
RESIDENCY_MEMORY_INFO* pVideoMemoryInfoOut) override;
HRESULT SetResidencyStatus(IResidencyHeap* pHeap,
const RESIDENCY_HEAP_STATUS& newStatus) override;

Expand Down Expand Up @@ -110,7 +110,7 @@ namespace gpgmm::d3d12 {

struct VideoMemorySegment {
LRUCache cache = {};
DXGI_QUERY_VIDEO_MEMORY_INFO Info = {};
RESIDENCY_MEMORY_INFO Info = {};
};

HRESULT MakeResident(const RESIDENCY_HEAP_SEGMENT heapSegment,
Expand All @@ -120,7 +120,7 @@ namespace gpgmm::d3d12 {

LRUCache* GetVideoMemorySegmentCache(const RESIDENCY_HEAP_SEGMENT& heapSegment);

DXGI_QUERY_VIDEO_MEMORY_INFO* GetVideoMemoryInfo(const RESIDENCY_HEAP_SEGMENT& heapSegment);
RESIDENCY_MEMORY_INFO* GetVideoMemoryInfo(const RESIDENCY_HEAP_SEGMENT& heapSegment);

HRESULT UpdateMemorySegmentInternal(const RESIDENCY_HEAP_SEGMENT& heapSegment);

Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ namespace gpgmm::d3d12 {
// checks this amount to determine if its appropriate to pre-allocate more memory or
// not.
if (IsResidencyEnabled() && !IsCreateHeapNotResidentEnabled()) {
DXGI_QUERY_VIDEO_MEMORY_INFO* currentVideoInfo =
RESIDENCY_MEMORY_INFO* currentVideoInfo =
mResidencyManager->GetVideoMemoryInfo(heapSegment);

// If over-budget, only free memory is considered available.
Expand Down
2 changes: 1 addition & 1 deletion src/tests/end2end/D3D12ResidencyManagerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class D3D12ResidencyManagerTests : public D3D12TestBase, public ::testing::Test

uint64_t GetBudgetLeft(IResidencyManager* residencyManager,
const RESIDENCY_HEAP_SEGMENT& heapSegment) {
DXGI_QUERY_VIDEO_MEMORY_INFO segment = {};
RESIDENCY_MEMORY_INFO segment = {};
residencyManager->QueryVideoMemoryInfo(heapSegment, &segment);
return (segment.Budget > segment.CurrentUsage) ? (segment.Budget - segment.CurrentUsage)
: 0;
Expand Down

0 comments on commit 209e366

Please sign in to comment.