librashader/librashader-runtime-d3d12/src/samplers.rs

71 lines
2.5 KiB
Rust
Raw Normal View History

2023-02-02 10:09:34 +11:00
use crate::descriptor_heap::{D3D12DescriptorHeap, D3D12DescriptorHeapSlot, SamplerPaletteHeap};
2023-02-06 08:17:23 +11:00
use crate::error;
2023-01-25 15:15:43 +11:00
use librashader_common::{FilterMode, WrapMode};
use rustc_hash::FxHashMap;
2023-02-06 08:17:23 +11:00
use std::ops::Deref;
2023-01-25 15:15:43 +11:00
use windows::Win32::Graphics::Direct3D12::{
ID3D12Device, D3D12_COMPARISON_FUNC_NEVER, D3D12_FLOAT32_MAX, D3D12_SAMPLER_DESC,
D3D12_TEXTURE_ADDRESS_MODE,
};
2023-01-22 07:34:13 +11:00
pub struct SamplerSet {
2023-01-25 15:15:43 +11:00
samplers: FxHashMap<(WrapMode, FilterMode), D3D12DescriptorHeapSlot<SamplerPaletteHeap>>,
2023-02-10 13:03:55 +11:00
_heap: D3D12DescriptorHeap<SamplerPaletteHeap>,
2023-01-24 18:02:27 +11:00
}
impl SamplerSet {
2023-02-17 10:19:38 +11:00
#[inline(always)]
2023-01-25 15:15:43 +11:00
pub fn get(
&self,
wrap: WrapMode,
filter: FilterMode,
) -> &D3D12DescriptorHeapSlot<SamplerPaletteHeap> {
2023-02-17 10:19:38 +11:00
// SAFETY: the sampler set is complete for the matrix
// wrap x filter
unsafe {
self.samplers.get(&(wrap, filter)).unwrap_unchecked()
}
2023-01-24 18:02:27 +11:00
}
pub fn new(device: &ID3D12Device) -> error::Result<SamplerSet> {
let mut samplers = FxHashMap::default();
let wrap_modes = &[
WrapMode::ClampToBorder,
WrapMode::ClampToEdge,
WrapMode::Repeat,
WrapMode::MirroredRepeat,
];
2023-02-01 09:50:47 +11:00
let mut heap = D3D12DescriptorHeap::new(device, 2 * wrap_modes.len())?;
2023-01-24 18:02:27 +11:00
for wrap_mode in wrap_modes {
2023-02-17 10:19:38 +11:00
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
unsafe {
let sampler = heap.alloc_slot()?;
device.CreateSampler(
&D3D12_SAMPLER_DESC {
Filter: FilterMode::Linear.into(),
AddressU: D3D12_TEXTURE_ADDRESS_MODE::from(*wrap_mode),
AddressV: D3D12_TEXTURE_ADDRESS_MODE::from(*wrap_mode),
AddressW: D3D12_TEXTURE_ADDRESS_MODE::from(*wrap_mode),
MipLODBias: 0.0,
MaxAnisotropy: 1,
ComparisonFunc: D3D12_COMPARISON_FUNC_NEVER,
BorderColor: [0.0, 0.0, 0.0, 0.0],
MinLOD: -D3D12_FLOAT32_MAX,
MaxLOD: D3D12_FLOAT32_MAX,
},
*sampler.deref().as_ref(),
);
samplers.insert((*wrap_mode, *filter_mode), sampler);
}
2023-01-24 18:02:27 +11:00
}
}
2023-02-17 10:19:38 +11:00
assert_eq!(samplers.len(), wrap_modes.len() * 2);
2023-02-10 13:03:55 +11:00
Ok(SamplerSet {
samplers,
_heap: heap,
})
2023-01-24 18:02:27 +11:00
}
2023-01-22 07:34:13 +11:00
}