mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-30 01:29:42 -06:00
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:
@ -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
|
||||
|
@ -2,47 +2,39 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "VideoBackends/D3D12/Common.h"
|
||||
#include "VideoBackends/D3D12/D3D12StreamBuffer.h"
|
||||
#include "VideoBackends/D3D12/DescriptorHeapManager.h"
|
||||
|
||||
#include "VideoCommon/BoundingBox.h"
|
||||
|
||||
namespace DX12
|
||||
{
|
||||
class BoundingBox
|
||||
class D3D12BoundingBox final : public BoundingBox
|
||||
{
|
||||
public:
|
||||
~BoundingBox();
|
||||
~D3D12BoundingBox() override;
|
||||
|
||||
static std::unique_ptr<BoundingBox> Create();
|
||||
bool Initialize() override;
|
||||
|
||||
const DescriptorHandle& GetGPUDescriptor() const { return m_gpu_descriptor; }
|
||||
|
||||
s32 Get(size_t index);
|
||||
void Set(size_t index, s32 value);
|
||||
|
||||
void Invalidate();
|
||||
void Flush();
|
||||
protected:
|
||||
std::vector<BBoxType> Read(u32 index, u32 length) override;
|
||||
void Write(u32 index, const std::vector<BBoxType>& values) override;
|
||||
|
||||
private:
|
||||
using ValueType = s32;
|
||||
static const u32 NUM_VALUES = 4;
|
||||
static const u32 BUFFER_SIZE = sizeof(ValueType) * NUM_VALUES;
|
||||
static const u32 MAX_UPDATES_PER_FRAME = 128;
|
||||
static const u32 STREAM_BUFFER_SIZE = BUFFER_SIZE * MAX_UPDATES_PER_FRAME;
|
||||
|
||||
BoundingBox();
|
||||
static constexpr u32 BUFFER_SIZE = sizeof(BBoxType) * NUM_BBOX_VALUES;
|
||||
static constexpr u32 MAX_UPDATES_PER_FRAME = 128;
|
||||
static constexpr u32 STREAM_BUFFER_SIZE = BUFFER_SIZE * MAX_UPDATES_PER_FRAME;
|
||||
|
||||
bool CreateBuffers();
|
||||
void Readback();
|
||||
|
||||
// Three buffers: GPU for read/write, CPU for reading back, and CPU for staging changes.
|
||||
ComPtr<ID3D12Resource> m_gpu_buffer;
|
||||
ComPtr<ID3D12Resource> m_readback_buffer;
|
||||
StreamBuffer m_upload_buffer;
|
||||
DescriptorHandle m_gpu_descriptor;
|
||||
std::array<ValueType, NUM_VALUES> m_values = {};
|
||||
std::array<bool, NUM_VALUES> m_dirty = {};
|
||||
bool m_valid = true;
|
||||
};
|
||||
}; // namespace DX12
|
||||
|
||||
} // namespace DX12
|
||||
|
@ -46,17 +46,11 @@ bool Renderer::Initialize()
|
||||
if (!::Renderer::Initialize())
|
||||
return false;
|
||||
|
||||
m_bounding_box = BoundingBox::Create();
|
||||
if (!m_bounding_box)
|
||||
return false;
|
||||
|
||||
SetPixelShaderUAV(m_bounding_box->GetGPUDescriptor().cpu_handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Renderer::Shutdown()
|
||||
{
|
||||
m_bounding_box.reset();
|
||||
m_swap_chain.reset();
|
||||
|
||||
::Renderer::Shutdown();
|
||||
@ -107,20 +101,9 @@ std::unique_ptr<AbstractPipeline> Renderer::CreatePipeline(const AbstractPipelin
|
||||
return DXPipeline::Create(config, cache_data, cache_data_length);
|
||||
}
|
||||
|
||||
u16 Renderer::BBoxReadImpl(int index)
|
||||
std::unique_ptr<BoundingBox> Renderer::CreateBoundingBox() const
|
||||
{
|
||||
return static_cast<u16>(m_bounding_box->Get(index));
|
||||
}
|
||||
|
||||
void Renderer::BBoxWriteImpl(int index, u16 value)
|
||||
{
|
||||
m_bounding_box->Set(index, value);
|
||||
}
|
||||
|
||||
void Renderer::BBoxFlushImpl()
|
||||
{
|
||||
m_bounding_box->Flush();
|
||||
m_bounding_box->Invalidate();
|
||||
return std::make_unique<D3D12BoundingBox>();
|
||||
}
|
||||
|
||||
void Renderer::Flush()
|
||||
|
@ -8,9 +8,10 @@
|
||||
#include "VideoBackends/D3D12/DescriptorHeapManager.h"
|
||||
#include "VideoCommon/RenderBase.h"
|
||||
|
||||
class BoundingBox;
|
||||
|
||||
namespace DX12
|
||||
{
|
||||
class BoundingBox;
|
||||
class DXFramebuffer;
|
||||
class DXTexture;
|
||||
class DXShader;
|
||||
@ -48,10 +49,6 @@ public:
|
||||
const void* cache_data = nullptr,
|
||||
size_t cache_data_length = 0) override;
|
||||
|
||||
u16 BBoxReadImpl(int index) override;
|
||||
void BBoxWriteImpl(int index, u16 value) override;
|
||||
void BBoxFlushImpl() override;
|
||||
|
||||
void Flush() override;
|
||||
void WaitForGPUIdle() override;
|
||||
|
||||
@ -100,6 +97,8 @@ public:
|
||||
protected:
|
||||
void OnConfigChanged(u32 bits) override;
|
||||
|
||||
std::unique_ptr<BoundingBox> CreateBoundingBox() const override;
|
||||
|
||||
private:
|
||||
static const u32 MAX_TEXTURES = 8;
|
||||
static const u32 NUM_CONSTANT_BUFFERS = 3;
|
||||
@ -150,7 +149,6 @@ private:
|
||||
|
||||
// Owned objects
|
||||
std::unique_ptr<SwapChain> m_swap_chain;
|
||||
std::unique_ptr<BoundingBox> m_bounding_box;
|
||||
|
||||
// Current state
|
||||
struct
|
||||
|
Reference in New Issue
Block a user