-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* π Upgrade to .net8 * ποΈ Rework vertex data - make IVertexData.VertexAttributes static and immutable - support double and integer vertex attributes - remove automatic normalisation for integer types - fix attribute size being incorrectly calculated based on attribute pointer type, instead of source data - make vertex attributes and templates structs and improve method signatures to reduce allocations - move all parameter validation to helper class * βͺ Undo separately hardcoded vertex attribute byte sizes * π Fix example C# versions
- Loading branch information
1 parent
dc357b7
commit 3ff71f9
Showing
18 changed files
with
220 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Bearded.Graphics.System.Drawing/Bearded.Graphics.System.Drawing.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,8 @@ | ||
ο»Ώ | ||
namespace Bearded.Graphics.Vertices | ||
ο»Ώusing System.Collections.Immutable; | ||
|
||
namespace Bearded.Graphics.Vertices; | ||
|
||
public interface IVertexData | ||
{ | ||
/// <summary> | ||
/// This interface must be implemented by any custom vertex data. | ||
/// </summary> | ||
public interface IVertexData | ||
{ | ||
/// <summary> | ||
/// Returns the vertex' <see cref="VertexAttributes"/> | ||
/// </summary> | ||
/// <returns>Array of <see cref="VertexAttribute"/></returns> | ||
VertexAttribute[] VertexAttributes { get; } | ||
} | ||
static abstract ImmutableArray<VertexAttribute> VertexAttributes { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,44 @@ | ||
ο»Ώusing Bearded.Graphics.Shading; | ||
ο»Ώusing System; | ||
using Bearded.Graphics.Shading; | ||
using OpenTK.Graphics.OpenGL; | ||
|
||
namespace Bearded.Graphics.Vertices | ||
namespace Bearded.Graphics.Vertices; | ||
|
||
public readonly struct VertexAttribute( | ||
string name, | ||
int size, | ||
VertexAttribPointerType type, | ||
int stride, | ||
int offset, | ||
VertexAttributeFormat format) | ||
{ | ||
public sealed class VertexAttribute | ||
public void SetAttribute(ShaderProgram program) | ||
{ | ||
private readonly string name; | ||
private readonly int size; | ||
private readonly VertexAttribPointerType type; | ||
private readonly bool normalize; | ||
private readonly int stride; | ||
private readonly int offset; | ||
var index = program.GetAttributeLocation(name); | ||
if (index == StatusCode.NotFound) | ||
return; | ||
|
||
public VertexAttribute( | ||
string name, int size, VertexAttribPointerType type, int stride, int offset, bool normalize = false) | ||
{ | ||
this.name = name; | ||
this.size = size; | ||
this.type = type; | ||
this.stride = stride; | ||
this.offset = offset; | ||
this.normalize = normalize; | ||
} | ||
GL.EnableVertexAttribArray(index); | ||
|
||
public void SetAttribute(ShaderProgram program) | ||
switch (format) | ||
{ | ||
var index = program.GetAttributeLocation(name); | ||
if (index == StatusCode.NotFound) | ||
return; | ||
GL.EnableVertexAttribArray(index); | ||
GL.VertexAttribPointer(index, size, type, normalize, stride, offset); | ||
case VertexAttributeFormat.Float: | ||
GL.VertexAttribPointer(index, size, type, false, stride, offset); | ||
break; | ||
case VertexAttributeFormat.FloatNormalized: | ||
GL.VertexAttribPointer(index, size, type, true, stride, offset); | ||
break; | ||
case VertexAttributeFormat.Double: | ||
GL.VertexAttribLPointer(index, size, VertexAttribDoubleType.Double, stride, new IntPtr(offset)); | ||
break; | ||
case VertexAttributeFormat.Integer: | ||
GL.VertexAttribIPointer(index, size, (VertexAttribIntegerType)type, stride, new IntPtr(offset)); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
public override string ToString() => | ||
$"{{name: {name}, size: {size}, type: {type}, normalize: {normalize}, stride: {stride}, offset: {offset}}}"; | ||
} | ||
|
||
public override string ToString() => | ||
$"{{name: {name}, size: {size}, type: {type}, format: {format}, stride: {stride}, offset: {offset}}}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
ο»Ώnamespace Bearded.Graphics.Vertices; | ||
|
||
public enum VertexAttributeFormat | ||
{ | ||
Float, | ||
FloatNormalized, | ||
Double, | ||
Integer, | ||
} |
51 changes: 10 additions & 41 deletions
51
Bearded.Graphics/Core/Vertices/VertexAttributeTemplate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using OpenTK.Graphics.OpenGL; | ||
|
||
namespace Bearded.Graphics.Vertices | ||
{ | ||
public sealed class VertexAttributeTemplate | ||
{ | ||
private static readonly ImmutableDictionary<VertexAttribPointerType, int> attributeByteSizes | ||
= ImmutableDictionary.CreateRange(new Dictionary<VertexAttribPointerType, int> | ||
{ | ||
{ VertexAttribPointerType.Byte, 1 }, | ||
{ VertexAttribPointerType.UnsignedByte, 1 }, | ||
{ VertexAttribPointerType.Short, 2 }, | ||
{ VertexAttribPointerType.UnsignedShort, 2 }, | ||
{ VertexAttribPointerType.HalfFloat, 2 }, | ||
{ VertexAttribPointerType.Int, 4 }, | ||
{ VertexAttribPointerType.UnsignedInt, 4 }, | ||
{ VertexAttribPointerType.Float, 4 }, | ||
{ VertexAttribPointerType.Double, 8 }, | ||
}); | ||
|
||
private readonly string name; | ||
private readonly int size; | ||
private readonly VertexAttribPointerType type; | ||
private readonly bool normalize; | ||
|
||
public int Bytes { get; } | ||
namespace Bearded.Graphics.Vertices; | ||
|
||
internal VertexAttributeTemplate(string name, int size, VertexAttribPointerType type, bool normalize) | ||
{ | ||
if (!attributeByteSizes.TryGetValue(type, out var bytes)) | ||
throw new ArgumentException($"Unknown VertexAttribPointerType: {type}"); | ||
|
||
Bytes = bytes * size; | ||
this.name = name; | ||
this.size = size; | ||
this.type = type; | ||
this.normalize = normalize; | ||
} | ||
|
||
public VertexAttribute ToAttribute(int offset, int stride) => new(name, size, type, stride, offset, normalize); | ||
} | ||
public readonly struct VertexAttributeTemplate( | ||
string name, | ||
int size, | ||
int bytes, | ||
VertexAttribPointerType type, | ||
VertexAttributeFormat format) | ||
{ | ||
public int Bytes => bytes; | ||
public VertexAttribute ToAttribute(int offset, int stride) => new(name, size, type, stride, offset, format); | ||
} |
Oops, something went wrong.