44namespace Staple . Internal ;
55
66internal class SDLGPURenderCommand ( SDLGPURendererBackend backend , RenderState state , nint pipeline , Texture [ ] vertexTextures ,
7- Texture [ ] fragmentTextures , StapleShaderUniform [ ] vertexUniformData , StapleShaderUniform [ ] fragmentUniformData ,
7+ Texture [ ] fragmentTextures , ( int , int ) vertexUniformData , ( int , int ) fragmentUniformData ,
88 VertexAttribute [ ] vertexAttributes ) : IRenderCommand
99{
1010 private readonly RenderState state = state . Clone ( ) ;
1111 private readonly Texture [ ] vertexTextures = ( Texture [ ] ) vertexTextures ? . Clone ( ) ;
1212 private readonly Texture [ ] fragmentTextures = ( Texture [ ] ) fragmentTextures ? . Clone ( ) ;
13+ private readonly SDLGPUVertexBuffer vertex = ( SDLGPUVertexBuffer ) state . vertexBuffer ;
14+ private readonly SDLGPUIndexBuffer index = ( SDLGPUIndexBuffer ) state . indexBuffer ;
15+ private readonly BufferAttributeContainer . Entries staticMeshEntries = state . staticMeshEntries ;
1316
1417 internal static readonly SDL . GPUBufferBinding [ ] vertexBinding = new SDL . GPUBufferBinding [ 1 ] ;
1518
@@ -20,28 +23,27 @@ internal class SDLGPURenderCommand(SDLGPURendererBackend backend, RenderState st
2023
2124 public void Update ( )
2225 {
23- var vertex = ( SDLGPUVertexBuffer ) state . vertexBuffer ;
24- var index = ( SDLGPUIndexBuffer ) state . indexBuffer ;
25- var staticMeshEntries = state . staticMeshEntries ;
26-
2726 SDLGPURendererBackend . BufferResource vertexBuffer = null ;
2827 SDLGPURendererBackend . BufferResource indexBuffer = null ;
2928
30- using var vertexUniformHandle = new GlobalAllocator < StapleShaderUniform > . GlobalAllocatorHandle ( vertexUniformData ) ;
31- using var fragmentUniformHandle = new GlobalAllocator < StapleShaderUniform > . GlobalAllocatorHandle ( fragmentUniformData ) ;
32-
33- if ( ( staticMeshEntries == null &&
34- ( ! backend . TryGetVertexBuffer ( vertex . handle , out vertexBuffer ) ||
35- ! backend . TryGetIndexBuffer ( index . handle , out indexBuffer ) ) ) ||
36- ( staticMeshEntries != null && SDLGPURendererBackend . staticMeshVertexBuffers [ 0 ] == nint . Zero ) ||
37- ! backend . TryGetTextureSamplers ( vertexTextures , fragmentTextures , state . shaderInstance ,
38- out var vertexSamplers , out var fragmentSamplers ) )
29+ if ( staticMeshEntries == null )
30+ {
31+ if ( ! backend . TryGetVertexBuffer ( vertex . handle , out vertexBuffer ) ||
32+ ! backend . TryGetIndexBuffer ( index . handle , out indexBuffer ) )
33+ {
34+ return ;
35+ }
36+ }
37+ else if ( SDLGPURendererBackend . staticMeshVertexBuffers [ 0 ] == nint . Zero )
3938 {
4039 return ;
4140 }
4241
43- using var vertexSamplerHandle = new GlobalAllocator < SDL . GPUTextureSamplerBinding > . GlobalAllocatorHandle ( vertexSamplers ) ;
44- using var fragmentSamplerHandle = new GlobalAllocator < SDL . GPUTextureSamplerBinding > . GlobalAllocatorHandle ( fragmentSamplers ) ;
42+ if ( ! backend . TryGetTextureSamplers ( vertexTextures , fragmentTextures , state . shaderInstance , out var vertexSamplers ,
43+ out var fragmentSamplers ) )
44+ {
45+ return ;
46+ }
4547
4648 if ( staticMeshEntries != null )
4749 {
@@ -122,22 +124,40 @@ public void Update()
122124 }
123125 }
124126
125- if ( vertexSamplers != null )
127+ if ( vertexSamplers . IsEmpty == false )
126128 {
127- SDL . BindGPUVertexSamplers ( renderPass , 0 , vertexSamplers , ( uint ) vertexSamplers . Length ) ;
129+ unsafe
130+ {
131+ fixed( void * ptr = vertexSamplers )
132+ {
133+ SDL . BindGPUVertexSamplers ( renderPass , 0 , ( nint ) ptr , ( uint ) vertexSamplers . Length ) ;
134+ }
135+ }
136+
128137 }
129138
130- if ( fragmentSamplers != null )
139+ if ( fragmentSamplers . IsEmpty == false )
131140 {
132- SDL . BindGPUFragmentSamplers ( renderPass , 0 , fragmentSamplers , ( uint ) fragmentSamplers . Length ) ;
141+ unsafe
142+ {
143+ fixed ( void * ptr = fragmentSamplers )
144+ {
145+ SDL . BindGPUFragmentSamplers ( renderPass , 0 , ( nint ) ptr , ( uint ) fragmentSamplers . Length ) ;
146+ }
147+ }
133148 }
134149
135150 if ( hasVertexStorageBuffers )
136151 {
137- var buffers = GlobalAllocator < nint > . Instance . Rent ( state . vertexStorageBuffers . Count ) ;
152+ var buffers = backend . nintBufferStaging ;
138153 var counter = 0 ;
139154 var firstBinding = - 1 ;
140155
156+ if ( state . vertexStorageBuffers . Count > buffers . Length )
157+ {
158+ return ;
159+ }
160+
141161 foreach ( var ( binding , buffer ) in state . vertexStorageBuffers )
142162 {
143163 if ( buffer . Disposed ||
@@ -157,17 +177,20 @@ buffer is not SDLGPUVertexBuffer v ||
157177 }
158178 }
159179
160- SDL . BindGPUVertexStorageBuffers ( renderPass , ( uint ) firstBinding , buffers , ( uint ) buffers . Length ) ;
161-
162- GlobalAllocator < nint > . Instance . Return ( buffers ) ;
180+ SDL . BindGPUVertexStorageBuffers ( renderPass , ( uint ) firstBinding , buffers , ( uint ) counter ) ;
163181 }
164182
165183 if ( hasFragmentStorageBuffers )
166184 {
167- var buffers = GlobalAllocator < nint > . Instance . Rent ( state . fragmentStorageBuffers . Count ) ;
185+ var buffers = backend . nintBufferStaging ;
168186 var counter = 0 ;
169187 var firstBinding = - 1 ;
170188
189+ if ( state . fragmentStorageBuffers . Count > buffers . Length )
190+ {
191+ return ;
192+ }
193+
171194 foreach ( var ( binding , buffer ) in state . fragmentStorageBuffers )
172195 {
173196 if ( buffer . Disposed ||
@@ -187,14 +210,14 @@ buffer is not SDLGPUVertexBuffer v ||
187210 }
188211 }
189212
190- SDL . BindGPUFragmentStorageBuffers ( renderPass , ( uint ) firstBinding , buffers , ( uint ) buffers . Length ) ;
191-
192- GlobalAllocator < nint > . Instance . Return ( buffers ) ;
213+ SDL . BindGPUFragmentStorageBuffers ( renderPass , ( uint ) firstBinding , buffers , ( uint ) counter ) ;
193214 }
194215
195- for ( var i = 0 ; i < vertexUniformData . Length ; i ++ )
216+ var vertexSpan = backend . shaderUniformFrameAllocator . GetSpan ( vertexUniformData . Item1 , vertexUniformData . Item2 ) ;
217+
218+ for ( var i = 0 ; i < vertexSpan . Length ; i ++ )
196219 {
197- var uniform = vertexUniformData [ i ] ;
220+ var uniform = vertexSpan [ i ] ;
198221
199222 if ( uniform . used == false )
200223 {
@@ -206,9 +229,11 @@ buffer is not SDLGPUVertexBuffer v ||
206229 SDL . PushGPUVertexUniformData ( backend . commandBuffer , uniform . binding , target , ( uint ) uniform . size ) ;
207230 }
208231
209- for ( var i = 0 ; i < fragmentUniformData . Length ; i ++ )
232+ var fragmentSpan = backend . shaderUniformFrameAllocator . GetSpan ( fragmentUniformData . Item1 , fragmentUniformData . Item2 ) ;
233+
234+ for ( var i = 0 ; i < fragmentSpan . Length ; i ++ )
210235 {
211- var uniform = fragmentUniformData [ i ] ;
236+ var uniform = fragmentSpan [ i ] ;
212237
213238 if ( uniform . used == false )
214239 {
0 commit comments