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;
22
23#[derive(JSTraceable, MallocSizeOf)]
24struct DroppableGPURenderBundle {
25    #[no_trace]
26    channel: WebGPU,
27    #[no_trace]
28    render_bundle: WebGPURenderBundle,
29}
30
31impl Drop for DroppableGPURenderBundle {
32    fn drop(&mut self) {
33        if let Err(e) = self
34            .channel
35            .0
36            .send(WebGPURequest::DropRenderBundle(self.render_bundle.0))
37        {
38            warn!(
39                "Failed to send DropRenderBundle ({:?}) ({})",
40                self.render_bundle.0, e
41            );
42        }
43    }
44}
45
46#[dom_struct]
47pub struct GPURenderBundle<D: DomTypes> {
48    reflector_: Reflector,
49    #[no_trace]
50    device: WebGPUDevice,
51    label: DomRefCell<USVString>,
52    droppable: DroppableGPURenderBundle,
53    #[no_trace = "PhantomData does not exist"]
54    phantom: PhantomData<D>,
55}
56
57impl<D> GPURenderBundle<D>
58where
59    D: DomTypes<GPURenderBundle = GPURenderBundle<D>>,
60{
61    fn new_inherited(
62        render_bundle: WebGPURenderBundle,
63        device: WebGPUDevice,
64        channel: WebGPU,
65        label: USVString,
66    ) -> Self {
67        Self {
68            reflector_: Reflector::new(),
69            device,
70            label: DomRefCell::new(label),
71            droppable: DroppableGPURenderBundle {
72                channel,
73                render_bundle,
74            },
75            phantom: PhantomData,
76        }
77    }
78
79    pub fn new(
80        cx: &mut JSContext,
81        global: &D::GlobalScope,
82        render_bundle: WebGPURenderBundle,
83        device: WebGPUDevice,
84        channel: WebGPU,
85        label: USVString,
86    ) -> DomRoot<Self> {
87        reflect_dom_object_with_wrap::<D, _, _>(
88            Box::new(GPURenderBundle::new_inherited(
89                render_bundle,
90                device,
91                channel,
92                label,
93            )),
94            global,
95            cx,
96            GPURenderBundleWrap::<D>,
97        )
98    }
99}
100
101impl<D: DomTypes> GPURenderBundle<D> {
102    pub fn id(&self) -> WebGPURenderBundle {
103        self.droppable.render_bundle
104    }
105}
106
107impl<D: DomTypes> GPURenderBundleMethods<D> for GPURenderBundle<D> {
108    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
109    fn Label(&self) -> USVString {
110        self.label.borrow().clone()
111    }
112
113    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
114    fn SetLabel(&self, no_gc: &NoGC, value: USVString) {
115        *self.label.safe_borrow_mut(no_gc) = value;
116    }
117}