| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/VKStreamBuffer.h" |
| 5 | |
| 6 | #include "Common/Align.h" |
| 7 | #include "Common/Assert.h" |
| 8 | #include "Common/MsgHandler.h" |
| 9 | |
| 10 | #include "VideoBackends/Vulkan/CommandBufferManager.h" |
| 11 | #include "VideoBackends/Vulkan/VulkanContext.h" |
| 12 | |
| 13 | namespace Vulkan |
| 14 | { |
| 15 | StreamBuffer::StreamBuffer(VkBufferUsageFlags usage, u32 size) : m_usage(usage), m_size(size) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | StreamBuffer::~StreamBuffer() |
| 20 | { |
| 21 | // VMA_ALLOCATION_CREATE_MAPPED_BIT automatically handles unmapping for us |
| 22 | if (m_buffer != VK_NULL_HANDLE) |
| 23 | g_command_buffer_mgr->DeferBufferDestruction(m_buffer, m_alloc); |
| 24 | } |
| 25 | |
| 26 | std::unique_ptr<StreamBuffer> StreamBuffer::Create(VkBufferUsageFlags usage, u32 size) |
| 27 | { |
| 28 | std::unique_ptr<StreamBuffer> buffer = std::make_unique<StreamBuffer>(usage, size); |
| 29 | if (!buffer->AllocateBuffer()) |
| 30 | return nullptr; |
| 31 | |
| 32 | return buffer; |
| 33 | } |
| 34 | |
| 35 | bool StreamBuffer::AllocateBuffer() |
| 36 | { |
| 37 | // Create the buffer descriptor |
| 38 | VkBufferCreateInfo buffer_create_info = { |
| 39 | VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType |
| 40 | nullptr, // const void* pNext |
| 41 | 0, // VkBufferCreateFlags flags |
| 42 | static_cast<VkDeviceSize>(m_size), // VkDeviceSize size |
| 43 | m_usage, // VkBufferUsageFlags usage |
| 44 | VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode |
| 45 | 0, // uint32_t queueFamilyIndexCount |
| 46 | nullptr // const uint32_t* pQueueFamilyIndices |
| 47 | }; |
| 48 | |
| 49 | VmaAllocationCreateInfo alloc_create_info = {}; |
| 50 | alloc_create_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | |
| 51 | VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT | |
| 52 | VMA_ALLOCATION_CREATE_MAPPED_BIT; |
| 53 | alloc_create_info.usage = |
| 54 | VMA_MEMORY_USAGE_AUTO_PREFER_HOST; // Host visible VRAM is slower in practice |
| 55 | alloc_create_info.pool = VK_NULL_HANDLE; |
| 56 | alloc_create_info.pUserData = nullptr; |
| 57 | alloc_create_info.priority = 0.0; |
| 58 | alloc_create_info.requiredFlags = 0; |
| 59 | alloc_create_info.preferredFlags = 0; |
| 60 | |
| 61 | VkBuffer buffer = VK_NULL_HANDLE; |
| 62 | VmaAllocation alloc = VK_NULL_HANDLE; |
| 63 | VmaAllocationInfo alloc_info; |
| 64 | VkResult res = vmaCreateBuffer(g_vulkan_context->GetMemoryAllocator(), &buffer_create_info, |
| 65 | &alloc_create_info, &buffer, &alloc, &alloc_info); |
| 66 | if (res != VK_SUCCESS) |
| 67 | { |
| 68 | LOG_VULKAN_ERROR(res, "vmaCreateBuffer failed: "); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // Destroy the backings for the buffer after the command buffer executes |
| 73 | // VMA_ALLOCATION_CREATE_MAPPED_BIT automatically handles unmapping for us |
| 74 | if (m_buffer != VK_NULL_HANDLE) |
| 75 | g_command_buffer_mgr->DeferBufferDestruction(m_buffer, m_alloc); |
| 76 | |
| 77 | // Replace with the new buffer |
| 78 | m_buffer = buffer; |
| 79 | m_alloc = alloc; |
| 80 | m_host_pointer = static_cast<u8*>(alloc_info.pMappedData); |
| 81 | m_current_offset = 0; |
| 82 | m_current_gpu_position = 0; |
| 83 | m_tracked_fences.clear(); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool StreamBuffer::ReserveMemory(u32 num_bytes, u32 alignment) |
| 88 | { |
| 89 | const u32 required_bytes = num_bytes + alignment; |
| 90 | |
| 91 | // Check for sane allocations |
| 92 | if (required_bytes > m_size) |
| 93 | { |
| 94 | PanicAlertFmt("Attempting to allocate {} bytes from a {} byte stream buffer", num_bytes, |
| 95 | m_size); |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // Is the GPU behind or up to date with our current offset? |
| 101 | UpdateCurrentFencePosition(); |
| 102 | if (m_current_offset >= m_current_gpu_position) |
| 103 | { |
| 104 | const u32 remaining_bytes = m_size - m_current_offset; |
| 105 | if (required_bytes <= remaining_bytes) |
| 106 | { |
| 107 | // Place at the current position, after the GPU position. |
| 108 | m_current_offset = Common::AlignUp(m_current_offset, alignment); |
| 109 | m_last_allocation_size = num_bytes; |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | // Check for space at the start of the buffer |
| 114 | // We use < here because we don't want to have the case of m_current_offset == |
| 115 | // m_current_gpu_position. That would mean the code above would assume the |
| 116 | // GPU has caught up to us, which it hasn't. |
| 117 | if (required_bytes < m_current_gpu_position) |
| 118 | { |
| 119 | // Reset offset to zero, since we're allocating behind the gpu now |
| 120 | m_current_offset = 0; |
| 121 | m_last_allocation_size = num_bytes; |
| 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Is the GPU ahead of our current offset? |
| 127 | if (m_current_offset < m_current_gpu_position) |
| 128 | { |
| 129 | // We have from m_current_offset..m_current_gpu_position space to use. |
| 130 | const u32 remaining_bytes = m_current_gpu_position - m_current_offset; |
| 131 | if (required_bytes < remaining_bytes) |
| 132 | { |
| 133 | // Place at the current position, since this is still behind the GPU. |
| 134 | m_current_offset = Common::AlignUp(m_current_offset, alignment); |
| 135 | m_last_allocation_size = num_bytes; |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Can we find a fence to wait on that will give us enough memory? |
| 141 | if (WaitForClearSpace(required_bytes)) |
| 142 | { |
| 143 | m_current_offset = Common::AlignUp(m_current_offset, alignment); |
| 144 | m_last_allocation_size = num_bytes; |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | // We tried everything we could, and still couldn't get anything. This means that too much space |
| 149 | // in the buffer is being used by the command buffer currently being recorded. Therefore, the |
| 150 | // only option is to execute it, and wait until it's done. |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | void StreamBuffer::CommitMemory(u32 final_num_bytes) |
| 155 | { |
| 156 | ASSERT((m_current_offset + final_num_bytes) <= m_size); |
| 157 | ASSERT(final_num_bytes <= m_last_allocation_size); |
| 158 | |
| 159 | // For non-coherent mappings, flush the memory range |
| 160 | // vmaFlushAllocation checks whether the allocation uses a coherent memory type internally |
| 161 | vmaFlushAllocation(g_vulkan_context->GetMemoryAllocator(), m_alloc, m_current_offset, |
| 162 | final_num_bytes); |
| 163 | |
| 164 | m_current_offset += final_num_bytes; |
| 165 | } |
| 166 | |
| 167 | void StreamBuffer::UpdateCurrentFencePosition() |
| 168 | { |
| 169 | // Don't create a tracking entry if the GPU is caught up with the buffer. |
| 170 | if (m_current_offset == m_current_gpu_position) |
| 171 | return; |
| 172 | |
| 173 | // Has the offset changed since the last fence? |
| 174 | const u64 counter = g_command_buffer_mgr->GetCurrentFenceCounter(); |
| 175 | if (!m_tracked_fences.empty() && m_tracked_fences.back().first == counter) |
| 176 | { |
| 177 | // Still haven't executed a command buffer, so just update the offset. |
| 178 | m_tracked_fences.back().second = m_current_offset; |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | // New buffer, so update the GPU position while we're at it. |
| 183 | UpdateGPUPosition(); |
| 184 | m_tracked_fences.emplace_back(counter, m_current_offset); |
| 185 | } |
| 186 | |
| 187 | void StreamBuffer::UpdateGPUPosition() |
| 188 | { |
| 189 | auto start = m_tracked_fences.begin(); |
| 190 | auto end = start; |
| 191 | |
| 192 | const u64 completed_counter = g_command_buffer_mgr->GetCompletedFenceCounter(); |
| 193 | while (end != m_tracked_fences.end() && completed_counter >= end->first) |
| 194 | { |
| 195 | m_current_gpu_position = end->second; |
| 196 | ++end; |
| 197 | } |
| 198 | |
| 199 | if (start != end) |
| 200 | m_tracked_fences.erase(start, end); |
| 201 | } |
| 202 | |
| 203 | bool StreamBuffer::WaitForClearSpace(u32 num_bytes) |
| 204 | { |
| 205 | u32 new_offset = 0; |
| 206 | u32 new_gpu_position = 0; |
| 207 | |
| 208 | auto iter = m_tracked_fences.begin(); |
| 209 | for (; iter != m_tracked_fences.end(); ++iter) |
| 210 | { |
| 211 | // Would this fence bring us in line with the GPU? |
| 212 | // This is the "last resort" case, where a command buffer execution has been forced |
| 213 | // after no additional data has been written to it, so we can assume that after the |
| 214 | // fence has been signaled the entire buffer is now consumed. |
| 215 | u32 gpu_position = iter->second; |
| 216 | if (m_current_offset == gpu_position) |
| 217 | { |
| 218 | new_offset = 0; |
| 219 | new_gpu_position = 0; |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | // Assuming that we wait for this fence, are we allocating in front of the GPU? |
| 224 | if (m_current_offset > gpu_position) |
| 225 | { |
| 226 | // This would suggest the GPU has now followed us and wrapped around, so we have from |
| 227 | // m_current_position..m_size free, as well as and 0..gpu_position. |
| 228 | const u32 remaining_space_after_offset = m_size - m_current_offset; |
| 229 | if (remaining_space_after_offset >= num_bytes) |
| 230 | { |
| 231 | // Switch to allocating in front of the GPU, using the remainder of the buffer. |
| 232 | new_offset = m_current_offset; |
| 233 | new_gpu_position = gpu_position; |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | // We can wrap around to the start, behind the GPU, if there is enough space. |
| 238 | // We use > here because otherwise we'd end up lining up with the GPU, and then the |
| 239 | // allocator would assume that the GPU has consumed what we just wrote. |
| 240 | if (gpu_position > num_bytes) |
| 241 | { |
| 242 | new_offset = 0; |
| 243 | new_gpu_position = gpu_position; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | // We're currently allocating behind the GPU. This would give us between the current |
| 250 | // offset and the GPU position worth of space to work with. Again, > because we can't |
| 251 | // align the GPU position with the buffer offset. |
| 252 | u32 available_space_inbetween = gpu_position - m_current_offset; |
| 253 | if (available_space_inbetween > num_bytes) |
| 254 | { |
| 255 | // Leave the offset as-is, but update the GPU position. |
| 256 | new_offset = m_current_offset; |
| 257 | new_gpu_position = gpu_position; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Did any fences satisfy this condition? |
| 264 | // Has the command buffer been executed yet? If not, the caller should execute it. |
| 265 | if (iter == m_tracked_fences.end() || |
| 266 | iter->first == g_command_buffer_mgr->GetCurrentFenceCounter()) |
| 267 | { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | // Wait until this fence is signaled. This will fire the callback, updating the GPU position. |
| 272 | g_command_buffer_mgr->WaitForFenceCounter(iter->first); |
| 273 | m_tracked_fences.erase(m_tracked_fences.begin(), |
| 274 | m_current_offset == iter->second ? m_tracked_fences.end() : ++iter); |
| 275 | m_current_offset = new_offset; |
| 276 | m_current_gpu_position = new_gpu_position; |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | } // namespace Vulkan |