From ac1cd8279b40a78f99e3d278b04f36a6f23e8529 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 5 Mar 2016 21:15:48 +1000 Subject: D3D12: Implement GPU-based bounding box --- Source/Core/VideoBackends/D3D12/BoundingBox.cpp | 143 +++++++++++++++++++++--- 1 file changed, 130 insertions(+), 13 deletions(-) (limited to 'Source/Core/VideoBackends/D3D12/BoundingBox.cpp') diff --git a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp index d847924cf2..48cb3ce613 100644 --- a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp +++ b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp @@ -2,43 +2,160 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include + #include "Common/CommonTypes.h" #include "Common/MsgHandler.h" #include "VideoBackends/D3D12/BoundingBox.h" +#include "VideoBackends/D3D12/D3DBase.h" +#include "VideoBackends/D3D12/D3DCommandListManager.h" +#include "VideoBackends/D3D12/D3DDescriptorHeapManager.h" +#include "VideoBackends/D3D12/D3DStreamBuffer.h" +#include "VideoBackends/D3D12/D3DUtil.h" +#include "VideoBackends/D3D12/FramebufferManager.h" +#include "VideoBackends/D3D12/Render.h" #include "VideoCommon/VideoConfig.h" -// D3D12TODO: Support bounding box behavior. namespace DX12 { -ID3D11UnorderedAccessView* BBox::GetUAV() +constexpr size_t BBOX_BUFFER_SIZE = sizeof(int) * 4; +constexpr size_t BBOX_STREAM_BUFFER_SIZE = BBOX_BUFFER_SIZE * 128; + +static ID3D12Resource* s_bbox_buffer; +static ID3D12Resource* s_bbox_staging_buffer; +static void* s_bbox_staging_buffer_map; +static std::unique_ptr s_bbox_stream_buffer; +static D3D12_GPU_DESCRIPTOR_HANDLE s_bbox_descriptor_handle; + +void BBox::Init() { - // D3D12TODO: Implement this; - return nullptr; + if (!g_ActiveConfig.backend_info.bSupportsBBox) + return; + + CD3DX12_RESOURCE_DESC buffer_desc(CD3DX12_RESOURCE_DESC::Buffer(BBOX_BUFFER_SIZE, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, 0)); + CD3DX12_RESOURCE_DESC staging_buffer_desc(CD3DX12_RESOURCE_DESC::Buffer(BBOX_BUFFER_SIZE, D3D12_RESOURCE_FLAG_NONE, 0)); + + CheckHR(D3D::device12->CreateCommittedResource( + &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), + D3D12_HEAP_FLAG_NONE, + &buffer_desc, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + nullptr, + IID_PPV_ARGS(&s_bbox_buffer))); + + CheckHR(D3D::device12->CreateCommittedResource( + &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), + D3D12_HEAP_FLAG_NONE, + &staging_buffer_desc, + D3D12_RESOURCE_STATE_COPY_DEST, + nullptr, + IID_PPV_ARGS(&s_bbox_staging_buffer))); + + s_bbox_stream_buffer = std::make_unique(BBOX_STREAM_BUFFER_SIZE, BBOX_STREAM_BUFFER_SIZE, nullptr); + + // D3D12 root signature UAV must be raw or structured buffers, not typed. Since we used a typed buffer, + // we have to use a descriptor table. Luckily, we only have to allocate this once, and it never changes. + D3D12_CPU_DESCRIPTOR_HANDLE cpu_descriptor_handle; + if (!D3D::gpu_descriptor_heap_mgr->Allocate(&cpu_descriptor_handle, &s_bbox_descriptor_handle, nullptr, false)) + PanicAlert("Failed to create bounding box UAV descriptor"); + + D3D12_UNORDERED_ACCESS_VIEW_DESC view_desc = { DXGI_FORMAT_R32_SINT, D3D12_UAV_DIMENSION_BUFFER }; + view_desc.Buffer.FirstElement = 0; + view_desc.Buffer.NumElements = 4; + view_desc.Buffer.StructureByteStride = 0; + view_desc.Buffer.CounterOffsetInBytes = 0; + view_desc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE; + D3D::device12->CreateUnorderedAccessView(s_bbox_buffer, nullptr, &view_desc, cpu_descriptor_handle); + + Bind(); } -void BBox::Init() +void BBox::Bind() { - if (g_ActiveConfig.backend_info.bSupportsBBox) - { - // D3D12TODO: Implement this; - } + if (s_bbox_buffer) + D3D::current_command_list->SetGraphicsRootDescriptorTable(DESCRIPTOR_TABLE_PS_UAV, s_bbox_descriptor_handle); +} + +void BBox::Invalidate() +{ + if (!s_bbox_staging_buffer_map) + return; + + s_bbox_staging_buffer->Unmap(0, nullptr); + s_bbox_staging_buffer_map = nullptr; } void BBox::Shutdown() { - // D3D12TODO: Implement this; + Invalidate(); + + if (s_bbox_buffer) + { + D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(s_bbox_buffer); + s_bbox_buffer = nullptr; + } + + if (s_bbox_staging_buffer) + { + D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(s_bbox_staging_buffer); + s_bbox_staging_buffer = nullptr; + } + + s_bbox_stream_buffer.reset(); } void BBox::Set(int index, int value) { - // D3D12TODO: Implement this; + // If the buffer is currently mapped, compare the value, and update the staging buffer. + if (s_bbox_staging_buffer_map) + { + int current_value; + memcpy(¤t_value, reinterpret_cast(s_bbox_staging_buffer_map) + (index * sizeof(int)), sizeof(int)); + if (current_value == value) + { + // Value hasn't changed. So skip updating completely. + return; + } + + memcpy(reinterpret_cast(s_bbox_staging_buffer_map) + (index * sizeof(int)), &value, sizeof(int)); + } + + if (s_bbox_stream_buffer->AllocateSpaceInBuffer(sizeof(int), sizeof(int))) + { + // Command list was executed, reset state + g_renderer->SetViewport(); + FramebufferManager::RestoreEFBRenderTargets(); + } + + // Allocate temporary bytes in upload buffer, then copy to real buffer. + memcpy(s_bbox_stream_buffer->GetCPUAddressOfCurrentAllocation(), &value, sizeof(int)); + D3D::ResourceBarrier(D3D::current_command_list, s_bbox_buffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST, 0); + D3D::current_command_list->CopyBufferRegion(s_bbox_buffer, index * sizeof(int), s_bbox_stream_buffer->GetBuffer(), s_bbox_stream_buffer->GetOffsetOfCurrentAllocation(), sizeof(int)); + D3D::ResourceBarrier(D3D::current_command_list, s_bbox_buffer, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, 0); } int BBox::Get(int index) { - // D3D12TODO: Implement this; - return 0; + if (!s_bbox_staging_buffer_map) + { + D3D::command_list_mgr->CPUAccessNotify(); + + // Copy from real buffer to staging buffer, then block until we have the results. + D3D::ResourceBarrier(D3D::current_command_list, s_bbox_buffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE, 0); + D3D::current_command_list->CopyBufferRegion(s_bbox_staging_buffer, 0, s_bbox_buffer, 0, BBOX_BUFFER_SIZE); + D3D::ResourceBarrier(D3D::current_command_list, s_bbox_buffer, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, 0); + + D3D::command_list_mgr->ExecuteQueuedWork(true); + g_renderer->SetViewport(); + FramebufferManager::RestoreEFBRenderTargets(); + + CheckHR(s_bbox_staging_buffer->Map(0, nullptr, &s_bbox_staging_buffer_map)); + } + + int value; + memcpy(&value, &reinterpret_cast(s_bbox_staging_buffer_map)[index], sizeof(int)); + return value; } }; -- cgit v1.2.3 From acfa93372ec58faac05f9ed7c8ac0d6ed5ad403e Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 5 Mar 2016 22:38:14 +1000 Subject: D3D12: Refactoring and cleanups Moves render target restoring to RestoreAPIState, this also means no need to manually restore after allocating in a buffer that caused execution, because the manager restores it for us. Remove a method that wasn't used from D3DUtil.cpp, and fixes a few errors in EFB poke drawing. --- Source/Core/VideoBackends/D3D12/BoundingBox.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'Source/Core/VideoBackends/D3D12/BoundingBox.cpp') diff --git a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp index 48cb3ce613..8e64d2a16e 100644 --- a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp +++ b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp @@ -121,12 +121,7 @@ void BBox::Set(int index, int value) memcpy(reinterpret_cast(s_bbox_staging_buffer_map) + (index * sizeof(int)), &value, sizeof(int)); } - if (s_bbox_stream_buffer->AllocateSpaceInBuffer(sizeof(int), sizeof(int))) - { - // Command list was executed, reset state - g_renderer->SetViewport(); - FramebufferManager::RestoreEFBRenderTargets(); - } + s_bbox_stream_buffer->AllocateSpaceInBuffer(sizeof(int), sizeof(int)); // Allocate temporary bytes in upload buffer, then copy to real buffer. memcpy(s_bbox_stream_buffer->GetCPUAddressOfCurrentAllocation(), &value, sizeof(int)); @@ -147,8 +142,6 @@ int BBox::Get(int index) D3D::ResourceBarrier(D3D::current_command_list, s_bbox_buffer, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, 0); D3D::command_list_mgr->ExecuteQueuedWork(true); - g_renderer->SetViewport(); - FramebufferManager::RestoreEFBRenderTargets(); CheckHR(s_bbox_staging_buffer->Map(0, nullptr, &s_bbox_staging_buffer_map)); } -- cgit v1.2.3 From 3372bfa6ab72c38763ed5b37dcb0fe6a9068a7eb Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 6 Mar 2016 18:45:57 +1000 Subject: D3D12: Remove feature level checks We don't create a device below feature level 11_0 anyway, so no point checking, we can just assume support. --- Source/Core/VideoBackends/D3D12/BoundingBox.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'Source/Core/VideoBackends/D3D12/BoundingBox.cpp') diff --git a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp index 8e64d2a16e..197ab2bdaa 100644 --- a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp +++ b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp @@ -30,9 +30,6 @@ static D3D12_GPU_DESCRIPTOR_HANDLE s_bbox_descriptor_handle; void BBox::Init() { - if (!g_ActiveConfig.backend_info.bSupportsBBox) - return; - CD3DX12_RESOURCE_DESC buffer_desc(CD3DX12_RESOURCE_DESC::Buffer(BBOX_BUFFER_SIZE, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, 0)); CD3DX12_RESOURCE_DESC staging_buffer_desc(CD3DX12_RESOURCE_DESC::Buffer(BBOX_BUFFER_SIZE, D3D12_RESOURCE_FLAG_NONE, 0)); @@ -73,8 +70,7 @@ void BBox::Init() void BBox::Bind() { - if (s_bbox_buffer) - D3D::current_command_list->SetGraphicsRootDescriptorTable(DESCRIPTOR_TABLE_PS_UAV, s_bbox_descriptor_handle); + D3D::current_command_list->SetGraphicsRootDescriptorTable(DESCRIPTOR_TABLE_PS_UAV, s_bbox_descriptor_handle); } void BBox::Invalidate() -- cgit v1.2.3 From ccf9470241309114bdc46d9dc80bb9b99aed4a41 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 15 Mar 2016 22:26:01 +1000 Subject: D3D12: Specify read/write ranges when calling Map/Unmap --- Source/Core/VideoBackends/D3D12/BoundingBox.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'Source/Core/VideoBackends/D3D12/BoundingBox.cpp') diff --git a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp index 197ab2bdaa..b9ee9b4929 100644 --- a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp +++ b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp @@ -78,7 +78,8 @@ void BBox::Invalidate() if (!s_bbox_staging_buffer_map) return; - s_bbox_staging_buffer->Unmap(0, nullptr); + D3D12_RANGE write_range = {}; + s_bbox_staging_buffer->Unmap(0, &write_range); s_bbox_staging_buffer_map = nullptr; } @@ -139,7 +140,8 @@ int BBox::Get(int index) D3D::command_list_mgr->ExecuteQueuedWork(true); - CheckHR(s_bbox_staging_buffer->Map(0, nullptr, &s_bbox_staging_buffer_map)); + D3D12_RANGE read_range = { 0, BBOX_BUFFER_SIZE }; + CheckHR(s_bbox_staging_buffer->Map(0, &read_range, &s_bbox_staging_buffer_map)); } int value; -- cgit v1.2.3