Skip to content

Commit

Permalink
⏪ Undo separately hardcoded vertex attribute byte sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcscharf committed Feb 9, 2024
1 parent 9db73c2 commit 2040141
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions Bearded.Graphics/Core/Vertices/VertexData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -54,10 +54,12 @@ public static VertexAttributeTemplate MakeAttributeTemplate<T>(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(
Expand All @@ -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<Type, Defaults> knownTypes
private static readonly ReadOnlyDictionary<Type, Defaults> defaultsForType
= new Dictionary<Type, Defaults>
{
{ 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}")
};
}

0 comments on commit 2040141

Please sign in to comment.