Rendered from AUTHORING.md at commit af54ddad5. The package is imported here by its 0.2.0 name, typeshade.
Migrating a GLSL shader
After this page you can take the GLSL construct in front of you, look up how it is spelled
in a "use typeshade" file, and see what that spelling turns into on each target. The rest
of this guide is ordered for someone writing a new shader. This page is ordered for someone
holding a working GLSL ES 3.00 shader and asking what each line becomes.
The construct table
A construct here is one piece of GLSL source you have to account for: a uniform block, a
preprocessor branch, an #include, an extension directive. Find the row, follow the
spelling to the page that explains it, and read the note for what changes.
| GLSL construct | "use typeshade" spelling | WGSL result | Notes |
|---|---|---|---|
uniform Block { … } name; | declare const name: uniform<Block> | @group/@binding var<uniform> | the std140 layout comes from reflect(), so no offset is counted by hand |
a loose uniform float u_x; | a field of a uniform struct | a field of a var<uniform> struct | declare const u_x: uniform<f32> is not supported yet by the GLSL backend (Rule 10.3); use a struct |
| a symbol or a block the host prelude declares | none; the IR builder’s externVar, hostUniform and hostBlock | the same reference, spelled per target | docs/ir-builder.md has them |
in / out / inout on a function parameter | none / an object the function writes | a pointer parameter | an object is the caller’s, a value is returned |
#ifdef FEATURE where this module decides | one file per variant | no preprocessor | the variant that does not declare a binding never has it |
#ifdef FEATURE where the host decides | variantFamily | one module per point in the matrix | emitGuarded generates the #if ladder for a GLSL host that owns the define, and every arm is byte-identical to the standalone variant |
| a variant that changes only a value | override<T> with overrideValues | override plus pipeline constants | building a separate variant for this multiplies pipelines for nothing |
#include "helper.glsl" | import from another shader file | one module | for a host that composes its own program, emitGlslFragment and emitFragment return a module fragment |
precision mediump float; | the floatPrecision emit option | nothing, since WGSL has no precision qualifiers | it sets the stage-wide default at emit |
usampler2D and isampler2D | texture_2d<u32> and texture_2d<i32> | texture_2d<u32> and texture_2d<i32> | the sampler precision line is emitted for you |
#extension … : require | the use that needs it, or "enable …" | enable …; | fails closed with SD0030 on a backend whose profile has no row. That page also says which capabilities survive on WGSL |
| comparing two emits after an optimizer pass | semanticDiff | the same | compares IR and reflection, so folding and renaming do not drown the diff. Declare the production plugins as transforms and their rewrites classify into explained |
A block this module owns is the most common first row. Declare the struct and the binding, and read the fields by name:
"use typeshade";
interface Camera { u_matrix: mat4; u_viewport_px: vec2;}
// uniform Camera { mat4 u_matrix; vec2 u_viewport_px; } u_camera;declare const u_camera: uniform<Camera>;
@vertexexport function vs(pos: vec2): vec4 { return u_camera.u_matrix * vec4(pos / u_camera.u_viewport_px, 0., 1.);}Builtin values
A builtin is a value the hardware supplies to a stage. The vocabulary for them is WGSL’s,
written as the type builtin<"id"> on a parameter or a struct field, so a gl_* spelling or a typo
is an error that names TypeShade’s spelling. Each backend then spells the id its own way.
| GLSL global | "use typeshade" spelling | Notes |
|---|---|---|
gl_Position | builtin<"position"> on the vertex output | writes gl_Position on GLSL. A vertex entry that returns a bare vec4 returns the position |
gl_FragCoord | builtin<"position"> on a fragment input | reads gl_FragCoord on GLSL. Mind the y origin: GL window space is bottom-left and WGSL framebuffer space is top-left, so flip per target, or derive a y-symmetric value, before consuming .y |
gl_VertexID | vi: builtin<"vertex_index"> | GLSL wraps the read as uint(gl_VertexID), since the surface types it u32 and GLSL’s is int |
gl_InstanceID | ii: builtin<"instance_index"> | the same uint() wrap |
gl_FrontFacing | front: builtin<"front_facing"> | |
gl_FragDepth | builtin<"frag_depth"> on a field of the return struct | |
gl_PointSize and gl_PointCoord | unsupported on both writers | point size caps vary per vendor, and WebGPU point primitives are always one pixel. Expand an instanced quad in the vertex stage and interpolate a vec2 corner uv |
float mod(x, y) | the builtin mod() | that is floor-mod. % is trunc-mod, which is WGSL’s semantics and spells portably on GLSL too. Pick by the semantics you mean on negative operands |
A fragment stage reads the framebuffer coordinate through the same position builtin the
vertex stage writes, and writes the depth through a field of its return struct:
"use typeshade";
interface FragOut { color: vec4; depth: builtin<"frag_depth">;}
@fragmentexport function fsCoord(pos: builtin<"position">): FragOut { return { color: vec4(pos.x * 0.001, pos.y * 0.001, 0., 1.), depth: 0.5 };}Targeting WebGL2 only
Nothing on this page narrows what the GLSL writer can express. The neutral names are
spellings, and several of the rules behind them exist to make WebGL2 output more defined:
round emits roundEven, and float % emits a trunc-mod that GLSL ES 3.00 compiles.
A file that uses a form the GLSL backend does not support yet, a scalar uniform, a "read_write"
storage binding in a draw or { interpolate: "linear" }, still compiles: compile() keeps the WGSL, leaves
glsl undefined and says why in a TS8015 warning. The reverse has no spelling. A GLSL
construct the language has no spelling for yet, gl_PointSize for one, is not supported yet in a
"use typeshade" file. An engine that must emit one builds that statement with the IR
builder’s raw statements, which docs/ir-builder.md describes, with
what a raw statement costs the rest of the module.