Skip to content

Declarations

Global Declarations

Declarations outside any @stage block are global and are emitted to every stage's GLSL output.

Struct

axsl
struct Material {
    vec3 albedo;
    float roughness;
    float metallic;
};

Emitted as-is to all stages that are compiled.

Constant

axsl
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)

axsl
@binding(0) uniform mat4 view_projection;

With dead-code elimination, this uniform will only appear in stages that actually reference it.

Global Uniform Block

axsl
@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)

axsl
@std430 @binding(1) buffer LightBuffer {
    vec4 positions[];
    vec4 colors[];
} lights;

Interface Definition

axsl
interface Varyings {
    vec2 v_uv;
    vec3 v_normal;
};

Not emitted directly. Used as a template when referenced by stages. See Interface System.

Global Function

axsl
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

axsl
float square(float x) {
    return x * x;
}

Function with Qualifiers

axsl
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

axsl
void setup_state() {
    // no return statement needed
}

Uniform Declarations

Simple Uniform

axsl
uniform mat4 u_transform;
uniform float u_time;
uniform sampler2D u_texture;

Uniform with Default Value

axsl
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

axsl
@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

axsl
@std140 @binding(0) uniform SceneData {
    mat4 view;
    mat4 projection;
};

Access fields directly: view, projection (no prefix).

Buffer Declarations (SSBOs)

Runtime-Sized Array

axsl
@std430 @binding(2) buffer InstanceBuffer {
    mat4 models[];
} instances;

Access with: instances.models[index]

Multiple Fields

axsl
@std430 @binding(0) buffer ParticleBuffer {
    vec4 positions[];
    vec4 velocities[];
    float lifetimes[];
} particles;

Struct Declarations

Basic Struct

axsl
struct PointLight {
    vec3 position;
    vec3 color;
    float intensity;
    float radius;
};

Nested Structs

axsl
struct Material {
    vec3 albedo;
    float roughness;
};

struct Surface {
    Material material;
    vec3 normal;
    vec2 uv;
};

Struct with Arrays

axsl
struct Cluster {
    vec3 min_bounds;
    vec3 max_bounds;
    uint light_indices[64];
    uint light_count;
};

Interface Blocks

Input Interface (Inline)

axsl
in Attributes {
    @location(0) vec3 a_pos;
    @location(1) vec2 a_uv;
    @location(2) vec3 a_normal;
} a;

Output Interface (Inline)

axsl
out VertexOut {
    vec2 v_uv;
    vec3 v_normal;
    vec3 v_world_pos;
} v;

Interface Reference

axsl
out Varyings v;  // References global 'interface Varyings'

Variable Declarations

Local Variables (in functions)

axsl
void main() {
    float x = 0.0;
    vec3 position;
    int count = 10;

    for (int i = 0; i < count; i++) {
        // ...
    }
}

Multiple Declarations

axsl
float a = 1.0, b = 2.0, c = 3.0;  // Not recommended

WARNING

While technically supported, declaring multiple variables in one statement is not recommended for clarity.

Array Declarations

Fixed-Size Arrays

axsl
uniform PointLight lights[4];
float coefficients[16];

Runtime-Sized Arrays (SSBO only)

axsl
@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

axsl
@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);
    }
}