Skip to content

Commit

Permalink
Prefer ToBackend over static_cast for base types. (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbernhar authored Jan 9, 2024
1 parent 1a421f4 commit 1dc9f23
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
16 changes: 14 additions & 2 deletions src/gpgmm/common/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace gpgmm {

// Forward declare common types.
class MemoryAllocationBase;
class MemoryAllocatorBase;
class MemoryBase;

template <typename CommonType, typename BackendTraits>
struct CommonTraits;
Expand All @@ -29,17 +31,27 @@ namespace gpgmm {
using CommonType = typename BackendTraits::AllocationType;
};

template <typename BackendTraits>
struct CommonTraits<MemoryAllocatorBase, BackendTraits> {
using CommonType = typename BackendTraits::AllocatorType;
};

template <typename BackendTraits>
struct CommonTraits<MemoryBase, BackendTraits> {
using CommonType = typename BackendTraits::MemoryType;
};

// Convert common to backend type.

template <typename BackendTraits, typename CommonT>
typename CommonTraits<CommonT, BackendTraits>::CommonType* ToBackend(CommonT* common) {
return reinterpret_cast<typename CommonTraits<CommonT, BackendTraits>::CommonType*>(common);
return static_cast<typename CommonTraits<CommonT, BackendTraits>::CommonType*>(common);
}

template <typename BackendTraits, typename CommonT>
const typename CommonTraits<CommonT, BackendTraits>::CommonType* ToBackend(
const CommonT* common) {
return reinterpret_cast<const typename CommonTraits<CommonT, BackendTraits>::CommonType*>(
return static_cast<const typename CommonTraits<CommonT, BackendTraits>::CommonType*>(
common);
}

Expand Down
4 changes: 4 additions & 0 deletions src/gpgmm/d3d12/BackendD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@

namespace gpgmm::d3d12 {

class ResidencyHeap;
class ResourceAllocation;
class ResourceAllocator;

struct BackendTraits {
using AllocationType = ResourceAllocation;
using AllocatorType = ResourceAllocator;
using MemoryType = ResidencyHeap;
};

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/BufferAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace gpgmm::d3d12 {

// Optimized clear is not supported for buffers.
ComPtr<ResidencyHeap> resourceHeap;
HRESULT hr = static_cast<ResourceAllocator*>(GetNextInChain())
HRESULT hr = ToBackend(GetNextInChain())
->CreateCommittedResource(
mHeapProperties, mHeapFlags, info, &bufferDescriptor,
/*pOptimizedClearValue*/ nullptr, mInitialBufferState, &resourceHeap);
Expand Down
10 changes: 3 additions & 7 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1371,9 +1371,7 @@ namespace gpgmm::d3d12 {
allocationDesc.Type = RESOURCE_ALLOCATION_TYPE_SUBALLOCATED_WITHIN;
allocationDesc.OffsetFromResource = subAllocation.GetOffset();

ResidencyHeap* resourceHeap =
static_cast<ResidencyHeap*>(subAllocation.GetMemory());

ResidencyHeap* resourceHeap = ToBackend(subAllocation.GetMemory());
GPGMM_RETURN_IF_FAILED(ResourceAllocation::CreateResourceAllocation(
allocationDesc, /*resourceAllocator*/ this, subAllocation.GetAllocator(),
resourceHeap, subAllocation.GetBlock(), nullptr, ppResourceAllocationOut));
Expand All @@ -1399,8 +1397,7 @@ namespace gpgmm::d3d12 {
// Each allocation maps to a disjoint (physical) address range so no physical
// memory is can be aliased or will overlap.
ComPtr<ID3D12Resource> placedResource;
ResidencyHeap* resourceHeap =
static_cast<ResidencyHeap*>(subAllocation.GetMemory());
ResidencyHeap* resourceHeap = ToBackend(subAllocation.GetMemory());
GPGMM_RETURN_IF_FAILED(CreatePlacedResource(
resourceHeap, subAllocation.GetOffset(), &newResourceDesc, clearValue,
initialResourceState, &placedResource));
Expand Down Expand Up @@ -1441,8 +1438,7 @@ namespace gpgmm::d3d12 {

GPGMM_RETURN_IF_NOT_FATAL(TryAllocateResource(
allocator, dedicatedRequest, [&](const auto& allocation) -> HRESULT {
ResidencyHeap* resourceHeap =
static_cast<ResidencyHeap*>(allocation.GetMemory());
ResidencyHeap* resourceHeap = ToBackend(allocation.GetMemory());
ComPtr<ID3D12Resource> placedResource;
GPGMM_RETURN_IF_FAILED(
CreatePlacedResource(resourceHeap, allocation.GetOffset(), &newResourceDesc,
Expand Down

0 comments on commit 1dc9f23

Please sign in to comment.