D3D11: Ownership fixes for objects in DXTexture

This commit is contained in:
Silent 2019-07-21 16:31:22 +02:00
parent 77425ef83b
commit a6b8e8b9c3
No known key found for this signature in database
GPG Key ID: AE53149BB0C45AF1
4 changed files with 35 additions and 31 deletions

View File

@ -15,8 +15,8 @@
namespace DX11 namespace DX11
{ {
DXTexture::DXTexture(const TextureConfig& config, ID3D11Texture2D* texture) DXTexture::DXTexture(const TextureConfig& config, ComPtr<ID3D11Texture2D> texture)
: AbstractTexture(config), m_texture(texture) : AbstractTexture(config), m_texture(std::move(texture))
{ {
} }
@ -41,7 +41,7 @@ std::unique_ptr<DXTexture> DXTexture::Create(const TextureConfig& config)
CD3D11_TEXTURE2D_DESC desc(tex_format, config.width, config.height, config.layers, config.levels, CD3D11_TEXTURE2D_DESC desc(tex_format, config.width, config.height, config.layers, config.levels,
bindflags, D3D11_USAGE_DEFAULT, 0, config.samples, 0, 0); bindflags, D3D11_USAGE_DEFAULT, 0, config.samples, 0, 0);
ComPtr<ID3D11Texture2D> d3d_texture; ComPtr<ID3D11Texture2D> d3d_texture;
HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, &d3d_texture); HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, d3d_texture.GetAddressOf());
if (FAILED(hr)) if (FAILED(hr))
{ {
PanicAlert("Failed to create %ux%ux%u D3D backing texture", config.width, config.height, PanicAlert("Failed to create %ux%ux%u D3D backing texture", config.width, config.height,
@ -49,14 +49,14 @@ std::unique_ptr<DXTexture> DXTexture::Create(const TextureConfig& config)
return nullptr; return nullptr;
} }
std::unique_ptr<DXTexture> tex(new DXTexture(config, d3d_texture.Get())); std::unique_ptr<DXTexture> tex(new DXTexture(config, std::move(d3d_texture)));
if (!tex->CreateSRV() || (config.IsComputeImage() && !tex->CreateUAV())) if (!tex->CreateSRV() || (config.IsComputeImage() && !tex->CreateUAV()))
return nullptr; return nullptr;
return tex; return tex;
} }
std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ID3D11Texture2D* texture) std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ComPtr<ID3D11Texture2D> texture)
{ {
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
texture->GetDesc(&desc); texture->GetDesc(&desc);
@ -70,7 +70,7 @@ std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ID3D11Texture2D* texture)
if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS) if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS)
config.flags |= AbstractTextureFlag_ComputeImage; config.flags |= AbstractTextureFlag_ComputeImage;
std::unique_ptr<DXTexture> tex(new DXTexture(config, texture)); std::unique_ptr<DXTexture> tex(new DXTexture(config, std::move(texture)));
if (desc.BindFlags & D3D11_BIND_SHADER_RESOURCE && !tex->CreateSRV()) if (desc.BindFlags & D3D11_BIND_SHADER_RESOURCE && !tex->CreateSRV())
return nullptr; return nullptr;
if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS && !tex->CreateUAV()) if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS && !tex->CreateUAV())
@ -87,7 +87,8 @@ bool DXTexture::CreateSRV()
D3D11_SRV_DIMENSION_TEXTURE2DARRAY, D3D11_SRV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0, m_config.levels, 0, D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0, m_config.levels, 0,
m_config.layers); m_config.layers);
HRESULT hr = D3D::device->CreateShaderResourceView(m_texture.Get(), &desc, &m_srv); DEBUG_ASSERT(!m_srv);
HRESULT hr = D3D::device->CreateShaderResourceView(m_texture.Get(), &desc, m_srv.GetAddressOf());
if (FAILED(hr)) if (FAILED(hr))
{ {
PanicAlert("Failed to create %ux%ux%u D3D SRV", m_config.width, m_config.height, PanicAlert("Failed to create %ux%ux%u D3D SRV", m_config.width, m_config.height,
@ -103,7 +104,8 @@ bool DXTexture::CreateUAV()
const CD3D11_UNORDERED_ACCESS_VIEW_DESC desc( const CD3D11_UNORDERED_ACCESS_VIEW_DESC desc(
m_texture.Get(), D3D11_UAV_DIMENSION_TEXTURE2DARRAY, m_texture.Get(), D3D11_UAV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0, 0, m_config.layers); D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0, 0, m_config.layers);
HRESULT hr = D3D::device->CreateUnorderedAccessView(m_texture.Get(), &desc, &m_uav); DEBUG_ASSERT(!m_uav);
HRESULT hr = D3D::device->CreateUnorderedAccessView(m_texture.Get(), &desc, m_uav.GetAddressOf());
if (FAILED(hr)) if (FAILED(hr))
{ {
PanicAlert("Failed to create %ux%ux%u D3D UAV", m_config.width, m_config.height, PanicAlert("Failed to create %ux%ux%u D3D UAV", m_config.width, m_config.height,
@ -161,8 +163,8 @@ void DXTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8*
} }
DXStagingTexture::DXStagingTexture(StagingTextureType type, const TextureConfig& config, DXStagingTexture::DXStagingTexture(StagingTextureType type, const TextureConfig& config,
ID3D11Texture2D* tex) ComPtr<ID3D11Texture2D> tex)
: AbstractStagingTexture(type, config), m_tex(tex) : AbstractStagingTexture(type, config), m_tex(std::move(tex))
{ {
} }
@ -197,12 +199,12 @@ std::unique_ptr<DXStagingTexture> DXStagingTexture::Create(StagingTextureType ty
config.width, config.height, 1, 1, 0, usage, cpu_flags); config.width, config.height, 1, 1, 0, usage, cpu_flags);
ComPtr<ID3D11Texture2D> texture; ComPtr<ID3D11Texture2D> texture;
HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, &texture); HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, texture.GetAddressOf());
CHECK(SUCCEEDED(hr), "Create staging texture"); CHECK(SUCCEEDED(hr), "Create staging texture");
if (FAILED(hr)) if (FAILED(hr))
return nullptr; return nullptr;
return std::unique_ptr<DXStagingTexture>(new DXStagingTexture(type, config, texture.Get())); return std::unique_ptr<DXStagingTexture>(new DXStagingTexture(type, config, std::move(texture)));
} }
void DXStagingTexture::CopyFromTexture(const AbstractTexture* src, void DXStagingTexture::CopyFromTexture(const AbstractTexture* src,
@ -316,11 +318,12 @@ void DXStagingTexture::Flush()
DXFramebuffer::DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment, DXFramebuffer::DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
AbstractTextureFormat color_format, AbstractTextureFormat depth_format, AbstractTextureFormat color_format, AbstractTextureFormat depth_format,
u32 width, u32 height, u32 layers, u32 samples, u32 width, u32 height, u32 layers, u32 samples,
ID3D11RenderTargetView* rtv, ID3D11RenderTargetView* integer_rtv, ComPtr<ID3D11RenderTargetView> rtv,
ID3D11DepthStencilView* dsv) ComPtr<ID3D11RenderTargetView> integer_rtv,
ComPtr<ID3D11DepthStencilView> dsv)
: AbstractFramebuffer(color_attachment, depth_attachment, color_format, depth_format, width, : AbstractFramebuffer(color_attachment, depth_attachment, color_format, depth_format, width,
height, layers, samples), height, layers, samples),
m_rtv(rtv), m_integer_rtv(integer_rtv), m_dsv(dsv) m_rtv(std::move(rtv)), m_integer_rtv(std::move(integer_rtv)), m_dsv(std::move(dsv))
{ {
} }
@ -351,8 +354,8 @@ std::unique_ptr<DXFramebuffer> DXFramebuffer::Create(DXTexture* color_attachment
D3D11_RTV_DIMENSION_TEXTURE2DARRAY, D3D11_RTV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetRTVFormatForAbstractFormat(color_attachment->GetFormat(), false), 0, 0, D3DCommon::GetRTVFormatForAbstractFormat(color_attachment->GetFormat(), false), 0, 0,
color_attachment->GetLayers()); color_attachment->GetLayers());
HRESULT hr = HRESULT hr = D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc,
D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc, &rtv); rtv.GetAddressOf());
CHECK(SUCCEEDED(hr), "Create render target view for framebuffer"); CHECK(SUCCEEDED(hr), "Create render target view for framebuffer");
if (FAILED(hr)) if (FAILED(hr))
return nullptr; return nullptr;
@ -364,7 +367,7 @@ std::unique_ptr<DXFramebuffer> DXFramebuffer::Create(DXTexture* color_attachment
{ {
desc.Format = integer_format; desc.Format = integer_format;
hr = D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc, hr = D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc,
&integer_rtv); integer_rtv.GetAddressOf());
CHECK(SUCCEEDED(hr), "Create integer render target view for framebuffer"); CHECK(SUCCEEDED(hr), "Create integer render target view for framebuffer");
} }
} }
@ -377,16 +380,16 @@ std::unique_ptr<DXFramebuffer> DXFramebuffer::Create(DXTexture* color_attachment
D3D11_DSV_DIMENSION_TEXTURE2DARRAY, D3D11_DSV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetDSVFormatForAbstractFormat(depth_attachment->GetFormat()), 0, 0, D3DCommon::GetDSVFormatForAbstractFormat(depth_attachment->GetFormat()), 0, 0,
depth_attachment->GetLayers(), 0); depth_attachment->GetLayers(), 0);
HRESULT hr = HRESULT hr = D3D::device->CreateDepthStencilView(depth_attachment->GetD3DTexture(), &desc,
D3D::device->CreateDepthStencilView(depth_attachment->GetD3DTexture(), &desc, &dsv); dsv.GetAddressOf());
CHECK(SUCCEEDED(hr), "Create depth stencil view for framebuffer"); CHECK(SUCCEEDED(hr), "Create depth stencil view for framebuffer");
if (FAILED(hr)) if (FAILED(hr))
return nullptr; return nullptr;
} }
return std::make_unique<DXFramebuffer>(color_attachment, depth_attachment, color_format, return std::make_unique<DXFramebuffer>(color_attachment, depth_attachment, color_format,
depth_format, width, height, layers, samples, rtv.Get(), depth_format, width, height, layers, samples,
integer_rtv.Get(), dsv.Get()); std::move(rtv), std::move(integer_rtv), std::move(dsv));
} }
} // namespace DX11 } // namespace DX11

