diff --git a/Bearded.Graphics/Core/Vertices/VertexData.cs b/Bearded.Graphics/Core/Vertices/VertexData.cs index 67810af..511c53d 100644 --- a/Bearded.Graphics/Core/Vertices/VertexData.cs +++ b/Bearded.Graphics/Core/Vertices/VertexData.cs @@ -12,7 +12,7 @@ namespace Bearded.Graphics.Vertices; using PointerType = VertexAttribPointerType; using Format = VertexAttributeFormat; -using Defaults = (VertexAttribPointerType Type, int Count, int Bytes, VertexAttributeFormat DefaultFormat); +using Defaults = (VertexAttribPointerType Type, int Count, VertexAttributeFormat DefaultFormat); public static class VertexData { @@ -54,10 +54,12 @@ public static VertexAttributeTemplate MakeAttributeTemplate(string name, Form public static VertexAttributeTemplate MakeAttributeTemplate(string name, Type type, Format? format = null) { - if (!knownTypes.TryGetValue(type, out var info)) + if (!defaultsForType.TryGetValue(type, out var info)) throw new ArgumentException($"Unknown type: {type.Name}"); - return MakeAttributeTemplate(name, info.Type, info.Count, info.Bytes, format ?? info.DefaultFormat); + var bytes = size(info.Type); + + return MakeAttributeTemplate(name, info.Type, info.Count, info.Count * bytes, format ?? info.DefaultFormat); } public static VertexAttributeTemplate MakeAttributeTemplate( @@ -74,33 +76,52 @@ public static VertexAttributeTemplate MakeAttributeTemplate( private static bool isValidIntegerType(PointerType type) => type is >= PointerType.Byte and <= PointerType.UnsignedInt; - private static readonly ReadOnlyDictionary knownTypes + private static readonly ReadOnlyDictionary defaultsForType = new Dictionary { - { typeof(float), (PointerType.Float, 1, 4, Format.Float) }, - { typeof(Vector2), (PointerType.Float, 2, 8, Format.Float) }, - { typeof(Vector3), (PointerType.Float, 3, 12, Format.Float) }, - { typeof(Vector4), (PointerType.Float, 4, 16, Format.Float) }, + { typeof(float), (PointerType.Float, 1, Format.Float) }, + { typeof(Vector2), (PointerType.Float, 2, Format.Float) }, + { typeof(Vector3), (PointerType.Float, 3, Format.Float) }, + { typeof(Vector4), (PointerType.Float, 4, Format.Float) }, - { typeof(Half), (PointerType.HalfFloat, 1, 2, Format.Float) }, - { typeof(Vector2h), (PointerType.HalfFloat, 2, 4, Format.Float) }, - { typeof(Vector3h), (PointerType.HalfFloat, 3, 6, Format.Float) }, - { typeof(Vector4h), (PointerType.HalfFloat, 4, 8, Format.Float) }, + { typeof(Half), (PointerType.HalfFloat, 1, Format.Float) }, + { typeof(Vector2h), (PointerType.HalfFloat, 2, Format.Float) }, + { typeof(Vector3h), (PointerType.HalfFloat, 3, Format.Float) }, + { typeof(Vector4h), (PointerType.HalfFloat, 4, Format.Float) }, - { typeof(double), (PointerType.Double, 1, 8, Format.Double) }, - { typeof(Vector2d), (PointerType.Double, 2, 16, Format.Double) }, - { typeof(Vector3d), (PointerType.Double, 3, 24, Format.Double) }, - { typeof(Vector4d), (PointerType.Double, 4, 32, Format.Double) }, + { typeof(double), (PointerType.Double, 1, Format.Double) }, + { typeof(Vector2d), (PointerType.Double, 2, Format.Double) }, + { typeof(Vector3d), (PointerType.Double, 3, Format.Double) }, + { typeof(Vector4d), (PointerType.Double, 4, Format.Double) }, - { typeof(byte), (PointerType.UnsignedByte, 1, 1, Format.Integer) }, - { typeof(sbyte), (PointerType.Byte, 1, 1, Format.Integer) }, + { typeof(byte), (PointerType.UnsignedByte, 1, Format.Integer) }, + { typeof(sbyte), (PointerType.Byte, 1, Format.Integer) }, - { typeof(short), (PointerType.Short, 1, 2, Format.Integer) }, - { typeof(ushort), (PointerType.UnsignedShort, 1, 2, Format.Integer) }, + { typeof(short), (PointerType.Short, 1, Format.Integer) }, + { typeof(ushort), (PointerType.UnsignedShort, 1, Format.Integer) }, - { typeof(int), (PointerType.Int, 1, 4, Format.Integer) }, - { typeof(uint), (PointerType.UnsignedInt, 1, 4, Format.Integer) }, + { typeof(int), (PointerType.Int, 1, Format.Integer) }, + { typeof(uint), (PointerType.UnsignedInt, 1, Format.Integer) }, - { typeof(Color), (PointerType.UnsignedByte, 4, 4, Format.FloatNormalized) }, + { typeof(Color), (PointerType.UnsignedByte, 4, Format.FloatNormalized) }, }.AsReadOnly(); + + private static int size(PointerType type) + => type switch + { + PointerType.Byte => 1, + PointerType.UnsignedByte => 1, + PointerType.Short => 2, + PointerType.UnsignedShort => 2, + PointerType.Int => 4, + PointerType.UnsignedInt => 4, + PointerType.Float => 4, + PointerType.Double => 8, + PointerType.HalfFloat => 2, + PointerType.Fixed => 4, + PointerType.UnsignedInt2101010Rev => 4, + PointerType.UnsignedInt10F11F11FRev => 4, + PointerType.Int2101010Rev => 4, + _ => throw new ArgumentOutOfRangeException($"Invalid {nameof(VertexAttribPointerType)} value: {type}") + }; }