Skip to content

Commit

Permalink
D3D11: Ownership fixes for objects in DXTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
CookiePLMonster committed Jul 21, 2019
1 parent ad5a5b9 commit bdbeaae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
47 changes: 25 additions & 22 deletions Source/Core/VideoBackends/D3D/DXTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

namespace DX11
{
DXTexture::DXTexture(const TextureConfig& config, ID3D11Texture2D* texture)
: AbstractTexture(config), m_texture(texture)
DXTexture::DXTexture(const TextureConfig& config, ComPtr<ID3D11Texture2D> texture)
: AbstractTexture(config), m_texture(std::move(texture))
{
}

Expand All @@ -41,22 +41,22 @@ std::unique_ptr<DXTexture> DXTexture::Create(const TextureConfig& config)
CD3D11_TEXTURE2D_DESC desc(tex_format, config.width, config.height, config.layers, config.levels,
bindflags, D3D11_USAGE_DEFAULT, 0, config.samples, 0, 0);
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))
{
PanicAlert("Failed to create %ux%ux%u D3D backing texture", config.width, config.height,
config.layers);
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()))
return nullptr;

return tex;
}

std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ID3D11Texture2D* texture)
std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ComPtr<ID3D11Texture2D> texture)
{
D3D11_TEXTURE2D_DESC desc;
texture->GetDesc(&desc);
Expand All @@ -70,7 +70,7 @@ std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ID3D11Texture2D* texture)
if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS)
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())
return nullptr;
if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS && !tex->CreateUAV())
Expand All @@ -87,7 +87,8 @@ bool DXTexture::CreateSRV()
D3D11_SRV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0, m_config.levels, 0,
m_config.layers);
HRESULT hr = D3D::device->CreateShaderResourceView(m_texture.Get(), &desc, &m_srv);
HRESULT hr =
D3D::device->CreateShaderResourceView(m_texture.Get(), &desc, m_srv.ReleaseAndGetAddressOf());
if (FAILED(hr))
{
PanicAlert("Failed to create %ux%ux%u D3D SRV", m_config.width, m_config.height,
Expand All @@ -103,7 +104,8 @@ bool DXTexture::CreateUAV()
const CD3D11_UNORDERED_ACCESS_VIEW_DESC desc(
m_texture.Get(), D3D11_UAV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0, 0, m_config.layers);
HRESULT hr = D3D::device->CreateUnorderedAccessView(m_texture.Get(), &desc, &m_uav);
HRESULT hr = D3D::device->CreateUnorderedAccessView(m_texture.Get(), &desc,
m_uav.ReleaseAndGetAddressOf());
if (FAILED(hr))
{
PanicAlert("Failed to create %ux%ux%u D3D UAV", m_config.width, m_config.height,
Expand Down Expand Up @@ -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,
ID3D11Texture2D* tex)
: AbstractStagingTexture(type, config), m_tex(tex)
ComPtr<ID3D11Texture2D> tex)
: AbstractStagingTexture(type, config), m_tex(std::move(tex))
{
}

Expand Down Expand Up @@ -197,12 +199,12 @@ std::unique_ptr<DXStagingTexture> DXStagingTexture::Create(StagingTextureType ty
config.width, config.height, 1, 1, 0, usage, cpu_flags);

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");
if (FAILED(hr))
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,
Expand Down Expand Up @@ -316,11 +318,12 @@ void DXStagingTexture::Flush()
DXFramebuffer::DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
AbstractTextureFormat color_format, AbstractTextureFormat depth_format,
u32 width, u32 height, u32 layers, u32 samples,
ID3D11RenderTargetView* rtv, ID3D11RenderTargetView* integer_rtv,
ID3D11DepthStencilView* dsv)
ComPtr<ID3D11RenderTargetView> rtv,
ComPtr<ID3D11RenderTargetView> integer_rtv,
ComPtr<ID3D11DepthStencilView> dsv)
: AbstractFramebuffer(color_attachment, depth_attachment, color_format, depth_format, width,
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))
{
}

