2021-01-20 03:33:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-02 21:04:12 +00:00
|
|
|
|
using System.IO;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
using RefreshCS;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Graphics
|
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public class GraphicsDevice : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public IntPtr Handle { get; }
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-08-02 21:04:12 +00:00
|
|
|
|
// Built-in video pipeline
|
|
|
|
|
private ShaderModule VideoVertexShader { get; }
|
|
|
|
|
private ShaderModule VideoFragmentShader { get; }
|
|
|
|
|
internal GraphicsPipeline VideoPipeline { get; }
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public bool IsDisposed { get; private set; }
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
private readonly List<WeakReference<GraphicsResource>> resources = new List<WeakReference<GraphicsResource>>();
|
2021-01-27 05:06:15 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public GraphicsDevice(
|
|
|
|
|
IntPtr deviceWindowHandle,
|
|
|
|
|
Refresh.PresentMode presentMode,
|
2022-02-23 06:16:06 +00:00
|
|
|
|
bool debugMode
|
2022-02-23 05:14:32 +00:00
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
var presentationParameters = new Refresh.PresentationParameters
|
|
|
|
|
{
|
|
|
|
|
deviceWindowHandle = deviceWindowHandle,
|
|
|
|
|
presentMode = presentMode
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Handle = Refresh.Refresh_CreateDevice(
|
|
|
|
|
presentationParameters,
|
|
|
|
|
Conversions.BoolToByte(debugMode)
|
|
|
|
|
);
|
2022-08-02 21:04:12 +00:00
|
|
|
|
|
|
|
|
|
VideoVertexShader = new ShaderModule(this, GetEmbeddedResource("MoonWorks.Shaders.FullscreenVert.spv"));
|
|
|
|
|
VideoFragmentShader = new ShaderModule(this, GetEmbeddedResource("MoonWorks.Shaders.YUV2RGBAFrag.spv"));
|
|
|
|
|
|
|
|
|
|
VideoPipeline = new GraphicsPipeline(
|
|
|
|
|
this,
|
|
|
|
|
new GraphicsPipelineCreateInfo
|
|
|
|
|
{
|
|
|
|
|
AttachmentInfo = new GraphicsPipelineAttachmentInfo(
|
|
|
|
|
new ColorAttachmentDescription(TextureFormat.R8G8B8A8, ColorAttachmentBlendState.None)
|
|
|
|
|
),
|
|
|
|
|
DepthStencilState = DepthStencilState.Disable,
|
|
|
|
|
VertexShaderInfo = GraphicsShaderInfo.Create(VideoVertexShader, "main", 0),
|
|
|
|
|
FragmentShaderInfo = GraphicsShaderInfo.Create(VideoFragmentShader, "main", 3),
|
|
|
|
|
VertexInputState = VertexInputState.Empty,
|
|
|
|
|
RasterizerState = RasterizerState.CCW_CullNone,
|
|
|
|
|
PrimitiveType = PrimitiveType.TriangleList,
|
|
|
|
|
MultisampleState = MultisampleState.None
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CommandBuffer AcquireCommandBuffer()
|
|
|
|
|
{
|
|
|
|
|
return new CommandBuffer(this, Refresh.Refresh_AcquireCommandBuffer(Handle, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe void Submit(params CommandBuffer[] commandBuffers)
|
|
|
|
|
{
|
|
|
|
|
var commandBufferPtrs = stackalloc IntPtr[commandBuffers.Length];
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < commandBuffers.Length; i += 1)
|
|
|
|
|
{
|
|
|
|
|
commandBufferPtrs[i] = commandBuffers[i].Handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Refresh.Refresh_Submit(
|
|
|
|
|
Handle,
|
|
|
|
|
(uint) commandBuffers.Length,
|
|
|
|
|
(IntPtr) commandBufferPtrs
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Wait()
|
|
|
|
|
{
|
|
|
|
|
Refresh.Refresh_Wait(Handle);
|
|
|
|
|
}
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-03-02 07:21:42 +00:00
|
|
|
|
public TextureFormat GetSwapchainFormat(Window window)
|
|
|
|
|
{
|
|
|
|
|
return (TextureFormat) Refresh.Refresh_GetSwapchainFormat(Handle, window.Handle);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
internal void AddResourceReference(WeakReference<GraphicsResource> resourceReference)
|
|
|
|
|
{
|
|
|
|
|
lock (resources)
|
|
|
|
|
{
|
|
|
|
|
resources.Add(resourceReference);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void RemoveResourceReference(WeakReference<GraphicsResource> resourceReference)
|
|
|
|
|
{
|
|
|
|
|
lock (resources)
|
|
|
|
|
{
|
|
|
|
|
resources.Remove(resourceReference);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 21:04:12 +00:00
|
|
|
|
private static Stream GetEmbeddedResource(string name)
|
|
|
|
|
{
|
|
|
|
|
return typeof(GraphicsDevice).Assembly.GetManifestResourceStream(name);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!IsDisposed)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
lock (resources)
|
|
|
|
|
{
|
2022-02-22 22:29:50 +00:00
|
|
|
|
for (var i = resources.Count - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
var resource = resources[i];
|
|
|
|
|
if (resource.TryGetTarget(out var target))
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
target.Dispose();
|
|
|
|
|
}
|
2022-02-22 22:29:50 +00:00
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
|
resources.Clear();
|
|
|
|
|
}
|
2021-01-27 05:06:15 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
Refresh.Refresh_DestroyDevice(Handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~GraphicsDevice()
|
|
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-20 03:33:27 +00:00
|
|
|
|
}
|