rework vertex input state creation to avoid reflection

pull/40/head
cosmonaut 2023-01-04 17:34:01 -08:00
parent 703b694bf6
commit 95981f0f03
4 changed files with 36 additions and 55 deletions

View File

@ -0,0 +1,7 @@
namespace MoonWorks.Graphics
{
public interface IVertexType
{
VertexAttribute[] Attributes(uint binding = 0);
}
}

View File

@ -127,12 +127,11 @@ namespace MoonWorks.Graphics
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>(uint binding = 0) where T : unmanaged
public unsafe static VertexBinding Create<T>() where T : unmanaged
{ {
return new VertexBinding return new VertexBinding
{ {
Binding = 0, Binding = binding,
InputRate = VertexInputRate.Vertex, InputRate = VertexInputRate.Vertex,
Stride = (uint) Marshal.SizeOf<T>() Stride = (uint) Marshal.SizeOf<T>()
}; };
@ -147,25 +146,18 @@ namespace MoonWorks.Graphics
public VertexElementFormat Format; public VertexElementFormat Format;
public uint Offset; public uint Offset;
public static VertexAttribute Create<T>( public static VertexAttribute Create(
string fieldName, uint binding,
uint location, uint location,
uint binding = 0 VertexElementFormat format,
) uint offset
{ ) {
var fieldInfo = typeof(T).GetField(fieldName);
if (fieldInfo == null)
{
throw new System.ArgumentException("Field not recognized!");
}
return new VertexAttribute return new VertexAttribute
{ {
Binding = binding,
Location = location, Location = location,
Format = Conversions.TypeToVertexElementFormat(fieldInfo.FieldType), Binding = binding,
Offset = (uint) Marshal.OffsetOf<T>(fieldName) Format = format,
Offset = offset
}; };
} }
} }

View File

@ -10,16 +10,32 @@
public static readonly VertexInputState Empty = new VertexInputState public static readonly VertexInputState Empty = new VertexInputState
{ {
VertexBindings = new VertexBinding[0], VertexBindings = System.Array.Empty<VertexBinding>(),
VertexAttributes = new VertexAttribute[0] VertexAttributes = System.Array.Empty<VertexAttribute>()
}; };
public VertexInputState( public VertexInputState(
VertexBinding vertexBinding, VertexBinding vertexBinding,
params VertexAttribute[] vertexAttributes VertexAttribute[] vertexAttributes
) { ) {
VertexBindings = new VertexBinding[] { vertexBinding }; VertexBindings = new VertexBinding[] { vertexBinding };
VertexAttributes = vertexAttributes; VertexAttributes = vertexAttributes;
} }
public VertexInputState(
VertexBinding[] vertexBindings,
VertexAttribute[] vertexAttributes
) {
VertexBindings = vertexBindings;
VertexAttributes = vertexAttributes;
}
public static VertexInputState CreateSingleBinding<T>() where T : unmanaged, IVertexType
{
return new VertexInputState(
VertexBinding.Create<T>(),
default(T).Attributes()
);
}
} }
} }

View File

@ -11,39 +11,5 @@
{ {
return b != 0; return b != 0;
} }
public static Graphics.VertexElementFormat TypeToVertexElementFormat(System.Type type)
{
if (type == typeof(uint))
{
return Graphics.VertexElementFormat.UInt;
}
if (type == typeof(float))
{
return Graphics.VertexElementFormat.Float;
}
else if (type == typeof(Math.Float.Vector2))
{
return Graphics.VertexElementFormat.Vector2;
}
else if (type == typeof(Math.Float.Vector3))
{
return Graphics.VertexElementFormat.Vector3;
}
else if (type == typeof(Math.Float.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!"
);
}
}
} }
} }