From e1eed2a3437f1f66f711a7aa1989b0f8d2c2ddf4 Mon Sep 17 00:00:00 2001 From: Dustinspace217 Date: Wed, 18 Nov 2026 21:36:40 -0800 Subject: [PATCH] Reuse texture upload fences in the Vulkan backend Every async texture upload created a fence and destroyed it when the upload completed. On at least one driver both calls are slow (about 0.5 ms each), so each streamed animation frame, such as a message portrait, paid about a millisecond, and mission load paid it once per texture. Keep completed fences and reuse them for later uploads. --- code/graphics/vulkan/VulkanTexture.cpp | 25 +++++++++++++++++++++---- code/graphics/vulkan/VulkanTexture.h | 3 +++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/code/graphics/vulkan/VulkanTexture.cpp b/code/graphics/vulkan/VulkanTexture.cpp index 24649f19165..ae34373eed1 100644 --- a/code/graphics/vulkan/VulkanTexture.cpp +++ b/code/graphics/vulkan/VulkanTexture.cpp @@ -298,6 +298,9 @@ bool VulkanTextureManager::init(vk::Device device, vk::PhysicalDevice physicalDe mprintf(("VulkanTextureManager: ETC2 Texture Support = %s\n", supportsETC2 ? "YES" : "NO")); mprintf(("VulkanTextureManager: BCn Texture Support = %s\n", supportsBC ? "YES" : "NO")); + // reserved up front so returning a fence to the list never allocates in the middle of retiring an upload + m_freeUploadFences.reserve(MAX_PENDING_UPLOADS); + m_initialized = true; return true; } @@ -311,6 +314,10 @@ void VulkanTextureManager::shutdown() // it before destroying the command pool or allocator during shutdown. m_graphicsQueue.waitIdle(); processPendingCommandBuffers(); + for (auto fence : m_freeUploadFences) { + m_device.destroyFence(fence); + } + m_freeUploadFences.clear(); // Destroy fallback 3D texture if (m_fallback3DView) { @@ -2778,12 +2785,11 @@ bool VulkanTextureManager::createStagingBuffer(size_t size, vk::Buffer& outBuffe { processPendingCommandBuffers(); constexpr size_t maxPendingUploadBytes = 64 * 1024 * 1024; - constexpr size_t maxPendingUploads = 64; // A single large upload may exceed this limit, but never accumulate more // uploads behind it. Waiting on an upload fence does not submit or retire // the render command buffer currently being recorded. while (!m_pendingCommandBuffers.empty() && - (m_pendingCommandBuffers.size() >= maxPendingUploads || m_pendingUploadBytes >= maxPendingUploadBytes || + (m_pendingCommandBuffers.size() >= MAX_PENDING_UPLOADS || m_pendingUploadBytes >= maxPendingUploadBytes || size > maxPendingUploadBytes - m_pendingUploadBytes)) { nprintf(("vulkan", "Texture upload budget: waiting with %zu uploads and %zu staging bytes pending\n", m_pendingCommandBuffers.size(), m_pendingUploadBytes)); @@ -2956,7 +2962,15 @@ void VulkanTextureManager::submitUploadAsync(vk::CommandBuffer cmd, vk::Buffer s submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &cmd; - auto fence = m_device.createFence(vk::FenceCreateInfo{}); + vk::Fence fence; + if (m_freeUploadFences.empty()) { + fence = m_device.createFence(vk::FenceCreateInfo{}); + } else { + // reset here, not on completion, so a failed reset cannot leave a half-released pending upload + m_device.resetFences(m_freeUploadFences.back()); + fence = m_freeUploadFences.back(); + m_freeUploadFences.pop_back(); + } try { m_graphicsQueue.submit(submitInfo, fence); } catch (...) { @@ -2965,6 +2979,9 @@ void VulkanTextureManager::submitUploadAsync(vk::CommandBuffer cmd, vk::Buffer s } m_pendingCommandBuffers.push_back({cmd, stagingBuffer, stagingAllocation, fence}); m_pendingUploadBytes += static_cast(stagingAllocation.size); + Assertion(m_pendingCommandBuffers.size() <= MAX_PENDING_UPLOADS, + "%zu texture uploads are pending; an upload skipped the budget wait in createStagingBuffer", + m_pendingCommandBuffers.size()); } void VulkanTextureManager::processPendingCommandBuffers() @@ -2978,7 +2995,7 @@ void VulkanTextureManager::processPendingCommandBuffers() m_device.destroyBuffer(pending.stagingBuffer); m_memoryManager->freeAllocation(pending.stagingAllocation); m_device.freeCommandBuffers(m_commandPool, pending.cb); - m_device.destroyFence(pending.fence); + m_freeUploadFences.push_back(pending.fence); ++completed; } if (completed != 0) { diff --git a/code/graphics/vulkan/VulkanTexture.h b/code/graphics/vulkan/VulkanTexture.h index 47a73b65b2c..df405448628 100644 --- a/code/graphics/vulkan/VulkanTexture.h +++ b/code/graphics/vulkan/VulkanTexture.h @@ -392,6 +392,9 @@ class VulkanTextureManager { }; SCP_vector m_pendingCommandBuffers; size_t m_pendingUploadBytes = 0; + static constexpr size_t MAX_PENDING_UPLOADS = 64; + // completed fences are kept for reuse because creating one per upload is slow on some drivers + SCP_vector m_freeUploadFences; vk::Device m_device; vk::PhysicalDevice m_physicalDevice;