summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngineD3D12
diff options
context:
space:
mode:
authorEgor Yusov <egor.yusov@gmail.com>2018-09-09 03:13:04 +0000
committerEgor Yusov <egor.yusov@gmail.com>2018-09-09 03:13:04 +0000
commitd7dda5023e6dfa999286a45ed314158330c18a5f (patch)
treef01498bdad1032243bd7a97732eb9284152c6d60 /Graphics/GraphicsEngineD3D12
parentFixed debug check in ShaderResourceBindingD3D12Impl::InitializeStaticResources() (diff)
downloadDiligentCore-d7dda5023e6dfa999286a45ed314158330c18a5f.tar.gz
DiligentCore-d7dda5023e6dfa999286a45ed314158330c18a5f.zip
Added alignment parameter to allocation managers (fixed https://github.com/DiligentGraphics/DiligentCore/issues/29)
Diffstat (limited to 'Graphics/GraphicsEngineD3D12')
-rw-r--r--Graphics/GraphicsEngineD3D12/include/DynamicUploadHeap.h9
-rw-r--r--Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp3
-rw-r--r--Graphics/GraphicsEngineD3D12/src/DescriptorHeap.cpp7
-rw-r--r--Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp5
-rw-r--r--Graphics/GraphicsEngineD3D12/src/DynamicUploadHeap.cpp13
5 files changed, 18 insertions, 19 deletions
diff --git a/Graphics/GraphicsEngineD3D12/include/DynamicUploadHeap.h b/Graphics/GraphicsEngineD3D12/include/DynamicUploadHeap.h
index 8edc4c57..b4e73676 100644
--- a/Graphics/GraphicsEngineD3D12/include/DynamicUploadHeap.h
+++ b/Graphics/GraphicsEngineD3D12/include/DynamicUploadHeap.h
@@ -28,9 +28,6 @@
namespace Diligent
{
-// Constant blocks must be multiples of 16 constants @ 16 bytes each
-#define DEFAULT_ALIGN 256
-
struct DynamicAllocation
{
DynamicAllocation()noexcept{}
@@ -86,9 +83,9 @@ public:
~GPURingBuffer();
- DynamicAllocation Allocate(size_t SizeInBytes)
+ DynamicAllocation Allocate(size_t SizeInBytes, size_t Alignment)
{
- auto Offset = RingBuffer::Allocate(SizeInBytes);
+ auto Offset = RingBuffer::Allocate(SizeInBytes, Alignment);
if (Offset != RingBuffer::InvalidOffset)
{
DynamicAllocation DynAlloc(m_pBuffer, Offset, SizeInBytes);
@@ -126,7 +123,7 @@ public:
DynamicUploadHeap& operator=(const DynamicUploadHeap&)= delete;
DynamicUploadHeap& operator=(DynamicUploadHeap&&) = delete;
- DynamicAllocation Allocate( size_t SizeInBytes, size_t Alignment = DEFAULT_ALIGN );
+ DynamicAllocation Allocate( size_t SizeInBytes, size_t Alignment);
void FinishFrame(Uint64 FenceValue, Uint64 LastCompletedFenceValue);
diff --git a/Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp
index 92b734f4..9427c271 100644
--- a/Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp
+++ b/Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp
@@ -354,7 +354,8 @@ void BufferD3D12Impl :: Map(IDeviceContext* pContext, MAP_TYPE MapType, Uint32 M
auto ContextId = pDeviceContextD3D12->GetContextId();
if ((MapFlags & MAP_FLAG_DISCARD) != 0 || m_DynamicData[ContextId].CPUAddress == nullptr)
{
- m_DynamicData[ContextId] = pCtxD3D12->AllocateDynamicSpace(m_Desc.uiSizeInBytes, 0);
+ size_t Alignment = (m_Desc.BindFlags & BIND_UNIFORM_BUFFER) ? D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT : 16;
+ m_DynamicData[ContextId] = pCtxD3D12->AllocateDynamicSpace(m_Desc.uiSizeInBytes, Alignment);
}
else
{
diff --git a/Graphics/GraphicsEngineD3D12/src/DescriptorHeap.cpp b/Graphics/GraphicsEngineD3D12/src/DescriptorHeap.cpp
index 6815b112..5d6b9a00 100644
--- a/Graphics/GraphicsEngineD3D12/src/DescriptorHeap.cpp
+++ b/Graphics/GraphicsEngineD3D12/src/DescriptorHeap.cpp
@@ -95,12 +95,15 @@ DescriptorHeapAllocation DescriptorHeapAllocationManager::Allocate(uint32_t Coun
// Methods of VariableSizeGPUAllocationsManager class are not thread safe!
// Use variable-size GPU allocations manager to allocate the requested number of descriptors
- auto DescriptorHandleOffset = m_FreeBlockManager.Allocate(Count);
+ auto Allocation = m_FreeBlockManager.Allocate(Count, 1);
+ auto DescriptorHandleOffset = Allocation.UnalignedOffset;
if (DescriptorHandleOffset == VariableSizeGPUAllocationsManager::InvalidOffset)
{
- return DescriptorHeapAllocation();
+ return DescriptorHeapAllocation{};
}
+ VERIFY_EXPR(Allocation.Size == Count);
+
// Compute the first CPU and GPU descriptor handles in the allocation by
// offseting the first CPU and GPU descriptor handle in the range
auto CPUHandle = m_FirstCPUHandle;
diff --git a/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp
index 1f7ab435..ea3b7a86 100644
--- a/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp
+++ b/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp
@@ -692,7 +692,7 @@ namespace Diligent
DynamicAllocation DeviceContextD3D12Impl::AllocateDynamicSpace(size_t NumBytes, size_t Alignment)
{
- return m_pUploadHeap->Allocate(NumBytes + Alignment);
+ return m_pUploadHeap->Allocate(NumBytes, Alignment);
}
void DeviceContextD3D12Impl::UpdateBufferRegion(class BufferD3D12Impl* pBuffD3D12, DynamicAllocation& Allocation, Uint64 DstOffset, Uint64 NumBytes)
@@ -711,7 +711,8 @@ namespace Diligent
{
VERIFY(pBuffD3D12->GetDesc().Usage != USAGE_DYNAMIC, "Dynamic buffers must be updated via Map()");
VERIFY_EXPR( static_cast<size_t>(NumBytes) == NumBytes );
- auto TmpSpace = m_pUploadHeap->Allocate(static_cast<size_t>(NumBytes));
+ constexpr size_t DefaultAlginment = 16;
+ auto TmpSpace = m_pUploadHeap->Allocate(static_cast<size_t>(NumBytes), DefaultAlginment);
memcpy(TmpSpace.CPUAddress, pData, static_cast<size_t>(NumBytes));
UpdateBufferRegion(pBuffD3D12, TmpSpace, DstOffset, NumBytes);
}
diff --git a/Graphics/GraphicsEngineD3D12/src/DynamicUploadHeap.cpp b/Graphics/GraphicsEngineD3D12/src/DynamicUploadHeap.cpp
index 232c5f8e..f884579c 100644
--- a/Graphics/GraphicsEngineD3D12/src/DynamicUploadHeap.cpp
+++ b/Graphics/GraphicsEngineD3D12/src/DynamicUploadHeap.cpp
@@ -117,7 +117,7 @@ namespace Diligent
m_RingBuffers.emplace_back(InitialSize, Allocator, pDevice->GetD3D12Device(), m_bIsCPUAccessible);
}
- DynamicAllocation DynamicUploadHeap::Allocate(size_t SizeInBytes, size_t Alignment /*= DEFAULT_ALIGN*/)
+ DynamicAllocation DynamicUploadHeap::Allocate(size_t SizeInBytes, size_t Alignment)
{
// Every device context has its own upload heap, so there is no need to lock
@@ -128,18 +128,15 @@ namespace Diligent
// across several frames!
//
- const size_t AlignmentMask = Alignment - 1;
- // Assert that it's a power of two.
- VERIFY_EXPR((AlignmentMask & Alignment) == 0);
+ VERIFY_EXPR(IsPowerOfTwo(Alignment));
// Align the allocation
- const size_t AlignedSize = (SizeInBytes + AlignmentMask) & ~AlignmentMask;
- auto DynAlloc = m_RingBuffers.back().Allocate(AlignedSize);
+ auto DynAlloc = m_RingBuffers.back().Allocate(SizeInBytes, Alignment);
if (!DynAlloc.pBuffer)
{
auto NewMaxSize = m_RingBuffers.back().GetMaxSize() * 2;
- while(NewMaxSize < AlignedSize)NewMaxSize*=2;
+ while(NewMaxSize < SizeInBytes)NewMaxSize*=2;
m_RingBuffers.emplace_back(NewMaxSize, m_Allocator, m_pDeviceD3D12->GetD3D12Device(), m_bIsCPUAccessible);
- DynAlloc = m_RingBuffers.back().Allocate(AlignedSize);
+ DynAlloc = m_RingBuffers.back().Allocate(SizeInBytes, Alignment);
}
#ifdef _DEBUG
DynAlloc.FrameNum = m_pDeviceD3D12->GetCurrentFrameNumber();