Skip to main content

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