Commit 276dbaa8 authored by Laszlo Agocs's avatar Laszlo Agocs
Browse files

d3d: add texture export/import

parent 435cfa4a
......@@ -1571,7 +1571,9 @@ void QD3D11Texture::release()
srv = nullptr;
}
tex->Release();
if (owns)
tex->Release();
tex = nullptr;
}
......@@ -1601,12 +1603,13 @@ static inline DXGI_FORMAT toD3DDepthTextureDSVFormat(QRhiTexture::Format format)
}
}
bool QD3D11Texture::build()
bool QD3D11Texture::prepareBuild(QSize *adjustedSize)
{
QRHI_RES_RHI(QRhiD3D11);
if (tex)
release();
QRHI_RES_RHI(QRhiD3D11);
const QSize size = m_pixelSize.isEmpty() ? QSize(16, 16) : m_pixelSize;
const bool isDepth = isDepthTextureFormat(m_format);
const bool isCube = m_flags.testFlag(CubeMap);
......@@ -1630,6 +1633,56 @@ bool QD3D11Texture::build()
return false;
}
if (adjustedSize)
*adjustedSize = size;
return true;
}
bool QD3D11Texture::finishBuild()
{
QRHI_RES_RHI(QRhiD3D11);
const bool isDepth = isDepthTextureFormat(m_format);
const bool isCube = m_flags.testFlag(CubeMap);
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
memset(&srvDesc, 0, sizeof(srvDesc));
srvDesc.Format = isDepth ? toD3DDepthTextureSRVFormat(m_format) : dxgiFormat;
if (isCube) {
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
srvDesc.TextureCube.MipLevels = mipLevelCount;
} else {
if (sampleDesc.Count > 1) {
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
} else {
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = mipLevelCount;
}
}
HRESULT hr = rhiD->dev->CreateShaderResourceView(tex, &srvDesc, &srv);
if (FAILED(hr)) {
qWarning("Failed to create srv: %s", qPrintable(comErrorMessage(hr)));
return false;
}
nativeHandlesStruct.texture = tex;
generation += 1;
return true;
}
bool QD3D11Texture::build()
{
QRHI_RES_RHI(QRhiD3D11);
QSize size;
if (!prepareBuild(&size))
return false;
const bool isDepth = isDepthTextureFormat(m_format);
const bool isCube = m_flags.testFlag(CubeMap);
uint bindFlags = D3D11_BIND_SHADER_RESOURCE;
uint miscFlags = isCube ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0;
if (m_flags.testFlag(RenderTarget)) {
......@@ -1665,31 +1718,36 @@ bool QD3D11Texture::build()
return false;
}
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
memset(&srvDesc, 0, sizeof(srvDesc));
srvDesc.Format = isDepth ? toD3DDepthTextureSRVFormat(m_format) : desc.Format;
if (isCube) {
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
srvDesc.TextureCube.MipLevels = desc.MipLevels;
} else {
if (sampleDesc.Count > 1) {
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
} else {
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels;
}
}
if (!finishBuild())
return false;
hr = rhiD->dev->CreateShaderResourceView(tex, &srvDesc, &srv);
if (FAILED(hr)) {
qWarning("Failed to create srv: %s", qPrintable(comErrorMessage(hr)));
owns = true;
return true;
}
bool QD3D11Texture::buildFrom(const QRhiNativeHandles *src)
{
const QRhiD3D11TextureNativeHandles *h = static_cast<const QRhiD3D11TextureNativeHandles *>(src);
if (!h || !h->texture)
return false;
}
generation += 1;
if (!prepareBuild())
return false;
tex = static_cast<ID3D11Texture2D *>(h->texture);
if (!finishBuild())
return false;
owns = false;
return true;
}
const QRhiNativeHandles *QD3D11Texture::nativeHandles()
{
return &nativeHandlesStruct;
}
QD3D11Sampler::QD3D11Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w)
......
......@@ -61,6 +61,11 @@ struct Q_RHI_EXPORT QRhiD3D11NativeHandles : public QRhiNativeHandles
void *context;
};
struct Q_RHI_EXPORT QRhiD3D11TextureNativeHandles : public QRhiNativeHandles
{
void *texture = nullptr; // ID3D11Texture2D
};
QT_END_NAMESPACE
#endif
......@@ -86,12 +86,19 @@ struct QD3D11Texture : public QRhiTexture
int sampleCount, Flags flags);
void release() override;
bool build() override;
bool buildFrom(const QRhiNativeHandles *src) override;
const QRhiNativeHandles *nativeHandles() override;
bool prepareBuild(QSize *adjustedSize = nullptr);
bool finishBuild();
ID3D11Texture2D *tex = nullptr;
bool owns = true;
ID3D11ShaderResourceView *srv = nullptr;
DXGI_FORMAT dxgiFormat;
uint mipLevelCount = 0;
DXGI_SAMPLE_DESC sampleDesc;
QRhiD3D11TextureNativeHandles nativeHandlesStruct;
uint generation = 0;
friend class QRhiD3D11;
};
......
Supports Markdown
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