aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/gpuqueue.rs
blob: c257ec5ef0953b3781d381f0b1b960da008baa2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::GPUQueueBinding::{self, GPUQueueMethods};
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubuffer::GPUBufferState;
use crate::dom::gpucommandbuffer::GPUCommandBuffer;
use dom_struct::dom_struct;
use webgpu::{WebGPU, WebGPUQueue, WebGPURequest};

#[dom_struct]
pub struct GPUQueue {
    reflector_: Reflector,
    #[ignore_malloc_size_of = "defined in webgpu"]
    channel: WebGPU,
    label: DomRefCell<Option<DOMString>>,
    queue: WebGPUQueue,
}

impl GPUQueue {
    fn new_inherited(channel: WebGPU, queue: WebGPUQueue) -> GPUQueue {
        GPUQueue {
            channel,
            reflector_: Reflector::new(),
            label: DomRefCell::new(None),
            queue,
        }
    }

    pub fn new(global: &GlobalScope, channel: WebGPU, queue: WebGPUQueue) -> DomRoot<GPUQueue> {
        reflect_dom_object(
            Box::new(GPUQueue::new_inherited(channel, queue)),
            global,
            GPUQueueBinding::Wrap,
        )
    }
}

impl GPUQueueMethods for GPUQueue {
    /// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
    fn GetLabel(&self) -> Option<DOMString> {
        self.label.borrow().clone()
    }

    /// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
    fn SetLabel(&self, value: Option<DOMString>) {
        *self.label.borrow_mut() = value;
    }

    /// https://gpuweb.github.io/gpuweb/#dom-gpuqueue-submit
    fn Submit(&self, command_buffers: Vec<DomRoot<GPUCommandBuffer>>) {
        let valid = command_buffers.iter().all(|cb| {
            cb.buffers().iter().all(|b| match *b.state() {
                GPUBufferState::Unmapped => true,
                _ => false,
            })
        });
        if !valid {
            // TODO: Generate error to the ErrorScope
            return;
        }
        let command_buffers = command_buffers.iter().map(|cb| cb.id().0).collect();
        self.channel
            .0
            .send(WebGPURequest::Submit {
                queue_id: self.queue.0,
                command_buffers,
            })
            .unwrap();
    }
}