Commit 9c79d004 authored by Laszlo Agocs's avatar Laszlo Agocs
Browse files

vk: use real, not the default values when doing the swapchain's renderpass

Also revise how the renderpass is handled when wrapping a QVulkanWindow.
parent d7508e37
...@@ -71,23 +71,20 @@ void Renderer::initResources() ...@@ -71,23 +71,20 @@ void Renderer::initResources()
params.gfxQueue = m_window->graphicsQueue(); params.gfxQueue = m_window->graphicsQueue();
m_r = QRhi::create(QRhi::Vulkan, &params); m_r = QRhi::create(QRhi::Vulkan, &params);
m_sc = m_r->newSwapChain();
m_sc->setTarget(m_window); // note: very different from setWindow(m_window)
m_scrp = m_sc->newCompatibleRenderPassDescriptor();
m_sc->setRenderPassDescriptor(m_scrp);
m_triRenderer.setRhi(m_r); m_triRenderer.setRhi(m_r);
m_triRenderer.setSampleCount(SAMPLES); m_triRenderer.setSampleCount(SAMPLES);
m_initPending = true; m_triRenderer.initResources(m_scrp);
m_sc = m_r->newSwapChain();
} }
void Renderer::initSwapChainResources() void Renderer::initSwapChainResources()
{ {
m_sc->setTarget(m_window); // note: very different from setWindow(m_window)
m_sc->buildOrResize(); // this just wraps the qvulkanwindow's swapchain and other resources m_sc->buildOrResize(); // this just wraps the qvulkanwindow's swapchain and other resources
if (m_initPending) {
m_initPending = false;
// had to defer init until we can query the imported renderpass
m_triRenderer.initResources(m_sc->renderPassDescriptor());
}
m_triRenderer.resize(m_sc->effectivePixelSize()); m_triRenderer.resize(m_sc->effectivePixelSize());
} }
...@@ -103,6 +100,11 @@ void Renderer::releaseResources() ...@@ -103,6 +100,11 @@ void Renderer::releaseResources()
delete m_sc; delete m_sc;
m_sc = nullptr; m_sc = nullptr;
if (m_scrp) {
m_scrp->releaseAndDestroy();
m_scrp = nullptr;
}
delete m_r; delete m_r;
m_r = nullptr; m_r = nullptr;
} }
......
...@@ -69,6 +69,6 @@ protected: ...@@ -69,6 +69,6 @@ protected:
QVulkanWindow *m_window; QVulkanWindow *m_window;
QRhi *m_r = nullptr; QRhi *m_r = nullptr;
QRhiSwapChain *m_sc = nullptr; QRhiSwapChain *m_sc = nullptr;
bool m_initPending = false; QRhiRenderPassDescriptor *m_scrp = nullptr;
TriangleRenderer m_triRenderer; TriangleRenderer m_triRenderer;
}; };
...@@ -2733,6 +2733,11 @@ void QVkRenderPassDescriptor::release() ...@@ -2733,6 +2733,11 @@ void QVkRenderPassDescriptor::release()
if (!rp) if (!rp)
return; return;
if (!ownsRp) {
rp = VK_NULL_HANDLE;
return;
}
QRhiVulkan::DeferredReleaseEntry e; QRhiVulkan::DeferredReleaseEntry e;
e.type = QRhiVulkan::DeferredReleaseEntry::RenderPass; e.type = QRhiVulkan::DeferredReleaseEntry::RenderPass;
e.lastActiveFrameSlot = lastActiveFrameSlot; e.lastActiveFrameSlot = lastActiveFrameSlot;
...@@ -2815,6 +2820,8 @@ QRhiRenderPassDescriptor *QVkTextureRenderTarget::newCompatibleRenderPassDescrip ...@@ -2815,6 +2820,8 @@ QRhiRenderPassDescriptor *QVkTextureRenderTarget::newCompatibleRenderPassDescrip
delete rp; delete rp;
return nullptr; return nullptr;
} }
rp->ownsRp = true;
return rp; return rp;
} }
...@@ -3217,11 +3224,8 @@ QVkSwapChain::QVkSwapChain(QRhiImplementation *rhi) ...@@ -3217,11 +3224,8 @@ QVkSwapChain::QVkSwapChain(QRhiImplementation *rhi)
void QVkSwapChain::release() void QVkSwapChain::release()
{ {
if (wrapWindow) { if (wrapWindow)
delete rtWrapper.d.rp;
rtWrapper.d.rp = nullptr;
return; return;
}
QRHI_RES_RHI(QRhiVulkan); QRHI_RES_RHI(QRhiVulkan);
rhiD->releaseSwapChainResources(this); rhiD->releaseSwapChainResources(this);
...@@ -3246,44 +3250,38 @@ QRhiRenderPassDescriptor *QVkSwapChain::newCompatibleRenderPassDescriptor() ...@@ -3246,44 +3250,38 @@ QRhiRenderPassDescriptor *QVkSwapChain::newCompatibleRenderPassDescriptor()
{ {
// not yet built so cannot rely on data computed in buildOrResize() // not yet built so cannot rely on data computed in buildOrResize()
if (m_target) {
QVulkanWindow *vkw = qobject_cast<QVulkanWindow *>(m_target);
if (vkw) {
QVkRenderPassDescriptor *rp = new QVkRenderPassDescriptor(rhi);
rp->rp = vkw->defaultRenderPass();
rp->ownsRp = false;
return rp;
}
return nullptr;
}
if (!ensureSurface()) // make sure sampleCount and colorFormat reflect what was requested
return nullptr;
QRHI_RES_RHI(QRhiVulkan); QRHI_RES_RHI(QRhiVulkan);
QVkRenderPassDescriptor *rp = new QVkRenderPassDescriptor(rhi); QVkRenderPassDescriptor *rp = new QVkRenderPassDescriptor(rhi);
if (!rhiD->createDefaultRenderPass(&rp->rp, if (!rhiD->createDefaultRenderPass(&rp->rp,
m_depthStencil != nullptr, m_depthStencil != nullptr,
rhiD->effectiveSampleCount(m_sampleCount), sampleCount,
colorFormat)) colorFormat))
{ {
delete rp; delete rp;
return nullptr; return nullptr;
} }
rp->ownsRp = true;
return rp; return rp;
} }
bool QVkSwapChain::buildOrResize() bool QVkSwapChain::ensureSurface()
{ {
if (m_target) { surface = QVulkanInstance::surfaceForWindow(m_window);
if (sc)
release();
QVulkanWindow *vkw = qobject_cast<QVulkanWindow *>(m_target);
if (vkw) {
rtWrapper.d.rp = new QVkRenderPassDescriptor(rhi);
rtWrapper.d.rp->rp = vkw->defaultRenderPass();
m_renderPassDesc = rtWrapper.d.rp;
m_requestedPixelSize = pixelSize = rtWrapper.d.pixelSize = vkw->swapChainImageSize();
rtWrapper.d.colorAttCount = 1;
rtWrapper.d.dsAttCount = 1;
rtWrapper.d.msaaAttCount = vkw->sampleCountFlagBits() > VK_SAMPLE_COUNT_1_BIT ? 1 : 0;
wrapWindow = vkw;
return true;
}
return false;
}
// Can be called multiple times due to window resizes - that is not the
// same as a simple release+build (as with other resources). Thus no
// release() here. See recreateSwapChain() below.
VkSurfaceKHR surface = QVulkanInstance::surfaceForWindow(m_window);
if (!surface) { if (!surface) {
qWarning("Failed to get surface for window"); qWarning("Failed to get surface for window");
return false; return false;
...@@ -3314,11 +3312,42 @@ bool QVkSwapChain::buildOrResize() ...@@ -3314,11 +3312,42 @@ bool QVkSwapChain::buildOrResize()
} }
sampleCount = rhiD->effectiveSampleCount(m_sampleCount); sampleCount = rhiD->effectiveSampleCount(m_sampleCount);
return true;
}
bool QVkSwapChain::buildOrResize()
{
if (m_target) {
if (sc)
release();
QVulkanWindow *vkw = qobject_cast<QVulkanWindow *>(m_target);
if (vkw) {
rtWrapper.d.rp = QRHI_RES(QVkRenderPassDescriptor, m_renderPassDesc);
Q_ASSERT(rtWrapper.d.rp && rtWrapper.d.rp->rp);
m_requestedPixelSize = pixelSize = rtWrapper.d.pixelSize = vkw->swapChainImageSize();
rtWrapper.d.colorAttCount = 1;
rtWrapper.d.dsAttCount = 1;
rtWrapper.d.msaaAttCount = vkw->sampleCountFlagBits() > VK_SAMPLE_COUNT_1_BIT ? 1 : 0;
wrapWindow = vkw;
return true;
}
return false;
}
// Can be called multiple times due to window resizes - that is not the
// same as a simple release+build (as with other resources). Thus no
// release() here. See recreateSwapChain() below.
if (!ensureSurface())
return false;
if (m_depthStencil && m_depthStencil->sampleCount() != m_sampleCount) { if (m_depthStencil && m_depthStencil->sampleCount() != m_sampleCount) {
qWarning("Depth-stencil buffer's sampleCount (%d) does not match color buffers' sample count (%d). Expect problems.", qWarning("Depth-stencil buffer's sampleCount (%d) does not match color buffers' sample count (%d). Expect problems.",
m_depthStencil->sampleCount(), m_sampleCount); m_depthStencil->sampleCount(), m_sampleCount);
} }
QRHI_RES_RHI(QRhiVulkan);
if (!rhiD->recreateSwapChain(surface, m_requestedPixelSize, m_flags, this)) if (!rhiD->recreateSwapChain(surface, m_requestedPixelSize, m_flags, this))
return false; return false;
......
...@@ -129,6 +129,7 @@ struct QVkRenderPassDescriptor : public QRhiRenderPassDescriptor ...@@ -129,6 +129,7 @@ struct QVkRenderPassDescriptor : public QRhiRenderPassDescriptor
void release() override; void release() override;
VkRenderPass rp = VK_NULL_HANDLE; VkRenderPass rp = VK_NULL_HANDLE;
bool ownsRp = false;
int lastActiveFrameSlot = -1; int lastActiveFrameSlot = -1;
}; };
...@@ -254,6 +255,8 @@ struct QVkSwapChain : public QRhiSwapChain ...@@ -254,6 +255,8 @@ struct QVkSwapChain : public QRhiSwapChain
QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override; QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override;
bool buildOrResize() override; bool buildOrResize() override;
bool ensureSurface();
static const int DEFAULT_BUFFER_COUNT = 2; static const int DEFAULT_BUFFER_COUNT = 2;
static const int MAX_BUFFER_COUNT = 3; static const int MAX_BUFFER_COUNT = 3;
...@@ -262,6 +265,7 @@ struct QVkSwapChain : public QRhiSwapChain ...@@ -262,6 +265,7 @@ struct QVkSwapChain : public QRhiSwapChain
bool supportsReadback = false; bool supportsReadback = false;
VkSwapchainKHR sc = VK_NULL_HANDLE; VkSwapchainKHR sc = VK_NULL_HANDLE;
int bufferCount = 0; int bufferCount = 0;
VkSurfaceKHR surface = VK_NULL_HANDLE;
VkFormat colorFormat = VK_FORMAT_B8G8R8A8_UNORM; VkFormat colorFormat = VK_FORMAT_B8G8R8A8_UNORM;
VkColorSpaceKHR colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; VkColorSpaceKHR colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
QVkRenderBuffer *ds = nullptr; QVkRenderBuffer *ds = nullptr;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment