resource destroy now requires a CB

pull/14/head
cosmonaut 2022-02-18 21:02:16 -08:00
parent 4acc2588e1
commit cb25e6feff
13 changed files with 35 additions and 12 deletions

@ -1 +1 @@
Subproject commit 95cd22c48a68f93204b44bc508dec86b6be74e56
Subproject commit 1e8abe379a0ccc891ce0f4d90793feac59d30948

View File

@ -133,6 +133,8 @@ namespace MoonWorks
var alpha = accumulatedElapsedTime / timestep;
Draw(timestep, alpha);
GraphicsDevice.SubmitDestroyCommandBuffer();
}
}
}

View File

@ -11,6 +11,7 @@ namespace MoonWorks.Graphics
public bool IsDisposed { get; private set; }
private readonly List<WeakReference<GraphicsResource>> resources = new List<WeakReference<GraphicsResource>>();
private Dictionary<IntPtr, Action<IntPtr, IntPtr, IntPtr>> resourcesToDestroy = new Dictionary<IntPtr, Action<IntPtr, IntPtr, IntPtr>>();
public GraphicsDevice(
IntPtr deviceWindowHandle,
@ -56,6 +57,26 @@ namespace MoonWorks.Graphics
Refresh.Refresh_Wait(Handle);
}
internal void SubmitDestroyCommandBuffer()
{
if (resourcesToDestroy.Count > 0)
{
var commandBuffer = AcquireCommandBuffer();
foreach (var kv in resourcesToDestroy)
{
kv.Value.Invoke(Handle, commandBuffer.Handle, kv.Key);
}
Submit(commandBuffer);
}
}
internal void PrepareDestroyResource(GraphicsResource resource, Action<IntPtr, IntPtr, IntPtr> destroyFunction)
{
resourcesToDestroy.Add(resource.Handle, destroyFunction);
}
internal void AddResourceReference(WeakReference<GraphicsResource> resourceReference)
{
lock (resources)

View File

@ -8,7 +8,7 @@ namespace MoonWorks.Graphics
public IntPtr Handle { get; protected set; }
public bool IsDisposed { get; private set; }
protected abstract Action<IntPtr, IntPtr> QueueDestroyFunction { get; }
protected abstract Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction { get; }
private WeakReference<GraphicsResource> selfReference;
@ -24,7 +24,7 @@ namespace MoonWorks.Graphics
{
if (!IsDisposed)
{
QueueDestroyFunction(Device.Handle, Handle);
Device.PrepareDestroyResource(this, QueueDestroyFunction);
if (selfReference != null)
{

View File

@ -9,7 +9,7 @@ namespace MoonWorks.Graphics
/// </summary>
public class Buffer : GraphicsResource
{
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyBuffer;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyBuffer;
/// <summary>
/// Creates a buffer.

View File

@ -6,7 +6,7 @@ namespace MoonWorks.Graphics
{
public class ComputePipeline : GraphicsResource
{
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyComputePipeline;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyComputePipeline;
public ShaderStageState ComputeShaderState { get; }

View File

@ -9,7 +9,7 @@ namespace MoonWorks.Graphics
/// </summary>
public class Framebuffer : GraphicsResource
{
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyFramebuffer;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyFramebuffer;
public RenderTarget DepthStencilTarget { get; }

View File

@ -10,7 +10,7 @@ namespace MoonWorks.Graphics
/// </summary>
public class GraphicsPipeline : GraphicsResource
{
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyGraphicsPipeline;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyGraphicsPipeline;
public ShaderStageState VertexShaderState { get; }
public ShaderStageState FragmentShaderState { get; }

View File

@ -8,7 +8,7 @@ namespace MoonWorks.Graphics
/// </summary>
public class RenderPass : GraphicsResource
{
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyRenderPass;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyRenderPass;
/// <summary>
/// Creates a render pass using color target descriptions.

View File

@ -11,7 +11,7 @@ namespace MoonWorks.Graphics
public TextureSlice TextureSlice { get; }
public TextureFormat Format => TextureSlice.Texture.Format;
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyRenderTarget;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyRenderTarget;
/// <summary>
/// Creates a render target backed by a texture.

View File

@ -8,7 +8,7 @@ namespace MoonWorks.Graphics
/// </summary>
public class Sampler : GraphicsResource
{
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroySampler;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroySampler;
public Sampler(
GraphicsDevice device,

View File

@ -8,7 +8,7 @@ namespace MoonWorks.Graphics
/// </summary>
public class ShaderModule : GraphicsResource
{
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyShaderModule;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyShaderModule;
public unsafe ShaderModule(GraphicsDevice device, string filePath) : base(device)
{

View File

@ -17,7 +17,7 @@ namespace MoonWorks.Graphics
public SampleCount SampleCount { get; }
public TextureUsageFlags UsageFlags { get; }
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture;
protected override Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture;
/// <summary>
/// Loads a PNG from a file path.