From ccbc5e4f998b916c7e7b7b4948c1023140cc810a Mon Sep 17 00:00:00 2001 From: WinterSnowfall Date: Wed, 22 Mar 2023 19:19:34 +0200 Subject: [PATCH] [d3d8] refcount refactoring --- src/d3d8/d3d8_blit.cpp | 2 +- src/d3d8/d3d8_device.h | 55 ++++++++++++++++++++++++++++------------- src/d3d8/d3d8_texture.h | 2 +- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/d3d8/d3d8_blit.cpp b/src/d3d8/d3d8_blit.cpp index 8a19b89caff3..6601a17dfe18 100644 --- a/src/d3d8/d3d8_blit.cpp +++ b/src/d3d8/d3d8_blit.cpp @@ -234,7 +234,7 @@ namespace dxvk { case D3DPOOL_SYSTEMMEM: { // RT (DEFAULT) -> SYSTEMMEM: Use GetRenderTargetData as fast path if possible - if ((srcDesc.Usage & D3DUSAGE_RENDERTARGET || m_renderTarget == src)) { + if ((srcDesc.Usage & D3DUSAGE_RENDERTARGET || m_renderTarget.ptr() == src.ptr())) { // GetRenderTargetData works if the formats and sizes match if (srcDesc.MultiSampleType == d3d9::D3DMULTISAMPLE_NONE diff --git a/src/d3d8/d3d8_device.h b/src/d3d8/d3d8_device.h index 81ce708278ca..039cecf2e2c7 100644 --- a/src/d3d8/d3d8_device.h +++ b/src/d3d8/d3d8_device.h @@ -158,7 +158,7 @@ namespace dxvk { if (FAILED(res)) return res; - m_backBuffers[iBackBuffer] = ref(new D3D8Surface(this, std::move(pSurface9))); + m_backBuffers[iBackBuffer] = new D3D8Surface(this, std::move(pSurface9)); *ppBackBuffer = m_backBuffers[iBackBuffer].ref(); return res; @@ -368,21 +368,42 @@ namespace dxvk { if (pRenderTarget != NULL) { D3D8Surface* surf = static_cast(pRenderTarget); - res = GetD3D9()->SetRenderTarget(0, surf->GetD3D9()); - if (FAILED(res)) return res; + D3DSURFACE_DESC rtDesc; + surf->GetDesc(&rtDesc); + + if (unlikely(!(rtDesc.Usage & D3DUSAGE_RENDERTARGET))) + return D3DERR_INVALIDCALL; + + if(likely(m_renderTarget.ptr() != surf)) { + res = GetD3D9()->SetRenderTarget(0, surf->GetD3D9()); - m_renderTarget = ref(surf); + if (FAILED(res)) return res; + + m_renderTarget = surf; + } } // SetDepthStencilSurface is a separate call D3D8Surface* zStencil = static_cast(pNewZStencil); - res = GetD3D9()->SetDepthStencilSurface(D3D8Surface::GetD3D9Nullable(zStencil)); - if (FAILED(res)) return res; + if(pNewZStencil != NULL) { + D3DSURFACE_DESC zsDesc; + zStencil->GetDesc(&zsDesc); - m_depthStencil = ref(zStencil); - return res; + if (unlikely(!(zsDesc.Usage & D3DUSAGE_DEPTHSTENCIL))) + return D3DERR_INVALIDCALL; + } + + if(likely(m_depthStencil.ptr() != zStencil)) { + res = GetD3D9()->SetDepthStencilSurface(D3D8Surface::GetD3D9Nullable(zStencil)); + + if (FAILED(res)) return res; + + m_depthStencil = zStencil; + } + + return D3D_OK; } HRESULT STDMETHODCALLTYPE GetRenderTarget(IDirect3DSurface8** ppRenderTarget) { @@ -391,7 +412,7 @@ namespace dxvk { Com pRT9 = nullptr; HRESULT res = GetD3D9()->GetRenderTarget(0, &pRT9); // use RT index 0 - m_renderTarget = ref(new D3D8Surface(this, std::move(pRT9))); + m_renderTarget = new D3D8Surface(this, std::move(pRT9)); *ppRenderTarget = m_renderTarget.ref(); return res; @@ -407,7 +428,7 @@ namespace dxvk { Com pStencil9 = nullptr; HRESULT res = GetD3D9()->GetDepthStencilSurface(&pStencil9); - m_depthStencil = ref(new D3D8Surface(this, std::move(pStencil9))); + m_depthStencil = new D3D8Surface(this, std::move(pStencil9)); *ppZStencilSurface = m_depthStencil.ref(); return res; @@ -567,7 +588,7 @@ namespace dxvk { D3D8Texture2D* tex = static_cast(pTexture); - m_textures[Stage] = ref(tex); + m_textures[Stage] = tex; return GetD3D9()->SetTexture(Stage, D3D8Texture2D::GetD3D9Nullable(tex)); } @@ -849,22 +870,22 @@ namespace dxvk { D3D8StateBlock* m_recorder = nullptr; struct D3D8VBO { - Com buffer = nullptr; - UINT stride = 0; + Com buffer = nullptr; + UINT stride = 0; }; // Remember to fill() these in the constructor! - std::array, d8caps::MAX_TEXTURE_STAGES> m_textures; + std::array, d8caps::MAX_TEXTURE_STAGES> m_textures; std::array m_streams; - Com m_indices; - INT m_baseVertexIndex = 0; + Com m_indices; + INT m_baseVertexIndex = 0; // TODO: Which of these should be a private ref std::vector> m_backBuffers; Com m_frontBuffer; - Com m_renderTarget; + Com m_renderTarget; Com m_depthStencil; std::vector m_vertexShaders; diff --git a/src/d3d8/d3d8_texture.h b/src/d3d8/d3d8_texture.h index 97570f322bd1..9c1532aac077 100644 --- a/src/d3d8/d3d8_texture.h +++ b/src/d3d8/d3d8_texture.h @@ -103,7 +103,7 @@ namespace dxvk { return ptr; } - std::vector> m_subresources; + std::vector> m_subresources; };