| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3D12/D3D12BoundingBox.h" |
| 5 | |
| 6 | #include "Common/Assert.h" |
| 7 | #include "Common/Logging/Log.h" |
| 8 | |
| 9 | #include "VideoBackends/D3D12/D3D12Gfx.h" |
| 10 | #include "VideoBackends/D3D12/DX12Context.h" |
| 11 | |
| 12 | namespace DX12 |
| 13 | { |
| 14 | D3D12BoundingBox::~D3D12BoundingBox() |
| 15 | { |
| 16 | if (m_gpu_descriptor) |
| 17 | g_dx_context->GetDescriptorHeapManager().Free(m_gpu_descriptor); |
| 18 | } |
| 19 | |
| 20 | bool D3D12BoundingBox::Initialize() |
| 21 | { |
| 22 | if (!CreateBuffers()) |
| 23 | return false; |
| 24 | |
| 25 | Gfx::GetInstance()->SetPixelShaderUAV(m_gpu_descriptor.cpu_handle); |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | std::vector<BBoxType> D3D12BoundingBox::Read(u32 index, u32 length) |
| 30 | { |
| 31 | // Copy from GPU->CPU buffer, and wait for the GPU to finish the copy. |
| 32 | ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(), |
| 33 | D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); |
| 34 | g_dx_context->GetCommandList()->CopyBufferRegion(m_readback_buffer.Get(), 0, m_gpu_buffer.Get(), |
| 35 | 0, BUFFER_SIZE); |
| 36 | ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(), |
| 37 | D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); |
| 38 | Gfx::GetInstance()->ExecuteCommandList(true); |
| 39 | |
| 40 | // Read back to cached values. |
| 41 | std::vector<BBoxType> values(length); |
| 42 | static constexpr D3D12_RANGE read_range = {0, BUFFER_SIZE}; |
| 43 | void* mapped_pointer; |
| 44 | HRESULT hr = m_readback_buffer->Map(0, &read_range, &mapped_pointer); |
| 45 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Map bounding box CPU buffer failed: {}", DX12HRWrap(hr)); |
| 46 | if (FAILED(hr)) |
| 47 | return values; |
| 48 | |
| 49 | // Copy out the values we want |
| 50 | std::memcpy(values.data(), static_cast<const u8*>(mapped_pointer) + sizeof(BBoxType) * index, |
| 51 | sizeof(BBoxType) * length); |
| 52 | |
| 53 | static constexpr D3D12_RANGE write_range = {0, 0}; |
| 54 | m_readback_buffer->Unmap(0, &write_range); |
| 55 | |
| 56 | return values; |
| 57 | } |
| 58 | |
| 59 | void D3D12BoundingBox::Write(u32 index, std::span<const BBoxType> values) |
| 60 | { |
| 61 | const u32 copy_size = static_cast<u32>(values.size() * sizeof(BBoxType)); |
| 62 | if (!m_upload_buffer.ReserveMemory(copy_size, sizeof(BBoxType))) |
| 63 | { |
| 64 | WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in bbox stream buffer"); |
| 65 | Gfx::GetInstance()->ExecuteCommandList(false); |
| 66 | if (!m_upload_buffer.ReserveMemory(copy_size, sizeof(BBoxType))) |
| 67 | { |
| 68 | PanicAlertFmt("Failed to allocate bbox stream buffer space"); |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const u32 upload_buffer_offset = m_upload_buffer.GetCurrentOffset(); |
| 74 | std::memcpy(m_upload_buffer.GetCurrentHostPointer(), values.data(), copy_size); |
| 75 | m_upload_buffer.CommitMemory(copy_size); |
| 76 | |
| 77 | ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(), |
| 78 | D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST); |
| 79 | |
| 80 | g_dx_context->GetCommandList()->CopyBufferRegion(m_gpu_buffer.Get(), index * sizeof(BBoxType), |
| 81 | m_upload_buffer.GetBuffer(), |
| 82 | upload_buffer_offset, copy_size); |
| 83 | |
| 84 | ResourceBarrier(g_dx_context->GetCommandList(), m_gpu_buffer.Get(), |
| 85 | D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); |
| 86 | } |
| 87 | |
| 88 | bool D3D12BoundingBox::CreateBuffers() |
| 89 | { |
| 90 | static constexpr D3D12_HEAP_PROPERTIES gpu_heap_properties = {D3D12_HEAP_TYPE_DEFAULT}; |
| 91 | static constexpr D3D12_HEAP_PROPERTIES cpu_heap_properties = {D3D12_HEAP_TYPE_READBACK}; |
| 92 | D3D12_RESOURCE_DESC buffer_desc = {D3D12_RESOURCE_DIMENSION_BUFFER, |
| 93 | 0, |
| 94 | BUFFER_SIZE, |
| 95 | 1, |
| 96 | 1, |
| 97 | 1, |
| 98 | DXGI_FORMAT_UNKNOWN, |
| 99 | {1, 0}, |
| 100 | D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 101 | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS}; |
| 102 | |
| 103 | HRESULT hr = g_dx_context->GetDevice()->CreateCommittedResource( |
| 104 | &gpu_heap_properties, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_COMMON, |
| 105 | nullptr, IID_PPV_ARGS(&m_gpu_buffer)); |
| 106 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Creating bounding box GPU buffer failed: {}", DX12HRWrap(hr)); |
| 107 | if (FAILED(hr) || !g_dx_context->GetDescriptorHeapManager().Allocate(&m_gpu_descriptor)) |
| 108 | return false; |
| 109 | |
| 110 | D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {DXGI_FORMAT_R32_SINT, D3D12_UAV_DIMENSION_BUFFER}; |
| 111 | uav_desc.Buffer.NumElements = NUM_BBOX_VALUES; |
| 112 | g_dx_context->GetDevice()->CreateUnorderedAccessView(m_gpu_buffer.Get(), nullptr, &uav_desc, |
| 113 | m_gpu_descriptor.cpu_handle); |
| 114 | |
| 115 | buffer_desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 116 | hr = g_dx_context->GetDevice()->CreateCommittedResource( |
| 117 | &cpu_heap_properties, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_COPY_DEST, |
| 118 | nullptr, IID_PPV_ARGS(&m_readback_buffer)); |
| 119 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Creating bounding box CPU buffer failed: {}", DX12HRWrap(hr)); |
| 120 | if (FAILED(hr)) |
| 121 | return false; |
| 122 | |
| 123 | if (!m_upload_buffer.AllocateBuffer(STREAM_BUFFER_SIZE)) |
| 124 | return false; |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | } // namespace DX12 |