Skip to content

Commit 6c0b3cb

Browse files
committed
CVulkanSwapchain allow 2 more acquiresInFlight
1 parent 51cbd88 commit 6c0b3cb

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

include/nbl/video/CVulkanSwapchain.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CVulkanSwapchain final : public ISwapchain
2727
inline uint8_t getMaxBlockingAcquiresBeforePresent() const override { return m_maxBlockingAcquiresBeforePresent; }
2828

2929
// returns the maximum number of acquires you can request without waiting for previous acquire semaphores to signal.
30-
uint8_t getMaxAcquiresInFlight() const override { return getImageCount(); }
30+
uint8_t getMaxAcquiresInFlight() const override { return m_maxAcquiresInFlight; }
3131

3232
private:
3333
CVulkanSwapchain(
@@ -39,7 +39,8 @@ class CVulkanSwapchain final : public ISwapchain
3939
const VkSemaphore* const _acquireAdaptorSemaphores,
4040
const VkSemaphore* const _prePresentSemaphores,
4141
const VkSemaphore* const _presentAdaptorSemaphores,
42-
const uint8_t maxAcquiresBeforePresent
42+
const uint8_t maxAcquiresBeforePresent,
43+
const uint8_t maxAcquiresInFlight
4344
);
4445
~CVulkanSwapchain();
4546

@@ -59,6 +60,7 @@ class CVulkanSwapchain final : public ISwapchain
5960
// nasty way to fight UB of the Vulkan spec
6061
bool m_needToWaitIdle = true;
6162
uint8_t m_maxBlockingAcquiresBeforePresent = 0u;
63+
uint8_t m_maxAcquiresInFlight = 0u;
6264
};
6365

6466
}

src/nbl/video/CVulkanSwapchain.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ core::smart_refctd_ptr<CVulkanSwapchain> CVulkanSwapchain::create(core::smart_re
138138
.pNext = nullptr,
139139
.flags = 0
140140
};
141+
142+
// imageCount + 2u, because we allow max 2 pending submissions to wait on image availability while others are being presented or rendered into.
143+
const uint8_t maxAcquiresinFlight = core::min(imageCount + 2u, ISwapchain::MaxImages);
141144
VkSemaphore semaphores[ISwapchain::MaxImages];
142-
for (auto i=0u; i<3*imageCount; i++)
143-
if (vk.vkCreateSemaphore(vk_device,i<imageCount ? (&timelineInfo):(&adaptorInfo),nullptr,semaphores+i)!=VK_SUCCESS)
145+
for (auto i=0u; i<3*maxAcquiresinFlight; i++)
146+
if (vk.vkCreateSemaphore(vk_device,i<maxAcquiresinFlight ? (&timelineInfo):(&adaptorInfo),nullptr,semaphores+i)!=VK_SUCCESS)
144147
{
145148
// handle successful allocs before failure
146149
for (auto j=0u; j<i; j++)
@@ -153,7 +156,7 @@ core::smart_refctd_ptr<CVulkanSwapchain> CVulkanSwapchain::create(core::smart_re
153156
// read https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap34.html#swapchain-acquire-forward-progress
154157
const uint8_t maxAcquiresWithoutPresent = core::min(imageCount,ISwapchain::MaxImages) - caps.minImageCount + 1u;
155158

156-
return core::smart_refctd_ptr<CVulkanSwapchain>(new CVulkanSwapchain(std::move(device),std::move(params),imageCount,std::move(oldSwapchain),vk_swapchain,semaphores+imageCount,semaphores,semaphores+2*imageCount, maxAcquiresWithoutPresent),core::dont_grab);
159+
return core::smart_refctd_ptr<CVulkanSwapchain>(new CVulkanSwapchain(std::move(device),std::move(params),imageCount,std::move(oldSwapchain),vk_swapchain,semaphores+maxAcquiresinFlight,semaphores,semaphores+2*maxAcquiresinFlight, maxAcquiresWithoutPresent, maxAcquiresinFlight),core::dont_grab);
157160
}
158161

159162
CVulkanSwapchain::CVulkanSwapchain(
@@ -165,7 +168,8 @@ CVulkanSwapchain::CVulkanSwapchain(
165168
const VkSemaphore* const _acquireAdaptorSemaphores,
166169
const VkSemaphore* const _prePresentSemaphores,
167170
const VkSemaphore* const _presentAdaptorSemaphores,
168-
const uint8_t maxAcquiresBeforePresent)
171+
const uint8_t maxAcquiresBeforePresent,
172+
const uint8_t maxAcquiresInFlight)
169173
: ISwapchain(std::move(logicalDevice),std::move(params),imageCount,std::move(oldSwapchain)),
170174
m_imgMemRequirements{.size=0,.memoryTypeBits=0x0u,.alignmentLog2=63,.prefersDedicatedAllocation=true,.requiresDedicatedAllocation=true}, m_vkSwapchainKHR(swapchain), m_maxBlockingAcquiresBeforePresent(maxAcquiresBeforePresent)
171175
{
@@ -179,8 +183,6 @@ CVulkanSwapchain::CVulkanSwapchain(
179183
auto retval = vulkanDevice->getFunctionTable()->vk.vkGetSwapchainImagesKHR(vulkanDevice->getInternalObject(),m_vkSwapchainKHR,&dummy,m_images);
180184
assert(retval==VK_SUCCESS && dummy==getImageCount());
181185
}
182-
const uint8_t maxAcquiresInFlight = getMaxAcquiresInFlight();
183-
assert(maxAcquiresInFlight == imageCount);
184186
assert(getMaxBlockingAcquiresBeforePresent() <= imageCount);
185187

186188
std::copy_n(_acquireAdaptorSemaphores,maxAcquiresInFlight,m_acquireAdaptorSemaphores);

0 commit comments

Comments
 (0)