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 Binding;
public uint Stride; public uint Stride;
public VertexInputRate InputRate; 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)] [StructLayout(LayoutKind.Sequential)]
@ -122,6 +133,28 @@ namespace MoonWorks.Graphics
public uint Binding; public uint Binding;
public VertexElementFormat Format; public VertexElementFormat Format;
public uint Offset; 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)] [StructLayout(LayoutKind.Sequential)]
@ -175,7 +208,7 @@ namespace MoonWorks.Graphics
StoreOp = storeOp; StoreOp = storeOp;
} }
public ColorAttachmentInfo(Texture texture, StoreOp storeOp = StoreOp.Store) public ColorAttachmentInfo(Texture texture, LoadOp loadOp = LoadOp.DontCare, StoreOp storeOp = StoreOp.Store)
{ {
Texture = texture; Texture = texture;
Depth = 0; Depth = 0;
@ -183,8 +216,8 @@ namespace MoonWorks.Graphics
Level = 0; Level = 0;
SampleCount = SampleCount.One; SampleCount = SampleCount.One;
ClearColor = Color.White; ClearColor = Color.White;
LoadOp = LoadOp.DontCare; LoadOp = loadOp;
StoreOp = StoreOp.Store; StoreOp = storeOp;
} }
public Refresh.ColorAttachmentInfo ToRefresh() public Refresh.ColorAttachmentInfo ToRefresh()
@ -258,5 +291,15 @@ namespace MoonWorks.Graphics
public TextureFormat Format; public TextureFormat Format;
public SampleCount SampleCount; public SampleCount SampleCount;
public ColorAttachmentBlendState BlendState; 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, entryPointName = computeShaderInfo.EntryPointName,
shaderModule = computeShaderInfo.ShaderModule.Handle, shaderModule = computeShaderInfo.ShaderModule.Handle,
uniformBufferSize = computeShaderInfo.UniformBufferSize, uniformBufferSize = computeShaderInfo.UniformBufferSize,
bufferBindingCount = computeShaderInfo.bufferBindingCount, bufferBindingCount = computeShaderInfo.BufferBindingCount,
imageBindingCount = computeShaderInfo.imageBindingCount imageBindingCount = computeShaderInfo.ImageBindingCount
}; };
Handle = Refresh.Refresh_CreateComputePipeline( Handle = Refresh.Refresh_CreateComputePipeline(

View File

@ -1,3 +1,5 @@
using System.Runtime.InteropServices;
namespace MoonWorks.Graphics namespace MoonWorks.Graphics
{ {
/// <summary> /// <summary>
@ -8,7 +10,41 @@ namespace MoonWorks.Graphics
public ShaderModule ShaderModule; public ShaderModule ShaderModule;
public string EntryPointName; public string EntryPointName;
public uint UniformBufferSize; public uint UniformBufferSize;
public uint bufferBindingCount; public uint BufferBindingCount;
public uint imageBindingCount; 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 ColorAttachmentDescription[] ColorAttachmentDescriptions;
public bool HasDepthStencilAttachment; public bool HasDepthStencilAttachment;
public TextureFormat DepthStencilFormat; 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> /// <summary>
/// Information that the pipeline needs about a shader. /// Information that the pipeline needs about a shader.
@ -9,5 +11,34 @@
public string EntryPointName; public string EntryPointName;
public uint UniformBufferSize; public uint UniformBufferSize;
public uint SamplerBindingCount; 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 VertexBinding[] VertexBindings;
public VertexAttribute[] VertexAttributes; 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; 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!"
);
}
}
} }
} }