Skip to content

Commit

Permalink
πŸ—οΈ Rework vertex data (#73)
Browse files Browse the repository at this point in the history
* πŸ†™ 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
paulcscharf authored Feb 13, 2024
1 parent dc357b7 commit 3ff71f9
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<OutputType>Exe</OutputType>
<RootNamespace>Bearded.Graphics.Examples.Basics</RootNamespace>
<AssemblyName>Bearded.Graphics.Examples.Basics</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>9</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>default</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Nullable>annotations</Nullable>
<IsPackable>false</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<OutputType>Exe</OutputType>
<RootNamespace>Bearded.Graphics.Examples.IndexBuffer</RootNamespace>
<AssemblyName>Bearded.Graphics.Examples.IndexBuffer</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>9</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>default</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Nullable>annotations</Nullable>
<IsPackable>false</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<OutputType>Exe</OutputType>
<RootNamespace>Bearded.Graphics.Examples.RenderSettings</RootNamespace>
<AssemblyName>Bearded.Graphics.Examples.RenderSettings</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>9</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>default</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Nullable>annotations</Nullable>
<IsPackable>false</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<OutputType>Exe</OutputType>
<RootNamespace>Bearded.Graphics.Examples.PostProcessing</RootNamespace>
<AssemblyName>Bearded.Graphics.Examples.PostProcessing</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>9</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>default</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Nullable>annotations</Nullable>
<IsPackable>false</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<OutputType>Exe</OutputType>
<RootNamespace>Bearded.Graphics.Examples.Text</RootNamespace>
<AssemblyName>Bearded.Graphics.Examples.Text</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>9</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>default</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Nullable>annotations</Nullable>
<IsPackable>false</IsPackable>
Expand Down
12 changes: 6 additions & 6 deletions Bearded.Graphics.Examples/12.Text/UVVertexData.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using System.Runtime.InteropServices;
using Bearded.Graphics.Vertices;
using OpenTK.Mathematics;
Expand All @@ -11,12 +12,11 @@ namespace Bearded.Graphics.Examples.Text
private readonly Vector3 position;
private readonly Vector2 uv;

public VertexAttribute[] VertexAttributes => vertexAttributes;

private static VertexAttribute[] vertexAttributes { get; } = MakeAttributeArray(
MakeAttributeTemplate<Vector3>("v_position"),
MakeAttributeTemplate<Vector2>("v_uv")
);
public static ImmutableArray<VertexAttribute> VertexAttributes { get; }
= MakeAttributeArray(
MakeAttributeTemplate<Vector3>("v_position"),
MakeAttributeTemplate<Vector2>("v_uv")
);

public UVVertexData(Vector3 position, Vector2 uv)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<OutputType>Exe</OutputType>
<RootNamespace>Bearded.Graphics.Examples.Mandelbrot</RootNamespace>
<AssemblyName>Bearded.Graphics.Examples.Mandelbrot</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>9</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>default</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Nullable>annotations</Nullable>
<IsPackable>false</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Bearded.Graphics.ImageSharp</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Bearded.Graphics.SkiaSharp</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Bearded.Graphics.System.Drawing</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion Bearded.Graphics/Bearded.Graphics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>annotations</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 6 additions & 13 deletions Bearded.Graphics/Core/Vertices/IVertexData.cs
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; }
}
62 changes: 34 additions & 28 deletions Bearded.Graphics/Core/Vertices/VertexAttribute.cs
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}}}";
}
9 changes: 9 additions & 0 deletions Bearded.Graphics/Core/Vertices/VertexAttributeFormat.cs
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 Bearded.Graphics/Core/Vertices/VertexAttributeTemplate.cs
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);
}
Loading

0 comments on commit 3ff71f9

Please sign in to comment.