Expand Down Expand Up @@ -351,8 +354,8 @@ std::unique_ptr<DXFramebuffer> DXFramebuffer::Create(DXTexture* color_attachment
D3D11_RTV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetRTVFormatForAbstractFormat(color_attachment->GetFormat(), false), 0, 0,
color_attachment->GetLayers());
HRESULT hr =
D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc, &rtv);
HRESULT hr = D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc,
rtv.GetAddressOf());
CHECK(SUCCEEDED(hr), "Create render target view for framebuffer");
if (FAILED(hr))
return nullptr;
Expand All @@ -364,7 +367,7 @@ std::unique_ptr<DXFramebuffer> DXFramebuffer::Create(DXTexture* color_attachment
{
desc.Format = integer_format;
hr = D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc,
&integer_rtv);
integer_rtv.GetAddressOf());
CHECK(SUCCEEDED(hr), "Create integer render target view for framebuffer");
}
}
Expand All @@ -377,16 +380,16 @@ std::unique_ptr<DXFramebuffer> DXFramebuffer::Create(DXTexture* color_attachment
D3D11_DSV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetDSVFormatForAbstractFormat(depth_attachment->GetFormat()), 0, 0,
depth_attachment->GetLayers(), 0);
HRESULT hr =
D3D::device->CreateDepthStencilView(depth_attachment->GetD3DTexture(), &desc, &dsv);
HRESULT hr = D3D::device->CreateDepthStencilView(depth_attachment->GetD3DTexture(), &desc,
dsv.GetAddressOf());
CHECK(SUCCEEDED(hr), "Create depth stencil view for framebuffer");
if (FAILED(hr))
return nullptr;
}

return std::make_unique<DXFramebuffer>(color_attachment, depth_attachment, color_format,
depth_format, width, height, layers, samples, rtv.Get(),
integer_rtv.Get(), dsv.Get());
depth_format, width, height, layers, samples,
std::move(rtv), std::move(integer_rtv), std::move(dsv));
}

} // namespace DX11
11 changes: 6 additions & 5 deletions Source/Core/VideoBackends/D3D/DXTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DXTexture final : public AbstractTexture
~DXTexture();

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,
const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
Expand All @@ -36,7 +36,7 @@ class DXTexture final : public AbstractTexture
ID3D11UnorderedAccessView* GetD3DUAV() const { return m_uav.Get(); }

private:
DXTexture(const TextureConfig& config, ID3D11Texture2D* texture);
DXTexture(const TextureConfig& config, ComPtr<ID3D11Texture2D> texture);

bool CreateSRV();
bool CreateUAV();
Expand Down Expand Up @@ -67,7 +67,8 @@ class DXStagingTexture final : public AbstractStagingTexture
const TextureConfig& config);

private:
DXStagingTexture(StagingTextureType type, const TextureConfig& config, ID3D11Texture2D* tex);
DXStagingTexture(StagingTextureType type, const TextureConfig& config,
ComPtr<ID3D11Texture2D> tex);

ComPtr<ID3D11Texture2D> m_tex = nullptr;
};
Expand All @@ -77,8 +78,8 @@ class DXFramebuffer final : public AbstractFramebuffer
public:
DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
AbstractTextureFormat color_format, AbstractTextureFormat depth_format, u32 width,
u32 height, u32 layers, u32 samples, ID3D11RenderTargetView* rtv,
ID3D11RenderTargetView* integer_rtv, ID3D11DepthStencilView* dsv);
u32 height, u32 layers, u32 samples, ComPtr<ID3D11RenderTargetView> rtv,
ComPtr<ID3D11RenderTargetView> integer_rtv, ComPtr<ID3D11DepthStencilView> dsv);
~DXFramebuffer() override;

ID3D11RenderTargetView* const* GetRTVArray() const { return m_rtv.GetAddressOf(); }
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/VideoBackends/D3D/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
D3D11_BIND_RENDER_TARGET, D3D11_USAGE_DEFAULT, 0, 1, 0, 0);
ID3D11Texture2D* texture;
HRESULT hr = D3D::device->CreateTexture2D(&texture_desc, &sys_data, &texture);
ComPtr<ID3D11Texture2D> texture;
HRESULT hr = D3D::device->CreateTexture2D(&texture_desc, &sys_data, texture.GetAddressOf());
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);
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/D3D/SwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool SwapChain::CreateSwapChainBuffers()
if (FAILED(hr))
return false;

m_texture = DXTexture::CreateAdopted(texture.Get());
m_texture = DXTexture::CreateAdopted(std::move(texture));
if (!m_texture)
return false;

Expand Down

0 comments on commit bdbeaae

Please sign in to comment.