└── 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 |
| WGSL | 14 |GLSL | 15 |Note | 16 |
|---|---|---|
| bool | 19 |bool | 20 |true or false | 21 |
| i32 | 24 |int | 25 |32-bit signed integer | 26 |
| u32 | 29 |uint | 30 |32-bit unsigned integer | 31 |
| f32 | 34 |float | 35 |Single-precision float | 36 |
| Not available | 39 |double | 40 |Double-precision float | 41 |
| WGSL | 49 |GLSL | 50 |Note | 51 |
|---|---|---|
| vecN<bool> | 54 |bvecN | 55 |Vector of N ∈ {2, 3, 4} bools. | 56 |
| vecN<i32> | 59 |ivecN | 60 |Vector of N ∈ {2, 3, 4} signed integers. | 61 |
| vecN<u32> | 64 |uvecN | 65 |Vector of N ∈ {2, 3, 4} unsigned integers. | 66 |
| vecN<f32> | 69 |vecN | 70 |Vector of N ∈ {2, 3, 4} floats. | 71 |
| Not available | 74 |dvecN | 75 |Vector of N ∈ {2, 3, 4} double-precision vectors. | 76 |
| matNxM<f32> | 79 |matNxM, matN | 80 |Matrix of NxM ∈ {2, 3, 4} columns and rows (or NxN). | 81 |
| WGSL | 89 |GLSL | 90 |Note | 91 |
|---|---|---|
| var m: i32 = 4; | 94 |int m = 4; | 95 |var creates a local variable. | 96 |
| let m: i32 = 4; | 99 |Not available? | 100 |let creates an immutable binding. | 101 |
| WGSL | 109 |GLSL | 110 |Note | 111 |
|---|---|---|
| 123 | 114 |123 (when used in int context) | 115 |116 | |
| 123u, 123u32 | 119 |123 (when used in uint context) | 120 |WGSL does not infer int type from context; you need to be explicit. | 121 |
| WGSL | 131 |GLSL | 132 |
|---|---|
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 | |
149 |
150 | switch (my_var) {
151 | case 0:
152 | case 1:
153 | foo = 1;
154 | break;
155 | case 2:
156 | foo = 4;
157 | }
158 | |
159 |
| WGSL | 169 |GLSL | 170 |
|---|---|
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 | |
196 |
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 | |
212 |