diff options
author | Istvan Miklos <istvan.miklos@h-lab.eu> | 2020-02-18 11:29:21 +0100 |
---|---|---|
committer | Istvan Miklos <istvan.miklos@h-lab.eu> | 2020-02-19 11:19:59 +0100 |
commit | 170e9971ac0acab6e5dba52fcc2e09064d1e0090 (patch) | |
tree | 514ebccdf98992fd4c80d008c08f0e4bef05290f /components/script/dom/gpucomputepassencoder.rs | |
parent | 5597ccf57ddc2b77fcb4a8071f575b0dc9389a12 (diff) | |
download | servo-170e9971ac0acab6e5dba52fcc2e09064d1e0090.tar.gz servo-170e9971ac0acab6e5dba52fcc2e09064d1e0090.zip |
Implement GPUComputePassEncoder functions
Implement the `dispatch`, `endPass`, `setBindGroup`, `setPipeline` functions of `GPUComputePassEncoder`.
Diffstat (limited to 'components/script/dom/gpucomputepassencoder.rs')
-rw-r--r-- | components/script/dom/gpucomputepassencoder.rs | 69 |
1 files changed, 63 insertions, 6 deletions
diff --git a/components/script/dom/gpucomputepassencoder.rs b/components/script/dom/gpucomputepassencoder.rs index ecdd4407a8d..f6116c88f9d 100644 --- a/components/script/dom/gpucomputepassencoder.rs +++ b/components/script/dom/gpucomputepassencoder.rs @@ -10,8 +10,20 @@ 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::gpubindgroup::GPUBindGroup; +use crate::dom::gpucomputepipeline::GPUComputePipeline; use dom_struct::dom_struct; -use webgpu::{wgpu::command::RawPass, WebGPU}; +use std::cell::RefCell; +use webgpu::{ + wgpu::command::{ + compute_ffi::{ + wgpu_compute_pass_dispatch, wgpu_compute_pass_set_bind_group, + wgpu_compute_pass_set_pipeline, + }, + RawPass, + }, + WebGPU, WebGPUCommandEncoder, WebGPURequest, +}; #[dom_struct] pub struct GPUComputePassEncoder { @@ -20,26 +32,26 @@ pub struct GPUComputePassEncoder { channel: WebGPU, label: DomRefCell<Option<DOMString>>, #[ignore_malloc_size_of = "defined in wgpu-core"] - pass: RawPass, + raw_pass: RefCell<Option<RawPass>>, } impl GPUComputePassEncoder { - pub fn new_inherited(channel: WebGPU, pass: RawPass) -> GPUComputePassEncoder { + fn new_inherited(channel: WebGPU, parent: WebGPUCommandEncoder) -> GPUComputePassEncoder { GPUComputePassEncoder { channel, reflector_: Reflector::new(), label: DomRefCell::new(None), - pass, + raw_pass: RefCell::new(Some(RawPass::new_compute(parent.0))), } } pub fn new( global: &GlobalScope, channel: WebGPU, - pass: RawPass, + parent: WebGPUCommandEncoder, ) -> DomRoot<GPUComputePassEncoder> { reflect_dom_object( - Box::new(GPUComputePassEncoder::new_inherited(channel, pass)), + Box::new(GPUComputePassEncoder::new_inherited(channel, parent)), global, GPUComputePassEncoderBinding::Wrap, ) @@ -56,4 +68,49 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder { fn SetLabel(&self, value: Option<DOMString>) { *self.label.borrow_mut() = value; } + + #[allow(unsafe_code)] + /// https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-dispatch + fn Dispatch(&self, x: u32, y: u32, z: u32) { + if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() { + unsafe { wgpu_compute_pass_dispatch(raw_pass, x, y, z) }; + } + } + + #[allow(unsafe_code)] + /// https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-endpass + fn EndPass(&self) { + if let Some(raw_pass) = self.raw_pass.borrow_mut().take() { + let (pass_data, id) = unsafe { raw_pass.finish_compute() }; + + self.channel + .0 + .send(WebGPURequest::RunComputePass(id, pass_data)) + .unwrap(); + } + } + + #[allow(unsafe_code)] + /// https://gpuweb.github.io/gpuweb/#dom-gpuprogrammablepassencoder-setbindgroup + fn SetBindGroup(&self, index: u32, bind_group: &GPUBindGroup, dynamic_offsets: Vec<u32>) { + if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() { + unsafe { + wgpu_compute_pass_set_bind_group( + raw_pass, + index, + bind_group.id().0, + dynamic_offsets.as_ptr(), + dynamic_offsets.len(), + ) + }; + } + } + + #[allow(unsafe_code)] + /// https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-setpipeline + fn SetPipeline(&self, pipeline: &GPUComputePipeline) { + if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() { + unsafe { wgpu_compute_pass_set_pipeline(raw_pass, pipeline.id().0) }; + } + } } |