redjs-webgpu 0.4.0

Native WebGPU for redjs over wgpu. A crate of its own so that redjs and wgpu compile in parallel.
Documentation
1
2
3
4
5
6
7
8
9
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
43
44
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Render bundles. Commands are recorded with owned resources and replayed
//! into a native bundle encoder at finish(), whose lifetime borrows them.
use super::*;
use std::ops::Range;

enum Command {
    Pipeline(wgpu::RenderPipeline),
    BindGroup(u32, Option<wgpu::BindGroup>, Vec<u32>),
    Vertex(u32, Option<(Arc<buffer::Buffer>, Range<u64>)>),
    Index(Arc<buffer::Buffer>, wgpu::IndexFormat, Range<u64>),
    Draw(Range<u32>, Range<u32>),
    DrawIndexed(Range<u32>, i32, Range<u32>),
    DrawIndirect(Arc<buffer::Buffer>, u64),
    DrawIndexedIndirect(Arc<buffer::Buffer>, u64),
    Immediates(u32, Vec<u8>),
}

pub(super) struct Encoder {
    color_formats: Vec<Option<wgpu::TextureFormat>>,
    depth_stencil: Option<wgpu::RenderBundleDepthStencil>,
    sample_count: u32,
    commands: Vec<Command>,
    resources: Vec<Resource>,
    // wgpu bundles carry no debug groups; their balance is still validated.
    debug_depth: u32,
    unbalanced: bool,
    finished: bool,
}

pub(super) struct Bundle {
    pub raw: Option<wgpu::RenderBundle>,
    pub resources: Vec<Resource>,
    pub valid: bool,
}

#[redjs::host_fn]
unsafe fn create(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let h = this(g, cf, Kind::Device)?;
        let c = client(g, &h)?;
        let d = Val::new(g, cf_arg(cf, 0));
        let label = text(g, field(g, d.v, "label")?.v, "")?;
        let formats = field(g, d.v, "colorFormats")?;
        if formats.v == UNDEF {
            invalid(
                g,
                "GPURenderBundleEncoderDescriptor.colorFormats is required",
            )?;
        }
        let mut color_formats = Vec::new();
        for f in seq(g, formats.v)? {
            color_formats.push(if f.v == NULL || f.v == UNDEF {
                None
            } else {
                Some(texture::parse_format(g, f.v)?)
            });
        }
        let depth = field(g, d.v, "depthStencilFormat")?;
        let depth_stencil = if depth.v == UNDEF {
            None
        } else {
            Some(wgpu::RenderBundleDepthStencil {
                format: texture::parse_format(g, depth.v)?,
                depth_read_only: boolean(g, field(g, d.v, "depthReadOnly")?.v),
                stencil_read_only: boolean(g, field(g, d.v, "stencilReadOnly")?.v),
            })
        };
        let sample_count = uint(g, field(g, d.v, "sampleCount")?.v, 1)?;
        if color_formats.len() > c.limits.max_color_attachments as usize {
            validation(
                g,
                &c,
                "Render bundle color formats exceed maxColorAttachments",
            );
        }
        Ok(wrap_labeled(
            g,
            Kind::BundleEncoder,
            Some(c),
            Resource::BundleEncoder(Arc::new(Mutex::new(Encoder {
                color_formats,
                depth_stencil,
                sample_count,
                commands: Vec::new(),
                resources: Vec::new(),
                debug_depth: 0,
                unbalanced: false,
                finished: false,
            }))),
            &label,
        ))
    })
}

unsafe fn encoder_this(
    g: *mut c_void,
    cf: *mut c_void,
) -> Result<(Arc<Client>, Arc<Mutex<Encoder>>), ()> {
    let h = this(g, cf, Kind::BundleEncoder)?;
    let c = client(g, &h)?;
    let Resource::BundleEncoder(e) = h.resource else {
        unreachable!()
    };
    Ok((c, e))
}

unsafe fn record(
    g: *mut c_void,
    c: &Arc<Client>,
    encoder: &Arc<Mutex<Encoder>>,
    command: Command,
    resources: &[Resource],
) {
    let mut e = encoder.lock().unwrap();
    if e.finished {
        drop(e);
        validation(g, c, "GPURenderBundleEncoder is already finished");
        return;
    }
    e.commands.push(command);
    e.resources.extend_from_slice(resources);
}

