Skip to main content

script_webgpu/
gpucomputepipeline.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 dom_struct::dom_struct;
6use js::context::{JSContext, NoGC};
7use log::warn;
8use malloc_size_of_derive::MallocSizeOf;
9use script_bindings::DomTypes;
10use script_bindings::cell::DomRefCell;
11use script_bindings::codegen::GenericBindings::WebGPUBinding::{
12    GPUComputePipelineDescriptor, GPUComputePipelineMethods, GPUComputePipelineWrap,
13};
14use script_bindings::interfaces::{GlobalScopeHelpers, PromiseHelpers};
15use script_bindings::reflector::{DomGlobalGeneric, Reflector, reflect_dom_object_with_wrap};
16use servo_base::generic_channel::GenericCallback;
17use webgpu_traits::{
18    WebGPU, WebGPUBindGroupLayout, WebGPUComputePipeline, WebGPUComputePipelineResponse,
19    WebGPURequest,
20};
21use wgpu_core::pipeline::ComputePipelineDescriptor;
22
23use crate::JSTraceable;
24use crate::dom::bindings::error::Fallible;
25use crate::dom::bindings::root::{Dom, DomRoot};
26use crate::dom::bindings::str::USVString;
27use crate::gpubindgrouplayout::GPUBindGroupLayout;
28use crate::gpuconvert::WebGPUConvert;
29use crate::traits::{Equivalence, GPUDeviceTrait, WebGPUGlobalTrait, WebGPUPromiseTrait};
30
31#[derive(JSTraceable, MallocSizeOf)]
32struct DroppableGPUComputePipeline {
33    #[no_trace]
34    channel: WebGPU,
35    #[no_trace]
36    compute_pipeline: WebGPUComputePipeline,
37}
38
39impl Drop for DroppableGPUComputePipeline {
40    fn drop(&mut self) {
41        if let Err(e) = self
42            .channel
43            .0
44            .send(WebGPURequest::DropComputePipeline(self.compute_pipeline.0))
45        {
46            warn!(
47                "Failed to send WebGPURequest::DropComputePipeline({:?}) ({})",
48                self.compute_pipeline.0, e
49            );
50        };
51    }
52}
53
54#[dom_struct]
55pub struct GPUComputePipeline<D: DomTypes> {
56    reflector_: Reflector,
57    label: DomRefCell<USVString>,
58    device: Dom<D::GPUDevice>,
59    droppable: DroppableGPUComputePipeline,
60}
61
62impl<D> GPUComputePipeline<D>
63where
64    D: Equivalence,
65    D::GPUDevice: GPUDeviceTrait<D>,
66{
67    fn new_inherited(
68        compute_pipeline: WebGPUComputePipeline,
69        label: USVString,
70        device: &D::GPUDevice,
71    ) -> Self {
72        Self {
73            reflector_: Reflector::new(),
74            label: DomRefCell::new(label),
75            device: Dom::from_ref(device),
76            droppable: DroppableGPUComputePipeline {
77                channel: device.channel(),
78                compute_pipeline,
79            },
80        }
81    }
82
83    pub fn new(
84        cx: &mut JSContext,
85        global: &D::GlobalScope,
86        compute_pipeline: WebGPUComputePipeline,
87        label: USVString,
88        device: &D::GPUDevice,
89    ) -> DomRoot<Self> {
90        reflect_dom_object_with_wrap::<D, _, _>(
91            Box::new(GPUComputePipeline::new_inherited(
92                compute_pipeline,
93                label,
94                device,
95            )),
96            global,
97            cx,
98            GPUComputePipelineWrap::<D>,
99        )
100    }
101}
102
103impl<D> GPUComputePipeline<D>
104where
105    D: Equivalence,
106    D::Promise: PromiseHelpers<D>,
107    <D::Promise as PromiseHelpers<D>>::StackRoot: WebGPUPromiseTrait<D>,
108    D::GlobalScope: WebGPUGlobalTrait + GlobalScopeHelpers<D>,
109    D::GPUDevice: GPUDeviceTrait<D>,
110{
111    pub(crate) fn id(&self) -> &WebGPUComputePipeline {
112        &self.droppable.compute_pipeline
113    }
114
115    /// <https://gpuweb.github.io/gpuweb/#dom-gpudevice-createcomputepipeline>
116    pub fn create(
117        device: &D::GPUDevice,
118        descriptor: &GPUComputePipelineDescriptor<D>,
119        async_sender: Option<GenericCallback<WebGPUComputePipelineResponse>>,
120    ) -> WebGPUComputePipeline {
121        let compute_pipeline_id = device
122            .global_from_reflector()
123            .global_wgpu_id_hub()
124            .create_compute_pipeline_id();
125
126        let pipeline_layout = device.get_pipeline_layout_data(&descriptor.parent.layout);
127
128        let desc = ComputePipelineDescriptor {
129            label: (&descriptor.parent.parent).convert(),
130            layout: pipeline_layout.explicit(),
131            stage: (&descriptor.compute).convert(),
132            cache: None,
133        };
134
135        device
136            .channel()
137            .0
138            .send(WebGPURequest::CreateComputePipeline {
139                device_id: device.id().0,
140                compute_pipeline_id,
141                descriptor: desc,
142                async_sender,
143            })
144            .expect("Failed to create WebGPU ComputePipeline");
145
146        WebGPUComputePipeline(compute_pipeline_id)
147    }
148}
149
150impl<D> GPUComputePipelineMethods<D> for GPUComputePipeline<D>
151where
152    D: Equivalence,
153    D::GlobalScope: WebGPUGlobalTrait,
154    D::GPUDevice: GPUDeviceTrait<D>,
155    Self: DomGlobalGeneric<D>,
156    D::Promise: PromiseHelpers<D>,
157    <D::Promise as PromiseHelpers<D>>::StackRoot: WebGPUPromiseTrait<D>,
158{
159    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
160    fn Label(&self) -> USVString {
161        self.label.borrow().clone()
162    }
163
164    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
165    fn SetLabel(&self, no_gc: &NoGC, value: USVString) {
166        *self.label.safe_borrow_mut(no_gc) = value;
167    }
168
169    /// <https://gpuweb.github.io/gpuweb/#dom-gpupipelinebase-getbindgrouplayout>
170    fn GetBindGroupLayout(
171        &self,
172        cx: &mut JSContext,
173        index: u32,
174    ) -> Fallible<DomRoot<GPUBindGroupLayout<D>>> {
175        let id = self
176            .global_from_reflector()
177            .global_wgpu_id_hub()
178            .create_bind_group_layout_id();
179
180        if let Err(e) = self
181            .droppable
182            .channel
183            .0
184            .send(WebGPURequest::ComputeGetBindGroupLayout {
185                device_id: self.device.id().0,
186                pipeline_id: self.id().0,
187                index,
188                id,
189            })
190        {
191            warn!("Failed to send WebGPURequest::ComputeGetBindGroupLayout {e:?}");
192        }
193
194        Ok(GPUBindGroupLayout::new(
195            cx,
196            &*self.global_from_reflector(),
197            self.droppable.channel.clone(),
198            WebGPUBindGroupLayout(id),
199            USVString::default(),
200        ))
201    }
202}