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

58 lines
2.2 KiB
Rust
Raw Normal View History

2023-01-17 10:45:02 +11:00
use crate::error::{assume_d3d11_init, Result};
2022-11-30 17:38:05 +11:00
use librashader_common::{FilterMode, WrapMode};
use rustc_hash::FxHashMap;
use windows::Win32::Graphics::Direct3D11::{
ID3D11Device, ID3D11SamplerState, D3D11_COMPARISON_NEVER, D3D11_FLOAT32_MAX,
D3D11_SAMPLER_DESC, D3D11_TEXTURE_ADDRESS_MODE,
};
2022-11-27 07:55:14 +11:00
pub struct SamplerSet {
2022-11-30 17:38:05 +11:00
samplers: FxHashMap<(WrapMode, FilterMode), ID3D11SamplerState>,
2022-11-27 07:55:14 +11:00
}
impl SamplerSet {
2023-02-17 10:19:38 +11:00
#[inline(always)]
2022-11-27 11:35:33 +11:00
pub fn get(&self, wrap: WrapMode, filter: FilterMode) -> &ID3D11SamplerState {
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() }
2022-11-27 11:35:33 +11:00
}
2023-02-17 10:19:38 +11:00
2022-11-27 07:55:14 +11:00
pub fn new(device: &ID3D11Device) -> Result<SamplerSet> {
let mut samplers = FxHashMap::default();
2022-11-30 17:38:05 +11:00
let wrap_modes = &[
WrapMode::ClampToBorder,
WrapMode::ClampToEdge,
WrapMode::Repeat,
WrapMode::MirroredRepeat,
];
2022-11-27 07:55:14 +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 mut sampler = None;
device.CreateSamplerState(
&D3D11_SAMPLER_DESC {
2023-02-22 09:07:48 +11:00
Filter: filter_mode.into(),
2023-02-17 10:19:38 +11:00
AddressU: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode),
AddressV: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode),
AddressW: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode),
MipLODBias: 0.0,
MaxAnisotropy: 1,
ComparisonFunc: D3D11_COMPARISON_NEVER,
BorderColor: [0.0, 0.0, 0.0, 0.0],
MinLOD: -D3D11_FLOAT32_MAX,
MaxLOD: D3D11_FLOAT32_MAX,
},
Some(&mut sampler),
)?;
2022-11-27 07:55:14 +11:00
2023-02-17 10:19:38 +11:00
assume_d3d11_init!(sampler, "CreateSamplerState");
samplers.insert((*wrap_mode, *filter_mode), sampler);
}
2022-11-27 07:55:14 +11:00
}
}
2023-02-17 10:19:38 +11:00
assert_eq!(samplers.len(), wrap_modes.len() * 2);
2022-11-30 17:38:05 +11:00
Ok(SamplerSet { samplers })
2022-11-27 07:55:14 +11:00
}
}