From 6bfe4715f05be10c73a788abd8727293a7eca77e Mon Sep 17 00:00:00 2001 From: gdkchan Date: Wed, 22 Apr 2020 03:00:11 -0300 Subject: Initial conditional rendering support (#1012) * Initial conditional rendering support * Properly reset state * Support conditional modes and skeleton a counter cache for future host conditional rendering * Address PR feedback --- Ryujinx.Graphics.Gpu/Engine/MethodClear.cs | 5 ++ .../Engine/MethodConditionalRendering.cs | 82 ++++++++++++++++++++++ Ryujinx.Graphics.Gpu/Engine/MethodDraw.cs | 9 ++- Ryujinx.Graphics.Gpu/Engine/MethodReport.cs | 7 ++ Ryujinx.Graphics.Gpu/Engine/Methods.cs | 2 + 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 Ryujinx.Graphics.Gpu/Engine/MethodConditionalRendering.cs (limited to 'Ryujinx.Graphics.Gpu/Engine') diff --git a/Ryujinx.Graphics.Gpu/Engine/MethodClear.cs b/Ryujinx.Graphics.Gpu/Engine/MethodClear.cs index ff538df3..a555015d 100644 --- a/Ryujinx.Graphics.Gpu/Engine/MethodClear.cs +++ b/Ryujinx.Graphics.Gpu/Engine/MethodClear.cs @@ -13,6 +13,11 @@ namespace Ryujinx.Graphics.Gpu.Engine /// Method call argument private void Clear(GpuState state, int argument) { + if (!GetRenderEnable(state)) + { + return; + } + // Scissor affects clears aswell. if (state.QueryModified(MethodOffset.ScissorState)) { diff --git a/Ryujinx.Graphics.Gpu/Engine/MethodConditionalRendering.cs b/Ryujinx.Graphics.Gpu/Engine/MethodConditionalRendering.cs new file mode 100644 index 00000000..4775de02 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Engine/MethodConditionalRendering.cs @@ -0,0 +1,82 @@ +using Ryujinx.Common.Logging; +using Ryujinx.Graphics.Gpu.State; + +namespace Ryujinx.Graphics.Gpu.Engine +{ + partial class Methods + { + /// + /// Checks if draws and clears should be performed, according + /// to currently set conditional rendering conditions. + /// + /// GPU state + /// True if rendering is enabled, false otherwise + private bool GetRenderEnable(GpuState state) + { + ConditionState condState = state.Get(MethodOffset.ConditionState); + + switch (condState.Condition) + { + case Condition.Always: + return true; + case Condition.Never: + return false; + case Condition.ResultNonZero: + return CounterNonZero(condState.Address.Pack()); + case Condition.Equal: + return CounterCompare(condState.Address.Pack(), true); + case Condition.NotEqual: + return CounterCompare(condState.Address.Pack(), false); + } + + Logger.PrintWarning(LogClass.Gpu, $"Invalid conditional render condition \"{condState.Condition}\"."); + + return true; + } + + /// + /// Checks if the counter value at a given GPU memory address is non-zero. + /// + /// GPU virtual address of the counter value + /// True if the value is not zero, false otherwise + private bool CounterNonZero(ulong gpuVa) + { + if (!FindAndFlush(gpuVa)) + { + return false; + } + + return _context.MemoryAccessor.ReadUInt64(gpuVa) != 0; + } + + /// + /// Checks if the counter at a given GPU memory address passes a specified equality comparison. + /// + /// GPU virtual address + /// True to check if the values are equal, false to check if they are not equal + /// True if the condition is met, false otherwise + private bool CounterCompare(ulong gpuVa, bool isEqual) + { + if (!FindAndFlush(gpuVa) && !FindAndFlush(gpuVa + 16)) + { + return false; + } + + ulong x = _context.MemoryAccessor.ReadUInt64(gpuVa); + ulong y = _context.MemoryAccessor.ReadUInt64(gpuVa + 16); + + return isEqual ? x == y : x != y; + } + + /// + /// Tries to find a counter that is supposed to be written at the specified address, + /// flushing if necessary. + /// + /// GPU virtual address where the counter is supposed to be written + /// True if a counter value is found at the specified address, false otherwise + private bool FindAndFlush(ulong gpuVa) + { + return _counterCache.Contains(gpuVa); + } + } +} diff --git a/Ryujinx.Graphics.Gpu/Engine/MethodDraw.cs b/Ryujinx.Graphics.Gpu/Engine/MethodDraw.cs index b13cc9ca..68131f62 100644 --- a/Ryujinx.Graphics.Gpu/Engine/MethodDraw.cs +++ b/Ryujinx.Graphics.Gpu/Engine/MethodDraw.cs @@ -35,8 +35,15 @@ namespace Ryujinx.Graphics.Gpu.Engine /// Method call argument private void DrawEnd(GpuState state, int argument) { - if (_instancedDrawPending) + bool renderEnable = GetRenderEnable(state); + + if (!renderEnable || _instancedDrawPending) { + if (!renderEnable) + { + PerformDeferredDraws(); + } + _drawIndexed = false; return; diff --git a/Ryujinx.Graphics.Gpu/Engine/MethodReport.cs b/Ryujinx.Graphics.Gpu/Engine/MethodReport.cs index 15151c62..eeec3569 100644 --- a/Ryujinx.Graphics.Gpu/Engine/MethodReport.cs +++ b/Ryujinx.Graphics.Gpu/Engine/MethodReport.cs @@ -1,5 +1,6 @@ using Ryujinx.Common; using Ryujinx.Graphics.GAL; +using Ryujinx.Graphics.Gpu.Memory; using Ryujinx.Graphics.Gpu.State; using System; using System.Runtime.InteropServices; @@ -11,6 +12,10 @@ namespace Ryujinx.Graphics.Gpu.Engine private const int NsToTicksFractionNumerator = 384; private const int NsToTicksFractionDenominator = 625; + private ulong _runningCounter; + + private readonly CounterCache _counterCache = new CounterCache(); + /// /// Writes a GPU counter to guest memory. /// @@ -98,6 +103,8 @@ namespace Ryujinx.Graphics.Gpu.Engine var rs = state.Get(MethodOffset.ReportState); _context.MemoryAccessor.Write(rs.Address.Pack(), data); + + _counterCache.AddOrUpdate(rs.Address.Pack()); } /// diff --git a/Ryujinx.Graphics.Gpu/Engine/Methods.cs b/Ryujinx.Graphics.Gpu/Engine/Methods.cs index 39a902e8..2e6c9828 100644 --- a/Ryujinx.Graphics.Gpu/Engine/Methods.cs +++ b/Ryujinx.Graphics.Gpu/Engine/Methods.cs @@ -52,6 +52,8 @@ namespace Ryujinx.Graphics.Gpu.Engine BufferManager = new BufferManager(context); TextureManager = new TextureManager(context); + + context.MemoryManager.MemoryUnmapped += _counterCache.MemoryUnmappedHandler; } /// -- cgit v1.2.3