This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
SDL-cs_gpu_examples is a C# GPU graphics programming examples project using SDL3-CS bindings. It demonstrates modern GPU programming patterns with SDL3 for .NET 10. Based on TheSpydog/SDL_gpu_examples.
# Build
dotnet build SDL-cs_gpu_examples.sln
# Run all examples
dotnet run --project SDL-cs_gpu_examples
# Run a specific example
dotnet run --project SDL-cs_gpu_examples -- <ExampleName>Available examples: ClearScreen, ClearScreenMultiWindow, BasicTriangle, BasicVertexBuffer, TexturedQuad, TexturedAnimatedQuad, CustomSampling, BlitMirror, GenerateMipmaps, Latency, BasicCompute, ComputeUniforms, ComputeSampler, CopyAndReadback, CopyConsistency, CullMode, BasicStencil, InstancedIndexed, WindowResize, TriangleMSAA, Clear3DSlice, DrawIndirect, Texture2DArray, Cubemap, Blit2DArray, BlitCube, DepthSampler, PullSpriteBatch, ComputeSpriteBatch
- Examples registered as
ExampleInfo(string Name, Func<int> Run)records inAllExamplesarray - CLI takes optional example name argument; runs all if none specified
- Initializes SDL ShaderCross at startup and cleans up on exit
- Vertex structs with
[StructLayout(LayoutKind.Sequential)]for GPU interop:PositionVertex,PositionColorVertex,PositionTextureVertex,PositionTextureColorVertex,ComputeSpriteInstance
- LoadShader() - Runtime shader compilation using ShaderCross:
- Reads HLSL source from
Content/Shaders/ - Compiles HLSL to SPIRV via
ShaderCross.CompileSPIRVFromHLSL() - Creates GPU shader via
ShaderCross.CompileGraphicsShaderFromSPIRV()(handles cross-compilation to DXIL/MSL internally)
- Reads HLSL source from
Each example is a static class with Main() method following this flow:
- SDL init with Video subsystem
- GPU device creation (with SPIRV | DXIL | MSL format flags)
- Window creation and claiming for GPU
- Event loop with command buffer acquire → render pass → submit
- Explicit resource cleanup
- Source files:
Content/Shaders/*.hlsl - Runtime compilation via SDL ShaderCross (HLSL → SPIRV → backend format)
- ShaderCross handles cross-compilation to the appropriate backend: D3D12 (DXIL), Vulkan (SPIRV), Metal (MSL)
- All GPU handles are
IntPtr; check forIntPtr.Zeroafter creation - Use
Marshal.AllocHGlobal/FreeHGlobalorSDL.StructureToPointer<T>()for GPU struct marshaling - Shader stage detected from filename convention (
.vert.hlsl,.frag.hlsl,.comp.hlsl) unsafeblocks required for GPU pointer operations; cast(nint)(&struct)for uniform data- Use
System.Numerics.Matrix4x4for matrix math (not custom structs) - Project supports Native AOT compilation (
PublishAot: true)
CompileComputePipelineFromSPIRV: C# binding incorrectly takesGraphicsShaderMetadatainstead ofComputePipelineMetadata. Workaround: direct P/Invoke inCommon.csDownloadFromGPUBuffer: C# binding incorrectly types second parameter asGPUTextureRegioninstead ofGPUBufferRegion. Workaround: direct P/Invoke inCopyAndReadback.cs
- SDL3-CS: C# bindings for SDL3
- SDL3-CS.Native: Native SDL3 libraries
- SDL3-CS.Native.Shadercross: SDL ShaderCross for runtime HLSL compilation and cross-compilation