MoonWorks/src/Graphics/Resources/GraphicsPipeline.cs

104 lines
5.2 KiB
C#
Raw Normal View History

2022-02-23 05:14:32 +00:00
using System;
using System.Runtime.InteropServices;
2024-06-05 19:34:24 +00:00
using RefreshCS;
namespace MoonWorks.Graphics
{
2022-02-23 05:14:32 +00:00
/// <summary>
2023-09-19 20:19:41 +00:00
/// Graphics pipelines encapsulate all of the render state in a single object. <br/>
2022-02-23 05:14:32 +00:00
/// These pipelines are bound before draw calls are issued.
/// </summary>
2024-06-05 19:34:24 +00:00
public class GraphicsPipeline : RefreshResource
2022-02-23 05:14:32 +00:00
{
2024-06-05 19:34:24 +00:00
protected override Action<IntPtr, IntPtr> ReleaseFunction => Refresh.Refresh_ReleaseGraphicsPipeline;
2022-02-23 05:14:32 +00:00
2024-06-04 23:04:19 +00:00
public GraphicsPipelineResourceInfo VertexShaderResourceInfo { get; }
public GraphicsPipelineResourceInfo FragmentShaderResourceInfo { get; }
public SampleCount SampleCount { get; }
#if DEBUG
internal GraphicsPipelineAttachmentInfo AttachmentInfo { get; }
#endif
2022-02-23 05:14:32 +00:00
public unsafe GraphicsPipeline(
GraphicsDevice device,
in GraphicsPipelineCreateInfo graphicsPipelineCreateInfo
) : base(device)
{
2024-06-05 19:37:02 +00:00
Refresh.GraphicsPipelineCreateInfo refreshGraphicsPipelineCreateInfo;
2024-06-04 23:04:19 +00:00
2024-06-05 19:34:24 +00:00
var vertexAttributes = (Refresh.VertexAttribute*) NativeMemory.Alloc(
(nuint) (graphicsPipelineCreateInfo.VertexInputState.VertexAttributes.Length * Marshal.SizeOf<Refresh.VertexAttribute>())
2022-02-23 05:14:32 +00:00
);
2024-06-04 23:04:19 +00:00
for (var i = 0; i < graphicsPipelineCreateInfo.VertexInputState.VertexAttributes.Length; i += 1)
{
2024-06-05 19:34:24 +00:00
vertexAttributes[i] = graphicsPipelineCreateInfo.VertexInputState.VertexAttributes[i].ToRefresh();
2024-06-04 23:04:19 +00:00
}
2024-06-05 19:34:24 +00:00
var vertexBindings = (Refresh.VertexBinding*) NativeMemory.Alloc(
(nuint) (graphicsPipelineCreateInfo.VertexInputState.VertexBindings.Length * Marshal.SizeOf<Refresh.VertexBinding>())
2022-02-23 05:14:32 +00:00
);
2024-06-04 23:04:19 +00:00
for (var i = 0; i < graphicsPipelineCreateInfo.VertexInputState.VertexBindings.Length; i += 1)
{
2024-06-05 19:34:24 +00:00
vertexBindings[i] = graphicsPipelineCreateInfo.VertexInputState.VertexBindings[i].ToRefresh();
2024-06-04 23:04:19 +00:00
}
2024-06-05 19:34:24 +00:00
var colorAttachmentDescriptions = stackalloc Refresh.ColorAttachmentDescription[
2024-06-04 23:04:19 +00:00
graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions.Length
];
2024-06-04 23:04:19 +00:00
for (var i = 0; i < graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions.Length; i += 1)
{
2024-06-05 19:34:24 +00:00
colorAttachmentDescriptions[i].Format = (Refresh.TextureFormat) graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions[i].Format;
colorAttachmentDescriptions[i].BlendState = graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions[i].BlendState.ToRefresh();
}
2024-06-05 19:37:02 +00:00
refreshGraphicsPipelineCreateInfo.VertexShader = graphicsPipelineCreateInfo.VertexShader.Handle;
refreshGraphicsPipelineCreateInfo.FragmentShader = graphicsPipelineCreateInfo.FragmentShader.Handle;
2024-06-04 23:04:19 +00:00
2024-06-05 19:37:02 +00:00
refreshGraphicsPipelineCreateInfo.VertexInputState.VertexAttributes = vertexAttributes;
refreshGraphicsPipelineCreateInfo.VertexInputState.VertexAttributeCount = (uint) graphicsPipelineCreateInfo.VertexInputState.VertexAttributes.Length;
refreshGraphicsPipelineCreateInfo.VertexInputState.VertexBindings = vertexBindings;
refreshGraphicsPipelineCreateInfo.VertexInputState.VertexBindingCount = (uint) graphicsPipelineCreateInfo.VertexInputState.VertexBindings.Length;
2024-06-04 23:04:19 +00:00
2024-06-05 19:37:02 +00:00
refreshGraphicsPipelineCreateInfo.PrimitiveType = (Refresh.PrimitiveType) graphicsPipelineCreateInfo.PrimitiveType;
2024-06-04 23:04:19 +00:00
2024-06-05 19:37:02 +00:00
refreshGraphicsPipelineCreateInfo.RasterizerState = graphicsPipelineCreateInfo.RasterizerState.ToRefresh();
refreshGraphicsPipelineCreateInfo.MultisampleState = graphicsPipelineCreateInfo.MultisampleState.ToRefresh();
refreshGraphicsPipelineCreateInfo.DepthStencilState = graphicsPipelineCreateInfo.DepthStencilState.ToRefresh();
2024-06-04 23:04:19 +00:00
2024-06-05 19:37:02 +00:00
refreshGraphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentCount = (uint) graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions.Length;
refreshGraphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions = colorAttachmentDescriptions;
refreshGraphicsPipelineCreateInfo.AttachmentInfo.DepthStencilFormat = (Refresh.TextureFormat) graphicsPipelineCreateInfo.AttachmentInfo.DepthStencilFormat;
refreshGraphicsPipelineCreateInfo.AttachmentInfo.HasDepthStencilAttachment = Conversions.BoolToInt(graphicsPipelineCreateInfo.AttachmentInfo.HasDepthStencilAttachment);
2024-06-04 23:04:19 +00:00
2024-06-05 19:37:02 +00:00
refreshGraphicsPipelineCreateInfo.VertexResourceInfo = graphicsPipelineCreateInfo.VertexShaderResourceInfo.ToRefresh();
refreshGraphicsPipelineCreateInfo.FragmentResourceInfo = graphicsPipelineCreateInfo.FragmentShaderResourceInfo.ToRefresh();
2024-06-04 23:04:19 +00:00
2024-06-05 19:37:02 +00:00
refreshGraphicsPipelineCreateInfo.BlendConstants[0] = graphicsPipelineCreateInfo.BlendConstants.R;
refreshGraphicsPipelineCreateInfo.BlendConstants[1] = graphicsPipelineCreateInfo.BlendConstants.G;
refreshGraphicsPipelineCreateInfo.BlendConstants[2] = graphicsPipelineCreateInfo.BlendConstants.B;
refreshGraphicsPipelineCreateInfo.BlendConstants[3] = graphicsPipelineCreateInfo.BlendConstants.A;
2024-06-04 23:04:19 +00:00
2024-06-05 19:37:02 +00:00
Handle = Refresh.Refresh_CreateGraphicsPipeline(device.Handle, refreshGraphicsPipelineCreateInfo);
if (Handle == IntPtr.Zero)
{
throw new Exception("Could not create graphics pipeline!");
}
2022-02-23 05:14:32 +00:00
2024-06-04 23:04:19 +00:00
NativeMemory.Free(vertexAttributes);
NativeMemory.Free(vertexBindings);
2022-02-23 05:14:32 +00:00
2024-06-04 23:04:19 +00:00
VertexShaderResourceInfo = graphicsPipelineCreateInfo.VertexShaderResourceInfo;
FragmentShaderResourceInfo = graphicsPipelineCreateInfo.FragmentShaderResourceInfo;
SampleCount = graphicsPipelineCreateInfo.MultisampleState.MultisampleCount;
#if DEBUG
2024-06-04 23:04:19 +00:00
AttachmentInfo = graphicsPipelineCreateInfo.AttachmentInfo;
#endif
2022-02-23 05:14:32 +00:00
}
}
}