From 1623ab524f54901e154c8b644e2315985d5bd0a0 Mon Sep 17 00:00:00 2001 From: riperiperi Date: Mon, 8 Mar 2021 21:43:39 +0000 Subject: Improve Buffer Textures and flush Image Stores (#2088) * Improve Buffer Textures and flush Image Stores Fixes a number of issues with buffer textures: - Reworked Buffer Textures to create their buffers in the TextureManager, then bind them with the BufferManager later. - Fixes an issue where a buffer texture's buffer could be invalidated after it is bound, but before use. - Fixed width unpacking for large buffer textures. The width is now 32-bit rather than 16. - Force buffer textures to be rebound whenever any buffer is created, as using the handle id wasn't reliable, and the cost of binding isn't too high. Fixes vertex explosions and flickering animations in UE4 games. * Set ImageStore flag... for ImageStore. * Check the offset and size. --- Ryujinx.Graphics.Gpu/Memory/BufferManager.cs | 60 +++++++++++++++++++--------- 1 file changed, 42 insertions(+), 18 deletions(-) (limited to 'Ryujinx.Graphics.Gpu/Memory/BufferManager.cs') diff --git a/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs b/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs index 08d52faa..b2cd1ced 100644 --- a/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs +++ b/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs @@ -1,9 +1,11 @@ using Ryujinx.Common; using Ryujinx.Graphics.GAL; +using Ryujinx.Graphics.Gpu.Image; using Ryujinx.Graphics.Gpu.State; using Ryujinx.Graphics.Shader; using Ryujinx.Memory.Range; using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -31,6 +33,7 @@ namespace Ryujinx.Graphics.Gpu.Memory private IndexBuffer _indexBuffer; private VertexBuffer[] _vertexBuffers; private BufferBounds[] _transformFeedbackBuffers; + private List _bufferTextures; /// /// Holds shader stage buffer state and binding information. @@ -138,6 +141,8 @@ namespace Ryujinx.Graphics.Gpu.Memory _gpStorageBuffers[index] = new BuffersPerStage(Constants.TotalGpStorageBuffers); _gpUniformBuffers[index] = new BuffersPerStage(Constants.TotalGpUniformBuffers); } + + _bufferTextures = new List(); } /// @@ -620,10 +625,39 @@ namespace Ryujinx.Graphics.Gpu.Memory _context.Renderer.Pipeline.SetUniformBuffers(uRanges); + CommitBufferTextureBindings(); + // Force rebind after doing compute work. _rebind = true; } + /// + /// Commit any queued buffer texture bindings. + /// + private void CommitBufferTextureBindings() + { + if (_bufferTextures.Count > 0) + { + foreach (var binding in _bufferTextures) + { + binding.Texture.SetStorage(GetBufferRange(binding.Address, binding.Size, binding.BindingInfo.Flags.HasFlag(TextureUsageFlags.ImageStore))); + + // The texture must be rebound to use the new storage if it was updated. + + if (binding.IsImage) + { + _context.Renderer.Pipeline.SetImage(binding.BindingInfo.Binding, binding.Texture, binding.Format); + } + else + { + _context.Renderer.Pipeline.SetTexture(binding.BindingInfo.Binding, binding.Texture); + } + } + + _bufferTextures.Clear(); + } + } + /// /// Ensures that the graphics engine bindings are visible to the host GPU. /// Note: this actually performs the binding using the host graphics API. @@ -743,6 +777,8 @@ namespace Ryujinx.Graphics.Gpu.Memory UpdateBuffers(_gpUniformBuffers); } + CommitBufferTextureBindings(); + _rebind = false; } @@ -813,31 +849,19 @@ namespace Ryujinx.Graphics.Gpu.Memory } /// - /// Sets the buffer storage of a buffer texture. + /// Sets the buffer storage of a buffer texture. This will be bound when the buffer manager commits bindings. /// /// Buffer texture /// Address of the buffer in memory /// Size of the buffer in bytes - /// Indicates if the buffer texture belongs to the compute or graphics pipeline - public void SetBufferTextureStorage(ITexture texture, ulong address, ulong size, bool compute) + /// Binding info for the buffer texture + /// Format of the buffer texture + /// Whether the binding is for an image or a sampler + public void SetBufferTextureStorage(ITexture texture, ulong address, ulong size, TextureBindingInfo bindingInfo, Format format, bool isImage) { CreateBuffer(address, size); - if (_rebind) - { - // We probably had to modify existing buffers to create the texture buffer, - // so rebind everything to ensure we're using the new buffers for all bound resources. - if (compute) - { - CommitComputeBindings(); - } - else - { - CommitGraphicsBindings(); - } - } - - texture.SetStorage(GetBufferRange(address, size)); + _bufferTextures.Add(new BufferTextureBinding(texture, address, size, bindingInfo, format, isImage)); } /// -- cgit v1.2.3