TypeShade language guide
Learn how to write TypeShade shaders using TypeScript syntax. This guide introduces types, functions, resources and shader stages with examples you can compile and run.
1. What is use typeshade?
"use typeshade" marks a TypeShade shader file. The compiler applies TypeShade rules for GPU types, resources, stages and builtin inputs.
"use typeshade"The directive must appear first in the file.
2. TypeScript syntax
TypeShade uses TypeScript syntax for variables, functions, type aliases, classes, conditionals and loops. The compiler accepts only values and operations it can lower to GPU code.
| TypeScript | TypeShade | GPU meaning |
|---|---|---|
| function | helper or entry point | The compiler lowers supported function bodies to GPU code. |
| type | GPU value type | The compiler checks it against GPU type rules. |
| class | GPU struct | It defines a struct; it does not create a JavaScript object. |
| if / for | GPU control flow | The compiler checks conditions and loops for GPU support. |
3. GPU interface
The source declares the GPU interface. Use declare for resources, stage attributes for entry points, and builtin<N> parameters for GPU inputs.
declare const camera: uniform<Camera>declare const pixels: storage<array<f32>, "read_write">
@compute([64, 1, 1])export function paint(gid: builtin<"global_invocation_id">) { const i = gid.x pixels[i] = pixels[i] + camera.pos.x}The shader does not create camera or pixels; the host provides them. gid is a function parameter, not a global.
4. Functions and entry points
Parameters and return types define a function’s inputs and result. Stage attributes and builtin parameters define an entry point’s GPU role.
function addBias(value: f32): f32 { return value + 0.5}
@compute([64, 1, 1])export function paint(gid: builtin<"global_invocation_id">) { pixels[gid.x] = addBias(camera.pos.x)}addBias is a reusable helper. paint is a compute entry point. They use the same function syntax but have different pipeline roles.
5. GPU resources
uniform<T> and storage<T> name GPU resources supplied by the host. declare marks resources that the shader does not create.
declare const camera: uniform<Camera>declare const source: storage<f32>declare const pixels: storage<f32, "read_write">Declare each resource with const. A storage<T> binding is read-only until an entry writes it, and then the compiler makes it read-write.
6. Shader stages
@vertex, @fragment and @compute([64]) mark an entry point’s stage. Declare GPU builtins as parameters; the compiler does not inject them as globals.
@vertexexport function vs(vid: builtin<"vertex_index">): vec4 { return positions[vid]}
@fragmentexport function fs(): vec4 { return vec4(1, 0, 0, 1)}7. Complete example
This example combines the directive, a GPU struct, resources, a compute entry point and a builtin parameter.
"use typeshade";
class Camera { view: mat4; pos: vec3;}
declare const camera: uniform<Camera>;declare const pixels: storage<array<f32>, "read_write">;
@compute([64, 1, 1])export function paint(gid: builtin<"global_invocation_id">) { const i = gid.x; pixels[i] = pixels[i] + camera.pos.x;}8. TypeScript and TypeShade
| TypeScript | TypeShade |
|---|---|
declare | Names a GPU resource supplied by the host. |
| function parameter | Holds a GPU value or a stage builtin input. |
| class | Defines a GPU struct and its field metadata. |
| number | Inferred from its uses. Write f32, i32 or u32 to fix a width. |
TypeShade keeps TypeScript syntax and gives it GPU semantics. JavaScript runtime behavior does not carry over automatically.
9. Further reading
Read the TypeScript Handbook for types and functions, review the JavaScript execution model, then continue with the GPU concepts.
- TypeScript Handbook — Everyday Types
- TypeScript Handbook — Functions
- MDN — JavaScript Guide
- The Book of Shaders
10. Learning path
Start with Quick start, then read Types, Functions, Control flow, GPU types, Resources and Shader stages in order. Quick start