Avoid unncessary state changes?

This commit is contained in:
sunshineinabox 2024-05-18 14:31:38 -07:00
parent 5f2d924bac
commit 767abfe500

View file

@ -90,182 +90,229 @@ namespace Ryujinx.Graphics.Vulkan
private DirtyFlags _dirty; private DirtyFlags _dirty;
private const float Epsilon = 1e-6f;
private bool FloatCompare(float a, float b)
{
return Math.Abs(a - b) < Epsilon;
}
public void SetBlendConstants(float r, float g, float b, float a) public void SetBlendConstants(float r, float g, float b, float a)
{ {
_blendConstants[0] = r; if (!FloatCompare(_blendConstants[0], r) ||
_blendConstants[1] = g; !FloatCompare(_blendConstants[1], g) ||
_blendConstants[2] = b; !FloatCompare(_blendConstants[2], b) ||
_blendConstants[3] = a; !FloatCompare(_blendConstants[3], a))
{
_dirty |= DirtyFlags.Blend; _blendConstants[0] = r;
_blendConstants[1] = g;
_blendConstants[2] = b;
_blendConstants[3] = a;
_dirty |= DirtyFlags.Blend;
}
} }
public void SetDepthBias(float slopeFactor, float constantFactor, float clamp, bool enable) public void SetDepthBias(float slopeFactor, float constantFactor, float clamp, bool enable)
{ {
_depthBiasSlopeFactor = slopeFactor; if (!FloatCompare(_depthBiasSlopeFactor, slopeFactor) ||
_depthBiasConstantFactor = constantFactor; !FloatCompare(_depthBiasConstantFactor, constantFactor) ||
_depthBiasClamp = clamp; !FloatCompare(_depthBiasClamp, clamp) ||
_depthBiasEnable = enable; _depthBiasEnable != enable)
{
_dirty |= DirtyFlags.DepthBias; _depthBiasSlopeFactor = slopeFactor;
_depthBiasConstantFactor = constantFactor;
_depthBiasClamp = clamp;
_depthBiasEnable = enable;
_dirty |= DirtyFlags.DepthBias;
}
} }
public void SetScissor(int index, Rect2D scissor) public void SetScissor(int index, Rect2D scissor)
{ {
_scissors[index] = scissor; if (!_scissors[index].Equals(scissor))
{
_dirty |= DirtyFlags.Scissor; _scissors[index] = scissor;
_dirty |= DirtyFlags.Scissor;
}
} }
public void SetDepthTestBool(bool testEnable, bool writeEnable) public void SetDepthTestBool(bool testEnable, bool writeEnable)
{ {
_depthtestEnable = testEnable; if (_depthtestEnable != testEnable || _depthwriteEnable != writeEnable)
_depthwriteEnable = writeEnable; {
_depthtestEnable = testEnable;
_dirty |= DirtyFlags.DepthTestBool; _depthwriteEnable = writeEnable;
_dirty |= DirtyFlags.DepthTestBool;
}
} }
public void SetDepthTestCompareOp(CompareOp depthTestOp) public void SetDepthTestCompareOp(CompareOp depthTestOp)
{ {
_depthCompareOp = depthTestOp; if (_depthCompareOp != depthTestOp)
{
_dirty |= DirtyFlags.DepthTestCompareOp; _depthCompareOp = depthTestOp;
_dirty |= DirtyFlags.DepthTestCompareOp;
}
} }
public void SetStencilOp(StencilOp backFailOp, public void SetStencilOp(StencilOp backFailOp, StencilOp backPassOp, StencilOp backDepthFailOp,
StencilOp backPassOp, CompareOp backCompareOp, StencilOp frontFailOp, StencilOp frontPassOp, StencilOp frontDepthFailOp,
StencilOp backDepthFailOp,
CompareOp backCompareOp,
StencilOp frontFailOp,
StencilOp frontPassOp,
StencilOp frontDepthFailOp,
CompareOp frontCompareOp) CompareOp frontCompareOp)
{ {
_backfailop = backFailOp; if (_backfailop != backFailOp || _backpassop != backPassOp || _backdepthfailop != backDepthFailOp ||
_backpassop = backPassOp; _backcompareop != backCompareOp || _frontfailop != frontFailOp || _frontpassop != frontPassOp ||
_backdepthfailop = backDepthFailOp; _frontdepthfailop != frontDepthFailOp || _frontcompareop != frontCompareOp)
_backcompareop = backCompareOp; {
_backfailop = backFailOp;
_frontfailop = frontFailOp; _backpassop = backPassOp;
_frontpassop = frontPassOp; _backdepthfailop = backDepthFailOp;
_frontdepthfailop = frontDepthFailOp; _backcompareop = backCompareOp;
_frontcompareop = frontCompareOp; _frontfailop = frontFailOp;
_frontpassop = frontPassOp;
_opToo = true; _frontdepthfailop = frontDepthFailOp;
_frontcompareop = frontCompareOp;
_opToo = true;
}
} }
public void SetStencilMask( public void SetStencilMask(uint backCompareMask, uint backWriteMask, uint backReference,
uint backCompareMask, uint frontCompareMask, uint frontWriteMask, uint frontReference)
uint backWriteMask,
uint backReference,
uint frontCompareMask,
uint frontWriteMask,
uint frontReference)
{ {
_backCompareMask = backCompareMask; if (_backCompareMask != backCompareMask || _backWriteMask != backWriteMask ||
_backWriteMask = backWriteMask; _backReference != backReference || _frontCompareMask != frontCompareMask ||
_backReference = backReference; _frontWriteMask != frontWriteMask || _frontReference != frontReference)
_frontCompareMask = frontCompareMask; {
_frontWriteMask = frontWriteMask; _backCompareMask = backCompareMask;
_frontReference = frontReference; _backWriteMask = backWriteMask;
_backReference = backReference;
_dirty |= DirtyFlags.Stencil; _frontCompareMask = frontCompareMask;
_frontWriteMask = frontWriteMask;
_frontReference = frontReference;
_dirty |= DirtyFlags.Stencil;
}
} }
public void SetStencilTest(bool stencilTestEnable) public void SetStencilTest(bool stencilTestEnable)
{ {
_stencilTestEnable = stencilTestEnable; if (_stencilTestEnable != stencilTestEnable)
{
_dirty |= DirtyFlags.StencilTestEnable; _stencilTestEnable = stencilTestEnable;
_dirty |= DirtyFlags.StencilTestEnable;
}
} }
public void SetViewport(int index, Viewport viewport) public void SetViewport(int index, Viewport viewport)
{ {
Viewports[index] = viewport; if (!Viewports[index].Equals(viewport))
{
_dirty |= DirtyFlags.Viewport; Viewports[index] = viewport;
_dirty |= DirtyFlags.Viewport;
}
} }
public void SetViewports(ref Array16<Viewport> viewports, uint viewportsCount) public void SetViewports(ref Array16<Viewport> viewports, uint viewportsCount)
{ {
Viewports = viewports; if (!Viewports.Equals(viewports) || ViewportsCount != viewportsCount)
ViewportsCount = viewportsCount;
if (ViewportsCount != 0)
{ {
_dirty |= DirtyFlags.Viewport; Viewports = viewports;
ViewportsCount = viewportsCount;
if (ViewportsCount != 0)
{
_dirty |= DirtyFlags.Viewport;
}
} }
} }
public void SetCullMode(CullModeFlags cullMode) public void SetCullMode(CullModeFlags cullMode)
{ {
CullMode = cullMode; if (CullMode != cullMode)
{
_dirty |= DirtyFlags.CullMode; CullMode = cullMode;
_dirty |= DirtyFlags.CullMode;
}
} }
public void SetFrontFace(FrontFace frontFace) public void SetFrontFace(FrontFace frontFace)
{ {
FrontFace = frontFace; if (FrontFace != frontFace)
{
_dirty |= DirtyFlags.FrontFace; FrontFace = frontFace;
_dirty |= DirtyFlags.FrontFace;
}
} }
public void SetLineWidth(float width) public void SetLineWidth(float width)
{ {
_linewidth = width; if (!FloatCompare(_linewidth, width))
{
_linewidth = width;
_dirty |= DirtyFlags.LineWidth; _dirty |= DirtyFlags.LineWidth;
}
} }
public void SetRasterizerDiscard(bool discard) public void SetRasterizerDiscard(bool discard)
{ {
_discard = discard; if (_discard != discard)
{
_dirty |= DirtyFlags.RasterDiscard; _discard = discard;
_dirty |= DirtyFlags.RasterDiscard;
}
} }
public void SetLogicOp(LogicOp op) public void SetLogicOp(LogicOp op)
{ {
_logicOp = op; if (_logicOp != op)
{
_dirty |= DirtyFlags.LogicOp; _logicOp = op;
_dirty |= DirtyFlags.LogicOp;
}
} }
public void SetPatchControlPoints(uint points) public void SetPatchControlPoints(uint points)
{ {
_patchControlPoints = points; if (_patchControlPoints != points)
{
_dirty |= DirtyFlags.PatchControlPoints; _patchControlPoints = points;
_dirty |= DirtyFlags.PatchControlPoints;
}
} }
public void SetLogicOpEnable(bool logicOpEnable) public void SetLogicOpEnable(bool logicOpEnable)
{ {
_logicOpEnable = logicOpEnable; if (_logicOpEnable != logicOpEnable)
{
_dirty |= DirtyFlags.LogicOpEnalbe; _logicOpEnable = logicOpEnable;
_dirty |= DirtyFlags.LogicOpEnalbe;
}
} }
public void SetDepthClampEnable(bool depthClampEnable) public void SetDepthClampEnable(bool depthClampEnable)
{ {
_depthClampEnable = depthClampEnable; if (_depthClampEnable != depthClampEnable)
{
_dirty |= DirtyFlags.DepthClampEnable; _depthClampEnable = depthClampEnable;
_dirty |= DirtyFlags.DepthClampEnable;
}
} }
public void SetAlphaToCoverEnable(bool alphaToCoverEnable) public void SetAlphaToCoverEnable(bool alphaToCoverEnable)
{ {
_alphaToCoverEnable = alphaToCoverEnable; if (_alphaToCoverEnable != alphaToCoverEnable)
{
_dirty |= DirtyFlags.AlphaToCover; _alphaToCoverEnable = alphaToCoverEnable;
_dirty |= DirtyFlags.AlphaToCover;
}
} }
public void SetAlphaToOneEnable(bool alphaToOneEnable) public void SetAlphaToOneEnable(bool alphaToOneEnable)
{ {
_alphaToOneEnable = alphaToOneEnable; if (_alphaToOneEnable != alphaToOneEnable)
{
_dirty |= DirtyFlags.AlphaToOne; _alphaToOneEnable = alphaToOneEnable;
_dirty |= DirtyFlags.AlphaToOne;
}
} }
public void ForceAllDirty(VulkanRenderer gd) public void ForceAllDirty(VulkanRenderer gd)
@ -418,7 +465,7 @@ namespace Ryujinx.Graphics.Vulkan
api.CmdSetBlendConstants(commandBuffer, _blendConstants.AsSpan()); api.CmdSetBlendConstants(commandBuffer, _blendConstants.AsSpan());
} }
private readonly void RecordDepthBias(VulkanRenderer gd, CommandBuffer commandBuffer) private void RecordDepthBias(VulkanRenderer gd, CommandBuffer commandBuffer)
{ {
gd.Api.CmdSetDepthBias(commandBuffer, _depthBiasConstantFactor, _depthBiasClamp, _depthBiasSlopeFactor); gd.Api.CmdSetDepthBias(commandBuffer, _depthBiasConstantFactor, _depthBiasClamp, _depthBiasSlopeFactor);
@ -517,6 +564,11 @@ namespace Ryujinx.Graphics.Vulkan
private readonly void RecordLogicOp(VulkanRenderer gd, CommandBuffer commandBuffer) private readonly void RecordLogicOp(VulkanRenderer gd, CommandBuffer commandBuffer)
{ {
if (!_logicOpEnable)
{
return;
}
gd.ExtendedDynamicState2Api.CmdSetLogicOp(commandBuffer, _logicOp); gd.ExtendedDynamicState2Api.CmdSetLogicOp(commandBuffer, _logicOp);
} }