From 904621ee19ddc3b5f8d36d77a26c28c6a5e8a0fa Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Wed, 7 Jul 2021 00:34:11 -0400 Subject: [PATCH] hal/dx12: sampler and query set creation --- wgpu-hal/src/dx12/conv.rs | 41 ++++++++++++++++++++ wgpu-hal/src/dx12/device.rs | 76 +++++++++++++++++++++++++++++++++---- wgpu-hal/src/dx12/mod.rs | 21 ++++++++-- 3 files changed, 127 insertions(+), 11 deletions(-) diff --git a/wgpu-hal/src/dx12/conv.rs b/wgpu-hal/src/dx12/conv.rs index 42fb7c62b..a4266f235 100644 --- a/wgpu-hal/src/dx12/conv.rs +++ b/wgpu-hal/src/dx12/conv.rs @@ -159,3 +159,44 @@ pub fn map_texture_usage_to_resource_flags( flags } + +pub fn map_address_mode(mode: wgt::AddressMode) -> d3d12::D3D12_TEXTURE_ADDRESS_MODE { + use wgt::AddressMode as Am; + match mode { + Am::Repeat => d3d12::D3D12_TEXTURE_ADDRESS_MODE_WRAP, + Am::MirrorRepeat => d3d12::D3D12_TEXTURE_ADDRESS_MODE_MIRROR, + Am::ClampToEdge => d3d12::D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + Am::ClampToBorder => d3d12::D3D12_TEXTURE_ADDRESS_MODE_BORDER, + //Am::MirrorClamp => d3d12::D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE, + } +} + +pub fn map_filter_mode(mode: wgt::FilterMode) -> d3d12::D3D12_FILTER_TYPE { + match mode { + wgt::FilterMode::Nearest => d3d12::D3D12_FILTER_TYPE_POINT, + wgt::FilterMode::Linear => d3d12::D3D12_FILTER_TYPE_LINEAR, + } +} + +pub fn map_comparison(func: wgt::CompareFunction) -> d3d12::D3D12_COMPARISON_FUNC { + use wgt::CompareFunction as Cf; + match func { + Cf::Never => d3d12::D3D12_COMPARISON_FUNC_NEVER, + Cf::Less => d3d12::D3D12_COMPARISON_FUNC_LESS, + Cf::LessEqual => d3d12::D3D12_COMPARISON_FUNC_LESS_EQUAL, + Cf::Equal => d3d12::D3D12_COMPARISON_FUNC_EQUAL, + Cf::GreaterEqual => d3d12::D3D12_COMPARISON_FUNC_GREATER_EQUAL, + Cf::Greater => d3d12::D3D12_COMPARISON_FUNC_GREATER, + Cf::NotEqual => d3d12::D3D12_COMPARISON_FUNC_NOT_EQUAL, + Cf::Always => d3d12::D3D12_COMPARISON_FUNC_ALWAYS, + } +} + +pub fn map_border_color(border_color: Option) -> [f32; 4] { + use wgt::SamplerBorderColor as Sbc; + match border_color { + Some(Sbc::TransparentBlack) | None => [0.0; 4], + Some(Sbc::OpaqueBlack) => [0.0, 0.0, 0.0, 1.0], + Some(Sbc::OpaqueWhite) => [1.0; 4], + } +} diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 7c5aeccb3..9b489c337 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -53,6 +53,10 @@ impl super::Device { raw, native::DescriptorHeapType::CbvSrvUav, )), + sampler_pool: Mutex::new(descriptor::CpuPool::new( + raw, + native::DescriptorHeapType::Sampler, + )), }) } @@ -418,11 +422,10 @@ impl super::Device { impl crate::Device for super::Device { unsafe fn exit(self) { - //self.heap_srv_cbv_uav.0.destroy(); - //self.samplers.destroy(); self.rtv_pool.into_inner().destroy(); self.dsv_pool.into_inner().destroy(); self.srv_uav_pool.into_inner().destroy(); + self.sampler_pool.into_inner().destroy(); //self.descriptor_updater.lock().destroy(); @@ -651,17 +654,61 @@ impl crate::Device for super::Device { &self, desc: &crate::SamplerDescriptor, ) -> Result { - Ok(super::Sampler {}) + let handle = self.sampler_pool.lock().alloc_handle(); + + let reduction = match desc.compare { + Some(_) => d3d12::D3D12_FILTER_REDUCTION_TYPE_COMPARISON, + None => d3d12::D3D12_FILTER_REDUCTION_TYPE_STANDARD, + }; + let filter = conv::map_filter_mode(desc.min_filter) << d3d12::D3D12_MIN_FILTER_SHIFT + | conv::map_filter_mode(desc.mag_filter) << d3d12::D3D12_MAG_FILTER_SHIFT + | conv::map_filter_mode(desc.mipmap_filter) << d3d12::D3D12_MIP_FILTER_SHIFT + | reduction << d3d12::D3D12_FILTER_REDUCTION_TYPE_SHIFT + | desc + .anisotropy_clamp + .map_or(0, |_| d3d12::D3D12_FILTER_ANISOTROPIC); + + self.raw.create_sampler( + handle.raw, + filter, + [ + conv::map_address_mode(desc.address_modes[0]), + conv::map_address_mode(desc.address_modes[1]), + conv::map_address_mode(desc.address_modes[2]), + ], + 0.0, + desc.anisotropy_clamp.map_or(0, |aniso| aniso.get() as u32), + conv::map_comparison(desc.compare.unwrap_or(wgt::CompareFunction::Always)), + conv::map_border_color(desc.border_color), + desc.lod_clamp.clone().unwrap_or(0.0..16.0), + ); + + Ok(super::Sampler { handle }) + } + unsafe fn destroy_sampler(&self, sampler: super::Sampler) { + self.sampler_pool.lock().free_handle(sampler.handle); } - unsafe fn destroy_sampler(&self, sampler: super::Sampler) {} unsafe fn create_command_encoder( &self, desc: &crate::CommandEncoderDescriptor, ) -> Result { - Ok(super::CommandEncoder {}) + let allocator = self + .raw + .create_command_allocator(native::CmdListType::Direct) + .to_device_result("Command allocator creation")?; + Ok(super::CommandEncoder { + allocator, + list: None, + }) + } + unsafe fn destroy_command_encoder(&self, encoder: super::CommandEncoder) { + if let Some(list) = encoder.list { + list.close(); + list.destroy(); + } + encoder.allocator.destroy(); } - unsafe fn destroy_command_encoder(&self, encoder: super::CommandEncoder) {} unsafe fn create_bind_group_layout( &self, @@ -712,9 +759,22 @@ impl crate::Device for super::Device { &self, desc: &wgt::QuerySetDescriptor, ) -> Result { - Ok(super::QuerySet {}) + let heap_ty = match desc.ty { + wgt::QueryType::Occlusion => native::QueryHeapType::Occlusion, + wgt::QueryType::PipelineStatistics(_) => native::QueryHeapType::PipelineStatistics, + wgt::QueryType::Timestamp => native::QueryHeapType::Timestamp, + }; + + let raw = self + .raw + .create_query_heap(heap_ty, desc.count, 0) + .to_device_result("Query heap creation")?; + + Ok(super::QuerySet { raw, ty: desc.ty }) + } + unsafe fn destroy_query_set(&self, set: super::QuerySet) { + set.raw.destroy(); } - unsafe fn destroy_query_set(&self, set: super::QuerySet) {} unsafe fn create_fence(&self) -> Result { let mut raw = native::Fence::null(); diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index 6cec7e7fc..62a620c3e 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -165,6 +165,7 @@ pub struct Device { rtv_pool: Mutex, dsv_pool: Mutex, srv_uav_pool: Mutex, + sampler_pool: Mutex, } unsafe impl Send for Device {} @@ -177,7 +178,13 @@ pub struct Queue { unsafe impl Send for Queue {} unsafe impl Sync for Queue {} -pub struct CommandEncoder {} +pub struct CommandEncoder { + allocator: native::CommandAllocator, + list: Option, +} + +unsafe impl Send for CommandEncoder {} +unsafe impl Sync for CommandEncoder {} #[derive(Debug)] pub struct Buffer { @@ -210,13 +217,21 @@ unsafe impl Send for TextureView {} unsafe impl Sync for TextureView {} #[derive(Debug)] -pub struct Sampler {} +pub struct Sampler { + handle: descriptor::Handle, +} unsafe impl Send for Sampler {} unsafe impl Sync for Sampler {} #[derive(Debug)] -pub struct QuerySet {} +pub struct QuerySet { + raw: native::QueryHeap, + ty: wgt::QueryType, +} + +unsafe impl Send for QuerySet {} +unsafe impl Sync for QuerySet {} #[derive(Debug)] pub struct Fence {