convenience constructors for graphics state

pull/17/head
cosmonaut 2022-03-02 13:57:30 -08:00
parent 278db7d55b
commit 7d3a7901b2
7 changed files with 173 additions and 8 deletions

View File

@ -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<T>()
{
return new VertexBinding
{
Binding = 0,
InputRate = VertexInputRate.Vertex,
Stride = (uint) Marshal.SizeOf<T>()
};
}
}
[StructLayout(LayoutKind.Sequential)]
@ -122,6 +133,28 @@ namespace MoonWorks.Graphics
public uint Binding;
public VertexElementFormat Format;
public uint Offset;
public static VertexAttribute Create<T>(
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<T>(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;
}
}
}

View File

@ -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(

View File

@ -1,3 +1,5 @@
using System.Runtime.InteropServices;
namespace MoonWorks.Graphics
{
/// <summary>
@ -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<T>(
ShaderModule shaderModule,
string entryPointName,
uint bufferBindingCount,
uint imageBindingCount
)
{
return new ComputeShaderInfo
{
ShaderModule = shaderModule,
EntryPointName = entryPointName,
UniformBufferSize = (uint) Marshal.SizeOf<T>(),
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
};
}
}
}

View File

@ -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;
}
}
}

View File

@ -1,4 +1,6 @@
namespace MoonWorks.Graphics
using System.Runtime.InteropServices;
namespace MoonWorks.Graphics
{
/// <summary>
/// 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<T>(
ShaderModule shaderModule,
string entryPointName,
uint samplerBindingCount
) where T : unmanaged
{
return new GraphicsShaderInfo
{
ShaderModule = shaderModule,
EntryPointName = entryPointName,
UniformBufferSize = (uint) Marshal.SizeOf<T>(),
SamplerBindingCount = samplerBindingCount
};
}
public static GraphicsShaderInfo Create(
ShaderModule shaderModule,
string entryPointName,
uint samplerBindingCount
) {
return new GraphicsShaderInfo
{
ShaderModule = shaderModule,
EntryPointName = entryPointName,
UniformBufferSize = 0,
SamplerBindingCount = samplerBindingCount
};
}
}
}

View File

@ -7,5 +7,13 @@
{
public VertexBinding[] VertexBindings;
public VertexAttribute[] VertexAttributes;
public VertexInputState(
VertexBinding vertexBinding,
params VertexAttribute[] vertexAttributes
) {
VertexBindings = new VertexBinding[] { vertexBinding };
VertexAttributes = vertexAttributes;
}
}
}

View File

@ -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!"
);
}
}
}
}