From 1fd05608fb6eb330227b598394ad6fbd77acebdf Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 2 Oct 2018 11:43:26 -0400 Subject: [PATCH] native: compute pass boilerplate --- wgpu-native/src/command/compute.rs | 32 +++++++++++++++++++++++++++++- wgpu-native/src/command/mod.rs | 13 ++++++++++-- wgpu-native/src/command/render.rs | 19 ++++++++---------- wgpu-native/src/lib.rs | 1 + wgpu-native/src/registry.rs | 4 +++- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index fdf5473d52..245921fce5 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -1,7 +1,37 @@ use hal; -//use {CommandBuffer, CommandBufferId, ComputePassId}; +use registry::{self, Items, Registry}; +use { + Stored, + CommandBufferId, ComputePassId +}; + pub struct ComputePass { raw: B::CommandBuffer, + cmb_id: Stored, +} + +impl ComputePass { + pub fn new(raw: B::CommandBuffer, cmb_id: CommandBufferId) -> Self { + ComputePass { + raw, + cmb_id: Stored(cmb_id), + } + } +} + +#[no_mangle] +pub extern "C" fn wgpu_compute_pass_end_pass( + pass_id: ComputePassId, +) -> CommandBufferId { + let pass = registry::COMPUTE_PASS_REGISTRY + .lock() + .take(pass_id); + + registry::COMMAND_BUFFER_REGISTRY + .lock() + .get_mut(pass.cmb_id.0) + .raw = Some(pass.raw); + pass.cmb_id.0 } diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index c90f9c34f4..7b6eacb567 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -112,6 +112,15 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( } #[no_mangle] -pub extern "C" fn wgpu_command_buffer_begin_compute_pass() -> ComputePassId { - unimplemented!() +pub extern "C" fn wgpu_command_buffer_begin_compute_pass( + command_buffer_id: CommandBufferId, +) -> ComputePassId { + let mut cmb_guard = registry::COMMAND_BUFFER_REGISTRY.lock(); + let mut cmb = cmb_guard.get_mut(command_buffer_id); + + let raw = cmb.raw.take().unwrap(); + + registry::COMPUTE_PASS_REGISTRY + .lock() + .register(ComputePass::new(raw, command_buffer_id)) } diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 94df08394b..b807d42141 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -7,15 +7,12 @@ use { CommandBufferId, RenderPassId, }; + pub struct RenderPass { raw: B::CommandBuffer, cmb_id: Stored, } -// This is needed for `cmb_id` - would be great to remove. -#[cfg(not(feature = "remote"))] -unsafe impl Sync for RenderPass {} - impl RenderPass { pub fn new(raw: B::CommandBuffer, cmb_id: CommandBufferId) -> Self { RenderPass { @@ -27,16 +24,16 @@ impl RenderPass { #[no_mangle] pub extern "C" fn wgpu_render_pass_end_pass( - render_pass_id: RenderPassId, + pass_id: RenderPassId, ) -> CommandBufferId { - let mut rp = registry::RENDER_PASS_REGISTRY + let mut pass = registry::RENDER_PASS_REGISTRY .lock() - .take(render_pass_id); - rp.raw.end_render_pass(); + .take(pass_id); + pass.raw.end_render_pass(); registry::COMMAND_BUFFER_REGISTRY .lock() - .get_mut(rp.cmb_id.0) - .raw = Some(rp.raw); - rp.cmb_id.0 + .get_mut(pass.cmb_id.0) + .raw = Some(pass.raw); + pass.cmb_id.0 } diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 69e695f828..30a4b905b6 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -126,3 +126,4 @@ type CommandBufferHandle = CommandBuffer; pub type RenderPassId = Id; type RenderPassHandle = RenderPass; pub type ComputePassId = Id; +type ComputePassHandle = ComputePass; diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index c55ca6ecc6..89c6030b8a 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -12,7 +12,7 @@ use std::sync::Arc; use { AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BlendStateHandle, CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle, - RenderPassHandle, + RenderPassHandle, ComputePassHandle, PipelineLayoutHandle, RenderPipelineHandle, ShaderModuleHandle, }; @@ -178,4 +178,6 @@ lazy_static! { ConcreteRegistry::new(); pub(crate) static ref RENDER_PASS_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); + pub(crate) static ref COMPUTE_PASS_REGISTRY: ConcreteRegistry = + ConcreteRegistry::new(); }