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 js::context::JSContext;
9use js::realm::CurrentRealm;
10use script_bindings::cell::DomRefCell;
11use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
12use webgpu_traits::{ShaderCompilationInfo, WebGPU, WebGPURequest, WebGPUShaderModule};
13
14use super::gpucompilationinfo::GPUCompilationInfo;
15use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
16    GPUShaderModuleDescriptor, GPUShaderModuleMethods,
17};
18use crate::dom::bindings::reflector::DomGlobal;
19use crate::dom::bindings::root::DomRoot;
20use crate::dom::bindings::str::USVString;
21use crate::dom::bindings::trace::RootedTraceableBox;
22use crate::dom::globalscope::GlobalScope;
23use crate::dom::promise::Promise;
24use crate::dom::types::GPUDevice;
25use crate::routed_promise::{RoutedPromiseListener, callback_promise};
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        cx: &mut JSContext,
79        global: &GlobalScope,
80        channel: WebGPU,
81        shader_module: WebGPUShaderModule,
82        label: USVString,
83        promise: Rc<Promise>,
84    ) -> DomRoot<Self> {
85        reflect_dom_object_with_cx(
86            Box::new(GPUShaderModule::new_inherited(
87                channel,
88                shader_module,
89                label,
90                promise,
91            )),
92            global,
93            cx,
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        cx: &mut CurrentRealm<'_>,
106        device: &GPUDevice,
107        descriptor: RootedTraceableBox<GPUShaderModuleDescriptor>,
108    ) -> DomRoot<GPUShaderModule> {
109        let program_id = device.global().wgpu_id_hub().create_shader_module_id();
110        let promise = Promise::new_in_realm(cx);
111        let shader_module = GPUShaderModule::new(
112            cx,
113            &device.global(),
114            device.channel(),
115            WebGPUShaderModule(program_id),
116            descriptor.parent.label.clone(),
117            promise.clone(),
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        cx: &mut js::context::JSContext,
163        response: Option<ShaderCompilationInfo>,
164        promise: &Rc<Promise>,
165    ) {
166        let info = GPUCompilationInfo::from(cx, &self.global(), response);
167        promise.resolve_native_with_cx(cx, &info);
168    }
169}