View File

@ -20,7 +20,7 @@ public:
~DXTexture(); ~DXTexture();
static std::unique_ptr<DXTexture> Create(const TextureConfig& config); static std::unique_ptr<DXTexture> Create(const TextureConfig& config);
static std::unique_ptr<DXTexture> CreateAdopted(ID3D11Texture2D* texture); static std::unique_ptr<DXTexture> CreateAdopted(ComPtr<ID3D11Texture2D> texture);
void CopyRectangleFromTexture(const AbstractTexture* src, void CopyRectangleFromTexture(const AbstractTexture* src,
const MathUtil::Rectangle<int>& src_rect, u32 src_layer, const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
@ -36,7 +36,7 @@ public:
ID3D11UnorderedAccessView* GetD3DUAV() const { return m_uav.Get(); } ID3D11UnorderedAccessView* GetD3DUAV() const { return m_uav.Get(); }
private: private:
DXTexture(const TextureConfig& config, ID3D11Texture2D* texture); DXTexture(const TextureConfig& config, ComPtr<ID3D11Texture2D> texture);
bool CreateSRV(); bool CreateSRV();
bool CreateUAV(); bool CreateUAV();
@ -67,7 +67,8 @@ public:
const TextureConfig& config); const TextureConfig& config);
private: private:
DXStagingTexture(StagingTextureType type, const TextureConfig& config, ID3D11Texture2D* tex); DXStagingTexture(StagingTextureType type, const TextureConfig& config,
ComPtr<ID3D11Texture2D> tex);
ComPtr<ID3D11Texture2D> m_tex = nullptr; ComPtr<ID3D11Texture2D> m_tex = nullptr;
}; };
@ -77,8 +78,8 @@ class DXFramebuffer final : public AbstractFramebuffer
public: public:
DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment, DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
AbstractTextureFormat color_format, AbstractTextureFormat depth_format, u32 width, AbstractTextureFormat color_format, AbstractTextureFormat depth_format, u32 width,
u32 height, u32 layers, u32 samples, ID3D11RenderTargetView* rtv, u32 height, u32 layers, u32 samples, ComPtr<ID3D11RenderTargetView> rtv,
ID3D11RenderTargetView* integer_rtv, ID3D11DepthStencilView* dsv); ComPtr<ID3D11RenderTargetView> integer_rtv, ComPtr<ID3D11DepthStencilView> dsv);
~DXFramebuffer() override; ~DXFramebuffer() override;
ID3D11RenderTargetView* const* GetRTVArray() const { return m_rtv.GetAddressOf(); } ID3D11RenderTargetView* const* GetRTVArray() const { return m_rtv.GetAddressOf(); }

