Campari/src/GraphicsDevice.cs

110 lines
3.2 KiB
C#
Raw Normal View History

2021-01-14 08:31:03 +00:00
using System;
using System.Collections.Generic;
using RefreshCS;
namespace Campari
{
2021-01-16 02:13:53 +00:00
public class GraphicsDevice : IDisposable
2021-01-14 08:31:03 +00:00
{
public IntPtr Handle { get; }
public bool IsDisposed { get; private set; }
2021-01-16 03:08:16 +00:00
private readonly Queue<CommandBuffer> commandBufferPool;
2021-01-16 02:25:25 +00:00
2021-01-16 02:13:53 +00:00
public GraphicsDevice(
IntPtr deviceWindowHandle,
Refresh.PresentMode presentMode,
2021-01-16 02:25:25 +00:00
bool debugMode,
int initialCommandBufferPoolSize = 4
2021-01-14 08:31:03 +00:00
) {
2021-01-16 02:13:53 +00:00
var presentationParameters = new Refresh.PresentationParameters
{
deviceWindowHandle = deviceWindowHandle,
presentMode = presentMode
};
2021-01-14 08:31:03 +00:00
Handle = Refresh.Refresh_CreateDevice(
2021-01-15 01:25:15 +00:00
ref presentationParameters,
2021-01-15 03:14:56 +00:00
Conversions.BoolToByte(debugMode)
2021-01-14 08:31:03 +00:00
);
2021-01-16 02:25:25 +00:00
commandBufferPool = new Queue<CommandBuffer>(initialCommandBufferPoolSize);
for (var i = 0; i < initialCommandBufferPoolSize; i += 1)
{
commandBufferPool.Enqueue(new CommandBuffer(this));
}
2021-01-14 08:31:03 +00:00
}
2021-01-15 01:25:15 +00:00
public CommandBuffer AcquireCommandBuffer()
{
var commandBufferHandle = Refresh.Refresh_AcquireCommandBuffer(Handle, 0);
2021-01-16 02:25:25 +00:00
if (commandBufferPool.Count == 0)
{
commandBufferPool.Enqueue(new CommandBuffer(this));
}
var commandBuffer = commandBufferPool.Dequeue();
commandBuffer.Handle = commandBufferHandle;
return commandBuffer;
2021-01-15 01:25:15 +00:00
}
2021-01-15 03:14:56 +00:00
public unsafe void Submit(params CommandBuffer[] commandBuffers)
2021-01-15 01:25:15 +00:00
{
2021-01-15 03:14:56 +00:00
var commandBufferPtrs = stackalloc IntPtr[commandBuffers.Length];
for (var i = 0; i < commandBuffers.Length; i += 1)
{
commandBufferPtrs[i] = commandBuffers[i].Handle;
}
2021-01-15 01:25:15 +00:00
Refresh.Refresh_Submit(
Handle,
(uint) commandBuffers.Length,
2021-01-15 03:14:56 +00:00
(IntPtr) commandBufferPtrs
2021-01-15 01:25:15 +00:00
);
2021-01-16 02:25:25 +00:00
// return to pool
for (var i = 0; i < commandBuffers.Length; i += 1)
{
commandBuffers[i].Handle = IntPtr.Zero;
commandBufferPool.Enqueue(commandBuffers[i]);
}
2021-01-15 01:25:15 +00:00
}
2021-01-16 03:08:16 +00:00
public void Wait()
{
Refresh.Refresh_Wait(Handle);
}
2021-01-14 08:31:03 +00:00
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
}
Refresh.Refresh_DestroyDevice(Handle);
IsDisposed = true;
}
}
// TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
2021-01-16 02:13:53 +00:00
~GraphicsDevice()
2021-01-14 08:31:03 +00:00
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}