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