From d1197c0c2e53f847892c5c849fce208e502aae63 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Fri, 17 Apr 2020 00:06:07 -0230 Subject: [PATCH] Fix some remaining TODOs in the web backend --- Cargo.toml | 2 +- src/backend/native.rs | 36 +++++++++++-- src/backend/web.rs | 115 +++++++++++++++++++++++++----------------- src/lib.rs | 23 ++------- 4 files changed, 106 insertions(+), 70 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a75ea4be0..06971ca61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,6 @@ parking_lot = "0.10" [dev-dependencies] cgmath = "0.17" -#glsl-to-spirv = "0.1" log = "0.4" png = "0.15" winit = { version = "0.22.1", features = ["web-sys"] } @@ -154,6 +153,7 @@ web-sys = { version = "0.3.37", features = [ "GpuSwapChainDescriptor", "GpuTexture", "GpuTextureAspect", + "GpuTextureComponentType", "GpuTextureCopyView", "GpuTextureDescriptor", "GpuTextureDimension", diff --git a/src/backend/native.rs b/src/backend/native.rs index a7d7b57a3..b6f46bd0b 100644 --- a/src/backend/native.rs +++ b/src/backend/native.rs @@ -650,8 +650,32 @@ pub(crate) fn device_create_swap_chain( wgn::wgpu_device_create_swap_chain(*device, *surface, desc) } -pub(crate) fn swap_chain_get_next_texture(swap_chain: &SwapChainId) -> Option { - wgn::wgpu_swap_chain_get_next_texture(*swap_chain).view_id +pub(crate) fn device_drop(device: &DeviceId) { + #[cfg(not(target_arch = "wasm32"))] + wgn::wgpu_device_poll(*device, true); + //TODO: make this work in general + #[cfg(not(target_arch = "wasm32"))] + #[cfg(feature = "metal-auto-capture")] + wgn::wgpu_device_destroy(*device); +} + +pub(crate) fn swap_chain_get_next_texture( + swap_chain: &SwapChainId, +) -> Result { + match wgn::wgpu_swap_chain_get_next_texture(*swap_chain).view_id { + Some(id) => Ok(crate::SwapChainOutput { + view: crate::TextureView { id, owned: false }, + detail: SwapChainOutputDetail { + swap_chain_id: *swap_chain, + }, + }), + None => Err(crate::TimeOut), + } +} + +#[derive(Debug)] +pub(crate) struct SwapChainOutputDetail { + swap_chain_id: SwapChainId, } pub(crate) fn command_encoder_begin_render_pass<'a>( @@ -883,8 +907,12 @@ pub(crate) fn texture_view_drop(texture_view: &TextureViewId) { wgn::wgpu_texture_view_destroy(*texture_view); } -pub(crate) fn swap_chain_present(swap_chain: &SwapChainId) { - wgn::wgpu_swap_chain_present(*swap_chain); +pub(crate) fn bind_group_drop(bind_group: &BindGroupId) { + wgn::wgpu_bind_group_destroy(*bind_group); +} + +pub(crate) fn swap_chain_present(swap_chain_output: &crate::SwapChainOutput) { + wgn::wgpu_swap_chain_present(swap_chain_output.detail.swap_chain_id); } pub(crate) fn device_poll(device: &DeviceId, maintain: crate::Maintain) { diff --git a/src/backend/web.rs b/src/backend/web.rs index ec3b55f03..e69f66de8 100644 --- a/src/backend/web.rs +++ b/src/backend/web.rs @@ -67,7 +67,6 @@ pub(crate) async fn request_device_and_queue( // TODO: Extensions let mut mapped_limits = web_sys::GpuLimits::new(); mapped_limits.max_bind_groups(d.limits.max_bind_groups); - // TODO: Other fields mapped_descriptor.limits(&mapped_limits); adapter.request_device_with_descriptor(&mapped_descriptor) } @@ -109,35 +108,43 @@ pub(crate) fn create_bind_group_layout( BindingType::StorageTexture { .. } => bt::WriteonlyStorageTexture, }; - let mapped_dynamic = match bind.ty { - BindingType::UniformBuffer { dynamic } - | BindingType::StorageBuffer { dynamic, .. } => dynamic, - _ => false, - }; - - let mapped_multisampled = match bind.ty { - BindingType::SampledTexture { multisampled, .. } => multisampled, - _ => false, - }; - - let mapped_view_dimension = match bind.ty { - BindingType::SampledTexture { dimension, .. } - | BindingType::StorageTexture { dimension, .. } => { - map_texture_view_dimension(dimension) - } - _ => web_sys::GpuTextureViewDimension::N2d, - }; - let mut mapped_entry = web_sys::GpuBindGroupLayoutEntry::new( bind.binding, mapped_type, bind.visibility.bits(), ); - mapped_entry.has_dynamic_offset(mapped_dynamic); - mapped_entry.multisampled(mapped_multisampled); - mapped_entry.view_dimension(mapped_view_dimension); - // TODO: Texture component type, storage texture format + match bind.ty { + BindingType::UniformBuffer { dynamic } + | BindingType::StorageBuffer { dynamic, .. } => { + mapped_entry.has_dynamic_offset(dynamic); + } + _ => {} + } + + if let BindingType::SampledTexture { multisampled, .. } = bind.ty { + mapped_entry.multisampled(multisampled); + } + + match bind.ty { + BindingType::SampledTexture { dimension, .. } + | BindingType::StorageTexture { dimension, .. } => { + mapped_entry.view_dimension(map_texture_view_dimension(dimension)); + } + _ => {} + } + + if let BindingType::StorageTexture { format, .. } = bind.ty { + mapped_entry.storage_texture_format(map_texture_format(format)); + } + + match bind.ty { + BindingType::SampledTexture { component_type, .. } + | BindingType::StorageTexture { component_type, .. } => { + mapped_entry.texture_component_type(map_texture_component_type(component_type)); + } + _ => {} + } mapped_entry }) @@ -180,7 +187,6 @@ pub(crate) fn create_pipeline_layout( device: &DeviceId, desc: &PipelineLayoutDescriptor, ) -> PipelineLayoutId { - //TODO: avoid allocation here let temp_layouts = desc .bind_group_layouts .iter() @@ -235,6 +241,16 @@ fn map_texture_format(texture_format: wgt::TextureFormat) -> web_sys::GpuTexture } } +fn map_texture_component_type( + texture_component_type: wgt::TextureComponentType, +) -> web_sys::GpuTextureComponentType { + match texture_component_type { + wgt::TextureComponentType::Float => web_sys::GpuTextureComponentType::Float, + wgt::TextureComponentType::Sint => web_sys::GpuTextureComponentType::Sint, + wgt::TextureComponentType::Uint => web_sys::GpuTextureComponentType::Uint, + } +} + fn map_stage_descriptor( desc: &ProgrammableStageDescriptor, ) -> web_sys::GpuProgrammableStageDescriptor { @@ -794,9 +810,7 @@ pub(crate) fn compute_pass_set_bind_group<'a>( compute_pass.set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length( index, bind_group, - // TODO: `offsets` currently requires `&mut` so we have to clone it - // here, but this should be fixed upstream in web-sys in the future - &mut offsets.to_vec(), + offsets, 0f64, offsets.len() as u32, ); @@ -929,12 +943,26 @@ pub(crate) fn device_create_swap_chain( surface.configure_swap_chain(&mapped) } -pub(crate) fn swap_chain_get_next_texture(swap_chain: &SwapChainId) -> Option { +pub(crate) fn device_drop(_device: &DeviceId) { + // Device is dropped automatically +} + +pub(crate) fn swap_chain_get_next_texture( + swap_chain: &SwapChainId, +) -> Result { // TODO: Should we pass a descriptor here? // Or is the default view always correct? - Some(swap_chain.get_current_texture().create_view()) + Ok(crate::SwapChainOutput { + view: crate::TextureView { + id: swap_chain.get_current_texture().create_view(), + owned: false, + }, + detail: (), + }) } +pub(crate) type SwapChainOutputDetail = (); + fn map_store_op(op: wgt::StoreOp) -> web_sys::GpuStoreOp { match op { wgt::StoreOp::Clear => web_sys::GpuStoreOp::Clear, @@ -1012,9 +1040,7 @@ pub(crate) fn render_pass_set_bind_group( render_pass.set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length( index, bind_group, - // TODO: `offsets` currently requires `&mut` so we have to clone it - // here, but this should be fixed upstream in web-sys in the future - &mut offsets.to_vec(), + offsets, 0f64, offsets.len() as u32, ); @@ -1024,13 +1050,9 @@ pub(crate) fn render_pass_set_index_buffer<'a>( render_pass: &RenderPassEncoderId, buffer: &'a crate::Buffer, offset: wgt::BufferAddress, - _size: wgt::BufferAddress, + size: wgt::BufferAddress, ) { - render_pass.set_index_buffer_with_f64( - &buffer.id, - offset as f64, - // TODO: size, - ); + render_pass.set_index_buffer_with_f64_and_f64(&buffer.id, offset as f64, size as f64); } pub(crate) fn render_pass_set_vertex_buffer<'a>( @@ -1038,14 +1060,9 @@ pub(crate) fn render_pass_set_vertex_buffer<'a>( slot: u32, buffer: &'a crate::Buffer, offset: wgt::BufferAddress, - _size: wgt::BufferAddress, + size: wgt::BufferAddress, ) { - render_pass.set_vertex_buffer_with_f64( - slot, - &buffer.id, - offset as f64, - // TODO: size, - ); + render_pass.set_vertex_buffer_with_f64_and_f64(slot, &buffer.id, offset as f64, size as f64); } pub(crate) fn render_pass_set_scissor_rect( @@ -1151,7 +1168,11 @@ pub(crate) fn texture_view_drop(_texture_view: &TextureViewId) { // Texture view is dropped automatically } -pub(crate) fn swap_chain_present(_swap_chain: &SwapChainId) { +pub(crate) fn bind_group_drop(_bind_group: &BindGroupId) { + // Bind group is dropped automatically +} + +pub(crate) fn swap_chain_present(_swap_chain_output: &crate::SwapChainOutput) { // Swapchain is presented automatically } diff --git a/src/lib.rs b/src/lib.rs index 38a535d3c..af805e22c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,8 +140,7 @@ pub struct BindGroup { impl Drop for BindGroup { fn drop(&mut self) { - #[cfg(not(target_arch = "wasm32"))] - wgn::wgpu_bind_group_destroy(self.id); + backend::bind_group_drop(&self.id); } } @@ -460,7 +459,7 @@ pub struct TextureDescriptor<'a> { #[derive(Debug)] pub struct SwapChainOutput { pub view: TextureView, - swap_chain_id: backend::SwapChainId, + detail: backend::SwapChainOutputDetail, } /// A view of a buffer which can be used to copy to or from a texture. @@ -689,12 +688,7 @@ impl Device { // TODO impl Drop for Device { fn drop(&mut self) { - #[cfg(not(target_arch = "wasm32"))] - wgn::wgpu_device_poll(self.id, true); - //TODO: make this work in general - #[cfg(not(target_arch = "wasm32"))] - #[cfg(feature = "metal-auto-capture")] - wgn::wgpu_device_destroy(self.id); + backend::device_drop(&self.id); } } @@ -1097,7 +1091,7 @@ impl Queue { impl Drop for SwapChainOutput { fn drop(&mut self) { if !thread::panicking() { - backend::swap_chain_present(&self.swap_chain_id); + backend::swap_chain_present(&self); } } } @@ -1113,13 +1107,6 @@ impl SwapChain { /// When the [`SwapChainOutput`] returned by this method is dropped, the swapchain will present /// the texture to the associated [`Surface`]. pub fn get_next_texture(&mut self) -> Result { - match backend::swap_chain_get_next_texture(&self.id) { - Some(id) => Ok(SwapChainOutput { - view: TextureView { id, owned: false }, - // TODO: Remove from web backend - swap_chain_id: self.id.clone(), - }), - None => Err(TimeOut), - } + backend::swap_chain_get_next_texture(&self.id) } }