#[redjs::host_fn]
unsafe fn set_pipeline(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let h = owned(g, cf_arg(cf, 0), Kind::RenderPipeline, &c)?;
        let Resource::RenderPipeline(p) = h.resource else {
            unreachable!()
        };
        record(g, &c, &e, Command::Pipeline(p), &[]);
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn set_bind_group(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let index = uint(g, cf_arg(cf, 0), 0)?;
        let group = if cf_arg(cf, 1) == NULL {
            None
        } else {
            let h = owned(g, cf_arg(cf, 1), Kind::BindGroup, &c)?;
            let Resource::BindGroup(b) = h.resource else {
                unreachable!()
            };
            Some(b)
        };
        let offsets = compute::dynamic_offsets(g, cf, 2)?;
        let retained: Vec<_> = group.iter().cloned().map(Resource::BindGroup).collect();
        record(
            g,
            &c,
            &e,
            Command::BindGroup(index, group.map(|b| b.raw), offsets),
            &retained,
        );
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn set_vertex_buffer(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let slot = uint(g, cf_arg(cf, 0), 0)?;
        if cf_arg(cf, 1) == NULL {
            record(g, &c, &e, Command::Vertex(slot, None), &[]);
            return Ok(UNDEF);
        }
        let (b, range) = texture::buffer_range(g, cf, &c, 1, 2, 3)?;
        let retained = [Resource::Buffer(b.clone())];
        record(
            g,
            &c,
            &e,
            Command::Vertex(slot, Some((b, range))),
            &retained,
        );
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn set_index_buffer(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let format = texture::parse_index(g, cf_arg(cf, 1))?;
        let (b, range) = texture::buffer_range(g, cf, &c, 0, 2, 3)?;
        let retained = [Resource::Buffer(b.clone())];
        record(g, &c, &e, Command::Index(b, format, range), &retained);
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn draw(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let count = uint(g, cf_arg(cf, 0), 0)?;
        let instances = uint(g, cf_arg(cf, 1), 1)?;
        let first = uint(g, cf_arg(cf, 2), 0)?;
        let first_instance = uint(g, cf_arg(cf, 3), 0)?;
        let vertices = texture::draw_range(g, first, count)?;
        let instances = texture::draw_range(g, first_instance, instances)?;
        record(g, &c, &e, Command::Draw(vertices, instances), &[]);
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn draw_indexed(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let count = uint(g, cf_arg(cf, 0), 0)?;
        let instances = uint(g, cf_arg(cf, 1), 1)?;
        let first = uint(g, cf_arg(cf, 2), 0)?;
        let base = texture::signed(g, cf_arg(cf, 3), 0)?;
        let first_instance = uint(g, cf_arg(cf, 4), 0)?;
        let indices = texture::draw_range(g, first, count)?;
        let instances = texture::draw_range(g, first_instance, instances)?;
        record(
            g,
            &c,
            &e,
            Command::DrawIndexed(indices, base, instances),
            &[],
        );
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn draw_indirect(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let (b, offset) = texture::indirect_buffer(g, cf, &c)?;
        let retained = [Resource::Buffer(b.clone())];
        record(g, &c, &e, Command::DrawIndirect(b, offset), &retained);
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn draw_indexed_indirect(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let (b, offset) = texture::indirect_buffer(g, cf, &c)?;
        let retained = [Resource::Buffer(b.clone())];
        record(
            g,
            &c,
            &e,
            Command::DrawIndexedIndirect(b, offset),
            &retained,
        );
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn set_immediates(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        if let Some((offset, bytes)) = compute::immediates(g, cf, &c)? {
            record(g, &c, &e, Command::Immediates(offset, bytes), &[]);
        }
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn push_debug_group(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        text(g, cf_arg(cf, 0), "undefined")?;
        let mut e = e.lock().unwrap();
        if e.finished {
            drop(e);
            validation(g, &c, "GPURenderBundleEncoder is already finished");
        } else {
            e.debug_depth += 1;
        }
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn pop_debug_group(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let mut e = e.lock().unwrap();
        if e.finished {
            drop(e);
            validation(g, &c, "GPURenderBundleEncoder is already finished");
        } else if e.debug_depth == 0 {
            e.unbalanced = true;
        } else {
            e.debug_depth -= 1;
        }
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn insert_debug_marker(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        text(g, cf_arg(cf, 0), "undefined")?;
        if e.lock().unwrap().finished {
            validation(g, &c, "GPURenderBundleEncoder is already finished");
        }
        Ok(UNDEF)
    })
}

#[redjs::host_fn]
unsafe fn finish(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, e) = encoder_this(g, cf)?;
        let d = Val::new(g, cf_arg(cf, 0));
        let label = text(g, field(g, d.v, "label")?.v, "")?;
        let (formats, depth_stencil, sample_count, commands, resources, balanced, finished) = {
            let mut e = e.lock().unwrap();
            let finished = std::mem::replace(&mut e.finished, true);
            (
                std::mem::take(&mut e.color_formats),
                e.depth_stencil.clone(),
                e.sample_count,
                std::mem::take(&mut e.commands),
                std::mem::take(&mut e.resources),
                !e.unbalanced && e.debug_depth == 0,
                finished,
            )
        };
        if finished {
            validation(g, &c, "GPURenderBundleEncoder is already finished");
        } else if !balanced {
            validation(
                g,
                &c,
                "GPURenderBundleEncoder debug groups are not balanced",
            );
        }
        // wgpu treats a bundle validation failure as fatal and panics; WebGPU
        // makes it a validation error and an invalid bundle.
        let replay = std::panic::AssertUnwindSafe(|| {
            let mut encoder = c.context.device.create_render_bundle_encoder(
                &wgpu::RenderBundleEncoderDescriptor {
                    label: Some(&label),
                    color_formats: &formats,
                    depth_stencil,
                    sample_count,
                    multiview: None,
                },
            );
            for command in &commands {
                match command {
                    Command::Pipeline(p) => encoder.set_pipeline(p),
                    Command::BindGroup(index, group, offsets) => {
                        encoder.set_bind_group(*index, group.as_ref(), offsets)
                    }
                    Command::Vertex(slot, None) => {
                        encoder.set_vertex_buffer(*slot, None::<wgpu::BufferSlice<'_>>)
                    }
                    Command::Vertex(slot, Some((b, range))) => {
                        encoder.set_vertex_buffer(*slot, b.raw.slice(range.clone()))
                    }
                    Command::Index(b, format, range) => {
                        encoder.set_index_buffer(b.raw.slice(range.clone()), *format)
                    }
                    Command::Draw(vertices, instances) => {
                        encoder.draw(vertices.clone(), instances.clone())
                    }
                    Command::DrawIndexed(indices, base, instances) => {
                        encoder.draw_indexed(indices.clone(), *base, instances.clone())
                    }
                    Command::DrawIndirect(b, offset) => encoder.draw_indirect(&b.raw, *offset),
                    Command::DrawIndexedIndirect(b, offset) => {
                        encoder.draw_indexed_indirect(&b.raw, *offset)
                    }
                    Command::Immediates(offset, bytes) => encoder.set_immediates(*offset, bytes),
                }
            }
            encoder.finish(&wgpu::RenderBundleDescriptor {
                label: Some(&label),
            })
        });
        let raw = match checked(g, &c, || std::panic::catch_unwind(replay)) {
            Ok(raw) => Some(raw),
            Err(payload) => {
                let message = payload
                    .downcast_ref::<String>()
                    .map(String::as_str)
                    .or_else(|| payload.downcast_ref::<&str>().copied())
                    .unwrap_or("Render bundle validation failed");
                validation(
                    g,
                    &c,
                    &message.split_whitespace().collect::<Vec<_>>().join(" "),
                );
                None
            }
        };
        let valid = raw.is_some() && balanced && !finished;
        Ok(wrap_labeled(
            g,
            Kind::Bundle,
            Some(c),
            Resource::Bundle(Arc::new(Bundle {
                raw,
                resources,
                valid,
            })),
            &label,
        ))
    })
}

#[redjs::host_fn]
unsafe fn execute_bundles(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let (c, pass) = texture::pass_this(g, cf)?;
        let mut bundles = Vec::new();
        for v in seq(g, cf_arg(cf, 0))? {
            let h = owned(g, v.v, Kind::Bundle, &c)?;
            let Resource::Bundle(b) = h.resource else {
                unreachable!()
            };
            bundles.push(b);
        }
        if bundles.iter().any(|b| !b.valid) {
            validation(g, &c, "executeBundles was given an invalid GPURenderBundle");
            return Ok(UNDEF);
        }
        let retained: Vec<_> = bundles.iter().cloned().map(Resource::Bundle).collect();
        texture::retain_pass(g, &pass, &retained)?;
        texture::with_pass(g, &c, &pass, |raw| {
            raw.execute_bundles(bundles.iter().filter_map(|b| b.raw.as_ref()))
        })?;
        Ok(UNDEF)
    })
}

pub(super) unsafe fn install(g: *mut c_void) {
    class(
        g,
        Kind::BundleEncoder,
        "GPURenderBundleEncoder",
        &[
            ("setPipeline", 1, set_pipeline),
            ("setBindGroup", 2, set_bind_group),
            ("setVertexBuffer", 2, set_vertex_buffer),
            ("setIndexBuffer", 2, set_index_buffer),
            ("setImmediates", 2, set_immediates),
            ("draw", 1, draw),
            ("drawIndexed", 1, draw_indexed),
            ("drawIndirect", 2, draw_indirect),
            ("drawIndexedIndirect", 2, draw_indexed_indirect),
            ("pushDebugGroup", 1, push_debug_group),
            ("popDebugGroup", 0, pop_debug_group),
            ("insertDebugMarker", 1, insert_debug_marker),
            ("finish", 0, finish),
        ],
        &[],
    );
    class(g, Kind::Bundle, "GPURenderBundle", &[], &[]);
    method(g, Kind::Device, "createRenderBundleEncoder", 1, create);
    method(g, Kind::RenderPass, "executeBundles", 1, execute_bundles);
}