#include "d3d12_stream_buffer.h"
#include "d3d12_device.h"
#include "common/align.h"
#include "common/assert.h"
#include "common/error.h"
#include "common/log.h"
#include "D3D12MemAlloc.h"
#include <algorithm>
LOG_CHANNEL(GPUDevice);
D3D12StreamBuffer::D3D12StreamBuffer() = default;
D3D12StreamBuffer::~D3D12StreamBuffer()
{
Destroy();
}
bool D3D12StreamBuffer::Create(u32 size, Error* error)
{
const D3D12_RESOURCE_DESC resource_desc = {
D3D12_RESOURCE_DIMENSION_BUFFER, 0, size, 1, 1, 1, DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
D3D12_RESOURCE_FLAG_NONE};
D3D12MA::ALLOCATION_DESC allocationDesc = {};
allocationDesc.Flags = D3D12MA::ALLOCATION_FLAG_COMMITTED;
allocationDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD;
Microsoft::WRL::ComPtr<ID3D12Resource> buffer;
Microsoft::WRL::ComPtr<D3D12MA::Allocation> allocation;
HRESULT hr = D3D12Device::GetInstance().GetAllocator()->CreateResource(
&allocationDesc, &resource_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, allocation.ReleaseAndGetAddressOf(),
IID_PPV_ARGS(buffer.GetAddressOf()));
if (FAILED(hr)) [[unlikely]]
{
Error::SetHResult(error, "CreateResource() for stream buffer failed: ", hr);
return false;
}
static const D3D12_RANGE read_range = {};
u8* host_pointer;
hr = buffer->Map(0, &read_range, reinterpret_cast<void**>(&host_pointer));
if (FAILED(hr)) [[unlikely]]
{
Error::SetHResult(error, "Map() for stream buffer failed: ", hr);
return false;
}
Destroy(true);
m_buffer = std::move(buffer);
m_allocation = std::move(allocation);
m_host_pointer = host_pointer;
m_size = size;
m_gpu_pointer = m_buffer->GetGPUVirtualAddress();
return true;
}
bool D3D12StreamBuffer::ReserveMemory(u32 num_bytes, u32 alignment)
{
const u32 required_bytes = num_bytes + alignment;
if (num_bytes > m_size) [[unlikely]]
{
ERROR_LOG("Attempting to allocate {} bytes from a {} byte stream buffer", static_cast<u32>(num_bytes),
static_cast<u32>(m_size));
Panic("Stream buffer overflow");
}
UpdateCurrentFencePosition();
if (m_current_offset >= m_current_gpu_position)
{
const u32 aligned_required_bytes = (m_current_offset > 0) ? required_bytes : num_bytes;
const u32 remaining_bytes = m_size - m_current_offset;
if (aligned_required_bytes <= remaining_bytes)
{
m_current_offset = Common::AlignUp(m_current_offset, alignment);
m_current_space = m_size - m_current_offset;
return true;
}
if (required_bytes < m_current_gpu_position)
{
m_current_offset = 0;
m_current_space = m_current_gpu_position;
return true;
}
}
if (m_current_offset < m_current_gpu_position)
{
const u32 remaining_bytes = m_current_gpu_position - m_current_offset;
if (required_bytes < remaining_bytes)
{
m_current_offset = Common::AlignUp(m_current_offset, alignment);
m_current_space = m_current_gpu_position - m_current_offset;
return true;
}
}
if (WaitForClearSpace(required_bytes))
{
const u32 align_diff = Common::AlignUp(m_current_offset, alignment) - m_current_offset;
m_current_offset += align_diff;
m_current_space -= align_diff;
return true;
}
return false;
}
void D3D12StreamBuffer::CommitMemory(u32 final_num_bytes)
{
DebugAssert((m_current_offset + final_num_bytes) <= m_size);
DebugAssert(final_num_bytes <= m_current_space);
m_current_offset += final_num_bytes;
m_current_space -= final_num_bytes;
}
void D3D12StreamBuffer::Destroy(bool defer)
{
if (m_host_pointer)
{
const D3D12_RANGE written_range = {0, m_size};
m_buffer->Unmap(0, &written_range);
m_host_pointer = nullptr;
}
if (m_buffer && defer)
D3D12Device::GetInstance().DeferResourceDestruction(std::move(m_allocation), std::move(m_buffer));
m_buffer.Reset();
m_allocation.Reset();
m_current_offset = 0;
m_current_space = 0;
m_current_gpu_position = 0;
m_tracked_fences.clear();
}
void D3D12StreamBuffer::UpdateCurrentFencePosition()
{
if (m_current_offset == m_current_gpu_position)
return;
const u64 fence = D3D12Device::GetInstance().GetCurrentFenceValue();
if (!m_tracked_fences.empty() && m_tracked_fences.back().first == fence)
{
m_tracked_fences.back().second = m_current_offset;
return;
}
UpdateGPUPosition();
m_tracked_fences.emplace_back(fence, m_current_offset);
}
void D3D12StreamBuffer::UpdateGPUPosition()
{
auto start = m_tracked_fences.begin();
auto end = start;
const u64 completed_counter = D3D12Device::GetInstance().GetCompletedFenceValue();
while (end != m_tracked_fences.end() && completed_counter >= end->first)
{
m_current_gpu_position = end->second;
++end;
}
if (start != end)
m_tracked_fences.erase(start, end);
}
bool D3D12StreamBuffer::WaitForClearSpace(u32 num_bytes)
{
u32 new_offset = 0;
u32 new_space = 0;
u32 new_gpu_position = 0;
auto iter = m_tracked_fences.begin();
for (; iter != m_tracked_fences.end(); ++iter)
{
u32 gpu_position = iter->second;
if (m_current_offset == gpu_position)
{
new_offset = 0;
new_space = m_size;
new_gpu_position = 0;
break;
}
if (m_current_offset > gpu_position)
{
const u32 remaining_space_after_offset = m_size - m_current_offset;
if (remaining_space_after_offset >= num_bytes)
{
new_offset = m_current_offset;
new_space = m_size - m_current_offset;
new_gpu_position = gpu_position;
break;
}
if (gpu_position > num_bytes)
{
new_offset = 0;
new_space = gpu_position;
new_gpu_position = gpu_position;
break;
}
}
else
{
u32 available_space_inbetween = gpu_position - m_current_offset;
if (available_space_inbetween > num_bytes)
{
new_offset = m_current_offset;
new_space = gpu_position - m_current_offset;
new_gpu_position = gpu_position;
break;
}
}
}
if (iter == m_tracked_fences.end() || iter->first == D3D12Device::GetInstance().GetCurrentFenceValue())
return false;
D3D12Device::GetInstance().WaitForFence(iter->first);
m_tracked_fences.erase(m_tracked_fences.begin(), m_current_offset == iter->second ? m_tracked_fences.end() : ++iter);
m_current_offset = new_offset;
m_current_space = new_space;
m_current_gpu_position = new_gpu_position;
return true;
}