| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/VKVertexManager.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include "Common/Align.h" |
| 9 | #include "Common/CommonTypes.h" |
| 10 | #include "Common/Logging/Log.h" |
| 11 | #include "Common/MsgHandler.h" |
| 12 | |
| 13 | #include "Core/System.h" |
| 14 | |
| 15 | #include "VideoBackends/Vulkan/StateTracker.h" |
| 16 | #include "VideoBackends/Vulkan/VKGfx.h" |
| 17 | #include "VideoBackends/Vulkan/VKStreamBuffer.h" |
| 18 | #include "VideoBackends/Vulkan/VulkanContext.h" |
| 19 | |
| 20 | #include "VideoCommon/GeometryShaderManager.h" |
| 21 | #include "VideoCommon/IndexGenerator.h" |
| 22 | #include "VideoCommon/PixelShaderManager.h" |
| 23 | #include "VideoCommon/Statistics.h" |
| 24 | #include "VideoCommon/VertexShaderManager.h" |
| 25 | |
| 26 | namespace Vulkan |
| 27 | { |
| 28 | static VkBufferView CreateTexelBufferView(VkBuffer buffer, VkFormat vk_format) |
| 29 | { |
| 30 | // Create a view of the whole buffer, we'll offset our texel load into it |
| 31 | VkBufferViewCreateInfo view_info = { |
| 32 | VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO, // VkStructureType sType |
| 33 | nullptr, // const void* pNext |
| 34 | 0, // VkBufferViewCreateFlags flags |
| 35 | buffer, // VkBuffer buffer |
| 36 | vk_format, // VkFormat format |
| 37 | 0, // VkDeviceSize offset |
| 38 | VK_WHOLE_SIZE // VkDeviceSize range |
| 39 | }; |
| 40 | |
| 41 | VkBufferView view; |
| 42 | VkResult res = vkCreateBufferView(g_vulkan_context->GetDevice(), &view_info, nullptr, &view); |
| 43 | if (res != VK_SUCCESS) |
| 44 | { |
| 45 | LOG_VULKAN_ERROR(res, "vkCreateBufferView failed: "); |
| 46 | return VK_NULL_HANDLE; |
| 47 | } |
| 48 | |
| 49 | return view; |
| 50 | } |
| 51 | |
| 52 | VertexManager::VertexManager() = default; |
| 53 | |
| 54 | VertexManager::~VertexManager() |
| 55 | { |
| 56 | DestroyTexelBufferViews(); |
| 57 | } |
| 58 | |
| 59 | bool VertexManager::Initialize() |
| 60 | { |
| 61 | if (!VertexManagerBase::Initialize()) |
| 62 | return false; |
| 63 | |
| 64 | m_vertex_stream_buffer = |
| 65 | StreamBuffer::Create(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, |
| 66 | VERTEX_STREAM_BUFFER_SIZE); |
| 67 | m_index_stream_buffer = |
| 68 | StreamBuffer::Create(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, INDEX_STREAM_BUFFER_SIZE); |
| 69 | m_uniform_stream_buffer = |
| 70 | StreamBuffer::Create(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, UNIFORM_STREAM_BUFFER_SIZE); |
| 71 | if (!m_vertex_stream_buffer || !m_index_stream_buffer || !m_uniform_stream_buffer) |
| 72 | { |
| 73 | PanicAlertFmt("Failed to allocate streaming buffers"); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // The validation layer complains if max(offsets) + max(ubo_ranges) >= ubo_size. |
| 78 | // To work around this we reserve the maximum buffer size at all times, but only commit |
| 79 | // as many bytes as we use. |
| 80 | m_uniform_buffer_reserve_size = sizeof(PixelShaderConstants); |
| 81 | m_uniform_buffer_reserve_size = Common::AlignUp(m_uniform_buffer_reserve_size, |
| 82 | g_vulkan_context->GetUniformBufferAlignment()) + |
| 83 | sizeof(VertexShaderConstants); |
| 84 | m_uniform_buffer_reserve_size = Common::AlignUp(m_uniform_buffer_reserve_size, |
| 85 | g_vulkan_context->GetUniformBufferAlignment()) + |
| 86 | sizeof(GeometryShaderConstants); |
| 87 | |
| 88 | // Prefer an 8MB buffer if possible, but use less if the device doesn't support this. |
| 89 | // This buffer is potentially going to be addressed as R8s in the future, so we assume |
| 90 | // that one element is one byte. |
| 91 | const u32 texel_buffer_size = |
| 92 | std::min(TEXEL_STREAM_BUFFER_SIZE, g_vulkan_context->GetDeviceInfo().maxTexelBufferElements); |
| 93 | m_texel_stream_buffer = |
| 94 | StreamBuffer::Create(VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT, texel_buffer_size); |
| 95 | if (!m_texel_stream_buffer) |
| 96 | { |
| 97 | PanicAlertFmt("Failed to allocate streaming texel buffer"); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | static constexpr std::array<std::pair<TexelBufferFormat, VkFormat>, NUM_TEXEL_BUFFER_FORMATS> |
| 102 | format_mapping = {{ |
| 103 | {TEXEL_BUFFER_FORMAT_R8_UINT, VK_FORMAT_R8_UINT}, |
| 104 | {TEXEL_BUFFER_FORMAT_R16_UINT, VK_FORMAT_R16_UINT}, |
| 105 | {TEXEL_BUFFER_FORMAT_RGBA8_UINT, VK_FORMAT_R8G8B8A8_UINT}, |
| 106 | {TEXEL_BUFFER_FORMAT_R32G32_UINT, VK_FORMAT_R32G32_UINT}, |
| 107 | }}; |
| 108 | for (const auto& it : format_mapping) |
| 109 | { |
| 110 | if ((m_texel_buffer_views[it.first] = CreateTexelBufferView(m_texel_stream_buffer->GetBuffer(), |
| 111 | it.second)) == VK_NULL_HANDLE) |
| 112 | { |
| 113 | PanicAlertFmt("Failed to create texel buffer view"); |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Bind the buffers to all the known spots even if it's not used, to keep the driver happy. |
| 119 | UploadAllConstants(); |
| 120 | StateTracker::GetInstance()->SetUtilityUniformBuffer(m_uniform_stream_buffer->GetBuffer(), 0, |
| 121 | sizeof(VertexShaderConstants)); |
| 122 | for (u32 i = 0; i < NUM_COMPUTE_TEXEL_BUFFERS; i++) |
| 123 | { |
| 124 | StateTracker::GetInstance()->SetTexelBuffer(i, |
| 125 | m_texel_buffer_views[TEXEL_BUFFER_FORMAT_R8_UINT]); |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | void VertexManager::DestroyTexelBufferViews() |
| 132 | { |
| 133 | for (VkBufferView view : m_texel_buffer_views) |
| 134 | { |
| 135 | if (view != VK_NULL_HANDLE) |
| 136 | vkDestroyBufferView(g_vulkan_context->GetDevice(), view, nullptr); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void VertexManager::ResetBuffer(u32 vertex_stride) |
| 141 | { |
| 142 | // Attempt to allocate from buffers |
| 143 | bool has_vbuffer_allocation = |
| 144 | m_vertex_stream_buffer->ReserveMemory(MAXVBUFFERSIZE, vertex_stride); |
| 145 | bool has_ibuffer_allocation = |
| 146 | m_index_stream_buffer->ReserveMemory(MAXIBUFFERSIZE * sizeof(u16), sizeof(u16)); |
| 147 | if (!has_vbuffer_allocation || !has_ibuffer_allocation) |
| 148 | { |
| 149 | // Flush any pending commands first, so that we can wait on the fences |
| 150 | WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in vertex/index buffer"); |
| 151 | VKGfx::GetInstance()->ExecuteCommandBuffer(false); |
| 152 | |
| 153 | // Attempt to allocate again, this may cause a fence wait |
| 154 | if (!has_vbuffer_allocation) |
| 155 | has_vbuffer_allocation = m_vertex_stream_buffer->ReserveMemory(MAXVBUFFERSIZE, vertex_stride); |
| 156 | if (!has_ibuffer_allocation) |
| 157 | has_ibuffer_allocation = |
| 158 | m_index_stream_buffer->ReserveMemory(MAXIBUFFERSIZE * sizeof(u16), sizeof(u16)); |
| 159 | |
| 160 | // If we still failed, that means the allocation was too large and will never succeed, so panic |
| 161 | if (!has_vbuffer_allocation || !has_ibuffer_allocation) |
| 162 | PanicAlertFmt("Failed to allocate space in streaming buffers for pending draw"); |
| 163 | } |
| 164 | |
| 165 | // Update pointers |
| 166 | m_base_buffer_pointer = m_vertex_stream_buffer->GetHostPointer(); |
| 167 | m_end_buffer_pointer = m_vertex_stream_buffer->GetCurrentHostPointer() + MAXVBUFFERSIZE; |
| 168 | m_cur_buffer_pointer = m_vertex_stream_buffer->GetCurrentHostPointer(); |
| 169 | m_index_generator.Start(reinterpret_cast<u16*>(m_index_stream_buffer->GetCurrentHostPointer())); |
| 170 | } |
| 171 | |
| 172 | void VertexManager::CommitBuffer(u32 num_vertices, u32 vertex_stride, u32 num_indices, |
| 173 | u32* out_base_vertex, u32* out_base_index) |
| 174 | { |
| 175 | const u32 vertex_data_size = num_vertices * vertex_stride; |
| 176 | const u32 index_data_size = num_indices * sizeof(u16); |
| 177 | |
| 178 | *out_base_vertex = |
| 179 | vertex_stride > 0 ? (m_vertex_stream_buffer->GetCurrentOffset() / vertex_stride) : 0; |
| 180 | *out_base_index = m_index_stream_buffer->GetCurrentOffset() / sizeof(u16); |
| 181 | |
| 182 | m_vertex_stream_buffer->CommitMemory(vertex_data_size); |
| 183 | m_index_stream_buffer->CommitMemory(index_data_size); |
| 184 | |
| 185 | ADDSTAT(g_stats.this_frame.bytes_vertex_streamed, static_cast<int>(vertex_data_size)); |
| 186 | ADDSTAT(g_stats.this_frame.bytes_index_streamed, static_cast<int>(index_data_size)); |
| 187 | |
| 188 | StateTracker::GetInstance()->SetVertexBuffer(m_vertex_stream_buffer->GetBuffer(), 0, |
| 189 | VERTEX_STREAM_BUFFER_SIZE); |
| 190 | StateTracker::GetInstance()->SetIndexBuffer(m_index_stream_buffer->GetBuffer(), 0, |
| 191 | VK_INDEX_TYPE_UINT16); |
| 192 | } |
| 193 | |
| 194 | void VertexManager::UploadUniforms() |
| 195 | { |
| 196 | UpdateVertexShaderConstants(); |
| 197 | UpdateGeometryShaderConstants(); |
| 198 | UpdatePixelShaderConstants(); |
| 199 | } |
| 200 | |
| 201 | void VertexManager::UpdateVertexShaderConstants() |
| 202 | { |
| 203 | auto& system = Core::System::GetInstance(); |
| 204 | auto& vertex_shader_manager = system.GetVertexShaderManager(); |
| 205 | |
| 206 | if (!vertex_shader_manager.dirty || !ReserveConstantStorage()) |
| 207 | return; |
| 208 | |
| 209 | StateTracker::GetInstance()->SetGXUniformBuffer( |
| 210 | UBO_DESCRIPTOR_SET_BINDING_VS, m_uniform_stream_buffer->GetBuffer(), |
| 211 | m_uniform_stream_buffer->GetCurrentOffset(), sizeof(VertexShaderConstants)); |
| 212 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer(), &vertex_shader_manager.constants, |
| 213 | sizeof(VertexShaderConstants)); |
| 214 | m_uniform_stream_buffer->CommitMemory(sizeof(VertexShaderConstants)); |
| 215 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, sizeof(VertexShaderConstants)); |
| 216 | vertex_shader_manager.dirty = false; |
| 217 | } |
| 218 | |
| 219 | void VertexManager::UpdateGeometryShaderConstants() |
| 220 | { |
| 221 | auto& system = Core::System::GetInstance(); |
| 222 | auto& geometry_shader_manager = system.GetGeometryShaderManager(); |
| 223 | |
| 224 | if (!geometry_shader_manager.dirty || !ReserveConstantStorage()) |
| 225 | return; |
| 226 | |
| 227 | StateTracker::GetInstance()->SetGXUniformBuffer( |
| 228 | UBO_DESCRIPTOR_SET_BINDING_GS, m_uniform_stream_buffer->GetBuffer(), |
| 229 | m_uniform_stream_buffer->GetCurrentOffset(), sizeof(GeometryShaderConstants)); |
| 230 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer(), &geometry_shader_manager.constants, |
| 231 | sizeof(GeometryShaderConstants)); |
| 232 | m_uniform_stream_buffer->CommitMemory(sizeof(GeometryShaderConstants)); |
| 233 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, sizeof(GeometryShaderConstants)); |
| 234 | geometry_shader_manager.dirty = false; |
| 235 | } |
| 236 | |
| 237 | void VertexManager::UpdatePixelShaderConstants() |
| 238 | { |
| 239 | auto& system = Core::System::GetInstance(); |
| 240 | auto& pixel_shader_manager = system.GetPixelShaderManager(); |
| 241 | |
| 242 | if (!ReserveConstantStorage()) |
| 243 | return; |
| 244 | |
| 245 | if (pixel_shader_manager.dirty) |
| 246 | { |
| 247 | StateTracker::GetInstance()->SetGXUniformBuffer( |
| 248 | UBO_DESCRIPTOR_SET_BINDING_PS, m_uniform_stream_buffer->GetBuffer(), |
| 249 | m_uniform_stream_buffer->GetCurrentOffset(), sizeof(PixelShaderConstants)); |
| 250 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer(), &pixel_shader_manager.constants, |
| 251 | sizeof(PixelShaderConstants)); |
| 252 | m_uniform_stream_buffer->CommitMemory(sizeof(PixelShaderConstants)); |
| 253 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, sizeof(PixelShaderConstants)); |
| 254 | pixel_shader_manager.dirty = false; |
| 255 | } |
| 256 | |
| 257 | if (pixel_shader_manager.custom_constants_dirty) |
| 258 | { |
| 259 | StateTracker::GetInstance()->SetGXUniformBuffer( |
| 260 | UBO_DESCRIPTOR_SET_BINDING_CUST, m_uniform_stream_buffer->GetBuffer(), |
| 261 | m_uniform_stream_buffer->GetCurrentOffset(), |
| 262 | static_cast<u32>(pixel_shader_manager.custom_constants.size())); |
| 263 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer(), |
| 264 | pixel_shader_manager.custom_constants.data(), |
| 265 | pixel_shader_manager.custom_constants.size()); |
| 266 | m_uniform_stream_buffer->CommitMemory( |
| 267 | static_cast<u32>(pixel_shader_manager.custom_constants.size())); |
| 268 | pixel_shader_manager.custom_constants_dirty = false; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | bool VertexManager::ReserveConstantStorage() |
| 273 | { |
| 274 | auto& system = Core::System::GetInstance(); |
| 275 | auto& pixel_shader_manager = system.GetPixelShaderManager(); |
| 276 | const u32 custom_constants_size = static_cast<u32>(pixel_shader_manager.custom_constants.size()); |
| 277 | |
| 278 | if (m_uniform_stream_buffer->ReserveMemory(m_uniform_buffer_reserve_size + custom_constants_size, |
| 279 | g_vulkan_context->GetUniformBufferAlignment())) |
| 280 | { |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | // The only places that call constant updates are safe to have state restored. |
| 285 | WARN_LOG_FMT(VIDEO, "Executing command buffer while waiting for space in uniform buffer"); |
| 286 | VKGfx::GetInstance()->ExecuteCommandBuffer(false); |
| 287 | |
| 288 | // Since we are on a new command buffer, all constants have been invalidated, and we need |
| 289 | // to reupload them. We may as well do this now, since we're issuing a draw anyway. |
| 290 | UploadAllConstants(); |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | void VertexManager::UploadAllConstants() |
| 295 | { |
| 296 | auto& system = Core::System::GetInstance(); |
| 297 | auto& pixel_shader_manager = system.GetPixelShaderManager(); |
| 298 | |
| 299 | const u32 custom_constants_size = static_cast<u32>(pixel_shader_manager.custom_constants.size()); |
| 300 | |
| 301 | // We are free to re-use parts of the buffer now since we're uploading all constants. |
| 302 | const u32 ub_alignment = static_cast<u32>(g_vulkan_context->GetUniformBufferAlignment()); |
| 303 | const u32 pixel_constants_offset = 0; |
| 304 | const u32 vertex_constants_offset = |
| 305 | Common::AlignUp(pixel_constants_offset + sizeof(PixelShaderConstants), ub_alignment); |
| 306 | const u32 geometry_constants_offset = |
| 307 | Common::AlignUp(vertex_constants_offset + sizeof(VertexShaderConstants), ub_alignment); |
| 308 | const u32 custom_pixel_constants_offset = |
| 309 | Common::AlignUp(geometry_constants_offset + sizeof(GeometryShaderConstants), ub_alignment); |
| 310 | const u32 allocation_size = custom_pixel_constants_offset + custom_constants_size; |
| 311 | |
| 312 | // Allocate everything at once. |
| 313 | // We should only be here if the buffer was full and a command buffer was submitted anyway. |
| 314 | if (!m_uniform_stream_buffer->ReserveMemory(allocation_size, ub_alignment)) |
| 315 | { |
| 316 | PanicAlertFmt("Failed to allocate space for constants in streaming buffer"); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | auto& vertex_shader_manager = system.GetVertexShaderManager(); |
| 321 | auto& geometry_shader_manager = system.GetGeometryShaderManager(); |
| 322 | |
| 323 | // Update bindings |
| 324 | StateTracker::GetInstance()->SetGXUniformBuffer( |
| 325 | UBO_DESCRIPTOR_SET_BINDING_PS, m_uniform_stream_buffer->GetBuffer(), |
| 326 | m_uniform_stream_buffer->GetCurrentOffset() + pixel_constants_offset, |
| 327 | sizeof(PixelShaderConstants)); |
| 328 | StateTracker::GetInstance()->SetGXUniformBuffer( |
| 329 | UBO_DESCRIPTOR_SET_BINDING_VS, m_uniform_stream_buffer->GetBuffer(), |
| 330 | m_uniform_stream_buffer->GetCurrentOffset() + vertex_constants_offset, |
| 331 | sizeof(VertexShaderConstants)); |
| 332 | |
| 333 | if (!pixel_shader_manager.custom_constants.empty()) |
| 334 | { |
| 335 | StateTracker::GetInstance()->SetGXUniformBuffer( |
| 336 | UBO_DESCRIPTOR_SET_BINDING_CUST, m_uniform_stream_buffer->GetBuffer(), |
| 337 | m_uniform_stream_buffer->GetCurrentOffset() + custom_pixel_constants_offset, |
| 338 | custom_constants_size); |
| 339 | } |
| 340 | StateTracker::GetInstance()->SetGXUniformBuffer( |
| 341 | UBO_DESCRIPTOR_SET_BINDING_GS, m_uniform_stream_buffer->GetBuffer(), |
| 342 | m_uniform_stream_buffer->GetCurrentOffset() + geometry_constants_offset, |
| 343 | sizeof(GeometryShaderConstants)); |
| 344 | |
| 345 | // Copy the actual data in |
| 346 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer() + pixel_constants_offset, |
| 347 | &pixel_shader_manager.constants, sizeof(PixelShaderConstants)); |
| 348 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer() + vertex_constants_offset, |
| 349 | &vertex_shader_manager.constants, sizeof(VertexShaderConstants)); |
| 350 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer() + geometry_constants_offset, |
| 351 | &geometry_shader_manager.constants, sizeof(GeometryShaderConstants)); |
| 352 | if (!pixel_shader_manager.custom_constants.empty()) |
| 353 | { |
| 354 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer() + custom_pixel_constants_offset, |
| 355 | pixel_shader_manager.custom_constants.data(), |
| 356 | pixel_shader_manager.custom_constants.size()); |
| 357 | } |
| 358 | |
| 359 | // Finally, flush buffer memory after copying |
| 360 | m_uniform_stream_buffer->CommitMemory(allocation_size); |
| 361 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, allocation_size); |
| 362 | |
| 363 | // Clear dirty flags |
| 364 | vertex_shader_manager.dirty = false; |
| 365 | geometry_shader_manager.dirty = false; |
| 366 | pixel_shader_manager.dirty = false; |
| 367 | } |
| 368 | |
| 369 | void VertexManager::UploadUtilityUniforms(const void* data, u32 data_size) |
| 370 | { |
| 371 | InvalidateConstants(); |
| 372 | if (!m_uniform_stream_buffer->ReserveMemory(data_size, |
| 373 | g_vulkan_context->GetUniformBufferAlignment())) |
| 374 | { |
| 375 | WARN_LOG_FMT(VIDEO, "Executing command buffer while waiting for ext space in uniform buffer"); |
| 376 | VKGfx::GetInstance()->ExecuteCommandBuffer(false); |
| 377 | } |
| 378 | |
| 379 | StateTracker::GetInstance()->SetUtilityUniformBuffer( |
| 380 | m_uniform_stream_buffer->GetBuffer(), m_uniform_stream_buffer->GetCurrentOffset(), data_size); |
| 381 | std::memcpy(m_uniform_stream_buffer->GetCurrentHostPointer(), data, data_size); |
| 382 | m_uniform_stream_buffer->CommitMemory(data_size); |
| 383 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size); |
| 384 | } |
| 385 | |
| 386 | bool VertexManager::UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format, |
| 387 | u32* out_offset) |
| 388 | { |
| 389 | if (data_size > m_texel_stream_buffer->GetCurrentSize()) |
| 390 | return false; |
| 391 | |
| 392 | const u32 elem_size = GetTexelBufferElementSize(format); |
| 393 | if (!m_texel_stream_buffer->ReserveMemory(data_size, elem_size)) |
| 394 | { |
| 395 | // Try submitting cmdbuffer. |
| 396 | WARN_LOG_FMT(VIDEO, "Submitting command buffer while waiting for space in texel buffer"); |
| 397 | VKGfx::GetInstance()->ExecuteCommandBuffer(false, false); |
| 398 | if (!m_texel_stream_buffer->ReserveMemory(data_size, elem_size)) |
| 399 | { |
| 400 | PanicAlertFmt("Failed to allocate {} bytes from texel buffer", data_size); |
| 401 | return false; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | std::memcpy(m_texel_stream_buffer->GetCurrentHostPointer(), data, data_size); |
| 406 | *out_offset = static_cast<u32>(m_texel_stream_buffer->GetCurrentOffset()) / elem_size; |
| 407 | m_texel_stream_buffer->CommitMemory(data_size); |
| 408 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size); |
| 409 | StateTracker::GetInstance()->SetTexelBuffer(0, m_texel_buffer_views[format]); |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | bool VertexManager::UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format, |
| 414 | u32* out_offset, const void* palette_data, u32 palette_size, |
| 415 | TexelBufferFormat palette_format, u32* out_palette_offset) |
| 416 | { |
| 417 | const u32 elem_size = GetTexelBufferElementSize(format); |
| 418 | const u32 palette_elem_size = GetTexelBufferElementSize(palette_format); |
| 419 | const u32 reserve_size = data_size + palette_size + palette_elem_size; |
| 420 | if (reserve_size > m_texel_stream_buffer->GetCurrentSize()) |
| 421 | return false; |
| 422 | |
| 423 | if (!m_texel_stream_buffer->ReserveMemory(reserve_size, elem_size)) |
| 424 | { |
| 425 | // Try submitting cmdbuffer. |
| 426 | WARN_LOG_FMT(VIDEO, "Submitting command buffer while waiting for space in texel buffer"); |
| 427 | VKGfx::GetInstance()->ExecuteCommandBuffer(false, false); |
| 428 | if (!m_texel_stream_buffer->ReserveMemory(reserve_size, elem_size)) |
| 429 | { |
| 430 | PanicAlertFmt("Failed to allocate {} bytes from texel buffer", reserve_size); |
| 431 | return false; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | const u32 palette_byte_offset = Common::AlignUp(data_size, palette_elem_size); |
| 436 | std::memcpy(m_texel_stream_buffer->GetCurrentHostPointer(), data, data_size); |
| 437 | std::memcpy(m_texel_stream_buffer->GetCurrentHostPointer() + palette_byte_offset, palette_data, |
| 438 | palette_size); |
| 439 | *out_offset = static_cast<u32>(m_texel_stream_buffer->GetCurrentOffset()) / elem_size; |
| 440 | *out_palette_offset = |
| 441 | (static_cast<u32>(m_texel_stream_buffer->GetCurrentOffset()) + palette_byte_offset) / |
| 442 | palette_elem_size; |
| 443 | |
| 444 | m_texel_stream_buffer->CommitMemory(palette_byte_offset + palette_size); |
| 445 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, palette_byte_offset + palette_size); |
| 446 | StateTracker::GetInstance()->SetTexelBuffer(0, m_texel_buffer_views[format]); |
| 447 | StateTracker::GetInstance()->SetTexelBuffer(1, m_texel_buffer_views[palette_format]); |
| 448 | return true; |
| 449 | } |
| 450 | } // namespace Vulkan |