From 7404d782cee8cf9e3e9495e4f1274d9a6476656b Mon Sep 17 00:00:00 2001 From: sunshineinabox Date: Fri, 5 Jul 2024 00:40:49 -0700 Subject: [PATCH] Avoid setting DepthBias when not needed. --- src/Ryujinx.Graphics.GAL/IPipeline.cs | 3 +- .../Multithreading/CommandHelper.cs | 1 + .../Multithreading/CommandType.cs | 1 + .../Commands/SetDepthBiasCommand.cs | 6 ++-- .../Commands/SetDepthBiasEnableCommand.cs | 19 +++++++++++ .../Multithreading/ThreadedPipeline.cs | 10 ++++-- .../Engine/Threed/StateUpdater.cs | 21 +++++++++--- src/Ryujinx.Graphics.OpenGL/Pipeline.cs | 33 ++++++++++--------- src/Ryujinx.Graphics.Vulkan/PipelineBase.cs | 8 ++++- src/Ryujinx.Graphics.Vulkan/PipelineState.cs | 22 +++++++++---- 10 files changed, 90 insertions(+), 34 deletions(-) create mode 100644 src/Ryujinx.Graphics.GAL/Multithreading/Commands/SetDepthBiasEnableCommand.cs diff --git a/src/Ryujinx.Graphics.GAL/IPipeline.cs b/src/Ryujinx.Graphics.GAL/IPipeline.cs index cbf1bc3a2..71f99fc0b 100644 --- a/src/Ryujinx.Graphics.GAL/IPipeline.cs +++ b/src/Ryujinx.Graphics.GAL/IPipeline.cs @@ -47,7 +47,8 @@ namespace Ryujinx.Graphics.GAL void SetBlendState(AdvancedBlendDescriptor blend); void SetBlendState(int index, BlendDescriptor blend); - void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp); + void SetDepthBias(float factor, float units, float clamp); + void SetDepthBiasEnable(PolygonModeMask enables); void SetDepthClamp(bool clamp); void SetDepthMode(DepthMode mode); void SetDepthTest(DepthTestDescriptor depthTest); diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/CommandHelper.cs b/src/Ryujinx.Graphics.GAL/Multithreading/CommandHelper.cs index ef227d4a5..67eccb34c 100644 --- a/src/Ryujinx.Graphics.GAL/Multithreading/CommandHelper.cs +++ b/src/Ryujinx.Graphics.GAL/Multithreading/CommandHelper.cs @@ -116,6 +116,7 @@ namespace Ryujinx.Graphics.GAL.Multithreading Register(CommandType.SetBlendStateAdvanced); Register(CommandType.SetBlendState); Register(CommandType.SetDepthBias); + Register(CommandType.SetDepthBiasEnable); Register(CommandType.SetDepthClamp); Register(CommandType.SetDepthMode); Register(CommandType.SetDepthTest); diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/CommandType.cs b/src/Ryujinx.Graphics.GAL/Multithreading/CommandType.cs index cf3f5d6c1..cb009c75d 100644 --- a/src/Ryujinx.Graphics.GAL/Multithreading/CommandType.cs +++ b/src/Ryujinx.Graphics.GAL/Multithreading/CommandType.cs @@ -76,6 +76,7 @@ namespace Ryujinx.Graphics.GAL.Multithreading SetBlendStateAdvanced, SetBlendState, SetDepthBias, + SetDepthBiasEnable, SetDepthClamp, SetDepthMode, SetDepthTest, diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/Commands/SetDepthBiasCommand.cs b/src/Ryujinx.Graphics.GAL/Multithreading/Commands/SetDepthBiasCommand.cs index 0c46fbda2..1fa380327 100644 --- a/src/Ryujinx.Graphics.GAL/Multithreading/Commands/SetDepthBiasCommand.cs +++ b/src/Ryujinx.Graphics.GAL/Multithreading/Commands/SetDepthBiasCommand.cs @@ -3,14 +3,12 @@ namespace Ryujinx.Graphics.GAL.Multithreading.Commands struct SetDepthBiasCommand : IGALCommand, IGALCommand { public readonly CommandType CommandType => CommandType.SetDepthBias; - private PolygonModeMask _enables; private float _factor; private float _units; private float _clamp; - public void Set(PolygonModeMask enables, float factor, float units, float clamp) + public void Set(float factor, float units, float clamp) { - _enables = enables; _factor = factor; _units = units; _clamp = clamp; @@ -18,7 +16,7 @@ namespace Ryujinx.Graphics.GAL.Multithreading.Commands public static void Run(ref SetDepthBiasCommand command, ThreadedRenderer threaded, IRenderer renderer) { - renderer.Pipeline.SetDepthBias(command._enables, command._factor, command._units, command._clamp); + renderer.Pipeline.SetDepthBias(command._factor, command._units, command._clamp); } } } diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/Commands/SetDepthBiasEnableCommand.cs b/src/Ryujinx.Graphics.GAL/Multithreading/Commands/SetDepthBiasEnableCommand.cs new file mode 100644 index 000000000..176da6f39 --- /dev/null +++ b/src/Ryujinx.Graphics.GAL/Multithreading/Commands/SetDepthBiasEnableCommand.cs @@ -0,0 +1,19 @@ +namespace Ryujinx.Graphics.GAL.Multithreading.Commands +{ + struct SetDepthBiasEnableCommand : IGALCommand, IGALCommand + { + public readonly CommandType CommandType => CommandType.SetDepthBias; + private PolygonModeMask _enables; + + + public void Set(PolygonModeMask enables) + { + _enables = enables; + } + + public static void Run(ref SetDepthBiasEnableCommand command, ThreadedRenderer threaded, IRenderer renderer) + { + renderer.Pipeline.SetDepthBiasEnable(command._enables); + } + } +} diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/ThreadedPipeline.cs b/src/Ryujinx.Graphics.GAL/Multithreading/ThreadedPipeline.cs index edd79d8a0..45ae679ce 100644 --- a/src/Ryujinx.Graphics.GAL/Multithreading/ThreadedPipeline.cs +++ b/src/Ryujinx.Graphics.GAL/Multithreading/ThreadedPipeline.cs @@ -141,9 +141,15 @@ namespace Ryujinx.Graphics.GAL.Multithreading _renderer.QueueCommand(); } - public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp) + public void SetDepthBias(float factor, float units, float clamp) { - _renderer.New().Set(enables, factor, units, clamp); + _renderer.New().Set(factor, units, clamp); + _renderer.QueueCommand(); + } + + public void SetDepthBiasEnable(PolygonModeMask enables) + { + _renderer.New().Set(enables); _renderer.QueueCommand(); } diff --git a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs index 1dc77b52d..569481d8c 100644 --- a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs +++ b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs @@ -849,12 +849,25 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed PolygonModeMask enables; - enables = (depthBias.PointEnable ? PolygonModeMask.Point : 0); - enables |= (depthBias.LineEnable ? PolygonModeMask.Line : 0); - enables |= (depthBias.FillEnable ? PolygonModeMask.Fill : 0); + if (factor == 0 && units == 0) + { + enables = 0; + } + else + { + enables = (depthBias.PointEnable ? PolygonModeMask.Point : 0); + enables |= (depthBias.LineEnable ? PolygonModeMask.Line : 0); + enables |= (depthBias.FillEnable ? PolygonModeMask.Fill : 0); + } _pipeline.BiasEnable = enables; - _context.Renderer.Pipeline.SetDepthBias(enables, factor, units / 2f, clamp); + + _context.Renderer.Pipeline.SetDepthBiasEnable(enables); + + if (enables != 0) + { + _context.Renderer.Pipeline.SetDepthBias(factor, units / 2f, clamp); + } } /// diff --git a/src/Ryujinx.Graphics.OpenGL/Pipeline.cs b/src/Ryujinx.Graphics.OpenGL/Pipeline.cs index 54f6b3f7b..1dcafd718 100644 --- a/src/Ryujinx.Graphics.OpenGL/Pipeline.cs +++ b/src/Ryujinx.Graphics.OpenGL/Pipeline.cs @@ -831,8 +831,25 @@ namespace Ryujinx.Graphics.OpenGL GL.Enable(IndexedEnableCap.Blend, index); } - public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp) + public void SetDepthBias(float factor, float units, float clamp) { + if (HwCapabilities.SupportsPolygonOffsetClamp) + { + GL.PolygonOffsetClamp(factor, units, clamp); + } + else + { + GL.PolygonOffset(factor, units); + } + } + + public void SetDepthBiasEnable(PolygonModeMask enables) + { + if (enables == 0) + { + return; + } + if ((enables & PolygonModeMask.Point) != 0) { GL.Enable(EnableCap.PolygonOffsetPoint); @@ -859,20 +876,6 @@ namespace Ryujinx.Graphics.OpenGL { GL.Disable(EnableCap.PolygonOffsetFill); } - - if (enables == 0) - { - return; - } - - if (HwCapabilities.SupportsPolygonOffsetClamp) - { - GL.PolygonOffsetClamp(factor, units, clamp); - } - else - { - GL.PolygonOffset(factor, units); - } } public void SetDepthClamp(bool clamp) diff --git a/src/Ryujinx.Graphics.Vulkan/PipelineBase.cs b/src/Ryujinx.Graphics.Vulkan/PipelineBase.cs index bda6167d7..b664980a7 100644 --- a/src/Ryujinx.Graphics.Vulkan/PipelineBase.cs +++ b/src/Ryujinx.Graphics.Vulkan/PipelineBase.cs @@ -785,11 +785,17 @@ namespace Ryujinx.Graphics.Vulkan SignalStateChange(); } - public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp) + public void SetDepthBias(float factor, float units, float clamp) { DynamicState.SetDepthBias(factor, units, clamp); + SignalStateChange(); + } + + public void SetDepthBiasEnable(PolygonModeMask enables) + { _newState.DepthBiasEnable = enables != 0; + SignalStateChange(); } diff --git a/src/Ryujinx.Graphics.Vulkan/PipelineState.cs b/src/Ryujinx.Graphics.Vulkan/PipelineState.cs index 2a8f93081..8373c9737 100644 --- a/src/Ryujinx.Graphics.Vulkan/PipelineState.cs +++ b/src/Ryujinx.Graphics.Vulkan/PipelineState.cs @@ -564,21 +564,29 @@ namespace Ryujinx.Graphics.Vulkan } bool supportsExtDynamicState = gd.Capabilities.SupportsExtendedDynamicState; - int dynamicStatesCount = supportsExtDynamicState ? 8 : 7; + int dynamicStatesCount = supportsExtDynamicState ? 7 : 6; + if (DepthBiasEnable) + { + dynamicStatesCount++; + } DynamicState* dynamicStates = stackalloc DynamicState[dynamicStatesCount]; dynamicStates[0] = DynamicState.Viewport; dynamicStates[1] = DynamicState.Scissor; - dynamicStates[2] = DynamicState.DepthBias; - dynamicStates[3] = DynamicState.StencilCompareMask; - dynamicStates[4] = DynamicState.StencilWriteMask; - dynamicStates[5] = DynamicState.StencilReference; - dynamicStates[6] = DynamicState.BlendConstants; + dynamicStates[2] = DynamicState.StencilCompareMask; + dynamicStates[3] = DynamicState.StencilWriteMask; + dynamicStates[4] = DynamicState.StencilReference; + dynamicStates[5] = DynamicState.BlendConstants; + + if (DepthBiasEnable) + { + dynamicStates[6] = DynamicState.DepthBias; + } if (supportsExtDynamicState) { - dynamicStates[7] = DynamicState.VertexInputBindingStrideExt; + dynamicStates[dynamicStatesCount - 1] = DynamicState.VertexInputBindingStrideExt; } var pipelineDynamicStateCreateInfo = new PipelineDynamicStateCreateInfo