VideoCommon: Abstract bounding box

This moves much of the duplicated bounding box code into VideoCommon,
leaving only the specific buffer implementations in each backend.
This commit is contained in:
Techjar
2021-06-09 07:42:21 -04:00
parent 7ec02ee4d3
commit 1161af8059
41 changed files with 617 additions and 708 deletions

View File

@ -8,24 +8,81 @@
namespace DX12
{
BoundingBox::BoundingBox() = default;
BoundingBox::~BoundingBox()
D3D12BoundingBox::~D3D12BoundingBox()
{
if (m_gpu_descriptor)
g_dx_context->GetDescriptorHeapManager().Free(m_gpu_descriptor);
}
std::unique_ptr<BoundingBox> BoundingBox::Create()
bool D3D12BoundingBox::Initialize()
{
auto bbox = std::unique_ptr<BoundingBox>(new BoundingBox());
if (!bbox->CreateBuffers())
return nullptr;
if (!CreateBuffers())
return false;
return bbox;
Renderer::GetInstance()->SetPixelShaderUAV(m_gpu_descriptor.cpu_handle);
return true;
}
bool BoundingBox::CreateBuffers()
std::vector<BBoxType> D3D12BoundingBox::Read(u32 index, u32 length)
{
// Copy from GPU->CPU buffer, and wait for the GPU to finish the copy.
ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
g_dx_context->GetCommandList()->CopyBufferRegion(m_readback_buffer.Get(), 0, m_gpu_buffer.Get(),
0, BUFFER_SIZE);
ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(),
D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
Renderer::GetInstance()->ExecuteCommandList(true);
// Read back to cached values.
std::vector<BBoxType> values(length);
static constexpr D3D12_RANGE read_range = {0, BUFFER_SIZE};
void* mapped_pointer;
HRESULT hr = m_readback_buffer->Map(0, &read_range, &mapped_pointer);
CHECK(SUCCEEDED(hr), "Map bounding box CPU buffer");
if (FAILED(hr))
return values;
// Copy out the values we want
std::memcpy(values.data(), reinterpret_cast<const u8*>(mapped_pointer) + sizeof(BBoxType) * index,
sizeof(BBoxType) * length);
static constexpr D3D12_RANGE write_range = {0, 0};
m_readback_buffer->Unmap(0, &write_range);
return values;
}
void D3D12BoundingBox::Write(u32 index, const std::vector<BBoxType>& values)
{
const u32 copy_size = static_cast<u32>(values.size()) * sizeof(BBoxType);
if (!m_upload_buffer.ReserveMemory(copy_size, sizeof(BBoxType)))
{
WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in bbox stream buffer");
Renderer::GetInstance()->ExecuteCommandList(false);
if (!m_upload_buffer.ReserveMemory(copy_size, sizeof(BBoxType)))
{
PanicAlertFmt("Failed to allocate bbox stream buffer space");
return;
}
}
const u32 upload_buffer_offset = m_upload_buffer.GetCurrentOffset();
std::memcpy(m_upload_buffer.GetCurrentHostPointer(), values.data(), copy_size);
m_upload_buffer.CommitMemory(copy_size);
ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST);
g_dx_context->GetCommandList()->CopyBufferRegion(m_gpu_buffer.Get(), index * sizeof(BBoxType),
m_upload_buffer.GetBuffer(),
upload_buffer_offset, copy_size);
ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(),
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}
bool D3D12BoundingBox::CreateBuffers()
{
static constexpr D3D12_HEAP_PROPERTIES gpu_heap_properties = {D3D12_HEAP_TYPE_DEFAULT};
static constexpr D3D12_HEAP_PROPERTIES cpu_heap_properties = {D3D12_HEAP_TYPE_READBACK};
@ -48,7 +105,7 @@ bool BoundingBox::CreateBuffers()
return false;
D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {DXGI_FORMAT_R32_SINT, D3D12_UAV_DIMENSION_BUFFER};
uav_desc.Buffer.NumElements = NUM_VALUES;
uav_desc.Buffer.NumElements = NUM_BBOX_VALUES;
g_dx_context->GetDevice()->CreateUnorderedAccessView(m_gpu_buffer.Get(), nullptr, &uav_desc,
m_gpu_descriptor.cpu_handle);
@ -63,120 +120,6 @@ bool BoundingBox::CreateBuffers()
if (!m_upload_buffer.AllocateBuffer(STREAM_BUFFER_SIZE))
return false;
// Both the CPU and GPU buffer's contents is unknown, so force a flush the first time.
m_values.fill(0);
m_dirty.fill(true);
m_valid = true;
return true;
}
void BoundingBox::Readback()
{
// Copy from GPU->CPU buffer, and wait for the GPU to finish the copy.
ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
g_dx_context->GetCommandList()->CopyBufferRegion(m_readback_buffer.Get(), 0, m_gpu_buffer.Get(),
0, BUFFER_SIZE);
ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(),
D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
Renderer::GetInstance()->ExecuteCommandList(true);
// Read back to cached values.
static constexpr D3D12_RANGE read_range = {0, BUFFER_SIZE};
void* mapped_pointer;
HRESULT hr = m_readback_buffer->Map(0, &read_range, &mapped_pointer);
CHECK(SUCCEEDED(hr), "Map bounding box CPU buffer");
if (FAILED(hr))
return;
static constexpr D3D12_RANGE write_range = {0, 0};
std::array<s32, NUM_VALUES> new_values;
std::memcpy(new_values.data(), mapped_pointer, BUFFER_SIZE);
m_readback_buffer->Unmap(0, &write_range);
// Preserve dirty values, that way we don't need to sync.
for (u32 i = 0; i < NUM_VALUES; i++)
{
if (!m_dirty[i])
m_values[i] = new_values[i];
}
m_valid = true;
}
s32 BoundingBox::Get(size_t index)
{
if (!m_valid)
Readback();
return m_values[index];
}
void BoundingBox::Set(size_t index, s32 value)
{
m_values[index] = value;
m_dirty[index] = true;
}
void BoundingBox::Invalidate()
{
m_dirty.fill(false);
m_valid = false;
}
void BoundingBox::Flush()
{
bool in_copy_state = false;
for (u32 start = 0; start < NUM_VALUES;)
{
if (!m_dirty[start])
{
start++;
continue;
}
u32 end = start + 1;
m_dirty[start] = false;
for (; end < NUM_VALUES; end++)
{
if (!m_dirty[end])
break;
m_dirty[end] = false;
}
const u32 copy_size = (end - start) * sizeof(ValueType);
if (!m_upload_buffer.ReserveMemory(copy_size, sizeof(ValueType)))
{
WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in bbox stream buffer");
Renderer::GetInstance()->ExecuteCommandList(false);
if (!m_upload_buffer.ReserveMemory(copy_size, sizeof(ValueType)))
{
PanicAlertFmt("Failed to allocate bbox stream buffer space");
return;
}
}
const u32 upload_buffer_offset = m_upload_buffer.GetCurrentOffset();
std::memcpy(m_upload_buffer.GetCurrentHostPointer(), &m_values[start], copy_size);
m_upload_buffer.CommitMemory(copy_size);
if (!in_copy_state)
{
ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST);
in_copy_state = true;
}
g_dx_context->GetCommandList()->CopyBufferRegion(m_gpu_buffer.Get(), start * sizeof(ValueType),
m_upload_buffer.GetBuffer(),
upload_buffer_offset, copy_size);
start = end;
}
if (in_copy_state)
{
ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(),
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}
}
}; // namespace DX12