Merge pull request #9803 from Techjar/bbox-videocommon

VideoCommon: Abstract bounding box
This commit is contained in:
Léo Lam
2021-10-08 22:24:38 +02:00
committed by GitHub
41 changed files with 617 additions and 708 deletions
+1 -1
View File
@@ -73,7 +73,7 @@ static Common::Event g_compressAndDumpStateSyncEvent;
static std::thread g_save_thread;
// Don't forget to increase this after doing changes on the savestate system
constexpr u32 STATE_VERSION = 136; // Last changed in PR 10058
constexpr u32 STATE_VERSION = 137; // Last changed in PR 9803
// Maps savestate versions to Dolphin versions.
// Versions after 42 don't need to be added to this list,
+3 -1
View File
@@ -539,7 +539,7 @@
<ClInclude Include="VideoBackends\D3DCommon\D3DCommon.h" />
<ClInclude Include="VideoBackends\D3DCommon\Shader.h" />
<ClInclude Include="VideoBackends\D3DCommon\SwapChain.h" />
<ClInclude Include="VideoBackends\Null\NullPerfQuery.h" />
<ClInclude Include="VideoBackends\Null\NullBoundingBox.h" />
<ClInclude Include="VideoBackends\Null\NullRender.h" />
<ClInclude Include="VideoBackends\Null\NullTexture.h" />
<ClInclude Include="VideoBackends\Null\NullVertexManager.h" />
@@ -566,6 +566,7 @@
<ClInclude Include="VideoBackends\Software\NativeVertexFormat.h" />
<ClInclude Include="VideoBackends\Software\Rasterizer.h" />
<ClInclude Include="VideoBackends\Software\SetupUnit.h" />
<ClInclude Include="VideoBackends\Software\SWBoundingBox.h" />
<ClInclude Include="VideoBackends\Software\SWOGLWindow.h" />
<ClInclude Include="VideoBackends\Software\SWRenderer.h" />
<ClInclude Include="VideoBackends\Software\SWTexture.h" />
@@ -1138,6 +1139,7 @@
<ClCompile Include="VideoBackends\Software\Rasterizer.cpp" />
<ClCompile Include="VideoBackends\Software\SetupUnit.cpp" />
<ClCompile Include="VideoBackends\Software\SWmain.cpp" />
<ClCompile Include="VideoBackends\Software\SWBoundingBox.cpp" />
<ClCompile Include="VideoBackends\Software\SWOGLWindow.cpp" />
<ClCompile Include="VideoBackends\Software\SWRenderer.cpp" />
<ClCompile Include="VideoBackends\Software\SWTexture.cpp" />
@@ -9,49 +9,43 @@
#include "VideoBackends/D3D/D3DBoundingBox.h"
#include "VideoBackends/D3D/D3DState.h"
#include "VideoBackends/D3DCommon/D3DCommon.h"
#include "VideoCommon/VideoConfig.h"
namespace DX11
{
static constexpr u32 NUM_BBOX_VALUES = 4;
static ComPtr<ID3D11Buffer> s_bbox_buffer;
static ComPtr<ID3D11Buffer> s_bbox_staging_buffer;
static ComPtr<ID3D11UnorderedAccessView> s_bbox_uav;
static std::array<s32, NUM_BBOX_VALUES> s_bbox_values;
static std::array<bool, NUM_BBOX_VALUES> s_bbox_dirty;
static bool s_bbox_valid = false;
ID3D11UnorderedAccessView* BBox::GetUAV()
D3DBoundingBox::~D3DBoundingBox()
{
return s_bbox_uav.Get();
m_uav.Reset();
m_staging_buffer.Reset();
m_buffer.Reset();
}
void BBox::Init()
bool D3DBoundingBox::Initialize()
{
if (!g_ActiveConfig.backend_info.bSupportsBBox)
return;
// Create 2 buffers here.
// First for unordered access on default pool.
auto desc = CD3D11_BUFFER_DESC(NUM_BBOX_VALUES * sizeof(s32), D3D11_BIND_UNORDERED_ACCESS,
D3D11_USAGE_DEFAULT, 0, 0, sizeof(s32));
const s32 initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
auto desc = CD3D11_BUFFER_DESC(NUM_BBOX_VALUES * sizeof(BBoxType), D3D11_BIND_UNORDERED_ACCESS,
D3D11_USAGE_DEFAULT, 0, 0, sizeof(BBoxType));
const BBoxType initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
D3D11_SUBRESOURCE_DATA data;
data.pSysMem = initial_values;
data.SysMemPitch = NUM_BBOX_VALUES * sizeof(s32);
data.SysMemPitch = NUM_BBOX_VALUES * sizeof(BBoxType);
data.SysMemSlicePitch = 0;
HRESULT hr;
hr = D3D::device->CreateBuffer(&desc, &data, &s_bbox_buffer);
hr = D3D::device->CreateBuffer(&desc, &data, &m_buffer);
CHECK(SUCCEEDED(hr), "Create BoundingBox Buffer.");
D3DCommon::SetDebugObjectName(s_bbox_buffer.Get(), "BoundingBox Buffer");
if (FAILED(hr))
return false;
D3DCommon::SetDebugObjectName(m_buffer.Get(), "BoundingBox Buffer");
// Second to use as a staging buffer.
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.BindFlags = 0;
hr = D3D::device->CreateBuffer(&desc, nullptr, &s_bbox_staging_buffer);
hr = D3D::device->CreateBuffer(&desc, nullptr, &m_staging_buffer);
CHECK(SUCCEEDED(hr), "Create BoundingBox Staging Buffer.");
D3DCommon::SetDebugObjectName(s_bbox_staging_buffer.Get(), "BoundingBox Staging Buffer");
if (FAILED(hr))
return false;
D3DCommon::SetDebugObjectName(m_staging_buffer.Get(), "BoundingBox Staging Buffer");
// UAV is required to allow concurrent access.
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc = {};
@@ -60,89 +54,43 @@ void BBox::Init()
UAVdesc.Buffer.FirstElement = 0;
UAVdesc.Buffer.Flags = 0;
UAVdesc.Buffer.NumElements = NUM_BBOX_VALUES;
hr = D3D::device->CreateUnorderedAccessView(s_bbox_buffer.Get(), &UAVdesc, &s_bbox_uav);
hr = D3D::device->CreateUnorderedAccessView(m_buffer.Get(), &UAVdesc, &m_uav);
CHECK(SUCCEEDED(hr), "Create BoundingBox UAV.");
D3DCommon::SetDebugObjectName(s_bbox_uav.Get(), "BoundingBox UAV");
D3D::stateman->SetOMUAV(s_bbox_uav.Get());
if (FAILED(hr))
return false;
D3DCommon::SetDebugObjectName(m_uav.Get(), "BoundingBox UAV");
D3D::stateman->SetOMUAV(m_uav.Get());
s_bbox_dirty = {};
s_bbox_valid = true;
return true;
}
void BBox::Shutdown()
std::vector<BBoxType> D3DBoundingBox::Read(u32 index, u32 length)
{
s_bbox_uav.Reset();
s_bbox_staging_buffer.Reset();
s_bbox_buffer.Reset();
}
void BBox::Flush()
{
s_bbox_valid = false;
if (std::none_of(s_bbox_dirty.begin(), s_bbox_dirty.end(), [](bool dirty) { return dirty; }))
return;
for (u32 start = 0; start < NUM_BBOX_VALUES;)
{
if (!s_bbox_dirty[start])
{
start++;
continue;
}
u32 end = start + 1;
s_bbox_dirty[start] = false;
for (; end < NUM_BBOX_VALUES; end++)
{
if (!s_bbox_dirty[end])
break;
s_bbox_dirty[end] = false;
}
D3D11_BOX box{start * sizeof(s32), 0, 0, end * sizeof(s32), 1, 1};
D3D::context->UpdateSubresource(s_bbox_buffer.Get(), 0, &box, &s_bbox_values[start], 0, 0);
}
}
void BBox::Readback()
{
D3D::context->CopyResource(s_bbox_staging_buffer.Get(), s_bbox_buffer.Get());
std::vector<BBoxType> values(length);
D3D::context->CopyResource(m_staging_buffer.Get(), m_buffer.Get());
D3D11_MAPPED_SUBRESOURCE map;
HRESULT hr = D3D::context->Map(s_bbox_staging_buffer.Get(), 0, D3D11_MAP_READ, 0, &map);
HRESULT hr = D3D::context->Map(m_staging_buffer.Get(), 0, D3D11_MAP_READ, 0, &map);
if (SUCCEEDED(hr))
{
for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
{
if (!s_bbox_dirty[i])
{
std::memcpy(&s_bbox_values[i], reinterpret_cast<const u8*>(map.pData) + sizeof(s32) * i,
sizeof(s32));
}
}
std::memcpy(values.data(), reinterpret_cast<const u8*>(map.pData) + sizeof(BBoxType) * index,
sizeof(BBoxType) * length);
D3D::context->Unmap(s_bbox_staging_buffer.Get(), 0);
D3D::context->Unmap(m_staging_buffer.Get(), 0);
}
s_bbox_valid = true;
return values;
}
void BBox::Set(int index, int value)
void D3DBoundingBox::Write(u32 index, const std::vector<BBoxType>& values)
{
if (s_bbox_valid && s_bbox_values[index] == value)
return;
s_bbox_values[index] = value;
s_bbox_dirty[index] = true;
D3D11_BOX box{index * sizeof(BBoxType),
0,
0,
static_cast<u32>(index + values.size()) * sizeof(BBoxType),
1,
1};
D3D::context->UpdateSubresource(m_buffer.Get(), 0, &box, values.data(), 0, 0);
}
int BBox::Get(int index)
{
if (!s_bbox_valid)
Readback();
return s_bbox_values[index];
}
}; // namespace DX11
+17 -9
View File
@@ -2,21 +2,29 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "Common/CommonTypes.h"
#include "VideoBackends/D3D/D3DBase.h"
#include "VideoCommon/BoundingBox.h"
namespace DX11
{
class BBox
class D3DBoundingBox final : public BoundingBox
{
public:
static ID3D11UnorderedAccessView* GetUAV();
static void Init();
static void Shutdown();
~D3DBoundingBox() override;
static void Flush();
static void Readback();
bool Initialize() override;
static void Set(int index, int value);
static int Get(int index);
protected:
std::vector<BBoxType> Read(u32 index, u32 length) override;
void Write(u32 index, const std::vector<BBoxType>& values) override;
private:
ComPtr<ID3D11Buffer> m_buffer;
ComPtr<ID3D11Buffer> m_staging_buffer;
ComPtr<ID3D11UnorderedAccessView> m_uav;
};
}; // namespace DX11
} // namespace DX11
@@ -162,7 +162,6 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi)
return false;
}
BBox::Init();
g_shader_cache->InitializeShaderCache();
return true;
}
@@ -172,8 +171,6 @@ void VideoBackend::Shutdown()
g_shader_cache->Shutdown();
g_renderer->Shutdown();
BBox::Shutdown();
g_perf_query.reset();
g_texture_cache.reset();
g_framebuffer_manager.reset();
+2 -12
View File
@@ -265,19 +265,9 @@ void Renderer::UnbindTexture(const AbstractTexture* texture)
D3D::stateman->ApplyTextures();
}
u16 Renderer::BBoxReadImpl(int index)
std::unique_ptr<BoundingBox> Renderer::CreateBoundingBox() const
{
return static_cast<u16>(BBox::Get(index));
}
void Renderer::BBoxWriteImpl(int index, u16 value)
{
BBox::Set(index, value);
}
void Renderer::BBoxFlushImpl()
{
BBox::Flush();
return std::make_unique<D3DBoundingBox>();
}
void Renderer::Flush()
+5 -4
View File
@@ -8,6 +8,8 @@
#include "VideoBackends/D3D/D3DState.h"
#include "VideoCommon/RenderBase.h"
class BoundingBox;
namespace DX11
{
class SwapChain;
@@ -62,15 +64,14 @@ public:
void SetFullscreen(bool enable_fullscreen) override;
bool IsFullscreen() const override;
u16 BBoxReadImpl(int index) override;
void BBoxWriteImpl(int index, u16 value) override;
void BBoxFlushImpl() override;
void Flush() override;
void WaitForGPUIdle() override;
void OnConfigChanged(u32 bits) override;
protected:
std::unique_ptr<BoundingBox> CreateBoundingBox() const override;
private:
void CheckForSwapChainChanges();
@@ -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
@@ -1,5 +1,6 @@
add_library(videonull
NullBackend.cpp
NullBoundingBox.h
NullRender.cpp
NullRender.h
NullTexture.cpp
@@ -0,0 +1,25 @@
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "Common/CommonTypes.h"
#include "VideoCommon/BoundingBox.h"
namespace Null
{
class NullBoundingBox final : public BoundingBox
{
public:
bool Initialize() override { return true; }
protected:
std::vector<BBoxType> Read(u32 index, u32 length) override
{
return std::vector<BBoxType>(length);
}
void Write(u32 index, const std::vector<BBoxType>& values) override {}
};
} // namespace Null
@@ -3,6 +3,7 @@
#include "VideoBackends/Null/NullRender.h"
#include "VideoBackends/Null/NullBoundingBox.h"
#include "VideoBackends/Null/NullTexture.h"
#include "VideoCommon/AbstractPipeline.h"
@@ -83,4 +84,9 @@ Renderer::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
{
return std::make_unique<NativeVertexFormat>(vtx_decl);
}
std::unique_ptr<BoundingBox> Renderer::CreateBoundingBox() const
{
return std::make_unique<NullBoundingBox>();
}
} // namespace Null
+5 -2
View File
@@ -5,6 +5,8 @@
#include "VideoCommon/RenderBase.h"
class BoundingBox;
namespace Null
{
class Renderer final : public ::Renderer
@@ -35,8 +37,6 @@ public:
u32 AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data) override { return 0; }
void PokeEFB(EFBAccessType type, const EfbPokeData* points, size_t num_points) override {}
u16 BBoxReadImpl(int index) override { return 0; }
void BBoxWriteImpl(int index, u16 value) override {}
void ClearScreen(const MathUtil::Rectangle<int>& rc, bool colorEnable, bool alphaEnable,
bool zEnable, u32 color, u32 z) override
@@ -44,5 +44,8 @@ public:
}
void ReinterpretPixelData(EFBReinterpretType convtype) override {}
protected:
std::unique_ptr<BoundingBox> CreateBoundingBox() const override;
};
} // namespace Null
+29 -104
View File
@@ -1,91 +1,35 @@
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
#include <array>
#include <cstring>
#include "Common/GL/GLUtil.h"
#include "VideoBackends/OGL/OGLBoundingBox.h"
#include "VideoBackends/OGL/OGLRender.h"
#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/VideoConfig.h"
enum : u32
{
NUM_BBOX_VALUES = 4,
};
static GLuint s_bbox_buffer_id;
static std::array<s32, NUM_BBOX_VALUES> s_bbox_values;
static std::array<bool, NUM_BBOX_VALUES> s_bbox_dirty;
static bool s_bbox_valid = false;
namespace OGL
{
void BoundingBox::Init()
OGLBoundingBox::~OGLBoundingBox()
{
if (!g_ActiveConfig.backend_info.bSupportsBBox)
return;
if (m_buffer_id)
glDeleteBuffers(1, &m_buffer_id);
}
const s32 initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
std::memcpy(s_bbox_values.data(), initial_values, sizeof(s_bbox_values));
s_bbox_dirty = {};
s_bbox_valid = true;
bool OGLBoundingBox::Initialize()
{
const BBoxType initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
glGenBuffers(1, &s_bbox_buffer_id);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
glGenBuffers(1, &m_buffer_id);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(initial_values), initial_values, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, s_bbox_buffer_id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_buffer_id);
return true;
}
void BoundingBox::Shutdown()
std::vector<BBoxType> OGLBoundingBox::Read(u32 index, u32 length)
{
if (!g_ActiveConfig.backend_info.bSupportsBBox)
return;
glDeleteBuffers(1, &s_bbox_buffer_id);
}
void BoundingBox::Flush()
{
s_bbox_valid = false;
if (std::none_of(s_bbox_dirty.begin(), s_bbox_dirty.end(), [](bool dirty) { return dirty; }))
return;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
for (u32 start = 0; start < NUM_BBOX_VALUES;)
{
if (!s_bbox_dirty[start])
{
start++;
continue;
}
u32 end = start + 1;
s_bbox_dirty[start] = false;
for (; end < NUM_BBOX_VALUES; end++)
{
if (!s_bbox_dirty[end])
break;
s_bbox_dirty[end] = false;
}
glBufferSubData(GL_SHADER_STORAGE_BUFFER, start * sizeof(s32), (end - start) * sizeof(s32),
&s_bbox_values[start]);
}
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
void BoundingBox::Readback()
{
glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
std::vector<BBoxType> values(length);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id);
// Using glMapBufferRange to read back the contents of the SSBO is extremely slow
// on nVidia drivers. This is more noticeable at higher internal resolutions.
@@ -101,52 +45,33 @@ void BoundingBox::Readback()
// explain why it needs the cache invalidate.
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
std::array<s32, NUM_BBOX_VALUES> gpu_values;
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(s32) * NUM_BBOX_VALUES,
gpu_values.data());
for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
{
if (!s_bbox_dirty[i])
s_bbox_values[i] = gpu_values[i];
}
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(BBoxType) * index,
sizeof(BBoxType) * length, values.data());
}
else
{
// Using glMapBufferRange is faster on AMD cards by a measurable margin.
void* ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(s32) * NUM_BBOX_VALUES,
void* ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(BBoxType) * NUM_BBOX_VALUES,
GL_MAP_READ_BIT);
if (ptr)
{
for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
{
if (!s_bbox_dirty[i])
{
std::memcpy(&s_bbox_values[i], reinterpret_cast<const u8*>(ptr) + sizeof(s32) * i,
sizeof(s32));
}
}
std::memcpy(values.data(), reinterpret_cast<const u8*>(ptr) + sizeof(BBoxType) * index,
sizeof(BBoxType) * length);
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
}
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
s_bbox_valid = true;
return values;
}
void BoundingBox::Set(int index, int value)
void OGLBoundingBox::Write(u32 index, const std::vector<BBoxType>& values)
{
if (s_bbox_valid && s_bbox_values[index] == value)
return;
s_bbox_values[index] = value;
s_bbox_dirty[index] = true;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(BBoxType) * index,
sizeof(BBoxType) * values.size(), values.data());
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
int BoundingBox::Get(int index)
{
if (!s_bbox_valid)
Readback();
return s_bbox_values[index];
}
}; // namespace OGL
} // namespace OGL
+16 -8
View File
@@ -3,18 +3,26 @@
#pragma once
#include "Common/CommonTypes.h"
#include "Common/GL/GLUtil.h"
#include "VideoCommon/BoundingBox.h"
namespace OGL
{
class BoundingBox
class OGLBoundingBox final : public BoundingBox
{
public:
static void Init();
static void Shutdown();
~OGLBoundingBox() override;
static void Flush();
static void Readback();
bool Initialize() override;
static void Set(int index, int value);
static int Get(int index);
protected:
std::vector<BBoxType> Read(u32 index, u32 length) override;
void Write(u32 index, const std::vector<BBoxType>& values) override;
private:
GLuint m_buffer_id = 0;
};
}; // namespace OGL
} // namespace OGL
@@ -44,7 +44,6 @@ Make AA apply instantly during gameplay if possible
#include "Core/Config/GraphicsSettings.h"
#include "VideoBackends/OGL/OGLBoundingBox.h"
#include "VideoBackends/OGL/OGLPerfQuery.h"
#include "VideoBackends/OGL/OGLRender.h"
#include "VideoBackends/OGL/OGLVertexManager.h"
@@ -186,7 +185,6 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi)
g_perf_query = GetPerfQuery();
g_texture_cache = std::make_unique<TextureCacheBase>();
g_sampler_cache = std::make_unique<SamplerCache>();
BoundingBox::Init();
if (!g_vertex_manager->Initialize() || !g_shader_cache->Initialize() ||
!g_renderer->Initialize() || !g_framebuffer_manager->Initialize() ||
@@ -205,7 +203,6 @@ void VideoBackend::Shutdown()
{
g_shader_cache->Shutdown();
g_renderer->Shutdown();
BoundingBox::Shutdown();
g_sampler_cache.reset();
g_texture_cache.reset();
g_perf_query.reset();
+2 -12
View File
@@ -851,19 +851,9 @@ void Renderer::SetScissorRect(const MathUtil::Rectangle<int>& rc)
glScissor(rc.left, rc.top, rc.GetWidth(), rc.GetHeight());
}
u16 Renderer::BBoxReadImpl(int index)
std::unique_ptr<::BoundingBox> Renderer::CreateBoundingBox() const
{
return static_cast<u16>(BoundingBox::Get(index));
}
void Renderer::BBoxWriteImpl(int index, u16 value)
{
BoundingBox::Set(index, value);
}
void Renderer::BBoxFlushImpl()
{
BoundingBox::Flush();
return std::make_unique<OGLBoundingBox>();
}
void Renderer::SetViewport(float x, float y, float width, float height, float near_depth,
+5 -4
View File
@@ -11,6 +11,8 @@
#include "Common/GL/GLExtensions/GLExtensions.h"
#include "VideoCommon/RenderBase.h"
class BoundingBox;
namespace OGL
{
class OGLFramebuffer;
@@ -128,10 +130,6 @@ public:
void BindBackbuffer(const ClearColor& clear_color = {}) override;
void PresentBackbuffer() override;
u16 BBoxReadImpl(int index) override;
void BBoxWriteImpl(int index, u16 value) override;
void BBoxFlushImpl() override;
void BeginUtilityDrawing() override;
void EndUtilityDrawing() override;
@@ -164,6 +162,9 @@ public:
// Restores FBO binding after it's been changed.
void RestoreFramebufferBinding();
protected:
std::unique_ptr<BoundingBox> CreateBoundingBox() const override;
private:
void CheckForSurfaceChange();
void CheckForSurfaceResize();

Some files were not shown because too many files have changed in this diff Show More