Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions code/graphics/vulkan/VulkanTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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 (...) {
Expand All @@ -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<size_t>(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()
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions code/graphics/vulkan/VulkanTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ class VulkanTextureManager {
};
SCP_vector<PendingCommandBuffer> 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<vk::Fence> m_freeUploadFences;

vk::Device m_device;
vk::PhysicalDevice m_physicalDevice;
Expand Down
Loading