Commit 8e80a130 authored by Laszlo Agocs's avatar Laszlo Agocs

d3d: report buf, tex, etc. as shareable and handle orphaning

This is simple since no deferred release queue is used here.
parent cb9daa45
...@@ -172,7 +172,7 @@ public: ...@@ -172,7 +172,7 @@ public:
return resources; return resources;
} }
bool orphanCheck(QRhiResource *res) static bool orphanCheck(QRhiResource *res)
{ {
if (res->orphanedWithRsh) { if (res->orphanedWithRsh) {
qWarning("Attempted to perform something on an orphaned QRhiResource %p (%s). This is invalid.", qWarning("Attempted to perform something on an orphaned QRhiResource %p (%s). This is invalid.",
......
...@@ -1686,10 +1686,13 @@ QD3D11Buffer::QD3D11Buffer(QRhiImplementation *rhi, Type type, UsageFlags usage, ...@@ -1686,10 +1686,13 @@ QD3D11Buffer::QD3D11Buffer(QRhiImplementation *rhi, Type type, UsageFlags usage,
{ {
} }
void QD3D11Buffer::release() bool QD3D11Buffer::isShareable() const
{ {
QRHI_RES_RHI(QRhiD3D11); return true;
}
void QD3D11Buffer::release()
{
if (!buffer) if (!buffer)
return; return;
...@@ -1698,10 +1701,12 @@ void QD3D11Buffer::release() ...@@ -1698,10 +1701,12 @@ void QD3D11Buffer::release()
buffer->Release(); buffer->Release();
buffer = nullptr; buffer = nullptr;
QRHI_PROF; if (!orphanedWithRsh) {
QRHI_PROF_F(releaseBuffer(this)); QRHI_RES_RHI(QRhiD3D11);
QRHI_PROF;
rhiD->unregisterResource(this); QRHI_PROF_F(releaseBuffer(this));
rhiD->unregisterResource(this);
}
} }
static inline uint toD3DBufferUsage(QRhiBuffer::UsageFlags usage) static inline uint toD3DBufferUsage(QRhiBuffer::UsageFlags usage)
...@@ -1718,6 +1723,9 @@ static inline uint toD3DBufferUsage(QRhiBuffer::UsageFlags usage) ...@@ -1718,6 +1723,9 @@ static inline uint toD3DBufferUsage(QRhiBuffer::UsageFlags usage)
bool QD3D11Buffer::build() bool QD3D11Buffer::build()
{ {
if (!QRhiImplementation::orphanCheck(this))
return false;
if (buffer) if (buffer)
release(); release();
...@@ -1760,10 +1768,13 @@ QD3D11RenderBuffer::QD3D11RenderBuffer(QRhiImplementation *rhi, Type type, const ...@@ -1760,10 +1768,13 @@ QD3D11RenderBuffer::QD3D11RenderBuffer(QRhiImplementation *rhi, Type type, const
{ {
} }
void QD3D11RenderBuffer::release() bool QD3D11RenderBuffer::isShareable() const
{ {
QRHI_RES_RHI(QRhiD3D11); return true;
}
void QD3D11RenderBuffer::release()
{
if (!tex) if (!tex)
return; return;
...@@ -1780,14 +1791,19 @@ void QD3D11RenderBuffer::release() ...@@ -1780,14 +1791,19 @@ void QD3D11RenderBuffer::release()
tex->Release(); tex->Release();
tex = nullptr; tex = nullptr;
QRHI_PROF; if (!orphanedWithRsh) {
QRHI_PROF_F(releaseRenderBuffer(this)); QRHI_RES_RHI(QRhiD3D11);
QRHI_PROF;
rhiD->unregisterResource(this); QRHI_PROF_F(releaseRenderBuffer(this));
rhiD->unregisterResource(this);
}
} }
bool QD3D11RenderBuffer::build() bool QD3D11RenderBuffer::build()
{ {
if (!QRhiImplementation::orphanCheck(this))
return false;
if (tex) if (tex)
release(); release();
...@@ -1868,10 +1884,13 @@ QD3D11Texture::QD3D11Texture(QRhiImplementation *rhi, Format format, const QSize ...@@ -1868,10 +1884,13 @@ QD3D11Texture::QD3D11Texture(QRhiImplementation *rhi, Format format, const QSize
{ {
} }
void QD3D11Texture::release() bool QD3D11Texture::isShareable() const
{ {
QRHI_RES_RHI(QRhiD3D11); return true;
}
void QD3D11Texture::release()
{
if (!tex) if (!tex)
return; return;
...@@ -1885,10 +1904,12 @@ void QD3D11Texture::release() ...@@ -1885,10 +1904,12 @@ void QD3D11Texture::release()
tex = nullptr; tex = nullptr;
QRHI_PROF; if (!orphanedWithRsh) {
QRHI_PROF_F(releaseTexture(this)); QRHI_RES_RHI(QRhiD3D11);
QRHI_PROF;
rhiD->unregisterResource(this); QRHI_PROF_F(releaseTexture(this));
rhiD->unregisterResource(this);
}
} }
static inline DXGI_FORMAT toD3DDepthTextureSRVFormat(QRhiTexture::Format format) static inline DXGI_FORMAT toD3DDepthTextureSRVFormat(QRhiTexture::Format format)
...@@ -1919,7 +1940,8 @@ static inline DXGI_FORMAT toD3DDepthTextureDSVFormat(QRhiTexture::Format format) ...@@ -1919,7 +1940,8 @@ static inline DXGI_FORMAT toD3DDepthTextureDSVFormat(QRhiTexture::Format format)
bool QD3D11Texture::prepareBuild(QSize *adjustedSize) bool QD3D11Texture::prepareBuild(QSize *adjustedSize)
{ {
QRHI_RES_RHI(QRhiD3D11); if (!QRhiImplementation::orphanCheck(this))
return false;
if (tex) if (tex)
release(); release();
...@@ -1929,6 +1951,7 @@ bool QD3D11Texture::prepareBuild(QSize *adjustedSize) ...@@ -1929,6 +1951,7 @@ bool QD3D11Texture::prepareBuild(QSize *adjustedSize)
const bool isCube = m_flags.testFlag(CubeMap); const bool isCube = m_flags.testFlag(CubeMap);
const bool hasMipMaps = m_flags.testFlag(MipMapped); const bool hasMipMaps = m_flags.testFlag(MipMapped);
QRHI_RES_RHI(QRhiD3D11);
dxgiFormat = toD3DTextureFormat(m_format, m_flags); dxgiFormat = toD3DTextureFormat(m_format, m_flags);
mipLevelCount = hasMipMaps ? rhiD->q->mipLevelsForSize(size) : 1; mipLevelCount = hasMipMaps ? rhiD->q->mipLevelsForSize(size) : 1;
sampleDesc = rhiD->effectiveSampleCount(m_sampleCount); sampleDesc = rhiD->effectiveSampleCount(m_sampleCount);
...@@ -1988,8 +2011,6 @@ bool QD3D11Texture::finishBuild() ...@@ -1988,8 +2011,6 @@ bool QD3D11Texture::finishBuild()
bool QD3D11Texture::build() bool QD3D11Texture::build()
{ {
QRHI_RES_RHI(QRhiD3D11);
QSize size; QSize size;
if (!prepareBuild(&size)) if (!prepareBuild(&size))
return false; return false;
...@@ -2026,6 +2047,7 @@ bool QD3D11Texture::build() ...@@ -2026,6 +2047,7 @@ bool QD3D11Texture::build()
desc.BindFlags = bindFlags; desc.BindFlags = bindFlags;
desc.MiscFlags = miscFlags; desc.MiscFlags = miscFlags;
QRHI_RES_RHI(QRhiD3D11);
HRESULT hr = rhiD->dev->CreateTexture2D(&desc, nullptr, &tex); HRESULT hr = rhiD->dev->CreateTexture2D(&desc, nullptr, &tex);
if (FAILED(hr)) { if (FAILED(hr)) {
qWarning("Failed to create texture: %s", qPrintable(comErrorMessage(hr))); qWarning("Failed to create texture: %s", qPrintable(comErrorMessage(hr)));
...@@ -2048,8 +2070,6 @@ bool QD3D11Texture::build() ...@@ -2048,8 +2070,6 @@ bool QD3D11Texture::build()
bool QD3D11Texture::buildFrom(const QRhiNativeHandles *src) bool QD3D11Texture::buildFrom(const QRhiNativeHandles *src)
{ {
QRHI_RES_RHI(QRhiD3D11);
const QRhiD3D11TextureNativeHandles *h = static_cast<const QRhiD3D11TextureNativeHandles *>(src); const QRhiD3D11TextureNativeHandles *h = static_cast<const QRhiD3D11TextureNativeHandles *>(src);
if (!h || !h->texture) if (!h || !h->texture)
return false; return false;
...@@ -2066,6 +2086,7 @@ bool QD3D11Texture::buildFrom(const QRhiNativeHandles *src) ...@@ -2066,6 +2086,7 @@ bool QD3D11Texture::buildFrom(const QRhiNativeHandles *src)
QRHI_PROF_F(newTexture(this, false, mipLevelCount, m_flags.testFlag(CubeMap) ? 6 : 1, sampleDesc.Count)); QRHI_PROF_F(newTexture(this, false, mipLevelCount, m_flags.testFlag(CubeMap) ? 6 : 1, sampleDesc.Count));
owns = false; owns = false;
QRHI_RES_RHI(QRhiD3D11);
rhiD->registerResource(this); rhiD->registerResource(this);
return true; return true;
} }
...@@ -2081,17 +2102,23 @@ QD3D11Sampler::QD3D11Sampler(QRhiImplementation *rhi, Filter magFilter, Filter m ...@@ -2081,17 +2102,23 @@ QD3D11Sampler::QD3D11Sampler(QRhiImplementation *rhi, Filter magFilter, Filter m
{ {
} }
void QD3D11Sampler::release() bool QD3D11Sampler::isShareable() const
{ {
QRHI_RES_RHI(QRhiD3D11); return true;
}
void QD3D11Sampler::release()
{
if (!samplerState) if (!samplerState)
return; return;
samplerState->Release(); samplerState->Release();
samplerState = nullptr; samplerState = nullptr;
rhiD->unregisterResource(this); if (!orphanedWithRsh) {
QRHI_RES_RHI(QRhiD3D11);
rhiD->unregisterResource(this);
}
} }
static inline D3D11_FILTER toD3DFilter(QRhiSampler::Filter minFilter, QRhiSampler::Filter magFilter, QRhiSampler::Filter mipFilter) static inline D3D11_FILTER toD3DFilter(QRhiSampler::Filter minFilter, QRhiSampler::Filter magFilter, QRhiSampler::Filter mipFilter)
...@@ -2147,6 +2174,9 @@ static inline D3D11_TEXTURE_ADDRESS_MODE toD3DAddressMode(QRhiSampler::AddressMo ...@@ -2147,6 +2174,9 @@ static inline D3D11_TEXTURE_ADDRESS_MODE toD3DAddressMode(QRhiSampler::AddressMo
bool QD3D11Sampler::build() bool QD3D11Sampler::build()
{ {
if (!QRhiImplementation::orphanCheck(this))
return false;
if (samplerState) if (samplerState)
release(); release();
......
...@@ -56,6 +56,7 @@ class QRhiResourceSharingHostPrivate; ...@@ -56,6 +56,7 @@ class QRhiResourceSharingHostPrivate;
struct QD3D11Buffer : public QRhiBuffer struct QD3D11Buffer : public QRhiBuffer
{ {
QD3D11Buffer(QRhiImplementation *rhi, Type type, UsageFlags usage, int size); QD3D11Buffer(QRhiImplementation *rhi, Type type, UsageFlags usage, int size);
bool isShareable() const override;
void release() override; void release() override;
bool build() override; bool build() override;
...@@ -70,6 +71,7 @@ struct QD3D11RenderBuffer : public QRhiRenderBuffer ...@@ -70,6 +71,7 @@ struct QD3D11RenderBuffer : public QRhiRenderBuffer
{ {
QD3D11RenderBuffer(QRhiImplementation *rhi, Type type, const QSize &pixelSize, QD3D11RenderBuffer(QRhiImplementation *rhi, Type type, const QSize &pixelSize,
int sampleCount, QRhiRenderBuffer::Flags flags); int sampleCount, QRhiRenderBuffer::Flags flags);
bool isShareable() const override;
void release() override; void release() override;
bool build() override; bool build() override;
QRhiTexture::Format backingFormat() const override; QRhiTexture::Format backingFormat() const override;
...@@ -86,6 +88,7 @@ struct QD3D11Texture : public QRhiTexture ...@@ -86,6 +88,7 @@ struct QD3D11Texture : public QRhiTexture
{ {
QD3D11Texture(QRhiImplementation *rhi, Format format, const QSize &pixelSize, QD3D11Texture(QRhiImplementation *rhi, Format format, const QSize &pixelSize,
int sampleCount, Flags flags); int sampleCount, Flags flags);
bool isShareable() const override;
void release() override; void release() override;
bool build() override; bool build() override;
bool buildFrom(const QRhiNativeHandles *src) override; bool buildFrom(const QRhiNativeHandles *src) override;
...@@ -109,6 +112,7 @@ struct QD3D11Sampler : public QRhiSampler ...@@ -109,6 +112,7 @@ struct QD3D11Sampler : public QRhiSampler
{ {
QD3D11Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode, QD3D11Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w); AddressMode u, AddressMode v, AddressMode w);
bool isShareable() const override;
void release() override; void release() override;
bool build() override; bool build() override;
......
...@@ -3718,8 +3718,7 @@ void QVkBuffer::release() ...@@ -3718,8 +3718,7 @@ void QVkBuffer::release()
bool QVkBuffer::build() bool QVkBuffer::build()
{ {
QRHI_RES_RHI(QRhiVulkan); if (!QRhiImplementation::orphanCheck(this))
if (!rhiD->orphanCheck(this))
return false; return false;
if (buffers[0]) if (buffers[0])
...@@ -3743,6 +3742,7 @@ bool QVkBuffer::build() ...@@ -3743,6 +3742,7 @@ bool QVkBuffer::build()
bufferInfo.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT; bufferInfo.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
} }
QRHI_RES_RHI(QRhiVulkan);
VkResult err = VK_SUCCESS; VkResult err = VK_SUCCESS;
for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) {
buffers[i] = VK_NULL_HANDLE; buffers[i] = VK_NULL_HANDLE;
...@@ -3829,8 +3829,7 @@ void QVkRenderBuffer::release() ...@@ -3829,8 +3829,7 @@ void QVkRenderBuffer::release()
bool QVkRenderBuffer::build() bool QVkRenderBuffer::build()
{ {
QRHI_RES_RHI(QRhiVulkan); if (!QRhiImplementation::orphanCheck(this))
if (!rhiD->orphanCheck(this))
return false; return false;
if (memory || backingTexture) if (memory || backingTexture)
...@@ -3839,6 +3838,7 @@ bool QVkRenderBuffer::build() ...@@ -3839,6 +3838,7 @@ bool QVkRenderBuffer::build()
if (m_pixelSize.isEmpty()) if (m_pixelSize.isEmpty())
return false; return false;
QRHI_RES_RHI(QRhiVulkan);
QRHI_PROF; QRHI_PROF;
samples = rhiD->effectiveSampleCount(m_sampleCount); samples = rhiD->effectiveSampleCount(m_sampleCount);
...@@ -3952,13 +3952,13 @@ void QVkTexture::release() ...@@ -3952,13 +3952,13 @@ void QVkTexture::release()
bool QVkTexture::prepareBuild(QSize *adjustedSize) bool QVkTexture::prepareBuild(QSize *adjustedSize)
{ {
QRHI_RES_RHI(QRhiVulkan); if (!QRhiImplementation::orphanCheck(this))
if (!rhiD->orphanCheck(this))
return false; return false;
if (image) if (image)
release(); release();
QRHI_RES_RHI(QRhiVulkan);
vkformat = toVkTextureFormat(m_format, m_flags); vkformat = toVkTextureFormat(m_format, m_flags);
VkFormatProperties props; VkFormatProperties props;
rhiD->f->vkGetPhysicalDeviceFormatProperties(rhiD->physDev, vkformat, &props); rhiD->f->vkGetPhysicalDeviceFormatProperties(rhiD->physDev, vkformat, &props);
...@@ -4028,8 +4028,6 @@ bool QVkTexture::finishBuild() ...@@ -4028,8 +4028,6 @@ bool QVkTexture::finishBuild()
bool QVkTexture::build() bool QVkTexture::build()
{ {
QRHI_RES_RHI(QRhiVulkan);
QSize size; QSize size;
if (!prepareBuild(&size)) if (!prepareBuild(&size))
return false; return false;
...@@ -4069,6 +4067,7 @@ bool QVkTexture::build() ...@@ -4069,6 +4067,7 @@ bool QVkTexture::build()
memset(&allocInfo, 0, sizeof(allocInfo)); memset(&allocInfo, 0, sizeof(allocInfo));
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
QRHI_RES_RHI(QRhiVulkan);
VmaAllocation allocation; VmaAllocation allocation;
VkResult err = vmaCreateImage(toVmaAllocator(rhiD->allocator), &imageInfo, &allocInfo, &image, &allocation, nullptr); VkResult err = vmaCreateImage(toVmaAllocator(rhiD->allocator), &imageInfo, &allocInfo, &image, &allocation, nullptr);
if (err != VK_SUCCESS) { if (err != VK_SUCCESS) {
...@@ -4093,8 +4092,6 @@ bool QVkTexture::build() ...@@ -4093,8 +4092,6 @@ bool QVkTexture::build()
bool QVkTexture::buildFrom(const QRhiNativeHandles *src) bool QVkTexture::buildFrom(const QRhiNativeHandles *src)
{ {
QRHI_RES_RHI(QRhiVulkan);
const QRhiVulkanTextureNativeHandles *h = static_cast<const QRhiVulkanTextureNativeHandles *>(src); const QRhiVulkanTextureNativeHandles *h = static_cast<const QRhiVulkanTextureNativeHandles *>(src);
if (!h || !h->image) if (!h || !h->image)
return false; return false;
...@@ -4112,6 +4109,7 @@ bool QVkTexture::buildFrom(const QRhiNativeHandles *src) ...@@ -4112,6 +4109,7 @@ bool QVkTexture::buildFrom(const QRhiNativeHandles *src)
owns = false; owns = false;
layout = h->layout; layout = h->layout;
QRHI_RES_RHI(QRhiVulkan);
rhiD->registerResource(this); rhiD->registerResource(this);
return true; return true;
} }
...@@ -4156,8 +4154,7 @@ void QVkSampler::release() ...@@ -4156,8 +4154,7 @@ void QVkSampler::release()
bool QVkSampler::build() bool QVkSampler::build()
{ {
QRHI_RES_RHI(QRhiVulkan); if (!QRhiImplementation::orphanCheck(this))
if (!rhiD->orphanCheck(this))
return false; return false;
if (sampler) if (sampler)
...@@ -4175,6 +4172,7 @@ bool QVkSampler::build() ...@@ -4175,6 +4172,7 @@ bool QVkSampler::build()
samplerInfo.maxAnisotropy = 1.0f; samplerInfo.maxAnisotropy = 1.0f;
samplerInfo.maxLod = m_mipmapMode == None ? 0.25f : 1000.0f; samplerInfo.maxLod = m_mipmapMode == None ? 0.25f : 1000.0f;
QRHI_RES_RHI(QRhiVulkan);
VkResult err = rhiD->df->vkCreateSampler(rhiD->dev, &samplerInfo, nullptr, &sampler); VkResult err = rhiD->df->vkCreateSampler(rhiD->dev, &samplerInfo, nullptr, &sampler);
if (err != VK_SUCCESS) { if (err != VK_SUCCESS) {
qWarning("Failed to create sampler: %d", err); qWarning("Failed to create sampler: %d", err);
......
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