Rendered from AUTHORING.md at commit af54ddad5. The package is imported here by its 0.2.0 name, typeshade.
Functions and entry points
After this page you can declare a helper, call it from another function, write an entry point for the vertex, fragment or compute stage, and assemble the module that carries them.
Declaring a function
A function declaration authors every function in a shader, both plain helpers and the
entry points a pipeline runs. Its parameters are typed, and its body is ordinary
TypeScript.
"use typeshade";
// Helper: the return type is f32, the type the body returns.export function distToSegment(p: vec2, a: vec2, b: vec2): f32 { const ab = b - a; const len2 = dot(ab, ab); const t = clamp(dot(p - a, ab) / max(len2, 1e-10), 0., 1.); const segDist = length(p - a - ab * t); return len2 < 1e-10 ? length(p - a) : segDist;}A parameter may carry a name that GLSL reserves, such as sample, filter or texture.
The IR keeps the name and the GLSL backend renames it at emit. A local function, a
function declared inside another, and a function that takes a function are TypeScript
shapes the surface takes too (docs/use-typeshade-surface.md §14).
Return types
Leave the return type out and it is the type the body returns, as TypeScript infers it.
Every return is checked against that type, so a wrongly typed return is a compile error.
A body that returns nothing is void.
"use typeshade";
declare const outputB: storage<array<f32>, "read_write">;
// Inferred: dot() yields f32, so luma returns f32.export function luma(c: vec3) { return dot(c, vec3(0.2126, 0.7152, 0.0722));}
// Inferred too: this one writes into a storage buffer and returns no value, so it is void.export function store(i: u32, v: f32) { outputB[i] = v;}
// Written: a return from inside a branch is an ordinary return.export function firstHit(d: f32): f32 { if (d < 0.) { return 0.; } return d;}Calling a function
A call passes its arguments by position, as TypeScript does, and the compiler checks their number and their types against the declaration. A function the file declares or imports wins over a builtin of the same name.
const d = distToSegment(uv, p0, p1);A default parameter value is filled in at the call site, and a function may take another
function as a parameter, which every call names where it is in scope
(docs/use-typeshade-surface.md §14). Recursion is refused, because WGSL has no call stack.
Entry points
An entry point is a top-level function declaration with exactly one stage attribute written
before it: @vertex, @fragment or @compute([…]). An entry takes its declaration’s name. A
compute entry takes its workgroup size as the attribute’s argument, @compute([64]) or
@compute([8, 8]). A bare @compute takes the default of 64. The stage call of change 0111
(export const main = compute([64], function main(…) { … })) is refused with TS8028, and so
is export default on an entry (changes 0130 and 0140). tshc migrate rewrites each to the
attribute form.
"use typeshade";
const WINDOW: u32 = 8;
declare const params: uniform<vec4u>;declare const input: storage<array<f32>>;declare const output: storage<array<f32>, "read_write">;
@compute([64])export function reduceWindows(gid: builtin<"global_invocation_id">): void { const idx = gid.x; if (idx >= params.x) { return; } const base = idx * WINDOW; let acc = 0.; for (let k: u32 = 0; k < WINDOW; k++) { acc += input[base + k]; } output[idx] = acc;}The guard leaves the function before its last statement with a plain return.
Control flow covers early exits.
A workgroup size may instead be an override<u32> name shared with a workgroup scratch
array’s outer length. Ordinary TypeScript can choose the specialization from the input
shape and pass it through program.compute(entry, { constants }), or through an imported
entry’s optional third argument, entry(bindings, groups, { constants }). A program pipeline
reports the resolved pipeline.workgroupShape. Entry calls, including calls in device.fuse(),
use the selected shape without changing the supplied workgroup count. See
docs/use-typeshade-surface.md §3,
§15,
§24 and
§69 for the source form,
limits and host call.
Stage parameters
A stage takes its inputs as parameters. builtin<"id">
declares a value the hardware supplies, such as "vertex_index", "position" or
"global_invocation_id", and means the type WGSL gives that value. A parameter with no
builtin<…> is a user-defined input: a vertex attribute on a vertex entry, and a value between
stages on a fragment entry, which takes the location of the vertex result’s field of the same
name. The compiler gives each its location; location<n, T> states the number, and a fragment
entry in a file with no vertex entry writes it. The same forms describe the fields of an IO struct,
an interface or a class both stages then share;
Layouts and resources has the interpolation
modes.
"use typeshade";
interface VsOut { pos: builtin<"position">; uv: location<0, vec2>;}
interface Gradient { top: vec4; bottom: vec4; mixBias: f32;}
declare const u: uniform<Gradient>;
@vertexexport function vsFull(idx: builtin<"vertex_index">): VsOut { let pos = vec2(-1., -1.); if (idx == 1) { pos = vec2(3., -1.); } else if (idx == 2) { pos = vec2(-1., 3.); } return { pos: vec4(pos, 0., 1.), uv: vec2((pos.x + 1.) * 0.5, (pos.y + 1.) * 0.5) };}
@fragmentexport function fsGradient(vo: VsOut): vec4 { const t = vo.uv.y + u.mixBias; const rgb = mix(u.bottom.rgb, u.top.rgb, t); return vec4(rgb, 1.);}A fragment entry that returns a plain value gets location 0, the first colour
attachment, without asking. A vertex entry that returns a plain vec4 returns the
position. An entry that returns a struct carries the attributes in the struct fields.
Assembling a module
A file is a module: its functions, its structs, its constants and its resources are what emit together. The WGSL carries every function the file declares, in the order the file declares them, since WGSL lets a function call one declared after it. Each GLSL stage carries only the functions its entry reaches, a callee before its caller, as GLSL ES 3.00 requires.
A file imports what another shader file exports, as any TypeScript module does. The file you compile and the shader files it imports, directly or through another, are one program, and the program becomes one module. An ordinary import brings the declarations your file reaches, including helpers, structs, constants and bindings; unrelated entries stay out. A pure root alias instead preserves its terminal target (see A shader module that imports another).
"use typeshade";
export function hash(p: vec2): f32 { return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);}
// clouds.shade.ts"use typeshade";
import { hash } from "./noise.shade.ts";
@fragmentexport function fs(uv: location<0, vec2>): vec4 { return vec4(hash(uv), hash(uv * 2.), 0., 1.);}A shader module that imports another has the host call that compiles such a program.
Naming the functions in a module
A function is emitted under the name you declared it with, on both targets, unless that target
cannot use the name. A function whose name a target reserves, use in WGSL for instance, is
emitted as use_ on that target. An entry point named like a builtin is emitted under a fresh
name too. The host still creates the pipeline by the name you wrote, since the record names both.
The compiler gives a helper another name in two more cases. A helper named like a builtin, a
fract of your own, is emitted as fract_, and every call through the declaration with it,
because WGSL would hide the builtin and GLSL ES 3.00 refuses the redeclaration. Two files
of one program that each declare a private helper of one name emit the second under a name
of its own. The CPU oracle calls each function by the name the file gave it.
docs/use-typeshade-surface.md §62 has the names each target reserves.