@@ -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