| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3D12/D3D12StreamBuffer.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <functional> |
| 8 | |
| 9 | #include "Common/Align.h" |
| 10 | #include "Common/Assert.h" |
| 11 | #include "Common/MsgHandler.h" |
| 12 | |
| 13 | #include "VideoBackends/D3D12/DX12Context.h" |
| 14 | |
| 15 | namespace DX12 |
| 16 | { |
| 17 | StreamBuffer::StreamBuffer() = default; |
| 18 | |
| 19 | StreamBuffer::~StreamBuffer() |
| 20 | { |
| 21 | if (m_host_pointer) |
| 22 | { |
| 23 | const D3D12_RANGE written_range = {0, m_size}; |
| 24 | m_buffer->Unmap(0, &written_range); |
| 25 | } |
| 26 | |
| 27 | // These get destroyed at shutdown anyway, so no need to defer destruction. |
| 28 | if (m_buffer) |
| 29 | m_buffer->Release(); |
| 30 | } |
| 31 | |
| 32 | bool StreamBuffer::AllocateBuffer(u32 size) |
| 33 | { |
| 34 | static const D3D12_HEAP_PROPERTIES heap_properties = {D3D12_HEAP_TYPE_UPLOAD}; |
| 35 | const D3D12_RESOURCE_DESC resource_desc = {D3D12_RESOURCE_DIMENSION_BUFFER, |
| 36 | 0, |
| 37 | size, |
| 38 | 1, |
| 39 | 1, |
| 40 | 1, |
| 41 | DXGI_FORMAT_UNKNOWN, |
| 42 | {1, 0}, |
| 43 | D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 44 | D3D12_RESOURCE_FLAG_NONE}; |
| 45 | |
| 46 | HRESULT hr = g_dx_context->GetDevice()->CreateCommittedResource( |
| 47 | &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_GENERIC_READ, |
| 48 | nullptr, IID_PPV_ARGS(&m_buffer)); |
| 49 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to allocate buffer of size {}: {}", size, |
| 50 | DX12HRWrap(hr)); |
| 51 | if (FAILED(hr)) |
| 52 | return false; |
| 53 | |
| 54 | static const D3D12_RANGE read_range = {}; |
| 55 | hr = m_buffer->Map(0, &read_range, reinterpret_cast<void**>(&m_host_pointer)); |
| 56 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to map buffer of size {}: {}", size, DX12HRWrap(hr)); |
| 57 | if (FAILED(hr)) |
| 58 | return false; |
| 59 | |
| 60 | m_size = size; |
| 61 | m_gpu_pointer = m_buffer->GetGPUVirtualAddress(); |
| 62 | m_current_offset = 0; |
| 63 | m_current_gpu_position = 0; |
| 64 | m_tracked_fences.clear(); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool StreamBuffer::ReserveMemory(u32 num_bytes, u32 alignment) |
| 69 | { |
| 70 | const u32 required_bytes = num_bytes + alignment; |
| 71 | |
| 72 | // Check for sane allocations |
| 73 | if (required_bytes > m_size) |
| 74 | { |
| 75 | PanicAlertFmt("Attempting to allocate {} bytes from a {} byte stream buffer", num_bytes, |
| 76 | m_size); |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // Is the GPU behind or up to date with our current offset? |
| 82 | UpdateCurrentFencePosition(); |
| 83 | if (m_current_offset >= m_current_gpu_position) |
| 84 | { |
| 85 | const u32 remaining_bytes = m_size - m_current_offset; |
| 86 | if (required_bytes <= remaining_bytes) |
| 87 | { |
| 88 | // Place at the current position, after the GPU position. |
| 89 | m_current_offset = Common::AlignUp(m_current_offset, alignment); |
| 90 | m_last_allocation_size = num_bytes; |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | // Check for space at the start of the buffer |
| 95 | // We use < here because we don't want to have the case of m_current_offset == |
| 96 | // m_current_gpu_position. That would mean the code above would assume the |
| 97 | // GPU has caught up to us, which it hasn't. |
| 98 | if (required_bytes < m_current_gpu_position) |
| 99 | { |
| 100 | // Reset offset to zero, since we're allocating behind the gpu now |
| 101 | m_current_offset = 0; |
| 102 | m_last_allocation_size = num_bytes; |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | // We have from m_current_offset..m_current_gpu_position space to use. |
| 109 | const u32 remaining_bytes = m_current_gpu_position - m_current_offset; |
| 110 | if (required_bytes < remaining_bytes) |
| 111 | { |
| 112 | // Place at the current position, since this is still behind the GPU. |
| 113 | m_current_offset = Common::AlignUp(m_current_offset, alignment); |
| 114 | m_last_allocation_size = num_bytes; |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Can we find a fence to wait on that will give us enough memory? |
| 120 | if (WaitForClearSpace(required_bytes)) |
| 121 | { |
| 122 | m_current_offset = Common::AlignUp(m_current_offset, alignment); |
| 123 | m_last_allocation_size = num_bytes; |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | // We tried everything we could, and still couldn't get anything. This means that too much space |
| 128 | // in the buffer is being used by the command buffer currently being recorded. Therefore, the |
| 129 | // only option is to execute it, and wait until it's done. |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | void StreamBuffer::CommitMemory(u32 final_num_bytes) |
| 134 | { |
| 135 | ASSERT((m_current_offset + final_num_bytes) <= m_size); |
| 136 | ASSERT(final_num_bytes <= m_last_allocation_size); |
| 137 | m_current_offset += final_num_bytes; |
| 138 | } |
| 139 | |
| 140 | void StreamBuffer::UpdateCurrentFencePosition() |
| 141 | { |
| 142 | // Don't create a tracking entry if the GPU is caught up with the buffer. |
| 143 | if (m_current_offset == m_current_gpu_position) |
| 144 | return; |
| 145 | |
| 146 | // Has the offset changed since the last fence? |
| 147 | const u64 fence = g_dx_context->GetCurrentFenceValue(); |
| 148 | if (!m_tracked_fences.empty() && m_tracked_fences.back().first == fence) |
| 149 | { |
| 150 | // Still haven't executed a command buffer, so just update the offset. |
| 151 | m_tracked_fences.back().second = m_current_offset; |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | UpdateGPUPosition(); |
| 156 | m_tracked_fences.emplace_back(fence, m_current_offset); |
| 157 | } |
| 158 | |
| 159 | void StreamBuffer::UpdateGPUPosition() |
| 160 | { |
| 161 | auto start = m_tracked_fences.begin(); |
| 162 | auto end = start; |
| 163 | |
| 164 | const u64 completed_counter = g_dx_context->GetCompletedFenceValue(); |
| 165 | while (end != m_tracked_fences.end() && completed_counter >= end->first) |
| 166 | { |
| 167 | m_current_gpu_position = end->second; |
| 168 | ++end; |
| 169 | } |
| 170 | |
| 171 | if (start != end) |
| 172 | m_tracked_fences.erase(start, end); |
| 173 | } |
| 174 | |
| 175 | bool StreamBuffer::WaitForClearSpace(u32 num_bytes) |
| 176 | { |
| 177 | u32 new_offset = 0; |
| 178 | u32 new_gpu_position = 0; |
| 179 | |
| 180 | auto iter = m_tracked_fences.begin(); |
| 181 | for (; iter != m_tracked_fences.end(); ++iter) |
| 182 | { |
| 183 | // Would this fence bring us in line with the GPU? |
| 184 | // This is the "last resort" case, where a command buffer execution has been forced |
| 185 | // after no additional data has been written to it, so we can assume that after the |
| 186 | // fence has been signaled the entire buffer is now consumed. |
| 187 | u32 gpu_position = iter->second; |
| 188 | if (m_current_offset == gpu_position) |
| 189 | { |
| 190 | new_offset = 0; |
| 191 | new_gpu_position = 0; |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | // Assuming that we wait for this fence, are we allocating in front of the GPU? |
| 196 | if (m_current_offset > gpu_position) |
| 197 | { |
| 198 | // This would suggest the GPU has now followed us and wrapped around, so we have from |
| 199 | // m_current_position..m_size free, as well as and 0..gpu_position. |
| 200 | const u32 remaining_space_after_offset = m_size - m_current_offset; |
| 201 | if (remaining_space_after_offset >= num_bytes) |
| 202 | { |
| 203 | // Switch to allocating in front of the GPU, using the remainder of the buffer. |
| 204 | new_offset = m_current_offset; |
| 205 | new_gpu_position = gpu_position; |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | // We can wrap around to the start, behind the GPU, if there is enough space. |
| 210 | // We use > here because otherwise we'd end up lining up with the GPU, and then the |
| 211 | // allocator would assume that the GPU has consumed what we just wrote. |
| 212 | if (gpu_position > num_bytes) |
| 213 | { |
| 214 | new_offset = 0; |
| 215 | new_gpu_position = gpu_position; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | // We're currently allocating behind the GPU. This would give us between the current |
| 222 | // offset and the GPU position worth of space to work with. Again, > because we can't |
| 223 | // align the GPU position with the buffer offset. |
| 224 | u32 available_space_inbetween = gpu_position - m_current_offset; |
| 225 | if (available_space_inbetween > num_bytes) |
| 226 | { |
| 227 | // Leave the offset as-is, but update the GPU position. |
| 228 | new_offset = m_current_offset; |
| 229 | new_gpu_position = gpu_position; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Did any fences satisfy this condition? |
| 236 | // Has the command buffer been executed yet? If not, the caller should execute it. |
| 237 | if (iter == m_tracked_fences.end() || iter->first == g_dx_context->GetCurrentFenceValue()) |
| 238 | return false; |
| 239 | |
| 240 | // Wait until this fence is signaled. This will fire the callback, updating the GPU position. |
| 241 | g_dx_context->WaitForFence(iter->first); |
| 242 | m_tracked_fences.erase(m_tracked_fences.begin(), |
| 243 | m_current_offset == iter->second ? m_tracked_fences.end() : ++iter); |
| 244 | m_current_offset = new_offset; |
| 245 | m_current_gpu_position = new_gpu_position; |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | } // namespace DX12 |