sparkles:shaders — Specification
IMPORTANT
This revision passed its Stage 0 gate on 2026-10-20: the owner accepted its scope and contracts. It sets the library a wider goal, a single-source replacement for the shading languages, and specifies the first slice towards it, which M6 implements (PLAN.md). Until M6 delivers a requirement, the shipped behaviour is governed by the specification of 2026-10-16; requirements carried over unchanged keep their identifiers and wording, and each revised one is marked (Revised.) after its title.
Abstract
sparkles:shaders lets a D program write its GPU shaders in D, once, and run their logic in two places: on the GPU, as the shader stages of a graphics pipeline, and on the CPU, where any D compiler calls the same functions. It provides the types and built-in functions shaders are written against, entry points for the pipeline's stages, the resources a shader reads, and a build step that compiles a package's shaders for each target it declares — SPIR-V for Vulkan, GLSL for OpenGL and OpenGL ES — checking at compile time that each uses only what its target offers. Its GPU half needs LDC, the LLVM-based D compiler, extended with graphics stages. Only a build that generates shaders needs that compiler; the CPU half, and every consumer that takes shaders already generated, builds with any D compiler.
1. Introduction
Programs that draw the same interface on very different surfaces need effects and rendering that behave the same on each. A colour transform may run as a fragment shader in a GPU window, as a function called once per character cell in a terminal, and as a function applied to a few known inputs in a test. A renderer that draws through Vulkan on one platform and OpenGL ES on another needs the same shader for both. The usual answer is several implementations — a GLSL file, an HLSL file, a fallback in the host language — that drift apart, and resource layouts kept in sync with host structs by hand.
The shading languages do not close that gap. Each serves its own API: GLSL for OpenGL and Vulkan, HLSL for Direct3D, MSL for Metal, WGSL for the web, Slang across several of them. None shares code with the program that drives it, and none runs on the CPU with its GPU semantics. Host languages that compile their own code for the GPU (Rust, C++, Zig, Julia, C#) show that one source is possible, but each leaves a part undone: memory layouts left to the user, GPU-only operations that do nothing on the CPU, or a back end that cannot structure ordinary control flow (research catalog).
This library writes shaders as single-source shaders: D modules marked as device code, over a vocabulary of vector types and built-ins that compute the same results on both sides. An entry point is an ordinary D function marked with its stage; its parameters are its inputs, its resources and its built-in variables, and its result is its outputs. The device compilation goes through dcompute — LDC's support for compiling D modules for a GPU — and its Vulkan target, extended with graphics stages by a fork of LDC that this project maintains and means to upstream, to SPIR-V, which the build step validates, translates where a target needs source (GLSL for OpenGL), and validates again. Each output is a profile: a target and the capabilities it grants. A shader declares its profiles beside its entry points; the build step compiles it once per profile, with the profile a compile-time constant that D code branches on; and the compiler and the build step reject any use of a capability the profile lacks, at the D source line. Resources are declared once in D and lowered per profile — a block with a binding, or a push-constant block, for Vulkan; named uniforms for OpenGL, push constants included — with the byte layout the host writes computed from the same declaration. The generated shaders are build products, never committed; a developer's build regenerates them behind a freshness stamp, and a packager (here, the Nix build) builds them once and hands them to every build, which then needs no custom compiler.
The scope of this revision is the vertex and fragment stages, for Vulkan, OpenGL 3.3 and OpenGL ES 2.0, with uniforms, uniform blocks, push constants and sampled images. Its consumers are the effects of sparkles:ui, on OpenGL and OpenGL ES, whose output the revision leaves unchanged, and the triangle of sparkles:vulkan-wsi, the first on Vulkan. On the CPU, the slice guarantees what the accepted specification did: the vocabulary, and every entry point that samples no image, run as plain D with the same results. The library's goal is wider — every stage and feature of the shading languages, further targets (GLSL 4.60, GLSL ES 3.x, HLSL, MSL, WGSL, and DXIL later), a CPU executor that runs subgroup, quad and derivative semantics — and the delivery plan orders it; each later contract is refined before it is implemented. Outside the library altogether is anything that draws: pipelines, descriptor writes, render passes and texture binding belong to the consumer's renderer, which this library hands complete shaders, their bindings and their layouts. Runtime compilation is outside it, because the device compiler is a build-time tool. The compiler is a pinned dependency: this specification states what it requires of the compiler, not how the compiler implements it. Editor diagnostics for shader code are specified by sparkles:dmd-lsp (target profiles). decisions.md records each exclusion with what would bring it in.
Section 2 lists the terms, section 3 states the contract at a glance, and section 4 maps the topic pages that hold the requirements. testing.md names the oracle and scenarios for each requirement and holds the evidence ledger, PLAN.md orders delivery and tracks progress, and decisions.md records the consequential choices and open questions.
2. Terminology
The terms this specification coins, or uses in a narrower sense than usual, are defined once in the glossary and listed here:
device configuration
In sparkles:shaders, a dub configuration whose dflags contain -mdcompute-targets=; shaders is the conventional name. The build step and editor analysis both read a package's device compilation from it, so it is declared once and never restated per shader.
fragment entry point
Also: @fragment function
In sparkles:shaders, a function marked @fragment in a @compute module. The compiler synthesises the shader interface from its signature: @input parameters become located varyings, @uniform parameters named uniforms, Sampler2D parameters bound images, and the return value the finalColor output. The fragment stage is an extension of LDC's dcompute, which upstream compiles only compute kernels.
freshness stamp
Also: stamp
In sparkles:shaders, the .stamp file in a build step's output directory: the SHA-256 of each module of the shader unit and each contributing build recipe, and the files produced. While it still matches, a build needs neither dub nor the device compiler; a stamp reading prebuilt marks output a packager supplied.
shader unit
In sparkles:shaders, the set of modules marked @compute among the source files of a package's device configuration and all its dependencies. It is discovered from the modules' own declarations, never listed, and compiled in one invocation because a SPIR-V module has no linker.
shader vocabulary
In sparkles:shaders, the types vec2, vec3 and vec4, their constructors and component access, and GLSL's built-in functions with GLSL's definitions. It is plain D on the host and SPIR-V on the device, and every vector occupies the same bytes on both.
single-source shader
Also: single-source effect
In sparkles:shaders, a per-pixel effect whose only definition is one D function over the shader vocabulary. The host compilation calls it directly, for example once per terminal cell; the device compilation wraps it in a fragment entry point and emits GLSL. Neither side is written separately.
Profiles defines capability, profile, active profile and requirement. GPU terms follow their standards and the research catalog's concepts: SPIR-V is the Khronos intermediate language for shaders; GLSL is the OpenGL Shading Language, at version 3.30 for OpenGL 3.3 and ES 1.00 for OpenGL ES 2.0. dcompute's terms keep their meaning: the host is the CPU side of a program, the device the GPU side.
3. The contract at a glance
- One source. A shader's logic is D. Its CPU behaviour and its GPU shaders derive from the same functions; none is written separately.
- One meaning. Every vocabulary operation computes the same result on the host and the device, within the bounds of
SHV7. - Device code is opt-in by target. Without a GPU compilation target the vocabulary is plain D: no device rules apply and no special compiler is needed.
- One unit per package, one compilation per profile. A package's shader modules, with those of its dependencies, compile together, once for each profile its entry points declare.
- What a target lacks is a compile-time error. A use of a capability the active profile does not grant fails the build at the D source line that reaches it.
- Declared once, lowered per target. A resource is declared once, in D; each profile gets the form it can express, and the host's byte layout comes from the same declaration.
- Generated, never committed. Generated shaders are build products. The build that ships them is a Nix derivation, which regenerates them exactly when an input changed, the toolchain included.
- No compiler unless asked. A consumer that does not opt in needs only its ordinary D compiler.
- Complete, validated output. Every generated file is a whole shader that its target's reference validator accepts.
4. Requirements
| Page | Holds | IDs |
|---|---|---|
| Vocabulary | vector types, constructors, component access, built-in functions, agreement | SHV1–SHV11 |
| Device modules | which modules are shader code; the device compilation's rules | SHM1–SHM6 |
| Entry points | stages, parameters, built-in variables, outputs, linking stages, handles | SHF1–SHF9 |
| Resources | uniforms, blocks, push constants, images, interface structs, layout, bindings | SHR1–SHR8 |
| Profiles | capabilities, profiles, @profiles, the manifest, the three capability checks | SHC1–SHC10 |
| Build step | the build step, its outputs and outcomes; the stamp and build integration | SHP1–SHP11, SHB1–SHB7 |
| Toolchain | the pinned compiler and tools, reproducibility, platforms | SHT1–SHT4 |
| CPU executor | the outline of the executor that runs GPU semantics on the CPU | SHX1–SHX3 |
Identifiers are stable: a requirement keeps its identifier when it moves between pages, and a retired one is not reused.