From 7d3a7901b25d9544e47e2f0ea5baf9089d4ccb08 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 2 Mar 2022 13:57:30 -0800 Subject: [PATCH] convenience constructors for graphics state --- src/Graphics/RefreshStructs.cs | 49 +++++++++++++++++-- src/Graphics/Resources/ComputePipeline.cs | 4 +- src/Graphics/State/ComputeShaderInfo.cs | 40 ++++++++++++++- .../State/GraphicsPipelineAttachmentInfo.cs | 17 +++++++ src/Graphics/State/GraphicsShaderInfo.cs | 33 ++++++++++++- src/Graphics/State/VertexInputState.cs | 8 +++ src/Graphics/Utility/Conversions.cs | 30 ++++++++++++ 7 files changed, 173 insertions(+), 8 deletions(-) diff --git a/src/Graphics/RefreshStructs.cs b/src/Graphics/RefreshStructs.cs index 9fb19eb..49c58a8 100644 --- a/src/Graphics/RefreshStructs.cs +++ b/src/Graphics/RefreshStructs.cs @@ -113,6 +113,17 @@ namespace MoonWorks.Graphics public uint Binding; public uint Stride; public VertexInputRate InputRate; + + // Shortcut for the common case of having a single vertex binding. + public static VertexBinding Create() + { + return new VertexBinding + { + Binding = 0, + InputRate = VertexInputRate.Vertex, + Stride = (uint) Marshal.SizeOf() + }; + } } [StructLayout(LayoutKind.Sequential)] @@ -122,6 +133,28 @@ namespace MoonWorks.Graphics public uint Binding; public VertexElementFormat Format; public uint Offset; + + public static VertexAttribute Create( + string fieldName, + uint location, + uint binding = 0 + ) + { + var fieldInfo = typeof(T).GetField(fieldName); + + if (fieldInfo == null) + { + throw new System.ArgumentException("Field not recognized!"); + } + + return new VertexAttribute + { + Binding = binding, + Location = location, + Format = Conversions.TypeToVertexElementFormat(fieldInfo.FieldType), + Offset = (uint) Marshal.OffsetOf(fieldName) + }; + } } [StructLayout(LayoutKind.Sequential)] @@ -175,7 +208,7 @@ namespace MoonWorks.Graphics StoreOp = storeOp; } - public ColorAttachmentInfo(Texture texture, StoreOp storeOp = StoreOp.Store) + public ColorAttachmentInfo(Texture texture, LoadOp loadOp = LoadOp.DontCare, StoreOp storeOp = StoreOp.Store) { Texture = texture; Depth = 0; @@ -183,8 +216,8 @@ namespace MoonWorks.Graphics Level = 0; SampleCount = SampleCount.One; ClearColor = Color.White; - LoadOp = LoadOp.DontCare; - StoreOp = StoreOp.Store; + LoadOp = loadOp; + StoreOp = storeOp; } public Refresh.ColorAttachmentInfo ToRefresh() @@ -258,5 +291,15 @@ namespace MoonWorks.Graphics public TextureFormat Format; public SampleCount SampleCount; public ColorAttachmentBlendState BlendState; + + public ColorAttachmentDescription( + TextureFormat format, + ColorAttachmentBlendState blendState, + SampleCount sampleCount = SampleCount.One + ) { + Format = format; + SampleCount = sampleCount; + BlendState = blendState; + } } } diff --git a/src/Graphics/Resources/ComputePipeline.cs b/src/Graphics/Resources/ComputePipeline.cs index 83a71d0..460871d 100644 --- a/src/Graphics/Resources/ComputePipeline.cs +++ b/src/Graphics/Resources/ComputePipeline.cs @@ -20,8 +20,8 @@ namespace MoonWorks.Graphics entryPointName = computeShaderInfo.EntryPointName, shaderModule = computeShaderInfo.ShaderModule.Handle, uniformBufferSize = computeShaderInfo.UniformBufferSize, - bufferBindingCount = computeShaderInfo.bufferBindingCount, - imageBindingCount = computeShaderInfo.imageBindingCount + bufferBindingCount = computeShaderInfo.BufferBindingCount, + imageBindingCount = computeShaderInfo.ImageBindingCount }; Handle = Refresh.Refresh_CreateComputePipeline( diff --git a/src/Graphics/State/ComputeShaderInfo.cs b/src/Graphics/State/ComputeShaderInfo.cs index 50b8163..7787de2 100644 --- a/src/Graphics/State/ComputeShaderInfo.cs +++ b/src/Graphics/State/ComputeShaderInfo.cs @@ -1,3 +1,5 @@ +using System.Runtime.InteropServices; + namespace MoonWorks.Graphics { /// @@ -8,7 +10,41 @@ namespace MoonWorks.Graphics public ShaderModule ShaderModule; public string EntryPointName; public uint UniformBufferSize; - public uint bufferBindingCount; - public uint imageBindingCount; + public uint BufferBindingCount; + public uint ImageBindingCount; + + public static ComputeShaderInfo Create( + ShaderModule shaderModule, + string entryPointName, + uint bufferBindingCount, + uint imageBindingCount + ) + { + return new ComputeShaderInfo + { + ShaderModule = shaderModule, + EntryPointName = entryPointName, + UniformBufferSize = (uint) Marshal.SizeOf(), + BufferBindingCount = bufferBindingCount, + ImageBindingCount = imageBindingCount + }; + } + + public static ComputeShaderInfo Create( + ShaderModule shaderModule, + string entryPointName, + uint bufferBindingCount, + uint imageBindingCount + ) + { + return new ComputeShaderInfo + { + ShaderModule = shaderModule, + EntryPointName = entryPointName, + UniformBufferSize = 0, + BufferBindingCount = bufferBindingCount, + ImageBindingCount = imageBindingCount + }; + } } } diff --git a/src/Graphics/State/GraphicsPipelineAttachmentInfo.cs b/src/Graphics/State/GraphicsPipelineAttachmentInfo.cs index 349d441..a1f6e9c 100644 --- a/src/Graphics/State/GraphicsPipelineAttachmentInfo.cs +++ b/src/Graphics/State/GraphicsPipelineAttachmentInfo.cs @@ -8,5 +8,22 @@ namespace MoonWorks.Graphics public ColorAttachmentDescription[] ColorAttachmentDescriptions; public bool HasDepthStencilAttachment; public TextureFormat DepthStencilFormat; + + public GraphicsPipelineAttachmentInfo( + params ColorAttachmentDescription[] colorAttachmentDescriptions + ) { + ColorAttachmentDescriptions = colorAttachmentDescriptions; + HasDepthStencilAttachment = false; + DepthStencilFormat = TextureFormat.D16; + } + + public GraphicsPipelineAttachmentInfo( + TextureFormat depthStencilFormat, + params ColorAttachmentDescription[] colorAttachmentDescriptions + ) { + ColorAttachmentDescriptions = colorAttachmentDescriptions; + HasDepthStencilAttachment = true; + DepthStencilFormat = depthStencilFormat; + } } } diff --git a/src/Graphics/State/GraphicsShaderInfo.cs b/src/Graphics/State/GraphicsShaderInfo.cs index d772cad..069cca1 100644 --- a/src/Graphics/State/GraphicsShaderInfo.cs +++ b/src/Graphics/State/GraphicsShaderInfo.cs @@ -1,4 +1,6 @@ -namespace MoonWorks.Graphics +using System.Runtime.InteropServices; + +namespace MoonWorks.Graphics { /// /// Information that the pipeline needs about a shader. @@ -9,5 +11,34 @@ public string EntryPointName; public uint UniformBufferSize; public uint SamplerBindingCount; + + public static GraphicsShaderInfo Create( + ShaderModule shaderModule, + string entryPointName, + uint samplerBindingCount + ) where T : unmanaged + { + return new GraphicsShaderInfo + { + ShaderModule = shaderModule, + EntryPointName = entryPointName, + UniformBufferSize = (uint) Marshal.SizeOf(), + SamplerBindingCount = samplerBindingCount + }; + } + + public static GraphicsShaderInfo Create( + ShaderModule shaderModule, + string entryPointName, + uint samplerBindingCount + ) { + return new GraphicsShaderInfo + { + ShaderModule = shaderModule, + EntryPointName = entryPointName, + UniformBufferSize = 0, + SamplerBindingCount = samplerBindingCount + }; + } } } diff --git a/src/Graphics/State/VertexInputState.cs b/src/Graphics/State/VertexInputState.cs index 1c7233f..a984219 100644 --- a/src/Graphics/State/VertexInputState.cs +++ b/src/Graphics/State/VertexInputState.cs @@ -7,5 +7,13 @@ { public VertexBinding[] VertexBindings; public VertexAttribute[] VertexAttributes; + + public VertexInputState( + VertexBinding vertexBinding, + params VertexAttribute[] vertexAttributes + ) { + VertexBindings = new VertexBinding[] { vertexBinding }; + VertexAttributes = vertexAttributes; + } } } diff --git a/src/Graphics/Utility/Conversions.cs b/src/Graphics/Utility/Conversions.cs index 5d05be9..3aad742 100644 --- a/src/Graphics/Utility/Conversions.cs +++ b/src/Graphics/Utility/Conversions.cs @@ -11,5 +11,35 @@ { return b != 0; } + + public static Graphics.VertexElementFormat TypeToVertexElementFormat(System.Type type) + { + if (type == typeof(float)) + { + return Graphics.VertexElementFormat.Single; + } + else if (type == typeof(Math.Vector2)) + { + return Graphics.VertexElementFormat.Vector2; + } + else if (type == typeof(Math.Vector3)) + { + return Graphics.VertexElementFormat.Vector3; + } + else if (type == typeof(Math.Vector4)) + { + return Graphics.VertexElementFormat.Vector4; + } + else if (type == typeof(Graphics.Color)) + { + return Graphics.VertexElementFormat.Color; + } + else + { + throw new System.ArgumentException( + "Cannot automatically convert this type to a VertexElementFormat!" + ); + } + } } }