Skip to content

Commit 2a88bd4

Browse files
danieljesusclaude
andcommitted
[local] Record only the dynamic draw state that changed
Every draw set 9-15 dynamic states. They persist across draws, guest pipeline binds (all declare the same dynamic states) and rendering scopes of one command buffer, so each group is now recorded only when it differs from what the command buffer last recorded. A new command buffer or tick, or the color-to-depth blit (its pipeline has static state), records everything again. SHARPEMU_DYNAMIC_STATE_FILTER=0 restores it. GTA V Fidelity RT, 3 runs on and 2 valid off (one off run took another game path, 155 draws per frame): DrawDynamicStateRecording 1.6-2.1 -> 0.3 ms per frame, back work 70.6 vs 72.1 ms, FPS 12.98 vs 12.88 (noise), same screenshots. Libs tests: the 27 known failures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JKcqXLB989bbHkxLTmvnCv
1 parent da4e32c commit 2a88bd4

2 files changed

Lines changed: 102 additions & 16 deletions

File tree

‎src/SharpEmu.Libs/Gpu/Images/ColorToMultisampleDepthBlit.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace SharpEmu.Libs.Gpu.Images;
1212
// Writes each channel of a single-sample color image into one sample of a multisample depth image.
1313
public sealed unsafe class ColorToMultisampleDepthBlit : IDisposable
1414
{
15+
// [local] Bumped whenever this blit binds its pipeline, which overrides the dynamic draw state.
16+
internal static int DynamicStateEpoch;
17+
1518
public const ImageLayout DestinationLayout = ImageLayout.DepthStencilAttachmentOptimal;
1619
private const uint SetsPerPool = 64;
1720

@@ -303,6 +306,8 @@ public void Reinterpret(CachedImage source, CachedImage destination)
303306
vk.CmdBeginRenderPass(command, &begin, SubpassContents.Inline);
304307
vk.CmdBindDescriptorSets(command, PipelineBindPoint.Graphics, _pipelineLayout, 0, 1, &set, 0, null);
305308
vk.CmdBindPipeline(command, PipelineBindPoint.Graphics, cached.Pipeline);
309+
// [local] This pipeline's static states replace the guest draws' dynamic ones.
310+
Interlocked.Increment(ref DynamicStateEpoch);
306311
var viewport = new Viewport(0.0f, 0.0f, extent.Width, extent.Height, 0.0f, 1.0f);
307312
var scissor = new Rect2D(new Offset2D(0, 0), extent);
308313
vk.CmdSetViewport(command, 0, 1, &viewport);

‎src/SharpEmu.Libs/VideoOut/VulkanVideoPresenter.Rendering.cs‎

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -690,29 +690,77 @@ private void RecordSampledDepthClear(CachedImage image, in ImageViewDescription
690690
_vk.CmdClearDepthStencilImage(command, image.Backing.Handle, ImageLayout.TransferDstOptimal, &value, 1, &vkRange);
691691
}
692692

693+
// [local] SHARPEMU_DYNAMIC_STATE_FILTER: dynamic state lasts across draws, pipeline binds (every guest
694+
// pipeline declares the same dynamic states) and rendering scopes of one command buffer, so each group
695+
// is recorded only when it differs from what this command buffer last recorded.
696+
private static readonly bool DynamicStateFilter = Environment.GetEnvironmentVariable("SHARPEMU_DYNAMIC_STATE_FILTER") != "0";
697+
private nint _dynamicStateCommand;
698+
private ulong _dynamicStateTick;
699+
private int _dynamicStateEpoch;
700+
private DynamicDrawState _dynamicState;
701+
693702
public void SetDynamicState(in DynamicDrawState state)
694703
{
695704
using var profileScope = RenderPhaseProfile.MeasureDetail(RenderPhaseProfile.Phase.DrawDynamicStateRecording);
696705
var command = BeginBatchedGuestCommands();
697-
var viewport = new Viewport(state.ViewportX, state.ViewportY, state.ViewportWidth, state.ViewportHeight, state.ViewportMinDepth, state.ViewportMaxDepth);
698-
_vk.CmdSetViewport(command, 0, 1, &viewport);
699-
var scissor = new Rect2D(
700-
new Offset2D(state.Scissor.Left, state.Scissor.Top),
701-
new Extent2D((uint)(state.Scissor.Right - state.Scissor.Left), (uint)(state.Scissor.Bottom - state.Scissor.Top)));
702-
_vk.CmdSetScissor(command, 0, 1, &scissor);
703-
_vk.CmdSetLineWidth(command, state.LineWidth);
704-
var blendConstants = stackalloc float[4] { state.BlendRed, state.BlendGreen, state.BlendBlue, state.BlendAlpha };
705-
_vk.CmdSetBlendConstants(command, blendConstants);
706-
_vk.CmdSetDepthTestEnable(command, state.DepthTestEnabled);
707-
_vk.CmdSetDepthWriteEnable(command, state.DepthWriteEnabled);
708-
_vk.CmdSetDepthCompareOp(command, state.DepthCompare);
709-
_vk.CmdSetDepthBiasEnable(command, state.DepthBiasEnabled);
710-
if (state.DepthBiasEnabled)
706+
var known = DynamicStateFilter && _dynamicStateCommand == command.Handle && _dynamicStateTick == _scheduler.CurrentTick &&
707+
_dynamicStateEpoch == Volatile.Read(ref Gpu.Images.ColorToMultisampleDepthBlit.DynamicStateEpoch);
708+
var last = _dynamicState;
709+
if (!known || state.ViewportX != last.ViewportX || state.ViewportY != last.ViewportY || state.ViewportWidth != last.ViewportWidth ||
710+
state.ViewportHeight != last.ViewportHeight || state.ViewportMinDepth != last.ViewportMinDepth || state.ViewportMaxDepth != last.ViewportMaxDepth)
711+
{
712+
var viewport = new Viewport(state.ViewportX, state.ViewportY, state.ViewportWidth, state.ViewportHeight, state.ViewportMinDepth, state.ViewportMaxDepth);
713+
_vk.CmdSetViewport(command, 0, 1, &viewport);
714+
}
715+
716+
if (!known || state.Scissor != last.Scissor)
717+
{
718+
var scissor = new Rect2D(
719+
new Offset2D(state.Scissor.Left, state.Scissor.Top),
720+
new Extent2D((uint)(state.Scissor.Right - state.Scissor.Left), (uint)(state.Scissor.Bottom - state.Scissor.Top)));
721+
_vk.CmdSetScissor(command, 0, 1, &scissor);
722+
}
723+
724+
if (!known || state.LineWidth != last.LineWidth)
725+
{
726+
_vk.CmdSetLineWidth(command, state.LineWidth);
727+
}
728+
729+
if (!known || state.BlendRed != last.BlendRed || state.BlendGreen != last.BlendGreen || state.BlendBlue != last.BlendBlue || state.BlendAlpha != last.BlendAlpha)
730+
{
731+
var blendConstants = stackalloc float[4] { state.BlendRed, state.BlendGreen, state.BlendBlue, state.BlendAlpha };
732+
_vk.CmdSetBlendConstants(command, blendConstants);
733+
}
734+
735+
if (!known || state.DepthTestEnabled != last.DepthTestEnabled)
736+
{
737+
_vk.CmdSetDepthTestEnable(command, state.DepthTestEnabled);
738+
}
739+
740+
if (!known || state.DepthWriteEnabled != last.DepthWriteEnabled)
741+
{
742+
_vk.CmdSetDepthWriteEnable(command, state.DepthWriteEnabled);
743+
}
744+
745+
if (!known || state.DepthCompare != last.DepthCompare)
746+
{
747+
_vk.CmdSetDepthCompareOp(command, state.DepthCompare);
748+
}
749+
750+
if (!known || state.DepthBiasEnabled != last.DepthBiasEnabled)
751+
{
752+
_vk.CmdSetDepthBiasEnable(command, state.DepthBiasEnabled);
753+
}
754+
755+
// Bias and stencil values are recorded only while enabled; the cache keeps the last recorded ones.
756+
if (state.DepthBiasEnabled && (!known || !last.DepthBiasEnabled ||
757+
state.DepthBiasConstantFactor != last.DepthBiasConstantFactor || state.DepthBiasClamp != last.DepthBiasClamp ||
758+
state.DepthBiasSlopeFactor != last.DepthBiasSlopeFactor))
711759
{
712760
_vk.CmdSetDepthBias(command, state.DepthBiasConstantFactor, _supportsDepthBiasClamp ? state.DepthBiasClamp : 0f, state.DepthBiasSlopeFactor);
713761
}
714762

715-
if (state.StencilTestEnabled)
763+
if (state.StencilTestEnabled && (!known || !last.StencilTestEnabled || state.FrontStencil != last.FrontStencil || state.BackStencil != last.BackStencil))
716764
{
717765
_vk.CmdSetStencilCompareMask(command, StencilFaceFlags.FaceFrontBit, state.FrontStencil.CompareMask);
718766
_vk.CmdSetStencilCompareMask(command, StencilFaceFlags.FaceBackBit, state.BackStencil.CompareMask);
@@ -722,7 +770,8 @@ public void SetDynamicState(in DynamicDrawState state)
722770
_vk.CmdSetStencilReference(command, StencilFaceFlags.FaceBackBit, state.BackStencil.Reference);
723771
}
724772

725-
if (_colorWriteEnableApi is { } colorWriteEnable && state.ColorWriteCount != 0)
773+
if (_colorWriteEnableApi is { } colorWriteEnable && state.ColorWriteCount != 0 &&
774+
(!known || state.ColorWriteCount != last.ColorWriteCount || state.ColorWriteEnableMask != last.ColorWriteEnableMask))
726775
{
727776
var enables = stackalloc Bool32[RenderingState.ColorAttachmentCapacity];
728777
for (var index = 0; index < state.ColorWriteCount; index++)
@@ -732,6 +781,38 @@ public void SetDynamicState(in DynamicDrawState state)
732781

733782
colorWriteEnable.CmdSetColorWriteEnable(command, state.ColorWriteCount, enables);
734783
}
784+
785+
if (!DynamicStateFilter)
786+
{
787+
return;
788+
}
789+
790+
// A group that was not recorded keeps the values recorded before it, not this draw's.
791+
var recorded = state;
792+
if (known && !state.DepthBiasEnabled)
793+
{
794+
recorded = recorded with
795+
{
796+
DepthBiasConstantFactor = last.DepthBiasConstantFactor,
797+
DepthBiasClamp = last.DepthBiasClamp,
798+
DepthBiasSlopeFactor = last.DepthBiasSlopeFactor,
799+
};
800+
}
801+
802+
if (known && !state.StencilTestEnabled)
803+
{
804+
recorded = recorded with { FrontStencil = last.FrontStencil, BackStencil = last.BackStencil };
805+
}
806+
807+
if (known && (state.ColorWriteCount == 0 || _colorWriteEnableApi is null))
808+
{
809+
recorded = recorded with { ColorWriteCount = last.ColorWriteCount, ColorWriteEnableMask = last.ColorWriteEnableMask };
810+
}
811+
812+
_dynamicState = recorded;
813+
_dynamicStateCommand = command.Handle;
814+
_dynamicStateTick = _scheduler.CurrentTick;
815+
_dynamicStateEpoch = Volatile.Read(ref Gpu.Images.ColorToMultisampleDepthBlit.DynamicStateEpoch);
735816
}
736817

737818

0 commit comments

Comments
 (0)