Skip to content

Cheat sheet for WGSL syntax for developers coming from GLSL.

Notifications You must be signed in to change notification settings

paulgb/wgsl-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

WGSL Cheat Sheet

This is a reference to WGSL syntax for users coming from GLSL. It is not meant to be complete, but to cover some common usages.

WGSL is still evolving, so some things may change before the final version. This reference is based on the WGSL draft spec.

Types

Primitives

WGSL GLSL Note
bool bool true or false
i32 int 32-bit signed integer
u32 uint 32-bit unsigned integer
f32 float Single-precision float
Not available double Double-precision float

Composite Types

WGSL GLSL Note
vecN<bool> bvecN Vector of N ∈ {2, 3, 4} bools.
vecN<i32> ivecN Vector of N ∈ {2, 3, 4} signed integers.
vecN<u32> uvecN Vector of N ∈ {2, 3, 4} unsigned integers.
vecN<f32> vecN Vector of N ∈ {2, 3, 4} floats.
Not available dvecN Vector of N ∈ {2, 3, 4} double-precision vectors.
matNxM<f32> matNxM, matN Matrix of NxM ∈ {2, 3, 4} columns and rows (or NxN).

Variables

WGSL GLSL Note
var m: i32 = 4; int m = 4; var creates a local variable.
let m: i32 = 4; Not available? let creates an immutable binding.

Literals

WGSL GLSL Note
123 123 (when used in int context)
123u, 123u32 123 (when used in uint context) WGSL does not infer int type from context; you need to be explicit.

switch syntax

WGSL needs explicit fallthrough to not break at the end of a switch block. my_var must be a u32 (I think?).

WGSL GLSL
switch (my_var) {
  case 0: {
    fallthrough;
  }
  case 1: {
    foo = 1;
  }
  case 2: {
    foo = 4;
  }
}
switch (my_var) {
  case 0:
  case 1:
    foo = 1;
    break;
  case 2:
    foo = 4;
}

Shader Function Structure

Vertex Shader

WGSL GLSL
// Structure of vertex shader output
struct VertexOutput {
	[[builtin(position)]] position: vec4<f32>;
	[[location(0)]] baz;
};
	
// Vertex shader function
[[stage(vertex)]]
fn vs_main(
	[[location(0)]] foo: vec2<f32>,
	[[location(1)]] bar: vec4<f32>,
) -> VertexOutput {
	var out: VertexOutput;
	if (foo.x > foo.y) {
		discard;
	}
	out.baz = vec4<f32>(0.0, 1.0, 0.0, 1.0);
	out.position = bar;
	return out;
}
// Inputs from vertex buffer.
layout(location=0) in vec2 foo;
layout(location=1) in vec4 bar;
	
// Output to frag shader.
layout(location=0) out vec4 baz;
	
void main() {
	if (foo.x > foo.y) {
		discard;
	}
	baz = vec4(0.0, 1.0, 0.0, 1.0);
	gl_Position = bar;
}

About

Cheat sheet for WGSL syntax for developers coming from GLSL.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages