VK: Added descriptor pool per frame.

This commit is contained in:
Бранимир Караџић
2025-11-04 14:15:49 -08:00
parent 5e71b83296
commit 0fb116cf45
2 changed files with 39 additions and 33 deletions

View File

@@ -1907,12 +1907,12 @@ VK_IMPORT_DEVICE
vkGetDeviceQueue(m_device, m_globalQueueFamily, 0, &m_globalQueue); vkGetDeviceQueue(m_device, m_globalQueueFamily, 0, &m_globalQueue);
{ {
m_numFramesInFlight = _init.resolution.maxFrameLatency == 0 m_maxFrameLatency = _init.resolution.maxFrameLatency == 0
? BGFX_CONFIG_MAX_FRAME_LATENCY ? BGFX_CONFIG_MAX_FRAME_LATENCY
: _init.resolution.maxFrameLatency : _init.resolution.maxFrameLatency
; ;
result = m_cmd.init(m_globalQueueFamily, m_globalQueue, m_numFramesInFlight); result = m_cmd.init(m_globalQueueFamily, m_globalQueue);
if (VK_SUCCESS != result) if (VK_SUCCESS != result)
{ {
@@ -1990,12 +1990,15 @@ VK_IMPORT_DEVICE
dpci.poolSizeCount = BX_COUNTOF(dps); dpci.poolSizeCount = BX_COUNTOF(dps);
dpci.pPoolSizes = dps; dpci.pPoolSizes = dps;
result = vkCreateDescriptorPool(m_device, &dpci, m_allocatorCb, &m_descriptorPool); for (uint32_t ii = 0; ii < m_maxFrameLatency; ++ii)
if (VK_SUCCESS != result)
{ {
BX_TRACE("Init error: vkCreateDescriptorPool failed %d: %s.", result, getName(result) ); result = vkCreateDescriptorPool(m_device, &dpci, m_allocatorCb, &m_descriptorPool[ii]);
goto error;
if (VK_SUCCESS != result)
{
BX_TRACE("Init error: vkCreateDescriptorPool failed %d: %s.", result, getName(result) );
goto error;
}
} }
VkPipelineCacheCreateInfo pcci; VkPipelineCacheCreateInfo pcci;
@@ -2017,13 +2020,13 @@ VK_IMPORT_DEVICE
const uint32_t size = 128; const uint32_t size = 128;
const uint32_t count = BGFX_CONFIG_MAX_DRAW_CALLS; const uint32_t count = BGFX_CONFIG_MAX_DRAW_CALLS;
for (uint32_t ii = 0; ii < m_numFramesInFlight; ++ii) for (uint32_t ii = 0; ii < m_maxFrameLatency; ++ii)
{ {
BX_TRACE("Create scratch buffer %d", ii); BX_TRACE("Create scratch buffer %d", ii);
m_scratchBuffer[ii].createUniform(size, count); m_scratchBuffer[ii].createUniform(size, count);
} }
for (uint32_t ii = 0; ii < m_numFramesInFlight; ++ii) for (uint32_t ii = 0; ii < m_maxFrameLatency; ++ii)
{ {
BX_TRACE("Create scratch staging buffer %d", ii); BX_TRACE("Create scratch staging buffer %d", ii);
m_scratchStagingBuffer[ii].createStaging(BGFX_CONFIG_MAX_SCRATCH_STAGING_BUFFER_PER_FRAME_SIZE); m_scratchStagingBuffer[ii].createStaging(BGFX_CONFIG_MAX_SCRATCH_STAGING_BUFFER_PER_FRAME_SIZE);
@@ -2091,13 +2094,13 @@ VK_IMPORT_DEVICE
[[fallthrough]]; [[fallthrough]];
case ErrorState::DescriptorCreated: case ErrorState::DescriptorCreated:
for (uint32_t ii = 0; ii < m_numFramesInFlight; ++ii) for (uint32_t ii = 0; ii < m_maxFrameLatency; ++ii)
{ {
m_scratchBuffer[ii].destroy(); m_scratchBuffer[ii].destroy();
m_scratchStagingBuffer[ii].destroy(); m_scratchStagingBuffer[ii].destroy();
vkDestroy(m_descriptorPool[ii]);
} }
vkDestroy(m_pipelineCache); vkDestroy(m_pipelineCache);
vkDestroy(m_descriptorPool);
[[fallthrough]]; [[fallthrough]];
case ErrorState::SwapChainCreated: case ErrorState::SwapChainCreated:
@@ -2154,12 +2157,12 @@ VK_IMPORT_DEVICE
m_samplerBorderColorCache.invalidate(); m_samplerBorderColorCache.invalidate();
m_imageViewCache.invalidate(); m_imageViewCache.invalidate();
for (uint32_t ii = 0; ii < m_numFramesInFlight; ++ii) for (uint32_t ii = 0; ii < m_maxFrameLatency; ++ii)
{ {
m_scratchBuffer[ii].destroy(); m_scratchBuffer[ii].destroy();
} }
for (uint32_t ii = 0; ii < m_numFramesInFlight; ++ii) for (uint32_t ii = 0; ii < m_maxFrameLatency; ++ii)
{ {
m_scratchStagingBuffer[ii].destroy(); m_scratchStagingBuffer[ii].destroy();
} }
@@ -2196,7 +2199,11 @@ VK_IMPORT_DEVICE
m_cmd.shutdown(); m_cmd.shutdown();
vkDestroy(m_pipelineCache); vkDestroy(m_pipelineCache);
vkDestroy(m_descriptorPool);
for (uint32_t ii = 0; ii < m_maxFrameLatency; ++ii)
{
vkDestroy(m_descriptorPool[ii]);
}
vkDestroyDevice(m_device, m_allocatorCb); vkDestroyDevice(m_device, m_allocatorCb);
@@ -3857,7 +3864,7 @@ VK_IMPORT_DEVICE
VkDescriptorSetAllocateInfo dsai; VkDescriptorSetAllocateInfo dsai;
dsai.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; dsai.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
dsai.pNext = NULL; dsai.pNext = NULL;
dsai.descriptorPool = m_descriptorPool; dsai.descriptorPool = m_descriptorPool[m_cmd.m_currentFrameInFlight];
dsai.descriptorSetCount = 1; dsai.descriptorSetCount = 1;
dsai.pSetLayouts = &program.m_descriptorSetLayout; dsai.pSetLayouts = &program.m_descriptorSetLayout;
@@ -4590,14 +4597,14 @@ VK_IMPORT_DEVICE
ScratchBufferVK m_scratchBuffer[BGFX_CONFIG_MAX_FRAME_LATENCY]; ScratchBufferVK m_scratchBuffer[BGFX_CONFIG_MAX_FRAME_LATENCY];
ScratchBufferVK m_scratchStagingBuffer[BGFX_CONFIG_MAX_FRAME_LATENCY]; ScratchBufferVK m_scratchStagingBuffer[BGFX_CONFIG_MAX_FRAME_LATENCY];
uint32_t m_numFramesInFlight; uint32_t m_maxFrameLatency;
CommandQueueVK m_cmd; CommandQueueVK m_cmd;
VkCommandBuffer m_commandBuffer; VkCommandBuffer m_commandBuffer;
VkDevice m_device; VkDevice m_device;
uint32_t m_globalQueueFamily; uint32_t m_globalQueueFamily;
VkQueue m_globalQueue; VkQueue m_globalQueue;
VkDescriptorPool m_descriptorPool; VkDescriptorPool m_descriptorPool[BGFX_CONFIG_MAX_FRAME_LATENCY];
VkPipelineCache m_pipelineCache; VkPipelineCache m_pipelineCache;
TimerQueryVK m_gpuTimer; TimerQueryVK m_gpuTimer;
@@ -4703,8 +4710,6 @@ VK_DESTROY
{ {
if (VK_NULL_HANDLE != _obj) if (VK_NULL_HANDLE != _obj)
{ {
BGFX_PROFILER_SCOPE("vkFreeDescriptorSets", kColorResource);
vkFreeDescriptorSets(s_renderVK->m_device, s_renderVK->m_descriptorPool, 1, &_obj);
_obj = VK_NULL_HANDLE; _obj = VK_NULL_HANDLE;
} }
} }
@@ -5663,7 +5668,7 @@ VK_DESTROY
Query& query = m_query[_idx]; Query& query = m_query[_idx];
query.m_ready = true; query.m_ready = true;
query.m_completed = s_renderVK->m_cmd.m_submitted + s_renderVK->m_cmd.m_numFramesInFlight; query.m_completed = s_renderVK->m_cmd.m_submitted + s_renderVK->m_maxFrameLatency;
const VkCommandBuffer commandBuffer = s_renderVK->m_commandBuffer; const VkCommandBuffer commandBuffer = s_renderVK->m_commandBuffer;
const uint32_t offset = _idx * 2 + 0; const uint32_t offset = _idx * 2 + 0;
@@ -8237,13 +8242,12 @@ VK_DESTROY
; ;
} }
VkResult CommandQueueVK::init(uint32_t _queueFamily, VkQueue _queue, uint32_t _numFramesInFlight) VkResult CommandQueueVK::init(uint32_t _queueFamily, VkQueue _queue)
{ {
m_queueFamily = _queueFamily; m_queueFamily = _queueFamily;
m_queue = _queue; m_queue = _queue;
m_numFramesInFlight = bx::clamp<uint32_t>(_numFramesInFlight, 1, BGFX_CONFIG_MAX_FRAME_LATENCY);
m_activeCommandBuffer = VK_NULL_HANDLE; m_activeCommandBuffer = VK_NULL_HANDLE;
m_consumeIndex = 0; m_consumeIndex = 0;
return reset(); return reset();
} }
@@ -8286,7 +8290,7 @@ VK_DESTROY
const VkAllocationCallbacks* allocatorCb = s_renderVK->m_allocatorCb; const VkAllocationCallbacks* allocatorCb = s_renderVK->m_allocatorCb;
const VkDevice device = s_renderVK->m_device; const VkDevice device = s_renderVK->m_device;
for (uint32_t ii = 0; ii < m_numFramesInFlight; ++ii) for (uint32_t ii = 0, maxFrameLatency = s_renderVK->m_maxFrameLatency; ii < maxFrameLatency; ++ii)
{ {
result = vkCreateCommandPool( result = vkCreateCommandPool(
device device
@@ -8337,7 +8341,7 @@ VK_DESTROY
kick(true); kick(true);
finish(true); finish(true);
for (uint32_t ii = 0; ii < m_numFramesInFlight; ++ii) for (uint32_t ii = 0, maxFrameLatency = s_renderVK->m_maxFrameLatency; ii < maxFrameLatency; ++ii)
{ {
vkDestroy(m_commandList[ii].m_fence); vkDestroy(m_commandList[ii].m_fence);
m_commandList[ii].m_commandBuffer = VK_NULL_HANDLE; m_commandList[ii].m_commandBuffer = VK_NULL_HANDLE;
@@ -8468,7 +8472,7 @@ VK_DESTROY
m_activeCommandBuffer = VK_NULL_HANDLE; m_activeCommandBuffer = VK_NULL_HANDLE;
m_currentFrameInFlight = (m_currentFrameInFlight + 1) % m_numFramesInFlight; m_currentFrameInFlight = (m_currentFrameInFlight + 1) % s_renderVK->m_maxFrameLatency;
m_submitted++; m_submitted++;
} }
} }
@@ -8479,7 +8483,7 @@ VK_DESTROY
if (_finishAll) if (_finishAll)
{ {
for (uint32_t ii = 0; ii < m_numFramesInFlight; ++ii) for (uint32_t ii = 0, maxFrameLatency = s_renderVK->m_maxFrameLatency; ii < maxFrameLatency; ++ii)
{ {
consume(); consume();
} }
@@ -8509,12 +8513,13 @@ VK_DESTROY
{ {
BGFX_PROFILER_SCOPE("CommandQueueVK::consume", kColorResource); BGFX_PROFILER_SCOPE("CommandQueueVK::consume", kColorResource);
m_consumeIndex = (m_consumeIndex + 1) % m_numFramesInFlight; m_consumeIndex = (m_consumeIndex + 1) % s_renderVK->m_maxFrameLatency;
for (DeviceMemoryAllocationVK &alloc : m_recycleAllocs[m_consumeIndex]) for (DeviceMemoryAllocationVK &alloc : m_recycleAllocs[m_consumeIndex])
{ {
s_renderVK->m_memoryLru.recycle(alloc); s_renderVK->m_memoryLru.recycle(alloc);
} }
m_recycleAllocs[m_consumeIndex].clear(); m_recycleAllocs[m_consumeIndex].clear();
for (const Resource& resource : m_release[m_consumeIndex]) for (const Resource& resource : m_release[m_consumeIndex])
@@ -8755,6 +8760,9 @@ VK_DESTROY
const uint64_t f2 = BGFX_STATE_BLEND_FACTOR<<4; const uint64_t f2 = BGFX_STATE_BLEND_FACTOR<<4;
const uint64_t f3 = BGFX_STATE_BLEND_INV_FACTOR<<4; const uint64_t f3 = BGFX_STATE_BLEND_INV_FACTOR<<4;
VkDescriptorPool& descriptorPool = m_descriptorPool[m_cmd.m_currentFrameInFlight];
vkResetDescriptorPool(m_device, descriptorPool, 0);
ScratchBufferVK& scratchBuffer = m_scratchBuffer[m_cmd.m_currentFrameInFlight]; ScratchBufferVK& scratchBuffer = m_scratchBuffer[m_cmd.m_currentFrameInFlight];
ScratchBufferVK& scratchStagingBuffer = m_scratchStagingBuffer[m_cmd.m_currentFrameInFlight]; ScratchBufferVK& scratchStagingBuffer = m_scratchStagingBuffer[m_cmd.m_currentFrameInFlight];

View File

@@ -876,7 +876,7 @@ VK_DESTROY_FUNC(DescriptorSet);
struct CommandQueueVK struct CommandQueueVK
{ {
VkResult init(uint32_t _queueFamily, VkQueue _queue, uint32_t _numFramesInFlight); VkResult init(uint32_t _queueFamily, VkQueue _queue);
VkResult reset(); VkResult reset();
void shutdown(); void shutdown();
@@ -893,8 +893,6 @@ VK_DESTROY_FUNC(DescriptorSet);
uint32_t m_queueFamily; uint32_t m_queueFamily;
VkQueue m_queue; VkQueue m_queue;
uint32_t m_numFramesInFlight;
uint32_t m_currentFrameInFlight; uint32_t m_currentFrameInFlight;
uint32_t m_consumeIndex; uint32_t m_consumeIndex;