2022-12-01 11:05:12 +11:00
|
|
|
use crate::error::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 {
|
2022-11-27 11:35:33 +11:00
|
|
|
pub fn get(&self, wrap: WrapMode, filter: FilterMode) -> &ID3D11SamplerState {
|
2022-11-30 17:38:05 +11:00
|
|
|
self.samplers.get(&(wrap, filter)).unwrap()
|
2022-11-27 11:35:33 +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 {
|
|
|
|
unsafe {
|
|
|
|
let linear = device.CreateSamplerState(&D3D11_SAMPLER_DESC {
|
|
|
|
Filter: FilterMode::Linear.into(),
|
|
|
|
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,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let nearest = device.CreateSamplerState(&D3D11_SAMPLER_DESC {
|
|
|
|
Filter: FilterMode::Nearest.into(),
|
|
|
|
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,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
samplers.insert((*wrap_mode, FilterMode::Linear), linear);
|
|
|
|
samplers.insert((*wrap_mode, FilterMode::Nearest), nearest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:38:05 +11:00
|
|
|
Ok(SamplerSet { samplers })
|
2022-11-27 07:55:14 +11:00
|
|
|
}
|
|
|
|
}
|