script_webgpu/
gpubindgrouplayout.rs1use std::borrow::Cow;
6use std::marker::PhantomData;
7
8use dom_struct::dom_struct;
9use js::context::{JSContext, NoGC};
10use jstraceable_derive::JSTraceable;
11use log::warn;
12use malloc_size_of_derive::MallocSizeOf;
13use script_bindings::DomTypes;
14use script_bindings::cell::DomRefCell;
15use script_bindings::codegen::GenericBindings::WebGPUBinding::{
16 GPUBindGroupLayoutDescriptor, GPUBindGroupLayoutMethods, GPUBindGroupLayoutWrap,
17};
18use script_bindings::interfaces::PromiseHelpers;
19use script_bindings::reflector::{DomGlobalGeneric, Reflector, reflect_dom_object_with_wrap};
20use webgpu_traits::{BindGroupLayoutDescriptor, WebGPU, WebGPUBindGroupLayout, WebGPURequest};
21
22use crate::dom::bindings::error::Fallible;
23use crate::dom::bindings::root::DomRoot;
24use crate::dom::bindings::str::USVString;
25use crate::gpuconvert::{WebGPUConvert, convert_bind_group_layout_entry};
26use crate::traits::{Equivalence, WebGPUGlobalTrait, WebGPUPromise};
27
28#[derive(JSTraceable, MallocSizeOf)]
29struct DroppableGPUBindGroupLayout {
30 #[no_trace]
31 channel: WebGPU,
32 #[no_trace]
33 bind_group_layout: WebGPUBindGroupLayout,
34}
35
36impl Drop for DroppableGPUBindGroupLayout {
37 fn drop(&mut self) {
38 if let Err(e) = self
39 .channel
40 .0
41 .send(WebGPURequest::DropBindGroupLayout(self.bind_group_layout.0))
42 {
43 warn!(
44 "Failed to send WebGPURequest::DropBindGroupLayout({:?}) ({})",
45 self.bind_group_layout.0, e
46 );
47 };
48 }
49}
50
51#[dom_struct]
52pub struct GPUBindGroupLayout<D: DomTypes> {
53 reflector_: Reflector,
54 label: DomRefCell<USVString>,
55 droppable: DroppableGPUBindGroupLayout,
56 #[no_trace = "PhantomData does not exist"]
57 phantom: PhantomData<D>,
58}
59
60impl<D: Equivalence> GPUBindGroupLayout<D> {
61 fn new_inherited(
62 channel: WebGPU,
63 bind_group_layout: WebGPUBindGroupLayout,
64 label: USVString,
65 ) -> Self {
66 Self {
67 reflector_: Reflector::new(),
68 label: DomRefCell::new(label),
69 droppable: DroppableGPUBindGroupLayout {
70 channel,
71 bind_group_layout,
72 },
73 phantom: PhantomData,
74 }
75 }
76
77 pub(crate) fn new(
78 cx: &mut JSContext,
79 global: &D::GlobalScope,
80 channel: WebGPU,
81 bind_group_layout: WebGPUBindGroupLayout,
82 label: USVString,
83 ) -> DomRoot<Self> {
84 reflect_dom_object_with_wrap::<D, _, _>(
85 Box::new(GPUBindGroupLayout::new_inherited(
86 channel,
87 bind_group_layout,
88 label,
89 )),
90 global,
91 cx,
92 GPUBindGroupLayoutWrap::<D>,
93 )
94 }
95}
96
97impl<D> GPUBindGroupLayout<D>
98where
99 D: Equivalence,
100 <D::Promise as PromiseHelpers<D>>::StackRoot: WebGPUPromise<D>,
101{
102 pub(crate) fn id(&self) -> WebGPUBindGroupLayout {
103 self.droppable.bind_group_layout
104 }
105
106 pub(crate) fn create(
108 cx: &mut JSContext,
109 device: &D::GPUDevice,
110 descriptor: &GPUBindGroupLayoutDescriptor,
111 ) -> Fallible<DomRoot<GPUBindGroupLayout<D>>> {
112 let entries = descriptor
113 .entries
114 .iter()
115 .map(|bgle| convert_bind_group_layout_entry::<D>(bgle, device))
116 .collect::<Fallible<Result<Vec<_>, _>>>()?;
117
118 let desc = match entries {
119 Ok(entries) => Some(BindGroupLayoutDescriptor {
120 label: (&descriptor.parent).convert(),
121 entries: Cow::Owned(entries),
122 }),
123 Err(error) => {
124 device.dispatch_error(error);
125 None
126 },
127 };
128
129 let global = device.global_from_reflector();
130 let bind_group_layout_id = global.global_wgpu_id_hub().create_bind_group_layout_id();
131 device
132 .channel()
133 .0
134 .send(WebGPURequest::CreateBindGroupLayout {
135 device_id: device.id().0,
136 bind_group_layout_id,
137 descriptor: desc,
138 })
139 .expect("Failed to create WebGPU BindGroupLayout");
140
141 let bgl = WebGPUBindGroupLayout(bind_group_layout_id);
142
143 Ok(GPUBindGroupLayout::new(
144 cx,
145 &*global,
146 device.channel(),
147 bgl,
148 descriptor.parent.label.clone(),
149 ))
150 }
151}
152
153impl<D: DomTypes> GPUBindGroupLayoutMethods<D> for GPUBindGroupLayout<D> {
154 fn Label(&self) -> USVString {
156 self.label.borrow().clone()
157 }
158
159 fn SetLabel(&self, no_gc: &NoGC, value: USVString) {
161 *self.label.safe_borrow_mut(no_gc) = value;
162 }
163}