diff --git a/src/Graphics/IVertexType.cs b/src/Graphics/IVertexType.cs new file mode 100644 index 00000000..fe5d6b68 --- /dev/null +++ b/src/Graphics/IVertexType.cs @@ -0,0 +1,7 @@ +namespace MoonWorks.Graphics +{ + public interface IVertexType + { + VertexAttribute[] Attributes(uint binding = 0); + } +} diff --git a/src/Graphics/RefreshStructs.cs b/src/Graphics/RefreshStructs.cs index 6704a9c7..0f018289 100644 --- a/src/Graphics/RefreshStructs.cs +++ b/src/Graphics/RefreshStructs.cs @@ -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() where T : unmanaged + public static VertexBinding Create(uint binding = 0) where T : unmanaged { return new VertexBinding { - Binding = 0, + Binding = binding, InputRate = VertexInputRate.Vertex, Stride = (uint) Marshal.SizeOf() }; @@ -147,25 +146,18 @@ namespace MoonWorks.Graphics public VertexElementFormat Format; public uint Offset; - public static VertexAttribute Create( - 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(fieldName) + Binding = binding, + Format = format, + Offset = offset }; } } diff --git a/src/Graphics/State/VertexInputState.cs b/src/Graphics/State/VertexInputState.cs index 9bf8dc18..6e35a601 100644 --- a/src/Graphics/State/VertexInputState.cs +++ b/src/Graphics/State/VertexInputState.cs @@ -10,16 +10,32 @@ public static readonly VertexInputState Empty = new VertexInputState { - VertexBindings = new VertexBinding[0], - VertexAttributes = new VertexAttribute[0] + VertexBindings = System.Array.Empty(), + VertexAttributes = System.Array.Empty() }; 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() where T : unmanaged, IVertexType + { + return new VertexInputState( + VertexBinding.Create(), + default(T).Attributes() + ); + } } } diff --git a/src/Graphics/Utility/Conversions.cs b/src/Graphics/Utility/Conversions.cs index 6dd0dd9a..5d05be93 100644 --- a/src/Graphics/Utility/Conversions.cs +++ b/src/Graphics/Utility/Conversions.cs @@ -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!" - ); - } - } } }