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

72 lines
2.9 KiB
Rust
Raw Normal View History

2023-01-22 07:34:13 +11:00
use rustc_hash::FxHashMap;
2023-01-24 18:02:27 +11:00
use windows::Win32::Graphics::Direct3D12::{D3D12_COMPARISON_FUNC_NEVER, D3D12_FLOAT32_MAX, D3D12_SAMPLER_DESC, D3D12_TEXTURE_ADDRESS_MODE, ID3D12Device};
2023-01-22 07:34:13 +11:00
use librashader_common::{FilterMode, WrapMode};
2023-01-24 18:02:27 +11:00
use crate::heap::{D3D12DescriptorHeap, D3D12DescriptorHeapSlot, SamplerHeap};
use crate::error;
2023-01-22 07:34:13 +11:00
pub struct SamplerSet {
2023-01-24 18:02:27 +11:00
samplers: FxHashMap<(WrapMode, FilterMode), D3D12DescriptorHeapSlot<SamplerHeap>>,
heap: D3D12DescriptorHeap<SamplerHeap>
}
impl SamplerSet {
pub fn get(&self, wrap: WrapMode, filter: FilterMode) -> &D3D12DescriptorHeapSlot<SamplerHeap> {
self.samplers.get(&(wrap, filter)).unwrap()
}
pub fn new(device: &ID3D12Device) -> error::Result<SamplerSet> {
let mut samplers = FxHashMap::default();
let wrap_modes = &[
WrapMode::ClampToBorder,
WrapMode::ClampToEdge,
WrapMode::Repeat,
WrapMode::MirroredRepeat,
];
let mut heap = D3D12DescriptorHeap::new(&device, (2 * wrap_modes.len()))?;
for wrap_mode in wrap_modes {
unsafe {
let mut linear = 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,
},
*linear.as_ref(),
);
let mut nearest = heap.alloc_slot()?;
device.CreateSampler(
&D3D12_SAMPLER_DESC {
Filter: FilterMode::Nearest.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,
},
*nearest.as_ref()
);
samplers.insert((*wrap_mode, FilterMode::Linear), linear);
samplers.insert((*wrap_mode, FilterMode::Nearest), nearest);
}
}
Ok(SamplerSet { samplers, heap })
}
2023-01-22 07:34:13 +11:00
}