@@ -35,7 +35,8 @@ public void ReqState(
3535 FResAccess Access ,
3636 FShaderStageFlags Stages ,
3737 FLegacyState Legacy ,
38- bool ? CrossQueue = null
38+ bool ? CrossQueue = null ,
39+ FImageBarrierFlags ImageFlags = FImageBarrierFlags . None
3940 ) => ReqState (
4041 self , new FResState
4142 {
@@ -44,9 +45,12 @@ public void ReqState(
4445 Stages = Stages ,
4546 Legacy = Legacy ,
4647 CrossQueue = CrossQueue ?? Resource . NativeState . CrossQueue ,
47- }
48+ } , ImageFlags
4849 ) ;
49- private void ReqState ( CommandList self , FResState NewState )
50+ private void ReqState (
51+ CommandList self , FResState NewState ,
52+ FImageBarrierFlags ImageFlags = FImageBarrierFlags . None
53+ )
5054 {
5155 ref var OldState = ref Resource . NativeState ;
5256 if ( First )
@@ -99,7 +103,10 @@ private void ReqState(CommandList self, FResState NewState)
99103 return ;
100104 }
101105
102- private FBarrier BuildBarrier ( in FResState OldState , in FResState NewState )
106+ private FBarrier BuildBarrier (
107+ in FResState OldState , in FResState NewState ,
108+ FImageBarrierFlags ImageFlags = FImageBarrierFlags . None
109+ )
103110 {
104111 var barrier = new FBarrier ( ) ;
105112 switch ( Resource . Type )
@@ -151,7 +158,7 @@ private FBarrier BuildBarrier(in FResState OldState, in FResState NewState)
151158 FirstPlane = 0 ,
152159 NumPlanes = Resource . Planes ,
153160 } ,
154- Flags = FImageBarrierFlags . None ,
161+ Flags = ImageFlags ,
155162 } ;
156163 if ( OldState . CrossQueue || NewState . CrossQueue )
157164 {
@@ -377,18 +384,20 @@ internal FResourceRef AddResource(IGpuResource resource)
377384 internal uint AddString ( string str )
378385 {
379386 var index = m_string16 . Count ;
380- CollectionsMarshal . SetCount ( m_string16 , index + str . Length ) ;
381- var dst = CollectionsMarshal . AsSpan ( m_string16 ) . Slice ( index , str . Length ) ;
387+ CollectionsMarshal . SetCount ( m_string16 , index + str . Length + 1 ) ;
388+ var dst = CollectionsMarshal . AsSpan ( m_string16 ) . Slice ( index , str . Length + 1 ) ;
382389 str . CopyTo ( dst ) ;
390+ dst [ str . Length ] = '\0 ' ;
383391 return ( uint ) index ;
384392 }
385393
386394 internal uint AddString ( ReadOnlySpan < byte > str )
387395 {
388396 var index = m_string8 . Count ;
389- CollectionsMarshal . SetCount ( m_string8 , index + str . Length ) ;
390- var dst = CollectionsMarshal . AsSpan ( m_string8 ) . Slice ( index , str . Length ) ;
397+ CollectionsMarshal . SetCount ( m_string8 , index + str . Length + 1 ) ;
398+ var dst = CollectionsMarshal . AsSpan ( m_string8 ) . Slice ( index , str . Length + 1 ) ;
391399 str . CopyTo ( dst ) ;
400+ dst [ str . Length ] = 0 ;
392401 return ( uint ) index ;
393402 }
394403
@@ -1048,15 +1057,9 @@ public RenderScope Render(
10481057
10491058 #endregion
10501059
1051- #region Add
1052-
1053- m_commands . Add ( new ( ) { Render = cmd } ) ;
1054-
1055- #endregion
1056-
10571060 #region Scope
10581061
1059- RenderScope scope = new ( this , info_index , m_render_commands . Count , m_render_commands , debug_scope ) ;
1062+ RenderScope scope = new ( this , info_index , cmd , m_render_commands , debug_scope ) ;
10601063
10611064 #endregion
10621065
@@ -1290,7 +1293,7 @@ public RtvInfo(IRtv View, LoadOp<float4> Load) : this(View, Load, StoreOp.Store)
12901293public unsafe struct RenderScope (
12911294 CommandList self ,
12921295 int info_index ,
1293- int cmd_index ,
1296+ FCommandRender render_cmd ,
12941297 List < FRenderCommandItem > m_commands ,
12951298 bool debug_scope
12961299) : IDisposable
@@ -1300,6 +1303,10 @@ bool debug_scope
13001303 private ShaderPipeline ? m_current_pipeline ;
13011304 private ShaderBinding ? m_current_binding ;
13021305 private bool m_disposed ;
1306+ private bool m_has_pixel_shader ;
1307+ private bool m_has_vertex_shader ;
1308+ private bool m_has_mesh_shader ;
1309+ private bool m_has_task_shader ;
13031310
13041311 #endregion
13051312
@@ -1319,7 +1326,35 @@ public void Dispose()
13191326 throw new InvalidOperationException (
13201327 "There is still Debug scope not ended, please check whether Dispose is missed."
13211328 ) ;
1322- Info . CommandCount = ( uint ) m_commands . Count - ( uint ) cmd_index ;
1329+ ref var info = ref Info ;
1330+ if ( self . AutoBarrierEnabled )
1331+ {
1332+ var stages = m_has_pixel_shader ? FShaderStageFlags . Pixel : FShaderStageFlags . None ;
1333+ if ( info . Dsv . ResourceIndex != 0 )
1334+ {
1335+ ref var state = ref self . StateAt ( info . Dsv ) ;
1336+ state . ReqState (
1337+ self ,
1338+ FResLayout . DepthStencilWrite ,
1339+ FResAccess . DepthStencilWrite ,
1340+ stages ,
1341+ FLegacyState . DepthWrite
1342+ ) ;
1343+ }
1344+ for ( var i = 0 ; i < info . NumRtv ; i ++ )
1345+ {
1346+ ref var state = ref self . StateAt ( info . Rtv [ i ] ) ;
1347+ state . ReqState (
1348+ self ,
1349+ FResLayout . RenderTarget ,
1350+ FResAccess . RenderTarget ,
1351+ stages ,
1352+ FLegacyState . RenderTarget
1353+ ) ;
1354+ }
1355+ }
1356+ self . m_commands . Add ( new ( ) { Render = render_cmd } ) ;
1357+ Info . CommandCount = ( uint ) m_commands . Count - render_cmd . CommandStartIndex ;
13231358 self . m_render_commands . Add ( new ( ) { Type = FCommandType . End } ) ;
13241359 self . m_in_render_or_compute_scope = false ;
13251360 if ( debug_scope ) new DebugScope ( self ) . Dispose ( ) ;
@@ -1598,6 +1633,8 @@ public void Draw(
15981633 Indexed = Indexed ,
15991634 } ;
16001635 m_commands . Add ( new ( ) { Draw = cmd } ) ;
1636+ m_has_pixel_shader = true ;
1637+ m_has_vertex_shader = true ;
16011638 }
16021639
16031640 #endregion
@@ -1634,6 +1671,9 @@ public void DispatchMesh(
16341671 Type = FDispatchType . Mesh ,
16351672 } ;
16361673 m_commands . Add ( new ( ) { Dispatch = cmd } ) ;
1674+ m_has_pixel_shader = true ;
1675+ m_has_mesh_shader = true ;
1676+ m_has_task_shader = m_current_pipeline ! . Shader . Stages . HasFlags ( ShaderStageFlags . Task ) ;
16371677 }
16381678
16391679 #endregion
0 commit comments