diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2020-02-12 08:51:41 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-12 08:51:41 -0500 |
commit | 0790c856d5c376a0839347a3f0d4e4f7b07779c6 (patch) | |
tree | d433aafc86cb75eec015efe255bce85216b6c6f5 /components/script/dom/gpucommandencoder.rs | |
parent | 6d6d16f7f492e6346f0391e352015226a4444806 (diff) | |
parent | 4facd3d4d21d131a222d3f44d5e8d077612f30e7 (diff) | |
download | servo-0790c856d5c376a0839347a3f0d4e4f7b07779c6.tar.gz servo-0790c856d5c376a0839347a3f0d4e4f7b07779c6.zip |
Auto merge of #25702 - szeged:wgpu_command_encoder, r=jdm
Initial implementation of GPUCommandEncoder
<!-- Please describe your changes on the following line: -->
Added WebIDL bindings for `GPUCommandEncoder`, `GPUCommandBuffer`, `GPUComputePassEncoder` and `GPUProgrammablePassEncoder`.
Implemented the `beginComputePass`, `copyBufferToBuffer` and `finish` functions of `GPUCommandEncoder`.
Implemented the `createCommandEncoder` function of `GPUDevice`.
This pull request is based on: #25700
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes addresses a part of #24706
cc @kvark @jdm @zakorgy
<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Diffstat (limited to 'components/script/dom/gpucommandencoder.rs')
-rw-r--r-- | components/script/dom/gpucommandencoder.rs | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/components/script/dom/gpucommandencoder.rs b/components/script/dom/gpucommandencoder.rs new file mode 100644 index 00000000000..5e86c0b8bfd --- /dev/null +++ b/components/script/dom/gpucommandencoder.rs @@ -0,0 +1,114 @@ +/* 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::GPUCommandEncoderBinding::{ + self, GPUCommandBufferDescriptor, GPUCommandEncoderMethods, GPUComputePassDescriptor, +}; +use crate::dom::bindings::reflector::DomObject; +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::GPUBuffer; +use crate::dom::gpucommandbuffer::GPUCommandBuffer; +use crate::dom::gpucomputepassencoder::GPUComputePassEncoder; +use dom_struct::dom_struct; +use ipc_channel::ipc; +use webgpu::{wgpu::command::RawPass, WebGPU, WebGPUCommandEncoder, WebGPURequest}; + +#[dom_struct] +pub struct GPUCommandEncoder { + reflector_: Reflector, + #[ignore_malloc_size_of = "defined in webgpu"] + channel: WebGPU, + label: DomRefCell<Option<DOMString>>, + encoder: WebGPUCommandEncoder, +} + +impl GPUCommandEncoder { + pub fn new_inherited(channel: WebGPU, encoder: WebGPUCommandEncoder) -> GPUCommandEncoder { + GPUCommandEncoder { + channel, + reflector_: Reflector::new(), + label: DomRefCell::new(None), + encoder, + } + } + + pub fn new( + global: &GlobalScope, + channel: WebGPU, + encoder: WebGPUCommandEncoder, + ) -> DomRoot<GPUCommandEncoder> { + reflect_dom_object( + Box::new(GPUCommandEncoder::new_inherited(channel, encoder)), + global, + GPUCommandEncoderBinding::Wrap, + ) + } +} + +impl GPUCommandEncoderMethods for GPUCommandEncoder { + /// 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-gpucommandencoder-begincomputepass + fn BeginComputePass( + &self, + _descriptor: &GPUComputePassDescriptor, + ) -> DomRoot<GPUComputePassEncoder> { + GPUComputePassEncoder::new( + &self.global(), + self.channel.clone(), + RawPass::new_compute(self.encoder.0), + ) + } + + /// https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-copybuffertobuffer + fn CopyBufferToBuffer( + &self, + source: &GPUBuffer, + source_offset: u64, + destination: &GPUBuffer, + destination_offset: u64, + size: u64, + ) { + self.channel + .0 + .send(WebGPURequest::CopyBufferToBuffer( + self.encoder.0, + source.id().0, + source_offset, + destination.id().0, + destination_offset, + size, + )) + .expect("Failed to send CopyBufferToBuffer"); + } + + /// https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-finish + fn Finish(&self, _descriptor: &GPUCommandBufferDescriptor) -> DomRoot<GPUCommandBuffer> { + let (sender, receiver) = ipc::channel().unwrap(); + self.channel + .0 + .send(WebGPURequest::CommandEncoderFinish( + sender, + self.encoder.0, + // TODO(zakorgy): We should use `_descriptor` here after it's not empty + // and the underlying wgpu-core struct is serializable + )) + .expect("Failed to send CopyBufferToBuffer"); + + let buffer = receiver.recv().unwrap(); + GPUCommandBuffer::new(&self.global(), self.channel.clone(), buffer) + } +} |