From 0369d9b2cbe9491b7e75615dfb21ac92786f5431 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 8 Aug 2025 11:03:03 +0200 Subject: [PATCH] Optimize `CSSetShader` on D3D11 when remains unchanged --- .../DirectX/DX11/GPUContextDX11.cpp | 15 +++++++++++++-- .../GraphicsDevice/DirectX/DX11/GPUContextDX11.h | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp index b68f5e595..62f9afd3a 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp @@ -95,6 +95,7 @@ void GPUContextDX11::FrameBegin() _srMaskDirtyCompute = 0; _rtCount = 0; _vertexLayout = nullptr; + _currentCompute = nullptr; _currentState = nullptr; _rtDepth = nullptr; Platform::MemoryClear(_rtHandles, sizeof(_rtHandles)); @@ -497,7 +498,12 @@ void GPUContextDX11::Dispatch(GPUShaderProgramCS* shader, uint32 threadGroupCoun flushOM(); // Dispatch - _context->CSSetShader((ID3D11ComputeShader*)shader->GetBufferHandle(), nullptr, 0); + auto compute = (ID3D11ComputeShader*)shader->GetBufferHandle(); + if (_currentCompute != compute) + { + _currentCompute = compute; + _context->CSSetShader(compute, nullptr, 0); + } _context->Dispatch(threadGroupCountX, threadGroupCountY, threadGroupCountZ); RENDER_STAT_DISPATCH_CALL(); @@ -518,7 +524,12 @@ void GPUContextDX11::DispatchIndirect(GPUShaderProgramCS* shader, GPUBuffer* buf flushOM(); // Dispatch - _context->CSSetShader((ID3D11ComputeShader*)shader->GetBufferHandle(), nullptr, 0); + auto compute = (ID3D11ComputeShader*)shader->GetBufferHandle(); + if (_currentCompute != compute) + { + _currentCompute = compute; + _context->CSSetShader(compute, nullptr, 0); + } _context->DispatchIndirect(bufferForArgsDX11->GetBuffer(), offsetForArgs); RENDER_STAT_DISPATCH_CALL(); diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h index 48de69b3f..6d1877534 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h @@ -59,6 +59,7 @@ private: bool _iaInputLayoutDirtyFlag; // Pipeline State + ID3D11ComputeShader* _currentCompute; GPUPipelineStateDX11* _currentState; ID3D11BlendState* CurrentBlendState; ID3D11RasterizerState* CurrentRasterizerState;