Skip to content

SDL: vertex layouts from a source generator instead of reflection - #39

Merged
lithiumtoast merged 1 commit into
mainfrom
sdl-vertex-generator
Dec 1, 2026
Merged

lithiumtoast merged 1 commit into
mainfrom
sdl-vertex-generator

Conversation

@lithiumtoast

Copy link
Copy Markdown
Member

SetVertexAttributes<TVertex>() read the vertex type's fields with reflection, which needed trimming annotations for NativeAOT and only failed at run time. Now a source generator does it at compile time.

[GpuVertex]
internal partial struct Vertex
{
    public Vector3 Position;   // location 0, float3
    public Rgba8U Color;       // location 1, 4 bytes read as 0 to 1
}

pipelineOptions.SetVertexAttributes<Vertex>();

What changes

  • [GpuVertex] on a partial struct makes it implement IGpuVertex: one GpuVertexElement (location, format, offset) per instance field, in order. Offsets are measured from the struct itself, so explicit layouts work too.
  • SetVertexAttributes<TVertex>() now requires TVertex : unmanaged, IGpuVertex. No reflection, no [DynamicallyAccessedMembers].
  • Supported field types: float, int, uint, Vector2, Vector3, Vector4, Rgba8U (UByte4Norm), Rgba32F.
  • Compile errors instead of exceptions at startup: not partial (BLSDL0001), unsupported field type (BLSDL0002), auto-property (BLSDL0003), no fields (BLSDL0004), generic struct (BLSDL0005).
  • Packaging: the generator (src/cs/production/SDL.Generators, netstandard2.0) ships inside bottlenoselabs.SDL at analyzers/dotnet/cs. No new package. The games in src/cs/games reference it as an analyzer.

Breaking: vertex types need [GpuVertex] and partial. Nothing is stable yet, so the reflection version is removed.

Tests

  • New src/cs/tests/SDL.Tests (xUnit v3), 14 tests: layouts of real [GpuVertex] structs (padding, explicit offsets, nested, read-only and private fields), SetVertexAttributes, and each compile error. CI now runs dotnet test after the build (global.json selects Microsoft.Testing.Platform).
  • The game consumer's TestVertex uses [GpuVertex]; its NativeAOT Release build, against the packed SDL, still prints "Vertex attributes: 2".

Ran locally on macOS: dotnet build + dotnet test for src/cs in Release, and TEST_SCOPE=platform src/cs/tests/Consumers/test.sh.

Mark a partial struct with [GpuVertex] and a source generator, shipped
inside bottlenoselabs.SDL, makes it implement IGpuVertex at compile
time: one vertex element per field, with its format and offset.
SetVertexAttributes<TVertex>() now requires IGpuVertex and reads that
layout, so it needs no reflection or trimming annotations.

Unsupported field types, auto-properties and non-partial structs are
compile errors (BLSDL0001 to BLSDL0005). Adds unit tests (SDL.Tests),
which CI now runs with dotnet test.
@lithiumtoast
lithiumtoast merged commit a94c4cb into main Dec 1, 2026
28 checks passed
@lithiumtoast
lithiumtoast deleted the sdl-vertex-generator branch December 1, 2026 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant