script/dom/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 dom_struct::dom_struct;
6use webgpu_traits::{WebGPU, WebGPUDevice, WebGPURenderBundle, WebGPURequest};
7
8use crate::dom::bindings::cell::DomRefCell;
9use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPURenderBundleMethods;
10use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::bindings::str::USVString;
13use crate::dom::globalscope::GlobalScope;
14use crate::script_runtime::CanGc;
15
16#[dom_struct]
17pub(crate) struct GPURenderBundle {
18    reflector_: Reflector,
19    #[no_trace]
20    channel: WebGPU,
21    #[no_trace]
22    device: WebGPUDevice,
23    #[no_trace]
24    render_bundle: WebGPURenderBundle,
25    label: DomRefCell<USVString>,
26}
27
28impl GPURenderBundle {
29    fn new_inherited(
30        render_bundle: WebGPURenderBundle,
31        device: WebGPUDevice,
32        channel: WebGPU,
33        label: USVString,
34    ) -> Self {
35        Self {
36            reflector_: Reflector::new(),
37            render_bundle,
38            device,
39            channel,
40            label: DomRefCell::new(label),
41        }
42    }
43
44    pub(crate) fn new(
45        global: &GlobalScope,
46        render_bundle: WebGPURenderBundle,
47        device: WebGPUDevice,
48        channel: WebGPU,
49        label: USVString,
50        can_gc: CanGc,
51    ) -> DomRoot<Self> {
52        reflect_dom_object(
53            Box::new(GPURenderBundle::new_inherited(
54                render_bundle,
55                device,
56                channel,
57                label,
58            )),
59            global,
60            can_gc,
61        )
62    }
63}
64
65impl GPURenderBundle {
66    pub(crate) fn id(&self) -> WebGPURenderBundle {
67        self.render_bundle
68    }
69}
70
71impl GPURenderBundleMethods<crate::DomTypeHolder> for GPURenderBundle {
72    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
73    fn Label(&self) -> USVString {
74        self.label.borrow().clone()
75    }
76
77    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
78    fn SetLabel(&self, value: USVString) {
79        *self.label.borrow_mut() = value;
80    }
81}
82
83impl Drop for GPURenderBundle {
84    fn drop(&mut self) {
85        if let Err(e) = self
86            .channel
87            .0
88            .send(WebGPURequest::DropRenderBundle(self.render_bundle.0))
89        {
90            warn!(
91                "Failed to send DropRenderBundle ({:?}) ({})",
92                self.render_bundle.0, e
93            );
94        }
95    }
96}