From dd681d2c773a81f2bf9b96b46793bd232a2d4787 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 2 Oct 2018 16:05:22 -0400 Subject: [PATCH 1/5] Compute dispatch call --- wgpu-native/src/command/compute.rs | 15 +++++++++++++-- wgpu-native/src/command/render.rs | 6 +++--- wgpu-rs/src/lib.rs | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index 2ab7d29263..c96df546b1 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -1,11 +1,12 @@ -use hal; - use registry::{HUB, Items, Registry}; use { Stored, CommandBufferId, ComputePassId }; +use hal; +use hal::command::RawCommandBuffer; + pub struct ComputePass { raw: B::CommandBuffer, @@ -35,3 +36,13 @@ pub extern "C" fn wgpu_compute_pass_end_pass( .raw = Some(pass.raw); pass.cmb_id.0 } + +pub extern "C" fn wgpu_compute_pass_dispatch( + pass_id: ComputePassId, x: u32, y: u32, z: u32, +) { + HUB.compute_passes + .lock() + .get_mut(pass_id) + .raw + .dispatch([x, y, z]); +} diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index d382bbf8d3..79dcd91daa 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -1,12 +1,12 @@ -use hal; -use hal::command::RawCommandBuffer; - use registry::{HUB, Items, Registry}; use { Stored, CommandBufferId, RenderPassId, }; +use hal; +use hal::command::RawCommandBuffer; + pub struct RenderPass { raw: B::CommandBuffer, diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index 3b859990c1..f288d1e726 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -273,6 +273,9 @@ impl<'a> ComputePass<'a> { wgn::wgpu_compute_pass_end_pass(self.id); self.parent } + pub fn dispatch(&self, x: u32, y: u32, z: u32) { + wgn::wgpu_compute_pass_dispatch(self.id, x, y, z); + } } impl Queue { From c7bb1b453ebb535ba6ec93e4eb7c50fd96c7c4db Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 2 Oct 2018 16:12:48 -0400 Subject: [PATCH 2/5] native: use iterators for descriptor set layouts --- wgpu-native/src/device.rs | 69 ++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 28c4c8694a..e0f409e1c2 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -44,20 +44,24 @@ pub extern "C" fn wgpu_device_create_bind_group_layout( desc: binding_model::BindGroupLayoutDescriptor, ) -> BindGroupLayoutId { let bindings = unsafe { slice::from_raw_parts(desc.bindings, desc.bindings_length) }; - let device_guard = HUB.devices.lock(); - let device = device_guard.get(device_id); - let descriptor_set_layout = device.raw.create_descriptor_set_layout( - bindings.iter().map(|binding| { - hal::pso::DescriptorSetLayoutBinding { - binding: binding.binding, - ty: conv::map_binding_type(&binding.ty), - count: bindings.len(), - stage_flags: conv::map_shader_stage_flags(binding.visibility), - immutable_samplers: false, // TODO - } - }), - &[], - ); + + let descriptor_set_layout = HUB.devices + .lock() + .get(device_id) + .raw + .create_descriptor_set_layout( + bindings.iter().map(|binding| { + hal::pso::DescriptorSetLayoutBinding { + binding: binding.binding, + ty: conv::map_binding_type(&binding.ty), + count: bindings.len(), + stage_flags: conv::map_shader_stage_flags(binding.visibility), + immutable_samplers: false, // TODO + } + }), + &[], + ); + HUB.bind_group_layouts .lock() .register(binding_model::BindGroupLayout { @@ -70,16 +74,21 @@ pub extern "C" fn wgpu_device_create_pipeline_layout( device_id: DeviceId, desc: binding_model::PipelineLayoutDescriptor, ) -> PipelineLayoutId { + let bind_group_layouts = unsafe { + slice::from_raw_parts(desc.bind_group_layouts, desc.bind_group_layouts_length) + }; let bind_group_layout_guard = HUB.bind_group_layouts.lock(); - let descriptor_set_layouts = - unsafe { slice::from_raw_parts(desc.bind_group_layouts, desc.bind_group_layouts_length) } - .iter() - .map(|id| bind_group_layout_guard.get(id.clone())) - .collect::>(); - let device_guard = HUB.devices.lock(); - let device = &device_guard.get(device_id).raw; - let pipeline_layout = - device.create_pipeline_layout(descriptor_set_layouts.iter().map(|d| &d.raw), &[]); // TODO: push constants + let descriptor_set_layouts = bind_group_layouts + .iter() + .map(|&id| &bind_group_layout_guard.get(id).raw); + + // TODO: push constants + let pipeline_layout = HUB.devices + .lock() + .get(device_id) + .raw + .create_pipeline_layout(descriptor_set_layouts, &[]); + HUB.pipeline_layouts .lock() .register(binding_model::PipelineLayout { @@ -116,11 +125,16 @@ pub extern "C" fn wgpu_device_create_shader_module( device_id: DeviceId, desc: pipeline::ShaderModuleDescriptor, ) -> ShaderModuleId { - let device_guard = HUB.devices.lock(); - let device = &device_guard.get(device_id).raw; - let shader = device - .create_shader_module(unsafe { slice::from_raw_parts(desc.code.bytes, desc.code.length) }) + let spv = unsafe { + slice::from_raw_parts(desc.code.bytes, desc.code.length) + }; + let shader = HUB.devices + .lock() + .get(device_id) + .raw + .create_shader_module(spv) .unwrap(); + HUB.shader_modules .lock() .register(ShaderModule { raw: shader }) @@ -133,6 +147,7 @@ pub extern "C" fn wgpu_device_create_command_buffer( ) -> CommandBufferId { let mut device_guard = HUB.devices.lock(); let device = device_guard.get_mut(device_id); + let mut cmd_buf = device.com_allocator.allocate(device_id, &device.raw); cmd_buf.raw.as_mut().unwrap().begin( hal::command::CommandBufferFlags::ONE_TIME_SUBMIT, From 45def957631a01c036c09cd110934f48a0fc5471 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 2 Oct 2018 21:37:27 -0400 Subject: [PATCH 3/5] native: basic compute resource binding --- wgpu-native/src/binding_model.rs | 4 ++-- wgpu-native/src/command/compute.rs | 32 +++++++++++++++++++++++++++++- wgpu-native/src/lib.rs | 5 ++++- wgpu-native/src/pipeline.rs | 4 ++-- wgpu-native/src/registry/mod.rs | 8 +++++--- 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/wgpu-native/src/binding_model.rs b/wgpu-native/src/binding_model.rs index a25c0b41d0..8bfbd570cf 100644 --- a/wgpu-native/src/binding_model.rs +++ b/wgpu-native/src/binding_model.rs @@ -76,6 +76,6 @@ pub struct BindGroupDescriptor { pub bindings_length: usize, } -pub struct BindGroup { - // TODO +pub(crate) struct BindGroup { + pub raw: B::DescriptorSet, } diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index c96df546b1..3f96a1774b 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -1,12 +1,14 @@ use registry::{HUB, Items, Registry}; use { Stored, - CommandBufferId, ComputePassId + BindGroupId, CommandBufferId, ComputePassId, ComputePipelineId, }; use hal; use hal::command::RawCommandBuffer; +use std::iter; + pub struct ComputePass { raw: B::CommandBuffer, @@ -37,6 +39,34 @@ pub extern "C" fn wgpu_compute_pass_end_pass( pass.cmb_id.0 } +pub extern "C" fn wgpu_compute_pass_set_bind_group( + pass_id: ComputePassId, index: u32, bind_group_id: BindGroupId, +) { + let bind_group_guard = HUB.bind_groups.lock(); + let set = &bind_group_guard.get(bind_group_id).raw; + let layout = unimplemented!(); + // see https://github.com/gpuweb/gpuweb/pull/93 + + HUB.compute_passes + .lock() + .get_mut(pass_id) + .raw + .bind_compute_descriptor_sets(layout, index as usize, iter::once(set), &[]); +} + +pub extern "C" fn wgpu_compute_pass_set_pipeline( + pass_id: ComputePassId, pipeline_id: ComputePipelineId, +) { + let pipeline_guard = HUB.compute_pipelines.lock(); + let pipeline = &pipeline_guard.get(pipeline_id).raw; + + HUB.compute_passes + .lock() + .get_mut(pass_id) + .raw + .bind_compute_pipeline(pipeline); +} + pub extern "C" fn wgpu_compute_pass_dispatch( pass_id: ComputePassId, x: u32, y: u32, z: u32, ) { diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 30a4b905b6..be09827a45 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -106,6 +106,8 @@ pub type BindGroupLayoutId = Id; type BindGroupLayoutHandle = BindGroupLayout; pub type PipelineLayoutId = Id; type PipelineLayoutHandle = PipelineLayout; +pub type BindGroupId = Id; +type BindGroupHandle = BindGroup; // Pipeline pub type BlendStateId = Id; @@ -117,9 +119,10 @@ pub type ShaderModuleId = Id; type ShaderModuleHandle = ShaderModule; pub type AttachmentStateId = Id; type AttachmentStateHandle = AttachmentState; -pub type ComputePipelineId = Id; pub type RenderPipelineId = Id; type RenderPipelineHandle = RenderPipeline; +pub type ComputePipelineId = Id; +type ComputePipelineHandle = ComputePipeline; pub type CommandBufferId = Id; type CommandBufferHandle = CommandBuffer; diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index 5f876224d4..a27c65cd9e 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -227,8 +227,8 @@ pub struct ComputePipelineDescriptor { pub stages: *const PipelineStageDescriptor, } -pub struct ComputePipeline { - // TODO +pub(crate) struct ComputePipeline { + pub raw: B::ComputePipeline, } #[repr(C)] diff --git a/wgpu-native/src/registry/mod.rs b/wgpu-native/src/registry/mod.rs index fecdf2c2b5..49dbe3ba4a 100644 --- a/wgpu-native/src/registry/mod.rs +++ b/wgpu-native/src/registry/mod.rs @@ -9,10 +9,10 @@ pub use self::local::{Id, ItemsGuard, Registry as ConcreteRegistry}; pub use self::remote::{Id, ItemsGuard, Registry as ConcreteRegistry}; use { - AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BlendStateHandle, - CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle, + AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BindGroupHandle, + BlendStateHandle, CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle, RenderPassHandle, ComputePassHandle, - PipelineLayoutHandle, RenderPipelineHandle, ShaderModuleHandle, + PipelineLayoutHandle, RenderPipelineHandle, ComputePipelineHandle, ShaderModuleHandle, }; @@ -37,12 +37,14 @@ pub struct Hub { pub(crate) devices: ConcreteRegistry, pub(crate) pipeline_layouts: ConcreteRegistry, pub(crate) bind_group_layouts: ConcreteRegistry, + pub(crate) bind_groups: ConcreteRegistry, pub(crate) attachment_states: ConcreteRegistry, pub(crate) blend_states: ConcreteRegistry, pub(crate) depth_stencil_states: ConcreteRegistry, pub(crate) shader_modules: ConcreteRegistry, pub(crate) command_buffers: ConcreteRegistry, pub(crate) render_pipelines: ConcreteRegistry, + pub(crate) compute_pipelines: ConcreteRegistry, pub(crate) render_passes: ConcreteRegistry, pub(crate) compute_passes: ConcreteRegistry, } From 444da40484278b89ed07cd6f9e017def36fb216e Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 2 Oct 2018 21:41:03 -0400 Subject: [PATCH 4/5] rust: compute resource binding --- wgpu-rs/src/lib.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index f288d1e726..3daa299070 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -35,6 +35,10 @@ pub struct BindGroupLayout { id: wgn::BindGroupLayoutId, } +pub struct BindGroup { + id: wgn::BindGroupId, +} + pub struct ShaderModule { id: wgn::ShaderModuleId, } @@ -59,6 +63,10 @@ pub struct RenderPipeline { id: wgn::RenderPipelineId, } +pub struct ComputePipeline { + id: wgn::ComputePipelineId, +} + pub struct CommandBuffer { id: wgn::CommandBufferId, } @@ -273,7 +281,16 @@ impl<'a> ComputePass<'a> { wgn::wgpu_compute_pass_end_pass(self.id); self.parent } - pub fn dispatch(&self, x: u32, y: u32, z: u32) { + + pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup) { + wgn::wgpu_compute_pass_set_bind_group(self.id, index, bind_group.id); + } + + pub fn set_pipeline(&mut self, pipeline: &ComputePipeline) { + wgn::wgpu_compute_pass_set_pipeline(self.id, pipeline.id); + } + + pub fn dispatch(&mut self, x: u32, y: u32, z: u32) { wgn::wgpu_compute_pass_dispatch(self.id, x, y, z); } } From 744a64f7fdbf3ac17667811fd20d38389a927312 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Wed, 3 Oct 2018 07:29:18 -0400 Subject: [PATCH 5/5] Regenerated C header --- wgpu-bindings/wgpu.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 8a1b918fba..36c1e7592b 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -108,6 +108,8 @@ typedef enum { WGPUTextureFormat_D32FloatS8Uint = 3, } WGPUTextureFormat; +typedef struct WGPURenderPassDescriptor_WGPUTextureViewId WGPURenderPassDescriptor_WGPUTextureViewId; + typedef WGPUId WGPUDeviceId; typedef WGPUId WGPUAdapterId; @@ -122,10 +124,10 @@ typedef struct { typedef WGPUId WGPUComputePassId; -typedef WGPUId WGPURenderPassId; - typedef WGPUId WGPUCommandBufferId; +typedef WGPUId WGPURenderPassId; + typedef WGPUId WGPUInstanceId; typedef WGPUId WGPUAttachmentStateId; @@ -234,9 +236,12 @@ typedef struct { WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id, WGPUDeviceDescriptor _desc); -WGPUComputePassId wgpu_command_buffer_begin_compute_pass(void); +WGPUComputePassId wgpu_command_buffer_begin_compute_pass(WGPUCommandBufferId command_buffer_id); -WGPURenderPassId wgpu_command_buffer_begin_render_pass(WGPUCommandBufferId _command_buffer); +WGPURenderPassId wgpu_command_buffer_begin_render_pass(WGPUCommandBufferId command_buffer_id, + WGPURenderPassDescriptor_WGPUTextureViewId _descriptor); + +WGPUCommandBufferId wgpu_compute_pass_end_pass(WGPUComputePassId pass_id); WGPUInstanceId wgpu_create_instance(void); @@ -252,7 +257,7 @@ WGPUBlendStateId wgpu_device_create_blend_state(WGPUDeviceId _device_id, WGPUCommandBufferId wgpu_device_create_command_buffer(WGPUDeviceId device_id, WGPUCommandBufferDescriptor _desc); -WGPUDepthStencilStateId wgpu_device_create_depth_stencil_state(WGPUDeviceId device_id, +WGPUDepthStencilStateId wgpu_device_create_depth_stencil_state(WGPUDeviceId _device_id, WGPUDepthStencilStateDescriptor desc); WGPUPipelineLayoutId wgpu_device_create_pipeline_layout(WGPUDeviceId device_id, @@ -271,3 +276,5 @@ WGPUAdapterId wgpu_instance_get_adapter(WGPUInstanceId instance_id, WGPUAdapterD void wgpu_queue_submit(WGPUQueueId queue_id, const WGPUCommandBufferId *command_buffer_ptr, uintptr_t command_buffer_count); + +WGPUCommandBufferId wgpu_render_pass_end_pass(WGPURenderPassId pass_id);