@@ -851,4 +851,69 @@ void RenderContextImpl::readPixels(RenderTarget* rt, UINT x, UINT y, UINT width,
851851 SafeRelease (stagingTex);
852852}
853853
854+ bool RenderContextImpl::copyTexture (Texture* src, Texture* dst)
855+ {
856+ if (!validateTextureCopy (src, dst))
857+ return false ;
858+
859+ auto * srcImpl = static_cast <TextureImpl*>(src);
860+ auto * dstImpl = static_cast <TextureImpl*>(dst);
861+
862+ auto * srcResource = srcImpl->internalHandle ().resource ;
863+ if (!srcResource)
864+ return false ;
865+
866+ if (!dstImpl->internalHandle ().resource )
867+ dstImpl->updateData (nullptr , dst->getWidth (), dst->getHeight (), 0 , 0 );
868+
869+ auto * dstResource = dstImpl->internalHandle ().resource ;
870+ if (!dstResource || srcResource == dstResource)
871+ return false ;
872+
873+ D3D11_TEXTURE2D_DESC srcDesc{};
874+ D3D11_TEXTURE2D_DESC dstDesc{};
875+ srcResource->GetDesc (&srcDesc);
876+ dstResource->GetDesc (&dstDesc);
877+
878+ if (srcDesc.Width != dstDesc.Width || srcDesc.Height != dstDesc.Height || srcDesc.Format != dstDesc.Format ||
879+ srcDesc.SampleDesc .Count != 1 || dstDesc.SampleDesc .Count != 1 || srcDesc.ArraySize != 1 ||
880+ dstDesc.ArraySize != 1 || srcDesc.MipLevels != 1 || dstDesc.MipLevels != 1 ||
881+ dstDesc.Usage != D3D11_USAGE_DEFAULT )
882+ return false ;
883+
884+ // D3D11 keeps the last render targets bound after endRenderPass(). Temporarily
885+ // unbind them so a just-rendered color attachment can be used as a copy source.
886+ ID3D11RenderTargetView* oldRTVs[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT ]{};
887+ ID3D11DepthStencilView* oldDSV = nullptr ;
888+ _d3d11Context->OMGetRenderTargets (D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT , oldRTVs, &oldDSV);
889+ _d3d11Context->OMSetRenderTargets (0 , nullptr , nullptr );
890+
891+ _d3d11Context->CopySubresourceRegion (dstResource, 0 , 0 , 0 , 0 , srcResource, 0 , nullptr );
892+
893+ UINT oldRTVCount = 0 ;
894+ for (UINT i = 0 ; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT ; ++i)
895+ {
896+ if (oldRTVs[i])
897+ oldRTVCount = i + 1 ;
898+ }
899+ _d3d11Context->OMSetRenderTargets (oldRTVCount, oldRTVs, oldDSV);
900+ for (auto * rtv : oldRTVs)
901+ SafeRelease (rtv);
902+ SafeRelease (oldDSV);
903+
904+ return true ;
905+ }
906+
907+ bool RenderContextImpl::copyTexture (RenderTarget* src, Texture* dst)
908+ {
909+ if (!src || !dst || src->_color .empty ())
910+ return false ;
911+
912+ const auto & color = src->_color [0 ];
913+ if (color.level != 0 || !color.texture )
914+ return false ;
915+
916+ return copyTexture (color.texture , dst);
917+ }
918+
854919} // namespace ax::rhi::d3d11
0 commit comments