Skip to content

Commit

Permalink
Keep Render Target Views and Shader Resource Views used for SDR<-->HD…
Browse files Browse the repository at this point in the history
…R copy operations in Flip Model games alive rather than creating a new one every frame.
  • Loading branch information
Kaldaien committed Dec 29, 2024
1 parent 6e1c8e2 commit 0aed436
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 26 deletions.
5 changes: 5 additions & 0 deletions include/SpecialK/render/d3d11/d3d11_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -3122,4 +3122,9 @@ void SK_D3D11_InitMutexes (void);
BOOL SK_D3D11_SetWrappedImmediateContext ( ID3D11Device *pDev,
ID3D11DeviceContext *pDevCtx );

void
SK_DXGI_ReleaseCachedSwapChainViews ( IUnknown *pSwapChain_ImGui,
IUnknown *pSwapChain_Other,
IUnknown *pDevice_Unknown );

extern bool SK_GL_OnD3D11;
2 changes: 1 addition & 1 deletion include/SpecialK/render/dxgi/dxgi_interfaces.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* This file is part of Special K.
*
* Special K is free software : you can redistribute it
Expand Down
11 changes: 11 additions & 0 deletions include/SpecialK/render/dxgi/dxgi_swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,17 @@ IWrapDXGISwapChain : IDXGISwapChain4
std::unordered_map <UINT, SK_ComPtr <ID3D11Texture2D>>
_backbuffers;

struct {
SK_ComPtr <ID3D11ShaderResourceView> srv;
SK_ComPtr <ID3D11RenderTargetView> rtv;

void release (void) noexcept
{
srv = nullptr;
rtv = nullptr;
}
} _backbuffer_views;

D3D11_FEATURE_DATA_D3D11_OPTIONS
_d3d11_feature_opts = { };

Expand Down
4 changes: 4 additions & 0 deletions src/imgui/backends/imgui_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,10 @@ SK_D3D11_RenderCtx::release (IDXGISwapChain* pSwapChain)
swapDesc.OutputWindow),
L"D3D11BkEnd" );

SK_DXGI_ReleaseCachedSwapChainViews (
_pSwapChain, pSwapChain, _pDevice
);

