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, NoGC};
9use js::realm::CurrentRealm;
10use script_bindings::cell::DomRefCell;
11use script_bindings::reflector::{Reflector, reflect_dom_object};
12use script_webgpu::traits::GPUShaderModuleTrait;
13use webgpu_traits::{ShaderCompilationInfo, WebGPU, WebGPURequest, WebGPUShaderModule};
14
15use super::gpucompilationinfo::GPUCompilationInfo;
16use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
17    GPUShaderModuleDescriptor, GPUShaderModuleMethods,
18};
19use crate::dom::bindings::reflector::DomGlobal;
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::bindings::str::USVString;
22use crate::dom::bindings::trace::RootedTraceableBox;
23use crate::dom::globalscope::GlobalScope;
24use crate::dom::promise::Promise;
25use crate::dom::types::GPUDevice;
26use crate::routed_promise::{RoutedPromiseListener, callback_promise};
27
28#[derive(JSTraceable, MallocSizeOf)]
29struct DroppableGPUShaderModule {
30    #[no_trace]
31    channel: WebGPU,
32    #[no_trace]
33    shader_module: WebGPUShaderModule,
34}
35
36impl Drop for DroppableGPUShaderModule {
37    fn drop(&mut self) {
38        if let Err(e) = self
39            .channel
40            .0
41            .send(WebGPURequest::DropShaderModule(self.shader_module.0))
42        {
43            warn!(
44                "Failed to send DropShaderModule ({:?}) ({})",
45                self.shader_module.0, e
46            );
47        }
48    }
49}
50
51#[dom_struct]
52pub(crate) struct GPUShaderModule {
53    reflector_: Reflector,
54    label: DomRefCell<USVString>,
55    #[ignore_malloc_size_of = "promise"]
56    compilation_info_promise: Rc<Promise>,
57    droppable: DroppableGPUShaderModule,
58}
59
60impl GPUShaderModule {
61    fn new_inherited(
62        channel: WebGPU,
63        shader_module: WebGPUShaderModule,
64        label: USVString,
65        promise: Rc<Promise>,
66    ) -> Self {
67        Self {
68            reflector_: Reflector::new(),
69            label: DomRefCell::new(label),
70            compilation_info_promise: promise,
71            droppable: DroppableGPUShaderModule {
72                channel,
73                shader_module,
74            },
75        }
76    }
77
78    pub(crate) fn new(
79        cx: &mut JSContext,
80        global: &GlobalScope,
81        channel: WebGPU,
82        shader_module: WebGPUShaderModule,
83        label: USVString,
84        promise: Rc<Promise>,
85    ) -> DomRoot<Self> {
86        reflect_dom_object(
87            cx,
88            Box::new(GPUShaderModule::new_inherited(
89                channel,
90                shader_module,
91                label,
92                promise,
93            )),
94            global,
95        )
96    }
97}
98
99impl GPUShaderModule {
100    pub(crate) fn id(&self) -> WebGPUShaderModule {
101        self.droppable.shader_module
102    }
103
104    /// <https://gpuweb.github.io/gpuweb/#dom-gpudevice-createshadermodule>
105    pub(crate) fn create(
106        cx: &mut CurrentRealm<'_>,
107        device: &GPUDevice,
108        descriptor: RootedTraceableBox<GPUShaderModuleDescriptor>,
109    ) -> DomRoot<GPUShaderModule> {
110        let program_id = device.global().wgpu_id_hub().create_shader_module_id();
111        let promise = Promise::new_in_realm(cx);
112        let shader_module = GPUShaderModule::new(
113            cx,
114            &device.global(),
115            device.channel(),
116            WebGPUShaderModule(program_id),
117            descriptor.parent.label.clone(),
118            promise.clone(),
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, no_gc: &NoGC, value: USVString) {
151        *self.label.safe_borrow_mut(no_gc) = 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(cx, &self.global(), response);
168        promise.resolve_native(cx, &info);
169    }
170}
171
172impl GPUShaderModuleTrait for GPUShaderModule {
173    fn id(&self) -> WebGPUShaderModule {
174        self.id()
175    }
176}