hal/dx12: sampler and query set creation

This commit is contained in:
Dzmitry Malyshau committed 2021-07-13 01:01:23 -04:00
1 parent 04ca3212de
commit 904621ee19
3 files changed
+127 -11

No files matched your search

+41
View File
@@ -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<wgt::SamplerBorderColor>) -> [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],
}
}
+68 -8
View File
@@ -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<super::Api> 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<super::Api> for super::Device {
&self,
desc: &crate::SamplerDescriptor,
) -> Result<super::Sampler, crate::DeviceError> {
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<super::Api>,
) -> Result<super::CommandEncoder, crate::DeviceError> {
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<super::Api> for super::Device {
&self,
desc: &wgt::QuerySetDescriptor<crate::Label>,
) -> Result<super::QuerySet, crate::DeviceError> {
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<super::Fence, crate::DeviceError> {
let mut raw = native::Fence::null();
+18 -3
View File
@@ -165,6 +165,7 @@ pub struct Device {
rtv_pool: Mutex<descriptor::CpuPool>,
dsv_pool: Mutex<descriptor::CpuPool>,
srv_uav_pool: Mutex<descriptor::CpuPool>,
sampler_pool: Mutex<descriptor::CpuPool>,
}
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<native::GraphicsCommandList>,
}
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 {