Skip to content

Commit ccd7a98

Browse files
[Rendering] Optimize frame allocator native address handling;
1 parent ef6dcad commit ccd7a98

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/Commands/SDLGPURenderCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pair.Value is not SDLGPUVertexBuffer v ||
142142
{
143143
var uniform = vertexUniformData[i];
144144

145-
var target = backend.frameAllocator.Get(uniform.position);
145+
var target = backend.frameAllocator.pinAddress + uniform.position;
146146

147147
SDL.PushGPUVertexUniformData(backend.commandBuffer, uniform.binding, target, (uint)uniform.size);
148148
}
@@ -151,7 +151,7 @@ pair.Value is not SDLGPUVertexBuffer v ||
151151
{
152152
var uniform = fragmentUniformData[i];
153153

154-
var target = backend.frameAllocator.Get(uniform.position);
154+
var target = backend.frameAllocator.pinAddress + uniform.position;
155155

156156
SDL.PushGPUFragmentUniformData(backend.commandBuffer, uniform.binding, target, (uint)uniform.size);
157157
}

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/Commands/SDLGPURenderTransientCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pair.Value is not SDLGPUVertexBuffer v ||
155155
{
156156
var uniform = vertexUniformData[i];
157157

158-
var target = backend.frameAllocator.Get(uniform.position);
158+
var target = backend.frameAllocator.pinAddress + uniform.position;
159159

160160
SDL.PushGPUVertexUniformData(backend.commandBuffer, uniform.binding, target, (uint)uniform.size);
161161
}
@@ -164,7 +164,7 @@ pair.Value is not SDLGPUVertexBuffer v ||
164164
{
165165
var uniform = fragmentUniformData[i];
166166

167-
var target = backend.frameAllocator.Get(uniform.position);
167+
var target = backend.frameAllocator.pinAddress + uniform.position;
168168

169169
SDL.PushGPUFragmentUniformData(backend.commandBuffer, uniform.binding, target, (uint)uniform.size);
170170
}

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/Commands/SDLGPURenderTransientUIntCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pair.Value is not SDLGPUVertexBuffer v ||
154154
{
155155
var uniform = vertexUniformData[i];
156156

157-
var target = backend.frameAllocator.Get(uniform.position);
157+
var target = backend.frameAllocator.pinAddress + uniform.position;
158158

159159
SDL.PushGPUVertexUniformData(backend.commandBuffer, uniform.binding, target, (uint)uniform.size);
160160
}
@@ -163,7 +163,7 @@ pair.Value is not SDLGPUVertexBuffer v ||
163163
{
164164
var uniform = fragmentUniformData[i];
165165

166-
var target = backend.frameAllocator.Get(uniform.position);
166+
var target = backend.frameAllocator.pinAddress + uniform.position;
167167

168168
SDL.PushGPUFragmentUniformData(backend.commandBuffer, uniform.binding, target, (uint)uniform.size);
169169
}

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/SDLGPURendererBackend.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ internal class MemoryAllocator
401401
{
402402
public byte[] buffer = new byte[1024];
403403

404+
public GCHandle pinHandle;
405+
406+
public nint pinAddress;
407+
404408
internal int position;
405409

406410
public void Allocate(int size)
@@ -419,23 +423,30 @@ public void Allocate(int size)
419423
newSize *= 2;
420424

421425
Array.Resize(ref buffer, newSize);
426+
427+
Repin();
422428
}
423429

424430
position += size;
425431
}
426432

427-
public nint Get(int position)
433+
private void Repin()
428434
{
429-
unsafe
435+
if (pinHandle.IsAllocated)
430436
{
431-
fixed (void* target = buffer)
432-
{
433-
byte* p = (byte*)target;
437+
pinHandle.Free();
438+
}
434439

435-
p += position;
440+
pinHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
436441

437-
return (nint)p;
438-
}
442+
pinAddress = pinHandle.AddrOfPinnedObject();
443+
}
444+
445+
public void EnsurePin()
446+
{
447+
if(pinHandle.IsAllocated == false)
448+
{
449+
pinHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
439450
}
440451
}
441452

@@ -810,6 +821,8 @@ public void EndFrame()
810821

811822
UpdateDepthTextureIfNeeded(false);
812823

824+
frameAllocator.EnsurePin();
825+
813826
foreach (var pair in transientBuffers)
814827
{
815828
pair.Value.CreateBuffers();

0 commit comments

Comments
 (0)