From 3ead348b82aa013ac16de2d23fa2a980eba96267 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Wed, 3 Aug 2022 16:41:50 +0200 Subject: [PATCH] [d3d11] Move UpdateSubresource code to D3D11CommonContext --- src/d3d11/d3d11_context.h | 62 -------------------- src/d3d11/d3d11_context_common.cpp | 91 ++++++++++++++++++++++++++++++ src/d3d11/d3d11_context_common.h | 26 +++++++++ src/d3d11/d3d11_context_def.cpp | 25 -------- src/d3d11/d3d11_context_def.h | 17 ------ src/d3d11/d3d11_context_imm.cpp | 25 -------- src/d3d11/d3d11_context_imm.h | 17 ------ 7 files changed, 117 insertions(+), 146 deletions(-) diff --git a/src/d3d11/d3d11_context.h b/src/d3d11/d3d11_context.h index b9eba9c19..361e0fd6d 100644 --- a/src/d3d11/d3d11_context.h +++ b/src/d3d11/d3d11_context.h @@ -806,68 +806,6 @@ namespace dxvk { ID3D11Resource* pResource, UINT Subresource); - template - static void UpdateResource( - ContextType* pContext, - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch, - UINT CopyFlags) { - D3D10DeviceLock lock = pContext->LockContext(); - - if (!pDstResource) - return; - - // We need a different code path for buffers - D3D11_RESOURCE_DIMENSION resourceType; - pDstResource->GetType(&resourceType); - - if (likely(resourceType == D3D11_RESOURCE_DIMENSION_BUFFER)) { - const auto bufferResource = static_cast(pDstResource); - uint64_t bufferSize = bufferResource->Desc()->ByteWidth; - - // Provide a fast path for mapped buffer updates since some - // games use UpdateSubresource to update constant buffers. - if (likely(bufferResource->GetMapMode() == D3D11_COMMON_BUFFER_MAP_MODE_DIRECT) && likely(!pDstBox)) { - pContext->UpdateMappedBuffer(bufferResource, 0, bufferSize, pSrcData, 0); - return; - } - - // Validate buffer range to update - uint64_t offset = 0; - uint64_t length = bufferSize; - - if (pDstBox) { - offset = pDstBox->left; - length = pDstBox->right - offset; - } - - if (unlikely(offset + length > bufferSize)) - return; - - // Still try to be fast if a box is provided but we update the full buffer - if (likely(bufferResource->GetMapMode() == D3D11_COMMON_BUFFER_MAP_MODE_DIRECT)) { - CopyFlags &= D3D11_COPY_DISCARD | D3D11_COPY_NO_OVERWRITE; - - if (likely(length == bufferSize) || unlikely(CopyFlags != 0)) { - pContext->UpdateMappedBuffer(bufferResource, offset, length, pSrcData, CopyFlags); - return; - } - } - - // Otherwise we can't really do anything fancy, so just do a GPU copy - pContext->UpdateBuffer(bufferResource, offset, length, pSrcData); - } else { - D3D11CommonTexture* textureResource = GetCommonTexture(pDstResource); - - pContext->UpdateTexture(textureResource, - DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch); - } - } - void UpdateBuffer( D3D11Buffer* pDstBuffer, UINT Offset, diff --git a/src/d3d11/d3d11_context_common.cpp b/src/d3d11/d3d11_context_common.cpp index 8b93c36f9..4e4adfdde 100644 --- a/src/d3d11/d3d11_context_common.cpp +++ b/src/d3d11/d3d11_context_common.cpp @@ -19,6 +19,97 @@ namespace dxvk { } + + template + void STDMETHODCALLTYPE D3D11CommonContext::UpdateSubresource( + ID3D11Resource* pDstResource, + UINT DstSubresource, + const D3D11_BOX* pDstBox, + const void* pSrcData, + UINT SrcRowPitch, + UINT SrcDepthPitch) { + UpdateResource(pDstResource, DstSubresource, pDstBox, + pSrcData, SrcRowPitch, SrcDepthPitch, 0); + } + + + template + void STDMETHODCALLTYPE D3D11CommonContext::UpdateSubresource1( + ID3D11Resource* pDstResource, + UINT DstSubresource, + const D3D11_BOX* pDstBox, + const void* pSrcData, + UINT SrcRowPitch, + UINT SrcDepthPitch, + UINT CopyFlags) { + UpdateResource(pDstResource, DstSubresource, pDstBox, + pSrcData, SrcRowPitch, SrcDepthPitch, CopyFlags); + } + + + template + void D3D11CommonContext::UpdateResource( + ID3D11Resource* pDstResource, + UINT DstSubresource, + const D3D11_BOX* pDstBox, + const void* pSrcData, + UINT SrcRowPitch, + UINT SrcDepthPitch, + UINT CopyFlags) { + auto context = static_cast(this); + D3D10DeviceLock lock = context->LockContext(); + + if (!pDstResource) + return; + + // We need a different code path for buffers + D3D11_RESOURCE_DIMENSION resourceType; + pDstResource->GetType(&resourceType); + + if (likely(resourceType == D3D11_RESOURCE_DIMENSION_BUFFER)) { + const auto bufferResource = static_cast(pDstResource); + uint64_t bufferSize = bufferResource->Desc()->ByteWidth; + + // Provide a fast path for mapped buffer updates since some + // games use UpdateSubresource to update constant buffers. + if (likely(bufferResource->GetMapMode() == D3D11_COMMON_BUFFER_MAP_MODE_DIRECT) && likely(!pDstBox)) { + context->UpdateMappedBuffer(bufferResource, 0, bufferSize, pSrcData, 0); + return; + } + + // Validate buffer range to update + uint64_t offset = 0; + uint64_t length = bufferSize; + + if (pDstBox) { + offset = pDstBox->left; + length = pDstBox->right - offset; + } + + if (unlikely(offset + length > bufferSize)) + return; + + // Still try to be fast if a box is provided but we update the full buffer + if (likely(bufferResource->GetMapMode() == D3D11_COMMON_BUFFER_MAP_MODE_DIRECT)) { + CopyFlags &= D3D11_COPY_DISCARD | D3D11_COPY_NO_OVERWRITE; + + if (likely(length == bufferSize) || unlikely(CopyFlags != 0)) { + context->UpdateMappedBuffer(bufferResource, offset, length, pSrcData, CopyFlags); + return; + } + } + + // Otherwise we can't really do anything fancy, so just do a GPU copy + context->UpdateBuffer(bufferResource, offset, length, pSrcData); + } else { + D3D11CommonTexture* textureResource = GetCommonTexture(pDstResource); + + context->UpdateTexture(textureResource, + DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch); + } + } + + // Explicitly instantiate here template class D3D11CommonContext; template class D3D11CommonContext; diff --git a/src/d3d11/d3d11_context_common.h b/src/d3d11/d3d11_context_common.h index bb4b295f0..9dedc793a 100644 --- a/src/d3d11/d3d11_context_common.h +++ b/src/d3d11/d3d11_context_common.h @@ -64,7 +64,33 @@ namespace dxvk { ~D3D11CommonContext(); + void STDMETHODCALLTYPE UpdateSubresource( + ID3D11Resource* pDstResource, + UINT DstSubresource, + const D3D11_BOX* pDstBox, + const void* pSrcData, + UINT SrcRowPitch, + UINT SrcDepthPitch); + void STDMETHODCALLTYPE UpdateSubresource1( + ID3D11Resource* pDstResource, + UINT DstSubresource, + const D3D11_BOX* pDstBox, + const void* pSrcData, + UINT SrcRowPitch, + UINT SrcDepthPitch, + UINT CopyFlags); + + protected: + + void UpdateResource( + ID3D11Resource* pDstResource, + UINT DstSubresource, + const D3D11_BOX* pDstBox, + const void* pSrcData, + UINT SrcRowPitch, + UINT SrcDepthPitch, + UINT CopyFlags); }; diff --git a/src/d3d11/d3d11_context_def.cpp b/src/d3d11/d3d11_context_def.cpp index 1304236e1..9400f0e63 100644 --- a/src/d3d11/d3d11_context_def.cpp +++ b/src/d3d11/d3d11_context_def.cpp @@ -236,31 +236,6 @@ namespace dxvk { } - void STDMETHODCALLTYPE D3D11DeferredContext::UpdateSubresource( - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch) { - UpdateResource(this, pDstResource, - DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch, 0); - } - - - void STDMETHODCALLTYPE D3D11DeferredContext::UpdateSubresource1( - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch, - UINT CopyFlags) { - UpdateResource(this, pDstResource, - DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch, CopyFlags); - } - - void STDMETHODCALLTYPE D3D11DeferredContext::SwapDeviceContextState( ID3DDeviceContextState* pState, ID3DDeviceContextState** ppPreviousState) { diff --git a/src/d3d11/d3d11_context_def.h b/src/d3d11/d3d11_context_def.h index bda8725ca..d88204745 100644 --- a/src/d3d11/d3d11_context_def.h +++ b/src/d3d11/d3d11_context_def.h @@ -80,23 +80,6 @@ namespace dxvk { ID3D11Resource* pResource, UINT Subresource); - void STDMETHODCALLTYPE UpdateSubresource( - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch); - - void STDMETHODCALLTYPE UpdateSubresource1( - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch, - UINT CopyFlags); - void STDMETHODCALLTYPE SwapDeviceContextState( ID3DDeviceContextState* pState, ID3DDeviceContextState** ppPreviousState); diff --git a/src/d3d11/d3d11_context_imm.cpp b/src/d3d11/d3d11_context_imm.cpp index de04b9ef9..2ce43e256 100644 --- a/src/d3d11/d3d11_context_imm.cpp +++ b/src/d3d11/d3d11_context_imm.cpp @@ -321,31 +321,6 @@ namespace dxvk { } } - void STDMETHODCALLTYPE D3D11ImmediateContext::UpdateSubresource( - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch) { - UpdateResource(this, pDstResource, - DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch, 0); - } - - - void STDMETHODCALLTYPE D3D11ImmediateContext::UpdateSubresource1( - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch, - UINT CopyFlags) { - UpdateResource(this, pDstResource, - DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch, CopyFlags); - } - - void STDMETHODCALLTYPE D3D11ImmediateContext::OMSetRenderTargets( UINT NumViews, ID3D11RenderTargetView* const* ppRenderTargetViews, diff --git a/src/d3d11/d3d11_context_imm.h b/src/d3d11/d3d11_context_imm.h index 2356fd505..1a0d3b762 100644 --- a/src/d3d11/d3d11_context_imm.h +++ b/src/d3d11/d3d11_context_imm.h @@ -78,23 +78,6 @@ namespace dxvk { ID3D11Resource* pResource, UINT Subresource); - void STDMETHODCALLTYPE UpdateSubresource( - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch); - - void STDMETHODCALLTYPE UpdateSubresource1( - ID3D11Resource* pDstResource, - UINT DstSubresource, - const D3D11_BOX* pDstBox, - const void* pSrcData, - UINT SrcRowPitch, - UINT SrcDepthPitch, - UINT CopyFlags); - void STDMETHODCALLTYPE OMSetRenderTargets( UINT NumViews, ID3D11RenderTargetView* const* ppRenderTargetViews,