if (_pReShadeRuntime != nullptr)
{
SK_ReShadeAddOn_DestroyEffectRuntime (_pReShadeRuntime);
Expand Down
78 changes: 55 additions & 23 deletions src/render/dxgi/dxgi_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,14 @@ STDMETHODCALLTYPE IWrapDXGISwapChain::GetPrivateData (REFGUID Name, UINT *pDataS
{
std::scoped_lock lock (_backbufferLock);

//
// TODO: Optimize this to store SRV and RTVs persistently
//
if (IsEqualGUID (Name, SKID_DXGI_SwapChainProxyBackbuffer_D3D11) && _backbuffers.contains (0))
if (IsEqualGUID (Name, SKID_DXGI_SwapChainProxyBackbuffer_D3D11) &&
_backbuffers.contains (0))
{
if (SK_ComQIPtr <ID3D11Device> pDev11 (pDev); pDev11.p != nullptr &&
pDataSize != nullptr)
{
if (pData != nullptr && *pDataSize >= sizeof (void *))
if ( pData != nullptr && *pDataSize >= sizeof (void*) &&
_backbuffer_views.srv.p == nullptr)
{
D3D11_TEXTURE2D_DESC texDesc = { };
_backbuffers [0]->GetDesc (&texDesc);
Expand All @@ -446,18 +445,18 @@ STDMETHODCALLTYPE IWrapDXGISwapChain::GetPrivateData (REFGUID Name, UINT *pDataS
srvDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;

SK_ComPtr <ID3D11ShaderResourceView> pSRV;
pDev11->CreateShaderResourceView (_backbuffers [0], &srvDesc, &pSRV.p);

if (pSRV.p != nullptr)
{
pSRV.p->AddRef ();
memcpy (pData, (void *)&pSRV.p, sizeof (void *));
return S_OK;
}
pDev11->CreateShaderResourceView (_backbuffers [0], &srvDesc,
&_backbuffer_views.srv.p);
}
}

if (pData != nullptr && *pDataSize >= sizeof (void *)
&& _backbuffer_views.srv.p != nullptr)
{ _backbuffer_views.srv.p->AddRef ();
memcpy (pData, (void *)&_backbuffer_views.srv.p, sizeof (void *));
return S_OK;
}

if (pData == nullptr)
{
*pDataSize = sizeof (void *);
Expand All @@ -471,7 +470,8 @@ STDMETHODCALLTYPE IWrapDXGISwapChain::GetPrivateData (REFGUID Name, UINT *pDataS
if (SK_ComQIPtr <ID3D11Device> pDev11 (pDev); pDev11.p != nullptr &&
pDataSize != nullptr)
{
if (pData != nullptr && *pDataSize >= sizeof (void *))
if ( pData != nullptr && *pDataSize >= sizeof (void*) &&
_backbuffer_views.rtv.p == nullptr)
{
SK_ComPtr <ID3D11Texture2D> pBackBuffer;
pReal->GetBuffer (0, IID_ID3D11Texture2D, (void **)&pBackBuffer.p);
Expand All @@ -490,15 +490,15 @@ STDMETHODCALLTYPE IWrapDXGISwapChain::GetPrivateData (REFGUID Name, UINT *pDataS
rtvDesc.Format = typed_format;
rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;

SK_ComPtr <ID3D11RenderTargetView> pRTV;
pDev11->CreateRenderTargetView (pBackBuffer, &rtvDesc, &pRTV.p);
pDev11->CreateRenderTargetView (pBackBuffer, &rtvDesc,
&_backbuffer_views.rtv.p);
}

if (pRTV.p != nullptr)
{
pRTV.p->AddRef ();
memcpy (pData, (void *)&pRTV.p, sizeof (void *));
return S_OK;
}
if (_backbuffer_views.rtv.p != nullptr && pData != nullptr && *pDataSize >= sizeof (void *))
{
_backbuffer_views.rtv.p->AddRef ();
memcpy (pData, (void *)&_backbuffer_views.rtv.p, sizeof (void *));
return S_OK;
}
}

Expand Down Expand Up @@ -971,6 +971,10 @@ IWrapDXGISwapChain::ResizeBuffers ( UINT BufferCount,
UINT Width, UINT Height,
DXGI_FORMAT NewFormat, UINT SwapChainFlags )
{
SK_DXGI_ReleaseCachedSwapChainViews (
pReal, this, SK_GetCurrentRenderBackend ().device
);

if (SK_DXGI_ZeroCopy == -1)
SK_DXGI_ZeroCopy = (__SK_HDR_16BitSwap || __SK_HDR_10BitSwap);

Expand Down Expand Up @@ -2887,4 +2891,32 @@ SK_DXGI_SwapChain_ResizeTarget_Impl (

return
_Return (ret);
}

void
SK_DXGI_ReleaseCachedSwapChainViews ( IUnknown *pSwapChain_ImGui,
IUnknown *pSwapChain_Other,
IUnknown *pUnkDevice )
{
SK_ComQIPtr <ID3D11Device>
pDevice (pUnkDevice);
if (! pDevice)
return;

SK_ComPtr<ID3D11DeviceContext>pImmediateCtx;
pDevice->GetImmediateContext(&pImmediateCtx);

if (pImmediateCtx.p != nullptr)
{
pImmediateCtx->ClearState ();
pImmediateCtx->Flush ();
}

SK_ComQIPtr <IWrapDXGISwapChain>
pSwapChainWrapped_Other (pSwapChain_Other),
pSwapChainWrapped_ImGui (pSwapChain_ImGui);
if (pSwapChainWrapped_ImGui != nullptr)
pSwapChainWrapped_ImGui->_backbuffer_views.release ();
if (pSwapChainWrapped_Other != nullptr)
pSwapChainWrapped_Other->_backbuffer_views.release ();
}
11 changes: 9 additions & 2 deletions src/render/render_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,11 @@ SK_RenderBackend_V2::getSwapWaitHandle (void) const
// thus do not return INVALID_HANDLE_VALUE; return 0.
}

void SK_HDR_ReleaseResources (void);
void SK_DXGI_ReleaseSRGBLinearizer (void);
void SK_HDR_ReleaseResources (void);
void SK_DXGI_ReleaseSRGBLinearizer (void);
void SK_DXGI_ReleaseCachedSwapChainViews (IUnknown *pSwapChain_ImGui,
IUnknown *pSwapChain_Other,
IUnknown *pDevice_Unknown);

void
SK_RenderBackend_V2::releaseOwnedResources (void)
Expand All @@ -1284,6 +1287,10 @@ SK_RenderBackend_V2::releaseOwnedResources (void)
SK_LOG1 ( ( L"API: %x", api ),
__SK_SUBSYSTEM__ );

SK_DXGI_ReleaseCachedSwapChainViews (
swapchain.p, nullptr, device.p
);

SK_HDR_ReleaseResources ();
SK_DXGI_ReleaseSRGBLinearizer ();

Expand Down

0 comments on commit 0aed436

Please sign in to comment.