└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # WGSL Cheat Sheet 2 | 3 | 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. 4 | 5 | WGSL is still evolving, so some things may change before the final version. This reference is based on the [WGSL draft spec](https://www.w3.org/TR/WGSL/). 6 | 7 | ## Types 8 | 9 | ### Primitives 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
WGSLGLSLNote
boolbooltrue or false
i32int32-bit signed integer
u32uint32-bit unsigned integer
f32floatSingle-precision float
Not availabledoubleDouble-precision float
43 | 44 | ### Composite Types 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 |
WGSLGLSLNote
vecN<bool>bvecNVector of N ∈ {2, 3, 4} bools.
vecN<i32>ivecNVector of N ∈ {2, 3, 4} signed integers.
vecN<u32>uvecNVector of N ∈ {2, 3, 4} unsigned integers.
vecN<f32>vecNVector of N ∈ {2, 3, 4} floats.
Not availabledvecNVector of N ∈ {2, 3, 4} double-precision vectors.
matNxM<f32>matNxM, matNMatrix of NxM ∈ {2, 3, 4} columns and rows (or NxN).
83 | 84 | ## Variables 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 |
WGSLGLSLNote
var m: i32 = 4;int m = 4;var creates a local variable.
let m: i32 = 4;Not available?let creates an immutable binding.
103 | 104 | ## Literals 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 |
WGSLGLSLNote
123123 (when used in int context)
123u, 123u32123 (when used in uint context)WGSL does not infer int type from context; you need to be explicit.
123 | 124 | ## `switch` syntax 125 | 126 | WGSL needs explicit `fallthrough` to not `break` at the end of a switch block. `my_var` must be a `u32` (I think?). 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 149 | 159 | 160 |
WGSLGLSL
135 |
136 | switch (my_var) {
137 |   case 0: {
138 |     fallthrough;
139 |   }
140 |   case 1: {
141 |     foo = 1;
142 |   }
143 |   case 2: {
144 |     foo = 4;
145 |   }
146 | }
147 | 
148 |
150 | switch (my_var) {
151 |   case 0:
152 |   case 1:
153 |     foo = 1;
154 |     break;
155 |   case 2:
156 |     foo = 4;
157 | }
158 | 
161 | 162 | ## Shader Function Structure 163 | 164 | ### Vertex Shader 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 196 | 212 | 213 |
WGSLGLSL
173 |
174 | // Structure of vertex shader output
175 | struct VertexOutput {
176 | 	[[builtin(position)]] position: vec4<f32>;
177 | 	[[location(0)]] baz;
178 | };
179 | 	
180 | // Vertex shader function
181 | [[stage(vertex)]]
182 | fn vs_main(
183 | 	[[location(0)]] foo: vec2<f32>,
184 | 	[[location(1)]] bar: vec4<f32>,
185 | ) -> VertexOutput {
186 | 	var out: VertexOutput;
187 | 	if (foo.x > foo.y) {
188 | 		discard;
189 | 	}
190 | 	out.baz = vec4<f32>(0.0, 1.0, 0.0, 1.0);
191 | 	out.position = bar;
192 | 	return out;
193 | }
194 | 
195 |
197 | // Inputs from vertex buffer.
198 | layout(location=0) in vec2 foo;
199 | layout(location=1) in vec4 bar;
200 | 	
201 | // Output to frag shader.
202 | layout(location=0) out vec4 baz;
203 | 	
204 | void main() {
205 | 	if (foo.x > foo.y) {
206 | 		discard;
207 | 	}
208 | 	baz = vec4(0.0, 1.0, 0.0, 1.0);
209 | 	gl_Position = bar;
210 | }
211 | 
214 | --------------------------------------------------------------------------------