diff --git a/Ryujinx.Graphics.Gpu/Engine/Threed/IbUtils.cs b/Ryujinx.Graphics.Gpu/Engine/Threed/IbUtils.cs new file mode 100644 index 000000000..15d9587d0 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Engine/Threed/IbUtils.cs @@ -0,0 +1,307 @@ +using Ryujinx.Graphics.GAL; +using Ryujinx.Graphics.Gpu.Memory; +using System; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; + +namespace Ryujinx.Graphics.Gpu.Engine.Threed +{ + /// + /// Index buffer utility methods. + /// + static class IbUtils + { + /// + /// Minimum size that the vertex buffer must have, in bytes, to make the index counting profitable. + /// + private const ulong MinimumVbSizeThreshold = 0x200000; // 2 MB + + /// + /// Maximum number of indices that the index buffer may have to make the index counting profitable. + /// + private const int MaximumIndexCountThreshold = 65536; + + /// + /// Checks if getting the vertex buffer size from the maximum index buffer index is worth it. + /// + /// Maximum size that the vertex buffer may possibly have, in bytes + /// Total number of indices on the index buffer + /// True if getting the vertex buffer size from the index buffer may yield performance improvements + public static bool IsIbCountingProfitable(ulong vbSizeMax, int indexCount) + { + return vbSizeMax >= MinimumVbSizeThreshold && indexCount <= MaximumIndexCountThreshold; + } + + /// + /// Gets the vertex count of the vertex buffer accessed with the indices from the current index buffer. + /// + /// GPU memory manager + /// Index buffer element integer type + /// GPU virtual address of the index buffer + /// Index of the first index buffer element used on the draw + /// Number of index buffer elements used on the draw + /// Vertex count + public static ulong GetVertexCount(MemoryManager mm, IndexType type, ulong gpuVa, int firstIndex, int indexCount) + { + return type switch + { + IndexType.UShort => CountU16(mm, gpuVa, firstIndex, indexCount), + IndexType.UInt => CountU32(mm, gpuVa, firstIndex, indexCount), + _ => CountU8(mm, gpuVa, firstIndex, indexCount) + }; + } + + /// + /// Gets the vertex count of the vertex buffer accessed with the indices from the current index buffer, with 8-bit indices. + /// + /// GPU memory manager + /// GPU virtual address of the index buffer + /// Index of the first index buffer element used on the draw + /// Number of index buffer elements used on the draw + /// Vertex count + private unsafe static ulong CountU8(MemoryManager mm, ulong gpuVa, int firstIndex, int indexCount) + { + uint max = 0; + ReadOnlySpan data = mm.GetSpan(gpuVa, firstIndex + indexCount); + + if (Avx2.IsSupported) + { + fixed (byte* pInput = data) + { + int endAligned = firstIndex + ((data.Length - firstIndex) & ~127); + + var result = Vector256.Zero; + + for (int i = firstIndex; i < endAligned; i += 128) + { + var dataVec0 = Avx.LoadVector256(pInput + (nuint)(uint)i); + var dataVec1 = Avx.LoadVector256(pInput + (nuint)(uint)i + 32); + var dataVec2 = Avx.LoadVector256(pInput + (nuint)(uint)i + 64); + var dataVec3 = Avx.LoadVector256(pInput + (nuint)(uint)i + 96); + + var max01 = Avx2.Max(dataVec0, dataVec1); + var max23 = Avx2.Max(dataVec2, dataVec3); + var max0123 = Avx2.Max(max01, max23); + + result = Avx2.Max(result, max0123); + } + + result = Avx2.Max(result, Avx2.Shuffle(result.AsInt32(), 0xee).AsByte()); + result = Avx2.Max(result, Avx2.Shuffle(result.AsInt32(), 0x55).AsByte()); + result = Avx2.Max(result, Avx2.ShuffleLow(result.AsUInt16(), 0x55).AsByte()); + result = Avx2.Max(result, Avx2.ShiftRightLogical(result.AsUInt16(), 8).AsByte()); + + max = Math.Max(result.GetElement(0), result.GetElement(16)); + + firstIndex = endAligned; + } + } + else if (Sse2.IsSupported) + { + fixed (byte* pInput = data) + { + int endAligned = firstIndex + ((data.Length - firstIndex) & ~63); + + var result = Vector128.Zero; + + for (int i = firstIndex; i < endAligned; i += 64) + { + var dataVec0 = Sse2.LoadVector128(pInput + (nuint)(uint)i); + var dataVec1 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 16); + var dataVec2 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 32); + var dataVec3 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 48); + + var max01 = Sse2.Max(dataVec0, dataVec1); + var max23 = Sse2.Max(dataVec2, dataVec3); + var max0123 = Sse2.Max(max01, max23); + + result = Sse2.Max(result, max0123); + } + + result = Sse2.Max(result, Sse2.Shuffle(result.AsInt32(), 0xee).AsByte()); + result = Sse2.Max(result, Sse2.Shuffle(result.AsInt32(), 0x55).AsByte()); + result = Sse2.Max(result, Sse2.ShuffleLow(result.AsUInt16(), 0x55).AsByte()); + result = Sse2.Max(result, Sse2.ShiftRightLogical(result.AsUInt16(), 8).AsByte()); + + max = result.GetElement(0); + + firstIndex = endAligned; + } + } + + for (int i = firstIndex; i < data.Length; i++) + { + if (max < data[i]) max = data[i]; + } + + return (ulong)max + 1; + } + + /// + /// Gets the vertex count of the vertex buffer accessed with the indices from the current index buffer, with 16-bit indices. + /// + /// GPU memory manager + /// GPU virtual address of the index buffer + /// Index of the first index buffer element used on the draw + /// Number of index buffer elements used on the draw + /// Vertex count + private unsafe static ulong CountU16(MemoryManager mm, ulong gpuVa, int firstIndex, int indexCount) + { + uint max = 0; + ReadOnlySpan data = MemoryMarshal.Cast(mm.GetSpan(gpuVa, (firstIndex + indexCount) * 2)); + + if (Avx2.IsSupported) + { + fixed (ushort* pInput = data) + { + int endAligned = firstIndex + ((data.Length - firstIndex) & ~63); + + var result = Vector256.Zero; + + for (int i = firstIndex; i < endAligned; i += 64) + { + var dataVec0 = Avx.LoadVector256(pInput + (nuint)(uint)i); + var dataVec1 = Avx.LoadVector256(pInput + (nuint)(uint)i + 16); + var dataVec2 = Avx.LoadVector256(pInput + (nuint)(uint)i + 32); + var dataVec3 = Avx.LoadVector256(pInput + (nuint)(uint)i + 48); + + var max01 = Avx2.Max(dataVec0, dataVec1); + var max23 = Avx2.Max(dataVec2, dataVec3); + var max0123 = Avx2.Max(max01, max23); + + result = Avx2.Max(result, max0123); + } + + result = Avx2.Max(result, Avx2.Shuffle(result.AsInt32(), 0xee).AsUInt16()); + result = Avx2.Max(result, Avx2.Shuffle(result.AsInt32(), 0x55).AsUInt16()); + result = Avx2.Max(result, Avx2.ShuffleLow(result, 0x55)); + + max = Math.Max(result.GetElement(0), result.GetElement(8)); + + firstIndex = endAligned; + } + } + else if (Sse41.IsSupported) + { + fixed (ushort* pInput = data) + { + int endAligned = firstIndex + ((data.Length - firstIndex) & ~31); + + var result = Vector128.Zero; + + for (int i = firstIndex; i < endAligned; i += 32) + { + var dataVec0 = Sse2.LoadVector128(pInput + (nuint)(uint)i); + var dataVec1 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 8); + var dataVec2 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 16); + var dataVec3 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 24); + + var max01 = Sse41.Max(dataVec0, dataVec1); + var max23 = Sse41.Max(dataVec2, dataVec3); + var max0123 = Sse41.Max(max01, max23); + + result = Sse41.Max(result, max0123); + } + + result = Sse41.Max(result, Sse2.Shuffle(result.AsInt32(), 0xee).AsUInt16()); + result = Sse41.Max(result, Sse2.Shuffle(result.AsInt32(), 0x55).AsUInt16()); + result = Sse41.Max(result, Sse2.ShuffleLow(result, 0x55)); + + max = result.GetElement(0); + + firstIndex = endAligned; + } + } + + for (int i = firstIndex; i < data.Length; i++) + { + if (max < data[i]) max = data[i]; + } + + return (ulong)max + 1; + } + + /// + /// Gets the vertex count of the vertex buffer accessed with the indices from the current index buffer, with 32-bit indices. + /// + /// GPU memory manager + /// GPU virtual address of the index buffer + /// Index of the first index buffer element used on the draw + /// Number of index buffer elements used on the draw + /// Vertex count + private unsafe static ulong CountU32(MemoryManager mm, ulong gpuVa, int firstIndex, int indexCount) + { + uint max = 0; + ReadOnlySpan data = MemoryMarshal.Cast(mm.GetSpan(gpuVa, (firstIndex + indexCount) * 4)); + + if (Avx2.IsSupported) + { + fixed (uint* pInput = data) + { + int endAligned = firstIndex + ((data.Length - firstIndex) & ~31); + + var result = Vector256.Zero; + + for (int i = firstIndex; i < endAligned; i += 32) + { + var dataVec0 = Avx.LoadVector256(pInput + (nuint)(uint)i); + var dataVec1 = Avx.LoadVector256(pInput + (nuint)(uint)i + 8); + var dataVec2 = Avx.LoadVector256(pInput + (nuint)(uint)i + 16); + var dataVec3 = Avx.LoadVector256(pInput + (nuint)(uint)i + 24); + + var max01 = Avx2.Max(dataVec0, dataVec1); + var max23 = Avx2.Max(dataVec2, dataVec3); + var max0123 = Avx2.Max(max01, max23); + + result = Avx2.Max(result, max0123); + } + + result = Avx2.Max(result, Avx2.Shuffle(result, 0xee)); + result = Avx2.Max(result, Avx2.Shuffle(result, 0x55)); + + max = Math.Max(result.GetElement(0), result.GetElement(4)); + + firstIndex = endAligned; + } + } + else if (Sse41.IsSupported) + { + fixed (uint* pInput = data) + { + int endAligned = firstIndex + ((data.Length - firstIndex) & ~15); + + var result = Vector128.Zero; + + for (int i = firstIndex; i < endAligned; i += 16) + { + var dataVec0 = Sse2.LoadVector128(pInput + (nuint)(uint)i); + var dataVec1 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 4); + var dataVec2 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 8); + var dataVec3 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 12); + + var max01 = Sse41.Max(dataVec0, dataVec1); + var max23 = Sse41.Max(dataVec2, dataVec3); + var max0123 = Sse41.Max(max01, max23); + + result = Sse41.Max(result, max0123); + } + + result = Sse41.Max(result, Sse2.Shuffle(result, 0xee)); + result = Sse41.Max(result, Sse2.Shuffle(result, 0x55)); + + max = result.GetElement(0); + + firstIndex = endAligned; + } + } + + for (int i = firstIndex; i < data.Length; i++) + { + if (max < data[i]) max = data[i]; + } + + return (ulong)max + 1; + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs b/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs index d9484cf65..fb4ab007c 100644 --- a/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs +++ b/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs @@ -34,6 +34,8 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed private byte _vsClipDistancesWritten; private bool _prevDrawIndexed; + private int _prevFirstIndex; + private int _prevIndexCount; private bool _prevTfEnable; /// @@ -182,10 +184,25 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed // method when doing indexed draws, so we need to make sure // to update the vertex buffers if we are doing a regular // draw after a indexed one and vice-versa. - if (_drawState.DrawIndexed != _prevDrawIndexed) + if (GraphicsConfig.EnableIndexedVbSizeDetection) { - _updateTracker.ForceDirty(VertexBufferStateIndex); - _prevDrawIndexed = _drawState.DrawIndexed; + if (_drawState.DrawIndexed != _prevDrawIndexed || + _drawState.FirstIndex != _prevFirstIndex || + _drawState.IndexCount != _prevIndexCount) + { + _updateTracker.ForceDirty(VertexBufferStateIndex); + _prevDrawIndexed = _drawState.DrawIndexed; + _prevFirstIndex = _drawState.FirstIndex; + _prevIndexCount = _drawState.IndexCount; + } + } + else + { + if (_drawState.DrawIndexed != _prevDrawIndexed) + { + _updateTracker.ForceDirty(VertexBufferStateIndex); + _prevDrawIndexed = _drawState.DrawIndexed; + } } bool tfEnable = _state.State.TfEnable; @@ -782,7 +799,25 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed { // This size may be (much) larger than the real vertex buffer size. // Avoid calculating it this way, unless we don't have any other option. - size = endAddress.Pack() - address + 1; + ulong vbSizeMax = endAddress.Pack() - address + 1; + + int firstIndex = _drawState.FirstIndex; + int indexCount = _drawState.IndexCount; + + bool ibCountingProfitable = GraphicsConfig.EnableIndexedVbSizeDetection && IbUtils.IsIbCountingProfitable(vbSizeMax, indexCount); + + if (ibCountingProfitable && !_drawState.IbStreamer.HasInlineIndexData && _drawState.DrawIndexed && stride != 0) + { + IndexType ibType = _state.State.IndexBufferState.Type; + ulong ibGpuVa = _state.State.IndexBufferState.Address.Pack(); + ulong vertexCount = IbUtils.GetVertexCount(_channel.MemoryManager, ibType, ibGpuVa, firstIndex, indexCount); + + size = Math.Min(vertexCount * (ulong)stride, vbSizeMax); + } + else + { + size = vbSizeMax; + } } else { diff --git a/Ryujinx.Graphics.Gpu/GraphicsConfig.cs b/Ryujinx.Graphics.Gpu/GraphicsConfig.cs index 7ef102e2c..14735e3bd 100644 --- a/Ryujinx.Graphics.Gpu/GraphicsConfig.cs +++ b/Ryujinx.Graphics.Gpu/GraphicsConfig.cs @@ -33,6 +33,11 @@ namespace Ryujinx.Graphics.Gpu /// public static bool EnableMacroJit = true; + /// + /// Enables or disables vertex buffer size detection from the index buffer, for indexed draws. + /// + public static bool EnableIndexedVbSizeDetection = true; + /// /// Title id of the current running game. /// Used by the shader cache. diff --git a/Ryujinx/Configuration/ConfigurationState.cs b/Ryujinx/Configuration/ConfigurationState.cs index fe4ff7746..5291ddc84 100644 --- a/Ryujinx/Configuration/ConfigurationState.cs +++ b/Ryujinx/Configuration/ConfigurationState.cs @@ -269,7 +269,7 @@ namespace Ryujinx.Configuration /// Enable or disable keyboard support (Independent from controllers binding) /// public ReactiveObject EnableKeyboard { get; private set; } - + /// /// Enable or disable mouse support (Independent from controllers binding) ///