Declarations
Global Declarations
Declarations outside any @stage block are global and are emitted to every stage's GLSL output.
Struct
struct Material {
vec3 albedo;
float roughness;
float metallic;
};Emitted as-is to all stages that are compiled.
Constant
const float PI = 3.14159;
const vec3 UP = vec3(0.0, 1.0, 0.0);Global constants are available in all stages.
Global Uniform (Simple)
@binding(0) uniform mat4 view_projection;With dead-code elimination, this uniform will only appear in stages that actually reference it.
Global Uniform Block
@binding(0) uniform CameraData {
mat4 view;
mat4 projection;
vec3 position;
} camera;Block uniforms follow the same dead-code elimination rules as simple uniforms.
Global Buffer (SSBO)
@std430 @binding(1) buffer LightBuffer {
vec4 positions[];
vec4 colors[];
} lights;Interface Definition
interface Varyings {
vec2 v_uv;
vec3 v_normal;
};Not emitted directly. Used as a template when referenced by stages. See Interface System.
Global Function
vec3 gamma_correct(vec3 color) {
return pow(color, vec3(1.0 / 2.2));
}
float luminance(vec3 color) {
return dot(color, vec3(0.2126, 0.7152, 0.0722));
}Global functions are available to all stages.
Function Declarations
Basic Function
float square(float x) {
return x * x;
}Function with Qualifiers
void transform(in vec3 pos, out vec4 result) {
result = vec4(pos, 1.0);
}
void modify(inout vec3 value) {
value = normalize(value);
}Function with No Return
void setup_state() {
// no return statement needed
}Uniform Declarations
Simple Uniform
uniform mat4 u_transform;
uniform float u_time;
uniform sampler2D u_texture;Uniform with Default Value
uniform float u_near = 0.1;
uniform float u_far = 100.0;
uniform bool u_enabled = true;Default values are emitted as initializers in GLSL.
Uniform Block
@std140 @binding(0) uniform SceneData {
mat4 view;
mat4 projection;
vec3 camera_pos;
float time;
} scene;Access fields with: scene.view, scene.camera_pos, etc.
Uniform Block Without Instance Name
@std140 @binding(0) uniform SceneData {
mat4 view;
mat4 projection;
};Access fields directly: view, projection (no prefix).
Buffer Declarations (SSBOs)
Runtime-Sized Array
@std430 @binding(2) buffer InstanceBuffer {
mat4 models[];
} instances;Access with: instances.models[index]
Multiple Fields
@std430 @binding(0) buffer ParticleBuffer {
vec4 positions[];
vec4 velocities[];
float lifetimes[];
} particles;Struct Declarations
Basic Struct
struct PointLight {
vec3 position;
vec3 color;
float intensity;
float radius;
};Nested Structs
struct Material {
vec3 albedo;
float roughness;
};
struct Surface {
Material material;
vec3 normal;
vec2 uv;
};Struct with Arrays
struct Cluster {
vec3 min_bounds;
vec3 max_bounds;
uint light_indices[64];
uint light_count;
};Interface Blocks
Input Interface (Inline)
in Attributes {
@location(0) vec3 a_pos;
@location(1) vec2 a_uv;
@location(2) vec3 a_normal;
} a;Output Interface (Inline)
out VertexOut {
vec2 v_uv;
vec3 v_normal;
vec3 v_world_pos;
} v;Interface Reference
out Varyings v; // References global 'interface Varyings'Variable Declarations
Local Variables (in functions)
void main() {
float x = 0.0;
vec3 position;
int count = 10;
for (int i = 0; i < count; i++) {
// ...
}
}Multiple Declarations
float a = 1.0, b = 2.0, c = 3.0; // Not recommendedWARNING
While technically supported, declaring multiple variables in one statement is not recommended for clarity.
Array Declarations
Fixed-Size Arrays
uniform PointLight lights[4];
float coefficients[16];Runtime-Sized Arrays (SSBO only)
@std430 @binding(0) buffer Data {
float values[]; // unbounded array
} data;Runtime-sized arrays can only appear as the last field in a buffer block.
Complete Example
@version 450;
// Global struct
struct Material {
vec3 albedo;
float roughness;
};
// Global constant
const float PI = 3.14159;
// Global uniform
@binding(0) uniform mat4 u_view_projection;
// Global function
vec3 gamma(vec3 c) {
return pow(c, vec3(1.0 / 2.2));
}
// Global interface
interface Varyings {
vec2 v_uv;
vec3 v_normal;
};
@stage vertex {
// Stage-local input
in Attributes {
@location(0) vec3 a_pos;
@location(1) vec2 a_uv;
} a;
// Reference global interface
out Varyings v;
// Stage-local uniform
@binding(1) uniform mat4 u_model;
void main() {
v.v_uv = a.a_uv;
v.v_normal = vec3(0.0, 1.0, 0.0);
gl_Position = u_view_projection * u_model * vec4(a.a_pos, 1.0);
}
}
@stage fragment {
in Varyings v;
out vec4 o_color;
uniform Material u_material;
void main() {
o_color = vec4(gamma(u_material.albedo), 1.0);
}
}