View File

@ -82,10 +82,10 @@ void Renderer::Create3DVisionTexture(int width, int height)
CD3D11_TEXTURE2D_DESC texture_desc(DXGI_FORMAT_R8G8B8A8_UNORM, width * 2, height + 1, 1, 1, CD3D11_TEXTURE2D_DESC texture_desc(DXGI_FORMAT_R8G8B8A8_UNORM, width * 2, height + 1, 1, 1,
D3D11_BIND_RENDER_TARGET, D3D11_USAGE_DEFAULT, 0, 1, 0, 0); D3D11_BIND_RENDER_TARGET, D3D11_USAGE_DEFAULT, 0, 1, 0, 0);
ID3D11Texture2D* texture; ComPtr<ID3D11Texture2D> texture;
HRESULT hr = D3D::device->CreateTexture2D(&texture_desc, &sys_data, &texture); HRESULT hr = D3D::device->CreateTexture2D(&texture_desc, &sys_data, texture.GetAddressOf());
CHECK(SUCCEEDED(hr), "Create 3D Vision Texture"); CHECK(SUCCEEDED(hr), "Create 3D Vision Texture");
m_3d_vision_texture = DXTexture::CreateAdopted(texture); m_3d_vision_texture = DXTexture::CreateAdopted(std::move(texture));
m_3d_vision_framebuffer = DXFramebuffer::Create(m_3d_vision_texture.get(), nullptr); m_3d_vision_framebuffer = DXFramebuffer::Create(m_3d_vision_texture.get(), nullptr);
} }

View File

@ -33,7 +33,7 @@ bool SwapChain::CreateSwapChainBuffers()
if (FAILED(hr)) if (FAILED(hr))
return false; return false;
m_texture = DXTexture::CreateAdopted(texture.Get()); m_texture = DXTexture::CreateAdopted(std::move(texture));
if (!m_texture) if (!m_texture)
return false; return false;