Skip to content

Add portable shader instance IDs - #34

Merged
treeform merged 1 commit into
masterfrom
agent/add-portable-instance-id
Aug 13, 2026
Merged

treeform merged 1 commit into
masterfrom
agent/add-portable-instance-id

Conversation

@treeform

Copy link
Copy Markdown
Owner

Summary

  • expose gl_InstanceID as Shady's portable instance identifier
  • map it to gl_InstanceID for desktop/Web GLSL, gl_InstanceIndex
    for Vulkan GLSL, SV_InstanceID for HLSL, and [[instance_id]]
    for Metal
  • map Vulkan's vertex identifier to gl_VertexIndex
  • allow local shader object declarations without emitting invalid synthetic
    constructors
  • cover portable identifiers and local parameter structs in backend codegen
    tests
  • compile the Metal instance-ID path with Apple's native shader compiler

Why

Stateless GPU particle systems and other instanced vertex shaders need a
per-instance identifier without uploading a CPU-generated ID buffer. Every
supported graphics API provides this concept, but each backend uses a
different builtin spelling or semantic.

Impact

Shader authors can use gl_InstanceID once in Shady source and generate valid
desktop GLSL, WebGL 2 GLSL, Vulkan GLSL, HLSL, and Metal output. Complex
instanced shaders can also assemble and pass flat local parameter structs
without Shady inventing an unsupported default constructor.

Root cause

Shady exposed gl_VertexID but had no shared instance-ID builtin or target
mappings. Vulkan also inherited OpenGL's vertex builtin name, while HLSL and
Metal entry generation did not inject their native instance semantics.

Validation

  • nim r tests/test_backends.nim
  • nim r tests/test_shady.nim
  • nim r tests/test_cpu.nim
  • nim r -d:shadyRunMetal tests/test_metal.nim

@treeform
treeform marked this pull request as ready for review August 13, 2026 03:44
@treeform
treeform merged commit abd3f2f into master Aug 13, 2026
1 check passed
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