Rendered from AUTHORING.md at commit af54ddad5. The package is imported here by its 0.2.0 name, typeshade.
Your first shader
After this page you have a shader written in TypeScript with two entry points, the same module emitted once as WGSL for WebGPU and once as GLSL ES 3.00 for WebGL2, and you know which call produced each string. The shader fills the screen with a colour gradient.
The file starts with the directive, and everything after it is the shader:
"use typeshade";The names a shader uses come from the language, not from an import. f32, u32, vec2
and vec4 are types where a declaration needs a type, and vec2(…) and vec4(…) build a
value where an expression needs one. Arithmetic is TypeScript’s own operators: x * 4. - 1.
is a multiply and a subtract.
A vertex entry point
An entry point is a function the GPU calls directly: once per vertex, once per fragment,
or once per compute invocation. This page uses the first two. You mark one with a stage
attribute before a top-level function declaration, @vertex, @fragment or @compute([64]):
@vertex export function vs(…) { … }. Every other function you write is a plain helper. The
stage call of change 0111, export const vs = vertex(function vs(…) { … });, is refused
(TS8028, changes 0130 and 0140). tshc migrate rewrites it to the attribute.
The vertex stage runs once per vertex. It produces the clip space position and whatever
the fragment stage needs from it, packed in an IO struct: an interface (or a class or a type). builtin<"position"> marks the value the hardware itself
consumes. Every other field, such as uv: vec2, is a value that is interpolated across the triangle and read back by the
fragment stage.
interface VsOut { pos: builtin<"position">; uv: vec2;}WGSL fixes the type of every builtin but clip_distances, so you write no type: builtin<"position">
is a vec4, and builtin<"vertex_index"> a u32. The compiler gives uv its location, @location(0)
here. Write location<n, T> only to fix a number or to state an option.
This shader has no vertex buffer. It draws three vertices that cover the screen, and it derives their positions from the vertex index alone. A stage input is an ordinary parameter with the same attribute:
@vertexexport function vs(vi: builtin<"vertex_index">): VsOut { const x = f32(vi & 1) * 4. - 1.; const y = f32(vi >> 1) * 4. - 1.; return { pos: vec4(x, y, 0., 1.), uv: vec2(x * 0.5 + 0.5, y * 0.5 + 0.5) };}vi is a u32, and f32(vi & 1) converts it. A number written without a decimal point
takes the integer type of the operand beside it, so vi & 1 needs no u32(1); a number
written with one, 4., is an f32. The object literal takes the struct the return type
declares, so a missing or misspelt field is an error.
A fragment entry point
The fragment stage runs once per candidate pixel and returns a colour. It takes the vertex
output as a parameter, so the fields declared on VsOut are what it reads: vo.uv is a
vec2 with .x and .y on it.
@fragmentexport function fs(vo: VsOut): vec4 { const shade = 1. - vo.uv.y; return vec4(vo.uv.x, shade, 0.5, 1.);}A bare fragment return goes to location 0, the first colour attachment, so nothing is written for it. A struct return carries one field per colour attachment, numbered in the order written.
Assembling the module
A module is the unit that emits, and a file is one. Everything the file declares is in it: the struct, the two entries and any helper they call. Put together, the file reads:
"use typeshade";
interface VsOut { pos: builtin<"position">; uv: vec2;}
@vertexexport function vs(vi: builtin<"vertex_index">): VsOut { const x = f32(vi & 1) * 4. - 1.; const y = f32(vi >> 1) * 4. - 1.; return { pos: vec4(x, y, 0., 1.), uv: vec2(x * 0.5 + 0.5, y * 0.5 + 0.5) };}
@fragmentexport function fs(vo: VsOut): vec4 { const shade = 1. - vo.uv.y; return vec4(vo.uv.x, shade, 0.5, 1.);}A file that imports another shader file is one module with it: Functions and entry points has the form.
Emitting both targets
compile(source) returns the whole module as one WGSL string, both entries included:
import { compile } from 'typeshade'
const { wgsl, glsl, diagnostics } = compile(source) // source: the file above, as textstruct VsOut { @builtin(position) pos: vec4<f32>, @location(0) uv: vec2<f32>,}
@vertexfn vs(@builtin(vertex_index) vi: u32) -> VsOut { let x = ((f32((vi & 1u)) * 4.0) - 1.0); let y = ((f32((vi >> 1u)) * 4.0) - 1.0); return VsOut(vec4<f32>(x, y, 0.0, 1.0), vec2<f32>(((x * 0.5) + 0.5), ((y * 0.5) + 0.5)));}
@fragmentfn fs(vo: VsOut) -> @location(0) vec4<f32> { let shade = (1.0 - vo.uv.y); return vec4<f32>(vo.uv.x, shade, 0.5, 1.0);}GLSL ES 3.00 compiles one stage at a time, each source with its own main, so GLSL comes
back one string per stage. glsl is { vertex, fragment }, from a single lowering of the
module, the pass that rewrites the IR into the shapes the target can spell:
#version 300 esprecision highp float;precision highp int;
out vec2 uv;
void main() { uint vi = uint(gl_VertexID); float x = ((float((vi & 1u)) * 4.0) - 1.0); float y = ((float((vi >> 1u)) * 4.0) - 1.0); gl_Position = vec4(x, y, 0.0, 1.0); uv = vec2(((x * 0.5) + 0.5), ((y * 0.5) + 0.5));}#version 300 esprecision highp float;precision highp int;
in vec2 uv;layout(location = 0) out vec4 _ret;
void main() { float shade = (1.0 - uv.y); _ret = vec4(uv.x, shade, 0.5, 1.0);}The source was the same for both strings. The differences between the two outputs are the
ones each language forces: the IO struct becomes a matching out and in pair of
varyings, the fragment return becomes a declared output variable, the builtin position
becomes gl_Position, and gl_VertexID is a signed int so the u32 parameter picks up a
cast. diagnostics is empty for this file; a file with an error has no wgsl and no
glsl, and its diagnostics say why.
Where to go next
Values and mutation covers const and let, and
how a variable is changed.
Functions and entry points has the rest of
what a function and an entry point take, including the compute stage.
Layouts and resources is how a uniform block, a
storage buffer or a texture is declared once and read everywhere.
Emitting and reflection covers the other emit
calls and the pipeline metadata a host binds from.