Skip to main content

script_webgpu/
gpurenderbundle.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::marker::PhantomData;
6
7use dom_struct::dom_struct;
8use js::context::{JSContext, NoGC};
9use jstraceable_derive::JSTraceable;
10use log::warn;
11use malloc_size_of_derive::MallocSizeOf;
12use script_bindings::DomTypes;
13use script_bindings::cell::DomRefCell;
14use script_bindings::codegen::GenericBindings::WebGPUBinding::{
15    GPURenderBundleMethods, GPURenderBundleWrap,
16};
17use script_bindings::reflector::{Reflector, reflect_dom_object_with_wrap};
18use webgpu_traits::{WebGPU, WebGPUDevice, WebGPURenderBundle, WebGPURequest};
19
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::bindings::str::USVString;
22use crate::traits::Equivalence;
23
24#[derive(JSTraceable, MallocSizeOf)]
25struct DroppableGPURenderBundle {
26    #[no_trace]
27    channel: WebGPU,
28    #[no_trace]
29    render_bundle: WebGPURenderBundle,
30}
31
32impl Drop for DroppableGPURenderBundle {
33    fn drop(&mut self) {
34        if let Err(e) = self
35            .channel
36            .0
37            .send(WebGPURequest::DropRenderBundle(self.render_bundle.0))
38        {
39            warn!(
40                "Failed to send DropRenderBundle ({:?}) ({})",
41                self.render_bundle.0, e
42            );
43        }
44    }
45}
46
47#[dom_struct]
48pub struct GPURenderBundle<D: DomTypes> {
49    reflector_: Reflector,
50    #[no_trace]
51    device: WebGPUDevice,
52    label: DomRefCell<USVString>,
53    droppable: DroppableGPURenderBundle,
54    #[no_trace = "PhantomData does not exist"]
55    phantom: PhantomData<D>,
56}
57
58impl<D: Equivalence> GPURenderBundle<D> {
59    fn new_inherited(
60        render_bundle: WebGPURenderBundle,
61        device: WebGPUDevice,
62        channel: WebGPU,
63        label: USVString,
64    ) -> Self {
65        Self {
66            reflector_: Reflector::new(),
67            device,
68            label: DomRefCell::new(label),
69            droppable: DroppableGPURenderBundle {
70                channel,
71                render_bundle,
72            },
73            phantom: PhantomData,
74        }
75    }
76
77    pub fn new(
78        cx: &mut JSContext,
79        global: &D::GlobalScope,
80        render_bundle: WebGPURenderBundle,
81        device: WebGPUDevice,
82        channel: WebGPU,
83        label: USVString,
84    ) -> DomRoot<Self> {
85        reflect_dom_object_with_wrap::<D, _, _>(
86            Box::new(GPURenderBundle::new_inherited(
87                render_bundle,
88                device,
89                channel,
90                label,
91            )),
92            global,
93            cx,
94            GPURenderBundleWrap::<D>,
95        )
96    }
97}
98
99impl<D: DomTypes> GPURenderBundle<D> {
100    pub fn id(&self) -> WebGPURenderBundle {
101        self.droppable.render_bundle
102    }
103}
104
105impl<D: DomTypes> GPURenderBundleMethods<D> for GPURenderBundle<D> {
106    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
107    fn Label(&self) -> USVString {
108        self.label.borrow().clone()
109    }
110
111    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
112    fn SetLabel(&self, no_gc: &NoGC, value: USVString) {
113        *self.label.safe_borrow_mut(no_gc) = value;
114    }
115}