From 6e784e0aca240b41c83bd3e77aecf5793fdc238d Mon Sep 17 00:00:00 2001 From: riperiperi Date: Sun, 6 Aug 2023 20:29:20 +0100 Subject: [PATCH] GPU: Don't sync/bind index buffer when it's not in use (#5526) * GPU: Don't sync/bind index buffer when it's not in use Sometimes draws don't use an index buffer. It's not necessary to check or upload data for the current index buffer binding as it won't be used. This fixes Pokemon: Legends Arceus updating a stale index buffer for every draw during its TFB pass, which was all non-indexed draws. This probably didn't cost much on normal PCs, but it had a large impact on MacOS, which the macos1 release build avoided by mirroring index buffers (the PR currently does not). Needs buffer mirrors still for the rest of the performance. There are additional cases where index buffers are bound or checked with non-indexed draws on the backend, but this one was straightforward to fix and has the largest impact. Testing is welcome to ensure nothing weird broke. * Fix case with _rebind --- .../Engine/Threed/StateUpdater.cs | 2 +- .../Memory/BufferManager.cs | 26 ++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs index c0c2d5b30..b08e7f260 100644 --- a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs +++ b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs @@ -331,7 +331,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed UpdateShaderState(); } - _channel.BufferManager.CommitGraphicsBindings(); + _channel.BufferManager.CommitGraphicsBindings(_drawState.DrawIndexed); } /// diff --git a/src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs b/src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs index 10224a6d4..c656b0f64 100644 --- a/src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs +++ b/src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs @@ -515,24 +515,32 @@ namespace Ryujinx.Graphics.Gpu.Memory /// Ensures that the graphics engine bindings are visible to the host GPU. /// Note: this actually performs the binding using the host graphics API. /// - public void CommitGraphicsBindings() + /// True if the index buffer is in use + public void CommitGraphicsBindings(bool indexed) { var bufferCache = _channel.MemoryManager.Physical.BufferCache; - if (_indexBufferDirty || _rebind) + if (indexed) { - _indexBufferDirty = false; - - if (_indexBuffer.Address != 0) + if (_indexBufferDirty || _rebind) { - BufferRange buffer = bufferCache.GetBufferRange(_indexBuffer.Address, _indexBuffer.Size); + _indexBufferDirty = false; - _context.Renderer.Pipeline.SetIndexBuffer(buffer, _indexBuffer.Type); + if (_indexBuffer.Address != 0) + { + BufferRange buffer = bufferCache.GetBufferRange(_indexBuffer.Address, _indexBuffer.Size); + + _context.Renderer.Pipeline.SetIndexBuffer(buffer, _indexBuffer.Type); + } + } + else if (_indexBuffer.Address != 0) + { + bufferCache.SynchronizeBufferRange(_indexBuffer.Address, _indexBuffer.Size); } } - else if (_indexBuffer.Address != 0) + else if (_rebind) { - bufferCache.SynchronizeBufferRange(_indexBuffer.Address, _indexBuffer.Size); + _indexBufferDirty = true; } uint vbEnableMask = _vertexBuffersEnableMask;