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