2022-02-23 05:14:32 +00:00
|
|
|
|
using System;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2024-06-04 23:04:19 +00:00
|
|
|
|
using SDL2_gpuCS;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
|
|
|
|
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-04 19:19:41 +00:00
|
|
|
|
public class GraphicsPipeline : SDL_GpuResource
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2024-06-04 23:04:19 +00:00
|
|
|
|
protected override Action<IntPtr, IntPtr> ReleaseFunction => SDL_Gpu.SDL_GpuReleaseGraphicsPipeline;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
|
2024-06-04 23:04:19 +00:00
|
|
|
|
public GraphicsPipelineResourceInfo VertexShaderResourceInfo { get; }
|
|
|
|
|
public GraphicsPipelineResourceInfo FragmentShaderResourceInfo { get; }
|
2022-12-29 03:22:47 +00:00
|
|
|
|
public SampleCount SampleCount { get; }
|
2022-11-17 20:35:21 +00:00
|
|
|
|
|
|
|
|
|
#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-04 23:04:19 +00:00
|
|
|
|
SDL_Gpu.GraphicsPipelineCreateInfo sdlGraphicsPipelineCreateInfo;
|
|
|
|
|
|
|
|
|
|
var vertexAttributes = (SDL_Gpu.VertexAttribute*) NativeMemory.Alloc(
|
|
|
|
|
(nuint) (graphicsPipelineCreateInfo.VertexInputState.VertexAttributes.Length * Marshal.SizeOf<SDL_Gpu.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)
|
|
|
|
|
{
|
|
|
|
|
vertexAttributes[i] = graphicsPipelineCreateInfo.VertexInputState.VertexAttributes[i].ToSDL();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var vertexBindings = (SDL_Gpu.VertexBinding*) NativeMemory.Alloc(
|
|
|
|
|
(nuint) (graphicsPipelineCreateInfo.VertexInputState.VertexBindings.Length * Marshal.SizeOf<SDL_Gpu.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)
|
|
|
|
|
{
|
|
|
|
|
vertexBindings[i] = graphicsPipelineCreateInfo.VertexInputState.VertexBindings[i].ToSDL();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var colorAttachmentDescriptions = stackalloc SDL_Gpu.ColorAttachmentDescription[
|
|
|
|
|
graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions.Length
|
2022-02-25 05:34:36 +00:00
|
|
|
|
];
|
|
|
|
|
|
2024-06-04 23:04:19 +00:00
|
|
|
|
for (var i = 0; i < graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions.Length; i += 1)
|
2022-02-25 05:34:36 +00:00
|
|
|
|
{
|
2024-06-04 23:04:19 +00:00
|
|
|
|
colorAttachmentDescriptions[i].Format = (SDL_Gpu.TextureFormat) graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions[i].Format;
|
|
|
|
|
colorAttachmentDescriptions[i].BlendState = graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions[i].BlendState.ToSDL();
|
2022-02-25 05:34:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 23:04:19 +00:00
|
|
|
|
sdlGraphicsPipelineCreateInfo.VertexShader = graphicsPipelineCreateInfo.VertexShader.Handle;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.FragmentShader = graphicsPipelineCreateInfo.FragmentShader.Handle;
|
|
|
|
|
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.VertexInputState.VertexAttributes = vertexAttributes;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.VertexInputState.VertexAttributeCount = (uint) graphicsPipelineCreateInfo.VertexInputState.VertexAttributes.Length;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.VertexInputState.VertexBindings = vertexBindings;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.VertexInputState.VertexBindingCount = (uint) graphicsPipelineCreateInfo.VertexInputState.VertexBindings.Length;
|
|
|
|
|
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.PrimitiveType = (SDL_Gpu.PrimitiveType) graphicsPipelineCreateInfo.PrimitiveType;
|
|
|
|
|
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.RasterizerState = graphicsPipelineCreateInfo.RasterizerState.ToSDL();
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.MultisampleState = graphicsPipelineCreateInfo.MultisampleState.ToSDL();
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.DepthStencilState = graphicsPipelineCreateInfo.DepthStencilState.ToSDL();
|
|
|
|
|
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentCount = (uint) graphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions.Length;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions = colorAttachmentDescriptions;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.AttachmentInfo.DepthStencilFormat = (SDL_Gpu.TextureFormat) graphicsPipelineCreateInfo.AttachmentInfo.DepthStencilFormat;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.AttachmentInfo.HasDepthStencilAttachment = Conversions.BoolToInt(graphicsPipelineCreateInfo.AttachmentInfo.HasDepthStencilAttachment);
|
|
|
|
|
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.VertexResourceInfo = graphicsPipelineCreateInfo.VertexShaderResourceInfo.ToSDL();
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.FragmentResourceInfo = graphicsPipelineCreateInfo.FragmentShaderResourceInfo.ToSDL();
|
|
|
|
|
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.BlendConstants[0] = graphicsPipelineCreateInfo.BlendConstants.R;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.BlendConstants[1] = graphicsPipelineCreateInfo.BlendConstants.G;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.BlendConstants[2] = graphicsPipelineCreateInfo.BlendConstants.B;
|
|
|
|
|
sdlGraphicsPipelineCreateInfo.BlendConstants[3] = graphicsPipelineCreateInfo.BlendConstants.A;
|
|
|
|
|
|
|
|
|
|
Handle = SDL_Gpu.SDL_GpuCreateGraphicsPipeline(device.Handle, sdlGraphicsPipelineCreateInfo);
|
2022-11-08 19:29:05 +00:00
|
|
|
|
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;
|
2022-11-17 20:35:21 +00:00
|
|
|
|
|
|
|
|
|
#if DEBUG
|
2024-06-04 23:04:19 +00:00
|
|
|
|
AttachmentInfo = graphicsPipelineCreateInfo.AttachmentInfo;
|
2022-11-17 20:35:21 +00:00
|
|
|
|
#endif
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-20 03:33:27 +00:00
|
|
|
|
}
|