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
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/BudgetUpdateD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace gpgmm::d3d12 {
if (FAILED(hr)) {
ErrorLog(ErrorCode::kBudgetInvalid, mResidencyManager)
<< "Unable to update budget: " +
GetErrorResultWithRemovalReason(hr, mResidencyManager->mDevice);
GetErrorResultMessage(hr, mResidencyManager->mDevice);
}

SetLastError(hr);
Expand Down
15 changes: 7 additions & 8 deletions src/gpgmm/d3d12/CapsD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,15 @@ namespace gpgmm::d3d12 {

// static
HRESULT Caps::CreateCaps(ID3D12Device* device, IDXGIAdapter* adapter, Caps** capsOut) {
GPGMM_RETURN_IF_NULLPTR(device);
GPGMM_RETURN_IF_NULL(device);

std::unique_ptr<Caps> caps(new Caps());
GPGMM_RETURN_IF_FAILED(SetMaxResourceSize(device, &caps->mMaxResourceSize), device);
GPGMM_RETURN_IF_FAILED(SetMaxResourceHeapSize(device, &caps->mMaxResourceHeapSize), device);
GPGMM_RETURN_IF_FAILED(SetMaxResourceHeapTierSupported(device, &caps->mMaxResourceHeapTier),
device);
GPGMM_RETURN_IF_FAILED(SetMaxResourceSize(device, &caps->mMaxResourceSize));
GPGMM_RETURN_IF_FAILED(SetMaxResourceHeapSize(device, &caps->mMaxResourceHeapSize));
GPGMM_RETURN_IF_FAILED(
SetCreateHeapNotResidentSupported(device, &caps->mIsCreateHeapNotResidentSupported),
device);
SetMaxResourceHeapTierSupported(device, &caps->mMaxResourceHeapTier));
GPGMM_RETURN_IF_FAILED(
SetCreateHeapNotResidentSupported(device, &caps->mIsCreateHeapNotResidentSupported));

D3D12_FEATURE_DATA_ARCHITECTURE arch = {};
GPGMM_RETURN_IF_FAILED(
Expand All @@ -104,7 +103,7 @@ namespace gpgmm::d3d12 {

if (adapter != nullptr) {
DXGI_ADAPTER_DESC adapterDesc;
GPGMM_RETURN_IF_FAILED(adapter->GetDesc(&adapterDesc), device);
GPGMM_RETURN_IF_FAILED(adapter->GetDesc(&adapterDesc));

caps->mSharedSegmentSize = adapterDesc.SharedSystemMemory;
caps->mDedicatedSegmentSize = adapterDesc.DedicatedVideoMemory;
Expand Down
12 changes: 8 additions & 4 deletions src/gpgmm/d3d12/ErrorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ namespace gpgmm::d3d12 {
return ss.str();
}

std::string GetErrorResultWithRemovalReason(HRESULT error, ID3D12Device* device) {
std::string GetErrorResultMessage(HRESULT error) {
// Device must be supplied when device removal errors are possible.
ASSERT(error != DXGI_ERROR_DEVICE_REMOVED);
return GetErrorResultToString(error);
}

std::string GetErrorResultMessage(HRESULT error, ID3D12Device* device) {
if (error == DXGI_ERROR_DEVICE_REMOVED) {
if (device == nullptr) {
return "Device was not found but removed " + GetErrorResultToString(error);
}
ASSERT(device != nullptr);
return GetErrorResultToString(error) +
" with reason: " + GetErrorResultToString(device->GetDeviceRemovedReason());
}
Expand Down
41 changes: 21 additions & 20 deletions src/gpgmm/d3d12/ErrorD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@

#include <string>

#define GPGMM_RETURN_IF_NULLPTR(ptr) \
#define GPGMM_RETURN_IF_NULL(ptr) \
GPGMM_RETURN_IF_FAILED((ptr == nullptr ? E_POINTER : S_OK), nullptr)

#define GPGMM_RETURN_IF_FAILED(expr, device) \
{ \
auto GPGMM_LOCAL_VAR(HRESULT) = expr; \
if (GPGMM_UNLIKELY(FAILED(GPGMM_LOCAL_VAR(HRESULT)))) { \
gpgmm::ErrorLog(GetErrorCode(GPGMM_LOCAL_VAR(HRESULT))) \
<< #expr << ": " \
<< GetErrorResultWithRemovalReason(GPGMM_LOCAL_VAR(HRESULT), device); \
return GPGMM_LOCAL_VAR(HRESULT); \
} \
} \
for (;;) \
// For D3D12 calls that could remove device, the (optional) device ptr should be supplied as the
// last argument so the reason can be appended the result message.
#define GPGMM_RETURN_IF_FAILED(expr, ...) \
{ \
auto GPGMM_LOCAL_VAR(HRESULT) = expr; \
if (GPGMM_UNLIKELY(FAILED(GPGMM_LOCAL_VAR(HRESULT)))) { \
gpgmm::ErrorLog(GetErrorCode(GPGMM_LOCAL_VAR(HRESULT))) \
<< #expr << ": " << GetErrorResultMessage(GPGMM_LOCAL_VAR(HRESULT), __VA_ARGS__); \
return GPGMM_LOCAL_VAR(HRESULT); \
} \
} \
for (;;) \
break

#define GPGMM_RETURN_IF_SUCCEEDED(expr) \
Expand All @@ -48,22 +49,22 @@
for (;;) \
break

#define GPGMM_ASSERT_FAILED(hr) ASSERT(SUCCEEDED(hr));
#define GPGMM_ASSERT_SUCCEEDED(hr) ASSERT(FAILED(hr));

// Same as FAILED but also returns true if S_FALSE.
// S_FALSE is used to denote a result where the operation didn't do anything.
// For example, passing NULL to create an object without returning it will destroy it.
// S_FALSE is used to denote a HRESULT where the operation didn't do anything.
// For example, passing NULL to create an object without returning it.
#define GPGMM_UNSUCCESSFUL(hr) (FAILED(hr) || (hr == S_FALSE))

namespace gpgmm::d3d12 {

HRESULT GetErrorResult(ErrorCode error);
ErrorCode GetErrorCode(HRESULT error);

// Returns HRESULT error as a printable message.
// If the device is also specified and removed, a detailed message is supplied.
std::string GetErrorResultWithRemovalReason(HRESULT error, ID3D12Device* device);
// Returns non device removal HRESULT error as a printable message.
std::string GetErrorResultMessage(HRESULT error);

// Returns device removal HRESULT error as a printable message.
std::string GetErrorResultMessage(HRESULT error, ID3D12Device* device);

std::string GetErrorResultToString(HRESULT error) noexcept;

} // namespace gpgmm::d3d12
Expand Down
24 changes: 10 additions & 14 deletions src/gpgmm/d3d12/ResidencyHeapD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace gpgmm::d3d12 {
"Heap.CreateResidencyHeap",
(RESIDENCY_HEAP_CREATE_RESIDENCY_HEAP_PARAMS{descriptor, pPageable}));

GPGMM_RETURN_IF_NULLPTR(pPageable);
GPGMM_RETURN_IF_NULL(pPageable);

ResidencyManager* residencyManager = static_cast<ResidencyManager*>(pResidencyManager);
const bool isResidencyDisabled = (pResidencyManager == nullptr);
Expand Down Expand Up @@ -183,18 +183,17 @@ namespace gpgmm::d3d12 {
// descriptor heap), they must be manually locked and unlocked to be inserted into the
// residency cache.
if (heap->GetInfo().Status != RESIDENCY_HEAP_STATUS_UNKNOWN) {
GPGMM_RETURN_IF_FAILED(residencyManager->InsertHeap(heap.get()),
GetDevice(pPageable).Get());
GPGMM_RETURN_IF_FAILED(residencyManager->InsertHeap(heap.get()));
} else {
if (newDescriptor.Flags & RESIDENCY_HEAP_FLAG_CREATE_RESIDENT) {
GPGMM_RETURN_IF_FAILED(heap->Lock(), GetDevice(pPageable).Get());
GPGMM_RETURN_IF_FAILED(heap->Unlock(), GetDevice(pPageable).Get());
GPGMM_RETURN_IF_FAILED(heap->Lock());
GPGMM_RETURN_IF_FAILED(heap->Unlock());
ASSERT(heap->GetInfo().Status == RESIDENCY_HEAP_STATUS_RESIDENT);
}
}

if (descriptor.Flags & RESIDENCY_HEAP_FLAG_CREATE_LOCKED) {
GPGMM_RETURN_IF_FAILED(heap->Lock(), GetDevice(pPageable).Get());
GPGMM_RETURN_IF_FAILED(heap->Lock());
}

} else {
Expand Down Expand Up @@ -232,7 +231,7 @@ namespace gpgmm::d3d12 {
CreateHeapFn createHeapFn,
void* pCreateHeapContext,
IResidencyHeap** ppResidencyHeapOut) {
GPGMM_RETURN_IF_NULLPTR(pCreateHeapContext);
GPGMM_RETURN_IF_NULL(pCreateHeapContext);

const bool isResidencyDisabled = (pResidencyManager == nullptr);

Expand All @@ -254,10 +253,8 @@ namespace gpgmm::d3d12 {
// Ensure enough budget exists before creating the heap to avoid an out-of-memory error.
if (!isResidencyDisabled && (descriptor.Flags & RESIDENCY_HEAP_FLAG_CREATE_IN_BUDGET)) {
uint64_t bytesEvicted = descriptor.SizeInBytes;
GPGMM_RETURN_IF_FAILED(
residencyManager->EvictInternal(descriptor.SizeInBytes, descriptor.HeapSegment,
&bytesEvicted),
residencyManager->mDevice);
GPGMM_RETURN_IF_FAILED(residencyManager->EvictInternal(
descriptor.SizeInBytes, descriptor.HeapSegment, &bytesEvicted));

if (bytesEvicted < descriptor.SizeInBytes) {
DXGI_QUERY_VIDEO_MEMORY_INFO currentVideoInfo = {};
Expand All @@ -277,8 +274,7 @@ namespace gpgmm::d3d12 {
}

ComPtr<ID3D12Pageable> pageable;
GPGMM_RETURN_IF_FAILED(createHeapFn(pCreateHeapContext, &pageable),
GetDevice(pageable.Get()).Get());
GPGMM_RETURN_IF_FAILED(createHeapFn(pCreateHeapContext, &pageable));

return CreateResidencyHeap(descriptor, pResidencyManager, pageable.Get(),
ppResidencyHeapOut);
Expand Down Expand Up @@ -397,7 +393,7 @@ namespace gpgmm::d3d12 {

HRESULT ResidencyHeap::GetResidencyManager(IResidencyManager** ppResidencyManagerOut) const {
ComPtr<IResidencyManager> residencyManager(mResidencyManager.Get());
GPGMM_RETURN_IF_NULLPTR(residencyManager.Get());
GPGMM_RETURN_IF_NULL(residencyManager.Get());
if (ppResidencyManagerOut != nullptr) {
*ppResidencyManagerOut = residencyManager.Detach();
} else {
Expand Down
Loading