Skip to main content

mln_webgpu/
command_encoder.rs

1// Ports include/mln/webgpu/command_encoder.hpp and src/mln/webgpu/command_encoder.cpp
2//
3// Debug groups are no-ops in C++ ("handled at the render pass level"); the
4// names are tracked for `visitDebugGroups` only.
5//
6// wgpu vs. Dawn: a render pass is recorded on the encoder through
7// `wgpu::RenderPass::forget_lifetime`, so passes are owned objects; wgpu then
8// rejects `finish()` while a pass is still open, as WebGPU requires (the
9// renderer drops its pass before `present`).
10
11use crate::context::Context;
12use crate::render_pass::RenderPass;
13use crate::renderable_resource::as_webgpu;
14use crate::upload_pass::UploadPass;
15use mln_gfx::command_encoder;
16use mln_gfx::debug_group::DebugGroupTarget;
17use mln_gfx::render_pass::RenderPassDescriptor;
18use mln_gfx::renderable::Renderable;
19use std::any::Any;
20use std::cell::{RefCell, RefMut};
21use std::rc::Rc;
22
23pub struct CommandEncoder {
24    context: Rc<Context>,
25    encoder: RefCell<Option<wgpu::CommandEncoder>>,
26    debug_group_names: RefCell<Vec<String>>,
27}
28
29fn new_encoder(context: &Context) -> Option<wgpu::CommandEncoder> {
30    let device = context.get_backend().get_device()?;
31    Some(device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("MapLibre Command Encoder") }))
32}
33
34impl CommandEncoder {
35    pub fn new(context: Rc<Context>) -> Self {
36        let encoder = new_encoder(&context);
37        CommandEncoder { context, encoder: RefCell::new(encoder), debug_group_names: RefCell::new(Vec::new()) }
38    }
39
40    pub fn get_context(&self) -> &Rc<Context> {
41        &self.context
42    }
43
44    /// `getEncoder()`
45    pub fn get_encoder(&self) -> RefMut<'_, Option<wgpu::CommandEncoder>> {
46        self.encoder.borrow_mut()
47    }
48
49    pub fn visit_debug_groups(&self, mut f: impl FnMut(&str)) {
50        for name in self.debug_group_names.borrow().iter() {
51            f(name);
52        }
53    }
54
55    /// WebGPU-specific: submit the command buffer directly, then start a new
56    /// encoder so subsequent passes in the same frame can continue recording.
57    pub fn submit_command_buffer(&self) {
58        let backend = self.context.get_backend();
59        let (Some(_device), Some(queue)) = (backend.get_device(), backend.get_queue()) else {
60            return;
61        };
62        let Some(encoder) = self.encoder.borrow_mut().take() else {
63            return;
64        };
65        // Finish and submit the command buffer
66        let command_buffer = encoder.finish();
67        queue.submit([command_buffer]);
68        // Create a new command encoder (C++ also updates
69        // context.setCurrentCommandEncoder).
70        *self.encoder.borrow_mut() = new_encoder(&self.context);
71    }
72}
73
74impl DebugGroupTarget for CommandEncoder {
75    fn push_debug_group(&self, name: &str) {
76        // Debug markers are currently handled at the render pass level.
77        self.debug_group_names.borrow_mut().push(name.to_owned());
78    }
79    fn pop_debug_group(&self) {
80        self.debug_group_names.borrow_mut().pop();
81    }
82}
83
84impl command_encoder::CommandEncoder for CommandEncoder {
85    fn create_upload_pass(&self, name: &str, renderable: &dyn Renderable) -> Box<dyn mln_gfx::upload_pass::UploadPass> {
86        Box::new(UploadPass::new(renderable, self, name))
87    }
88
89    fn create_render_pass(
90        &self,
91        name: &str,
92        descriptor: &RenderPassDescriptor<'_>,
93    ) -> Box<dyn mln_gfx::render_pass::RenderPass> {
94        Box::new(RenderPass::new(self, name, descriptor))
95    }
96
97    fn present(&self, renderable: &dyn Renderable) {
98        // Submit the command buffer first
99        self.submit_command_buffer();
100        // Then call swap on the RenderableResource to present the surface.
101        // (C++ catches the exception thrown when there is no resource.)
102        if let Some(resource) = renderable.get_resource()
103            && let Some(resource) = as_webgpu(&*resource)
104        {
105            resource.swap();
106        }
107    }
108
109    fn as_any(&self) -> &dyn Any {
110        self
111    }
112}