change command buffers to be pooled

main
cosmonaut 2021-01-15 18:25:25 -08:00
parent de32fe435c
commit 799c4a0627
2 changed files with 30 additions and 9 deletions

View File

@ -4,16 +4,16 @@ using RefreshCS;
namespace Campari namespace Campari
{ {
public struct CommandBuffer public class CommandBuffer
{ {
public GraphicsDevice Device { get; } public GraphicsDevice Device { get; }
public IntPtr Handle { get; } public IntPtr Handle { get; internal set; }
// called from RefreshDevice // called from RefreshDevice
internal CommandBuffer(GraphicsDevice device, IntPtr handle) internal CommandBuffer(GraphicsDevice device)
{ {
Device = device; Device = device;
Handle = handle; Handle = IntPtr.Zero;
} }
public void BeginRenderPass( public void BeginRenderPass(

View File

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using RefreshCS; using RefreshCS;
namespace Campari namespace Campari
@ -12,10 +10,13 @@ namespace Campari
public bool IsDisposed { get; private set; } public bool IsDisposed { get; private set; }
private Queue<CommandBuffer> commandBufferPool;
public GraphicsDevice( public GraphicsDevice(
IntPtr deviceWindowHandle, IntPtr deviceWindowHandle,
Refresh.PresentMode presentMode, Refresh.PresentMode presentMode,
bool debugMode bool debugMode,
int initialCommandBufferPoolSize = 4
) { ) {
var presentationParameters = new Refresh.PresentationParameters var presentationParameters = new Refresh.PresentationParameters
{ {
@ -27,13 +28,26 @@ namespace Campari
ref presentationParameters, ref presentationParameters,
Conversions.BoolToByte(debugMode) Conversions.BoolToByte(debugMode)
); );
commandBufferPool = new Queue<CommandBuffer>(initialCommandBufferPoolSize);
for (var i = 0; i < initialCommandBufferPoolSize; i += 1)
{
commandBufferPool.Enqueue(new CommandBuffer(this));
}
} }
/* FIXME: pool this */
public CommandBuffer AcquireCommandBuffer() public CommandBuffer AcquireCommandBuffer()
{ {
var commandBufferHandle = Refresh.Refresh_AcquireCommandBuffer(Handle, 0); var commandBufferHandle = Refresh.Refresh_AcquireCommandBuffer(Handle, 0);
return new CommandBuffer(this, commandBufferHandle); if (commandBufferPool.Count == 0)
{
commandBufferPool.Enqueue(new CommandBuffer(this));
}
var commandBuffer = commandBufferPool.Dequeue();
commandBuffer.Handle = commandBufferHandle;
return commandBuffer;
} }
public unsafe void Submit(params CommandBuffer[] commandBuffers) public unsafe void Submit(params CommandBuffer[] commandBuffers)
@ -50,6 +64,13 @@ namespace Campari
(uint) commandBuffers.Length, (uint) commandBuffers.Length,
(IntPtr) commandBufferPtrs (IntPtr) commandBufferPtrs
); );
// return to pool
for (var i = 0; i < commandBuffers.Length; i += 1)
{
commandBuffers[i].Handle = IntPtr.Zero;
commandBufferPool.Enqueue(commandBuffers[i]);
}
} }
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)