Repository navigation
SDL: vertex layouts from a source generator instead of reflection - #39
Merged
Merged
Conversation
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.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.What changes
[GpuVertex]on apartialstruct makes it implementIGpuVertex: oneGpuVertexElement(location, format, offset) per instance field, in order. Offsets are measured from the struct itself, so explicit layouts work too.SetVertexAttributes<TVertex>()now requiresTVertex : unmanaged, IGpuVertex. No reflection, no[DynamicallyAccessedMembers].float,int,uint,Vector2,Vector3,Vector4,Rgba8U(UByte4Norm),Rgba32F.partial(BLSDL0001), unsupported field type (BLSDL0002), auto-property (BLSDL0003), no fields (BLSDL0004), generic struct (BLSDL0005).src/cs/production/SDL.Generators, netstandard2.0) ships insidebottlenoselabs.SDLatanalyzers/dotnet/cs. No new package. The games insrc/cs/gamesreference it as an analyzer.Breaking: vertex types need
[GpuVertex]andpartial. Nothing is stable yet, so the reflection version is removed.Tests
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 runsdotnet testafter the build (global.jsonselects Microsoft.Testing.Platform).TestVertexuses[GpuVertex]; its NativeAOT Release build, against the packed SDL, still prints "Vertex attributes: 2".Ran locally on macOS:
dotnet build+dotnet testforsrc/csin Release, andTEST_SCOPE=platform src/cs/tests/Consumers/test.sh.