rework vertex input state creation to avoid reflection

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

View File

@ -10,16 +10,32 @@
public static readonly VertexInputState Empty = new VertexInputState
{
VertexBindings = new VertexBinding[0],
VertexAttributes = new VertexAttribute[0]
VertexBindings = System.Array.Empty<VertexBinding>(),
VertexAttributes = System.Array.Empty<VertexAttribute>()
};
public VertexInputState(
VertexBinding vertexBinding,
params VertexAttribute[] vertexAttributes
VertexAttribute[] vertexAttributes
) {
VertexBindings = new VertexBinding[] { vertexBinding };
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;
}
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!"
);
}
}
}
}