Commit 7970f0a1 authored by Laszlo Agocs's avatar Laszlo Agocs

Keep track of gfx res owning QRhiResources in all backends

parent f4266304
......@@ -155,6 +155,7 @@ public:
return p->rhiDWhenEnabled ? p : nullptr;
}
// only really care about resources that own native graphics resources underneath
void registerResource(QRhiResource *res)
{
res->orphanedWithRsh = nullptr;
......
......@@ -1688,6 +1688,8 @@ QD3D11Buffer::QD3D11Buffer(QRhiImplementation *rhi, Type type, UsageFlags usage,
void QD3D11Buffer::release()
{
QRHI_RES_RHI(QRhiD3D11);
if (!buffer)
return;
......@@ -1698,6 +1700,8 @@ void QD3D11Buffer::release()
QRHI_PROF;
QRHI_PROF_F(releaseBuffer(this));
rhiD->unregisterResource(this);
}
static inline uint toD3DBufferUsage(QRhiBuffer::UsageFlags usage)
......@@ -1746,6 +1750,7 @@ bool QD3D11Buffer::build()
QRHI_PROF_F(newBuffer(this, roundedSize, 1, m_type == Dynamic ? 1 : 0));
generation += 1;
rhiD->registerResource(this);
return true;
}
......@@ -1757,6 +1762,8 @@ QD3D11RenderBuffer::QD3D11RenderBuffer(QRhiImplementation *rhi, Type type, const
void QD3D11RenderBuffer::release()
{
QRHI_RES_RHI(QRhiD3D11);
if (!tex)
return;
......@@ -1775,6 +1782,8 @@ void QD3D11RenderBuffer::release()
QRHI_PROF;
QRHI_PROF_F(releaseRenderBuffer(this));
rhiD->unregisterResource(this);
}
bool QD3D11RenderBuffer::build()
......@@ -1844,6 +1853,7 @@ bool QD3D11RenderBuffer::build()
QRHI_PROF;
QRHI_PROF_F(newRenderBuffer(this, false, false, sampleDesc.Count));
rhiD->registerResource(this);
return true;
}
......@@ -1860,6 +1870,8 @@ QD3D11Texture::QD3D11Texture(QRhiImplementation *rhi, Format format, const QSize
void QD3D11Texture::release()
{
QRHI_RES_RHI(QRhiD3D11);
if (!tex)
return;
......@@ -1875,6 +1887,8 @@ void QD3D11Texture::release()
QRHI_PROF;
QRHI_PROF_F(releaseTexture(this));
rhiD->unregisterResource(this);
}
static inline DXGI_FORMAT toD3DDepthTextureSRVFormat(QRhiTexture::Format format)
......@@ -2028,11 +2042,14 @@ bool QD3D11Texture::build()
QRHI_PROF_F(newTexture(this, true, mipLevelCount, isCube ? 6 : 1, sampleDesc.Count));
owns = true;
rhiD->registerResource(this);
return true;
}
bool QD3D11Texture::buildFrom(const QRhiNativeHandles *src)
{
QRHI_RES_RHI(QRhiD3D11);
const QRhiD3D11TextureNativeHandles *h = static_cast<const QRhiD3D11TextureNativeHandles *>(src);
if (!h || !h->texture)
return false;
......@@ -2049,6 +2066,7 @@ bool QD3D11Texture::buildFrom(const QRhiNativeHandles *src)
QRHI_PROF_F(newTexture(this, false, mipLevelCount, m_flags.testFlag(CubeMap) ? 6 : 1, sampleDesc.Count));
owns = false;
rhiD->registerResource(this);
return true;
}
......@@ -2065,11 +2083,15 @@ QD3D11Sampler::QD3D11Sampler(QRhiImplementation *rhi, Filter magFilter, Filter m
void QD3D11Sampler::release()
{
QRHI_RES_RHI(QRhiD3D11);
if (!samplerState)
return;
samplerState->Release();
samplerState = nullptr;
rhiD->unregisterResource(this);
}
static inline D3D11_FILTER toD3DFilter(QRhiSampler::Filter minFilter, QRhiSampler::Filter magFilter, QRhiSampler::Filter mipFilter)
......@@ -2145,6 +2167,7 @@ bool QD3D11Sampler::build()
}
generation += 1;
rhiD->registerResource(this);
return true;
}
......@@ -2199,6 +2222,8 @@ QD3D11TextureRenderTarget::QD3D11TextureRenderTarget(QRhiImplementation *rhi,
void QD3D11TextureRenderTarget::release()
{
QRHI_RES_RHI(QRhiD3D11);
if (!rtv[0] && !dsv)
return;
......@@ -2215,6 +2240,8 @@ void QD3D11TextureRenderTarget::release()
rtv[i] = nullptr;
}
}
rhiD->unregisterResource(this);
}
QRhiRenderPassDescriptor *QD3D11TextureRenderTarget::newCompatibleRenderPassDescriptor()
......@@ -2310,6 +2337,7 @@ bool QD3D11TextureRenderTarget::build()
d.dsv = dsv;
d.rp = QRHI_RES(QD3D11RenderPassDescriptor, m_renderPassDesc);
rhiD->registerResource(this);
return true;
}
......@@ -2366,6 +2394,8 @@ QD3D11GraphicsPipeline::QD3D11GraphicsPipeline(QRhiImplementation *rhi)
void QD3D11GraphicsPipeline::release()
{
QRHI_RES_RHI(QRhiD3D11);
if (!dsState)
return;
......@@ -2396,6 +2426,8 @@ void QD3D11GraphicsPipeline::release()
fs->Release();
fs = nullptr;
}
rhiD->unregisterResource(this);
}
static inline D3D11_CULL_MODE toD3DCullMode(QRhiGraphicsPipeline::CullMode c)
......@@ -2778,6 +2810,7 @@ bool QD3D11GraphicsPipeline::build()
}
generation += 1;
rhiD->registerResource(this);
return true;
}
......@@ -2833,6 +2866,8 @@ void QD3D11SwapChain::releaseBuffers()
void QD3D11SwapChain::release()
{
QRHI_RES_RHI(QRhiD3D11);
if (!swapChain)
return;
......@@ -2857,6 +2892,8 @@ void QD3D11SwapChain::release()
QRHI_PROF;
QRHI_PROF_F(releaseSwapChain(this));
rhiD->unregisterResource(this);
}
QRhiCommandBuffer *QD3D11SwapChain::currentFrameCommandBuffer()
......@@ -2922,6 +2959,8 @@ bool QD3D11SwapChain::buildOrResize()
// same as a simple release+build (as with other resources). Just need to
// resize the buffers then.
const bool needsRegistration = !window || window != m_window;
// except if the window actually changes
if (window && window != m_window)
release();
......@@ -3051,6 +3090,9 @@ bool QD3D11SwapChain::buildOrResize()
// timestamp queries are optional so we can go on even if they failed
}
if (needsRegistration)
rhiD->registerResource(this);
return true;
}
......
......@@ -1741,6 +1741,8 @@ void QGles2Buffer::release()
QRHI_PROF;
QRHI_PROF_F(releaseBuffer(this));
rhiD->unregisterResource(this);
}
bool QGles2Buffer::build()
......@@ -1771,6 +1773,7 @@ bool QGles2Buffer::build()
rhiD->f->glBufferData(target, m_size, nullptr, m_type == Dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
QRHI_PROF_F(newBuffer(this, m_size, 1, 0));
rhiD->registerResource(this);
return true;
}
......@@ -1797,6 +1800,8 @@ void QGles2RenderBuffer::release()
QRHI_PROF;
QRHI_PROF_F(releaseRenderBuffer(this));
rhiD->unregisterResource(this);
}
bool QGles2RenderBuffer::build()
......@@ -1851,6 +1856,7 @@ bool QGles2RenderBuffer::build()
break;
}
rhiD->registerResource(this);
return true;
}
......@@ -2096,6 +2102,8 @@ void QGles2TextureRenderTarget::release()
QRHI_RES_RHI(QRhiGles2);
rhiD->releaseQueue.append(e);
rhiD->unregisterResource(this);
}
QRhiRenderPassDescriptor *QGles2TextureRenderTarget::newCompatibleRenderPassDescriptor()
......@@ -2159,6 +2167,7 @@ bool QGles2TextureRenderTarget::build()
return false;
}
rhiD->registerResource(this);
return true;
}
......@@ -2234,6 +2243,8 @@ void QGles2GraphicsPipeline::release()
QRHI_RES_RHI(QRhiGles2);
rhiD->releaseQueue.append(e);
rhiD->unregisterResource(this);
}
bool QGles2GraphicsPipeline::build()
......@@ -2357,6 +2368,7 @@ bool QGles2GraphicsPipeline::build()
lookupSamplers(v);
generation += 1;
rhiD->registerResource(this);
return true;
}
......
......@@ -1560,6 +1560,8 @@ void QMetalBuffer::release()
QRHI_PROF;
QRHI_PROF_F(releaseBuffer(this));
rhiD->unregisterResource(this);
}
bool QMetalBuffer::build()
......@@ -1602,6 +1604,7 @@ bool QMetalBuffer::build()
lastActiveFrameSlot = -1;
generation += 1;
rhiD->registerResource(this);
return true;
}
......@@ -1634,6 +1637,8 @@ void QMetalRenderBuffer::release()
QRHI_PROF;
QRHI_PROF_F(releaseRenderBuffer(this));
rhiD->unregisterResource(this);
}
bool QMetalRenderBuffer::build()
......@@ -1690,6 +1695,7 @@ bool QMetalRenderBuffer::build()
lastActiveFrameSlot = -1;
generation += 1;
rhiD->registerResource(this);
return true;
}
......@@ -1735,6 +1741,8 @@ void QMetalTexture::release()
QRHI_PROF;
QRHI_PROF_F(releaseTexture(this));
rhiD->unregisterResource(this);
}
static inline MTLPixelFormat toMetalTextureFormat(QRhiTexture::Format format, QRhiTexture::Flags flags)
......@@ -1926,11 +1934,14 @@ bool QMetalTexture::build()
lastActiveFrameSlot = -1;
generation += 1;
rhiD->registerResource(this);
return true;
}
bool QMetalTexture::buildFrom(const QRhiNativeHandles *src)
{
QRHI_RES_RHI(QRhiMetal);
const QRhiMetalTextureNativeHandles *h = static_cast<const QRhiMetalTextureNativeHandles *>(src);
if (!h || !h->texture)
return false;
......@@ -1948,6 +1959,7 @@ bool QMetalTexture::buildFrom(const QRhiNativeHandles *src)
lastActiveFrameSlot = -1;
generation += 1;
rhiD->registerResource(this);
return true;
}
......@@ -1982,6 +1994,8 @@ void QMetalSampler::release()
QRHI_RES_RHI(QRhiMetal);
rhiD->d->releaseQueue.append(e);
rhiD->unregisterResource(this);
}
static inline MTLSamplerMinMagFilter toMetalFilter(QRhiSampler::Filter f)
......@@ -2050,6 +2064,7 @@ bool QMetalSampler::build()
lastActiveFrameSlot = -1;
generation += 1;
rhiD->registerResource(this);
return true;
}
......@@ -2267,6 +2282,8 @@ QMetalGraphicsPipeline::~QMetalGraphicsPipeline()
void QMetalGraphicsPipeline::release()
{
QRHI_RES_RHI(QRhiMetal);
if (!d->ps)
return;
......@@ -2297,6 +2314,8 @@ void QMetalGraphicsPipeline::release()
[d->fsLib release];
d->fsLib = nil;
}
rhiD->unregisterResource(this);
}
static inline MTLVertexFormat toMetalAttributeFormat(QRhiVertexInputAttribute::Format format)
......@@ -2693,6 +2712,7 @@ bool QMetalGraphicsPipeline::build()
lastActiveFrameSlot = -1;
generation += 1;
rhiD->registerResource(this);
return true;
}
......@@ -2779,6 +2799,8 @@ void QMetalSwapChain::release()
QRHI_PROF;
QRHI_PROF_F(releaseSwapChain(this));
rhiD->unregisterResource(this);
}
QRhiCommandBuffer *QMetalSwapChain::currentFrameCommandBuffer()
......@@ -2837,12 +2859,14 @@ bool QMetalSwapChain::buildOrResize()
{
Q_ASSERT(m_window);
const bool needsRegistration = !window || window != m_window;
if (window && window != m_window)
release();
// else no release(), this is intentional
QRHI_RES_RHI(QRhiMetal);
if (!window)
if (needsRegistration)
rhiD->swapchains.insert(this);
window = m_window;
......@@ -2921,6 +2945,9 @@ bool QMetalSwapChain::buildOrResize()
QRHI_PROF;
QRHI_PROF_F(resizeSwapChain(this, QMTL_FRAMES_IN_FLIGHT, samples > 1 ? QMTL_FRAMES_IN_FLIGHT : 0, samples));
if (needsRegistration)
rhiD->registerResource(this);
return true;
}
......
......@@ -4910,7 +4910,7 @@ bool QVkSwapChain::buildOrResize()
}
QRHI_RES_RHI(QRhiVulkan);
const bool brandNew = !window || window != m_window;
const bool needsRegistration = !window || window != m_window;
// 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
......@@ -4927,7 +4927,7 @@ bool QVkSwapChain::buildOrResize()
if (!rhiD->recreateSwapChain(this))
return false;
if (brandNew)
if (needsRegistration)
rhiD->swapchains.insert(this);
if (m_depthStencil && m_depthStencil->sampleCount() != m_sampleCount) {
......@@ -4992,7 +4992,7 @@ bool QVkSwapChain::buildOrResize()
QRHI_PROF;
QRHI_PROF_F(resizeSwapChain(this, QVK_FRAMES_IN_FLIGHT, samples > VK_SAMPLE_COUNT_1_BIT ? QVK_FRAMES_IN_FLIGHT : 0, samples));
if (brandNew)
if (needsRegistration)
rhiD->registerResource(this);
return true;
......
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