From 82cefc8dd3babb781d4b7229435e26911fb083dd Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sun, 29 Aug 2021 16:52:38 -0300 Subject: [PATCH] Handle indirect draw counts with non-zero draw starts properly (#2593) --- Ryujinx.Cpu/IVirtualMemoryManagerTracked.cs | 8 ++++ Ryujinx.Cpu/MemoryManager.cs | 2 +- Ryujinx.Graphics.Gpu/Engine/MME/MacroHLE.cs | 43 +++++++++++++++---- Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs | 27 +++++++++++- Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs | 15 ++++++- 5 files changed, 81 insertions(+), 14 deletions(-) diff --git a/Ryujinx.Cpu/IVirtualMemoryManagerTracked.cs b/Ryujinx.Cpu/IVirtualMemoryManagerTracked.cs index 23b4119f15..8004d39bcf 100644 --- a/Ryujinx.Cpu/IVirtualMemoryManagerTracked.cs +++ b/Ryujinx.Cpu/IVirtualMemoryManagerTracked.cs @@ -8,6 +8,14 @@ namespace Ryujinx.Cpu { public interface IVirtualMemoryManagerTracked : IVirtualMemoryManager { + /// + /// Reads data from CPU mapped memory, with read tracking + /// + /// Type of the data being read + /// Virtual address of the data in memory + /// The data + T ReadTracked(ulong va) where T : unmanaged; + /// /// Writes data to CPU mapped memory, without write tracking. /// diff --git a/Ryujinx.Cpu/MemoryManager.cs b/Ryujinx.Cpu/MemoryManager.cs index 153bff629b..2c11bab810 100644 --- a/Ryujinx.Cpu/MemoryManager.cs +++ b/Ryujinx.Cpu/MemoryManager.cs @@ -117,7 +117,7 @@ namespace Ryujinx.Cpu /// public T Read(ulong va) where T : unmanaged { - return MemoryMarshal.Cast(GetSpan(va, Unsafe.SizeOf(), true))[0]; + return MemoryMarshal.Cast(GetSpan(va, Unsafe.SizeOf()))[0]; } /// diff --git a/Ryujinx.Graphics.Gpu/Engine/MME/MacroHLE.cs b/Ryujinx.Graphics.Gpu/Engine/MME/MacroHLE.cs index 77b44e8141..05f3df0eea 100644 --- a/Ryujinx.Graphics.Gpu/Engine/MME/MacroHLE.cs +++ b/Ryujinx.Graphics.Gpu/Engine/MME/MacroHLE.cs @@ -2,8 +2,6 @@ using Ryujinx.Common.Logging; using Ryujinx.Graphics.Device; using Ryujinx.Graphics.GAL; using Ryujinx.Graphics.Gpu.Engine.GPFifo; -using Ryujinx.Graphics.Gpu.Engine.Threed; -using Ryujinx.Graphics.Gpu.Memory; using System; using System.Collections.Generic; @@ -66,17 +64,42 @@ namespace Ryujinx.Graphics.Gpu.Engine.MME int arg2 = FetchParam().Word; int arg3 = FetchParam().Word; - int startOffset = arg0; - int endOffset = arg1; + int startDraw = arg0; + int endDraw = arg1; var topology = (PrimitiveTopology)arg2; int paddingWords = arg3; - int maxDrawCount = endOffset - startOffset; int stride = paddingWords * 4 + 0x14; - int indirectBufferSize = maxDrawCount * stride; ulong parameterBufferGpuVa = FetchParam().GpuVa; - ulong indirectBufferGpuVa = 0; + int maxDrawCount = endDraw - startDraw; + + if (startDraw != 0) + { + int drawCount = _processor.MemoryManager.Read(parameterBufferGpuVa, tracked: true); + + // Calculate maximum draw count based on the previous draw count and current draw count. + if ((uint)drawCount <= (uint)startDraw) + { + // The start draw is past our total draw count, so all draws were already performed. + maxDrawCount = 0; + } + else + { + // Perform just the missing number of draws. + maxDrawCount = (int)Math.Min((uint)maxDrawCount, (uint)(drawCount - startDraw)); + } + } + + if (maxDrawCount == 0) + { + Fifo.Clear(); + return; + } + + int indirectBufferSize = maxDrawCount * stride; + + ulong indirectBufferGpuVa = 0; int indexCount = 0; for (int i = 0; i < maxDrawCount; i++) @@ -106,8 +129,10 @@ namespace Ryujinx.Graphics.Gpu.Engine.MME // It should be empty at this point, but clear it just to be safe. Fifo.Clear(); - var parameterBuffer = _processor.MemoryManager.Physical.BufferCache.GetGpuBufferRange(_processor.MemoryManager, parameterBufferGpuVa, 4); - var indirectBuffer = _processor.MemoryManager.Physical.BufferCache.GetGpuBufferRange(_processor.MemoryManager, indirectBufferGpuVa, (ulong)indirectBufferSize); + var bufferCache = _processor.MemoryManager.Physical.BufferCache; + + var parameterBuffer = bufferCache.GetGpuBufferRange(_processor.MemoryManager, parameterBufferGpuVa, 4); + var indirectBuffer = bufferCache.GetGpuBufferRange(_processor.MemoryManager, indirectBufferGpuVa, (ulong)indirectBufferSize); _processor.ThreedClass.MultiDrawIndirectCount(indexCount, topology, indirectBuffer, parameterBuffer, maxDrawCount, stride); } diff --git a/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs b/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs index b747b558f4..2dc1edd24a 100644 --- a/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs +++ b/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs @@ -63,10 +63,33 @@ namespace Ryujinx.Graphics.Gpu.Memory /// /// Type of the data /// GPU virtual address where the data is located + /// True if read tracking is triggered on the memory region /// The data at the specified memory location - public T Read(ulong va) where T : unmanaged + public T Read(ulong va, bool tracked = false) where T : unmanaged { - return MemoryMarshal.Cast(GetSpan(va, Unsafe.SizeOf()))[0]; + int size = Unsafe.SizeOf(); + + if (IsContiguous(va, size)) + { + ulong address = Translate(va); + + if (tracked) + { + return Physical.ReadTracked(address); + } + else + { + return Physical.Read(address); + } + } + else + { + Span data = new byte[size]; + + ReadImpl(va, data, tracked); + + return MemoryMarshal.Cast(data)[0]; + } } /// diff --git a/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs b/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs index 8df3c8fb30..fd2a74766e 100644 --- a/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs +++ b/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs @@ -139,11 +139,22 @@ namespace Ryujinx.Graphics.Gpu.Memory /// Reads data from the application process. /// /// Type of the structure - /// Address to read from + /// Address to read from /// The data at the specified memory location public T Read(ulong address) where T : unmanaged { - return MemoryMarshal.Cast(GetSpan(address, Unsafe.SizeOf()))[0]; + return _cpuMemory.Read(address); + } + + /// + /// Reads data from the application process, with write tracking. + /// + /// Type of the structure + /// Address to read from + /// The data at the specified memory location + public T ReadTracked(ulong address) where T : unmanaged + { + return _cpuMemory.ReadTracked(address); } ///