script/dom/webgpu/
gpushadermodule.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::rc::Rc;
6
7use dom_struct::dom_struct;
8use webgpu_traits::{ShaderCompilationInfo, WebGPU, WebGPURequest, WebGPUShaderModule};
9
10use super::gpucompilationinfo::GPUCompilationInfo;
11use crate::dom::bindings::cell::DomRefCell;
12use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
13    GPUShaderModuleDescriptor, GPUShaderModuleMethods,
14};
15use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::USVString;
18use crate::dom::bindings::trace::RootedTraceableBox;
19use crate::dom::globalscope::GlobalScope;
20use crate::dom::promise::Promise;
21use crate::dom::types::GPUDevice;
22use crate::realms::InRealm;
23use crate::routed_promise::{RoutedPromiseListener, callback_promise};
24use crate::script_runtime::CanGc;
25
26#[derive(JSTraceable, MallocSizeOf)]
27struct DroppableGPUShaderModule {
28    #[no_trace]
29    channel: WebGPU,
30    #[no_trace]
31    shader_module: WebGPUShaderModule,
32}
33
34impl Drop for DroppableGPUShaderModule {
35    fn drop(&mut self) {
36        if let Err(e) = self
37            .channel
38            .0
39            .send(WebGPURequest::DropShaderModule(self.shader_module.0))
40        {
41            warn!(
42                "Failed to send DropShaderModule ({:?}) ({})",
43                self.shader_module.0, e
44            );
45        }
46    }
47}
48
49#[dom_struct]
50pub(crate) struct GPUShaderModule {
51    reflector_: Reflector,
52    label: DomRefCell<USVString>,
53    #[ignore_malloc_size_of = "promise"]
54    compilation_info_promise: Rc<Promise>,
55    droppable: DroppableGPUShaderModule,
56}
57
58impl GPUShaderModule {
59    fn new_inherited(
60        channel: WebGPU,
61        shader_module: WebGPUShaderModule,
62        label: USVString,
63        promise: Rc<Promise>,
64    ) -> Self {
65        Self {
66            reflector_: Reflector::new(),
67            label: DomRefCell::new(label),
68            compilation_info_promise: promise,
69            droppable: DroppableGPUShaderModule {
70                channel,
71                shader_module,
72            },
73        }
74    }
75
76    pub(crate) fn new(
77        global: &GlobalScope,
78        channel: WebGPU,
79        shader_module: WebGPUShaderModule,
80        label: USVString,
81        promise: Rc<Promise>,
82        can_gc: CanGc,
83    ) -> DomRoot<Self> {
84        reflect_dom_object(
85            Box::new(GPUShaderModule::new_inherited(
86                channel,
87                shader_module,
88                label,
89                promise,
90            )),
91            global,
92            can_gc,
93        )
94    }
95}
96
97impl GPUShaderModule {
98    pub(crate) fn id(&self) -> WebGPUShaderModule {
99        self.droppable.shader_module
100    }
101
102    /// <https://gpuweb.github.io/gpuweb/#dom-gpudevice-createshadermodule>
103    pub(crate) fn create(
104        device: &GPUDevice,
105        descriptor: RootedTraceableBox<GPUShaderModuleDescriptor>,
106        comp: InRealm,
107        can_gc: CanGc,
108    ) -> DomRoot<GPUShaderModule> {
109        let program_id = device.global().wgpu_id_hub().create_shader_module_id();
110        let promise = Promise::new_in_current_realm(comp, can_gc);
111        let shader_module = GPUShaderModule::new(
112            &device.global(),
113            device.channel(),
114            WebGPUShaderModule(program_id),
115            descriptor.parent.label.clone(),
116            promise.clone(),
117            can_gc,
118        );
119        let callback = callback_promise(
120            &promise,
121            &*shader_module,
122            device
123                .global()
124                .task_manager()
125                .dom_manipulation_task_source(),
126        );
127        device
128            .channel()
129            .0
130            .send(WebGPURequest::CreateShaderModule {
131                device_id: device.id().0,
132                program_id,
133                program: descriptor.code.0.clone(),
134                label: None,
135                callback,
136            })
137            .expect("Failed to create WebGPU ShaderModule");
138        shader_module
139    }
140}
141
142impl GPUShaderModuleMethods<crate::DomTypeHolder> for GPUShaderModule {
143    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
144    fn Label(&self) -> USVString {
145        self.label.borrow().clone()
146    }
147
148    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
149    fn SetLabel(&self, value: USVString) {
150        *self.label.borrow_mut() = value;
151    }
152
153    /// <https://gpuweb.github.io/gpuweb/#dom-gpushadermodule-getcompilationinfo>
154    fn GetCompilationInfo(&self) -> Rc<Promise> {
155        self.compilation_info_promise.clone()
156    }
157}
158
159impl RoutedPromiseListener<Option<ShaderCompilationInfo>> for GPUShaderModule {
160    fn handle_response(
161        &self,
162        response: Option<ShaderCompilationInfo>,
163        promise: &Rc<Promise>,
164        can_gc: CanGc,
165    ) {
166        let info = GPUCompilationInfo::from(&self.global(), response, can_gc);
167        promise.resolve_native(&info, can_gc);
168    }
169}