rt: clean up sampler sets

This commit is contained in:
chyyran 2023-02-16 18:19:38 -05:00
parent 302ef76a82
commit 8ed244f6fa
9 changed files with 108 additions and 178 deletions

View file

@ -137,7 +137,6 @@ impl FilterChainD3D11 {
// initialize passes // initialize passes
let filters = FilterChainD3D11::init_passes(device, passes, &semantics, disable_cache)?; let filters = FilterChainD3D11::init_passes(device, passes, &semantics, disable_cache)?;
println!("passes loded");
let immediate_context = unsafe { device.GetImmediateContext()? }; let immediate_context = unsafe { device.GetImmediateContext()? };
@ -165,7 +164,6 @@ impl FilterChainD3D11 {
let draw_quad = DrawQuad::new(device)?; let draw_quad = DrawQuad::new(device)?;
let state = D3D11State::new(device)?; let state = D3D11State::new(device)?;
println!("ready");
Ok(FilterChainD3D11 { Ok(FilterChainD3D11 {
passes: filters, passes: filters,
output_framebuffers, output_framebuffers,

View file

@ -10,9 +10,15 @@ pub struct SamplerSet {
} }
impl SamplerSet { impl SamplerSet {
#[inline(always)]
pub fn get(&self, wrap: WrapMode, filter: FilterMode) -> &ID3D11SamplerState { pub fn get(&self, wrap: WrapMode, filter: FilterMode) -> &ID3D11SamplerState {
self.samplers.get(&(wrap, filter)).unwrap() // SAFETY: the sampler set is complete for the matrix
// wrap x filter
unsafe {
self.samplers.get(&(wrap, filter)).unwrap_unchecked()
} }
}
pub fn new(device: &ID3D11Device) -> Result<SamplerSet> { pub fn new(device: &ID3D11Device) -> Result<SamplerSet> {
let mut samplers = FxHashMap::default(); let mut samplers = FxHashMap::default();
let wrap_modes = &[ let wrap_modes = &[
@ -22,8 +28,9 @@ impl SamplerSet {
WrapMode::MirroredRepeat, WrapMode::MirroredRepeat,
]; ];
for wrap_mode in wrap_modes { for wrap_mode in wrap_modes {
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
unsafe { unsafe {
let mut linear = None; let mut sampler = None;
device.CreateSamplerState( device.CreateSamplerState(
&D3D11_SAMPLER_DESC { &D3D11_SAMPLER_DESC {
Filter: FilterMode::Linear.into(), Filter: FilterMode::Linear.into(),
@ -37,34 +44,16 @@ impl SamplerSet {
MinLOD: -D3D11_FLOAT32_MAX, MinLOD: -D3D11_FLOAT32_MAX,
MaxLOD: D3D11_FLOAT32_MAX, MaxLOD: D3D11_FLOAT32_MAX,
}, },
Some(&mut linear), Some(&mut sampler),
)?; )?;
assume_d3d11_init!(linear, "CreateSamplerState"); assume_d3d11_init!(sampler, "CreateSamplerState");
samplers.insert((*wrap_mode, *filter_mode), sampler);
let mut nearest = None; }
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,
},
Some(&mut nearest),
)?;
assume_d3d11_init!(nearest, "CreateSamplerState");
samplers.insert((*wrap_mode, FilterMode::Linear), linear);
samplers.insert((*wrap_mode, FilterMode::Nearest), nearest);
} }
} }
assert_eq!(samplers.len(), wrap_modes.len() * 2);
Ok(SamplerSet { samplers }) Ok(SamplerSet { samplers })
} }
} }

View file

@ -1,3 +1,4 @@
use std::iter::Filter;
use crate::descriptor_heap::{D3D12DescriptorHeap, D3D12DescriptorHeapSlot, SamplerPaletteHeap}; use crate::descriptor_heap::{D3D12DescriptorHeap, D3D12DescriptorHeapSlot, SamplerPaletteHeap};
use crate::error; use crate::error;
use librashader_common::{FilterMode, WrapMode}; use librashader_common::{FilterMode, WrapMode};
@ -14,12 +15,17 @@ pub struct SamplerSet {
} }
impl SamplerSet { impl SamplerSet {
#[inline(always)]
pub fn get( pub fn get(
&self, &self,
wrap: WrapMode, wrap: WrapMode,
filter: FilterMode, filter: FilterMode,
) -> &D3D12DescriptorHeapSlot<SamplerPaletteHeap> { ) -> &D3D12DescriptorHeapSlot<SamplerPaletteHeap> {
self.samplers.get(&(wrap, filter)).unwrap() // SAFETY: the sampler set is complete for the matrix
// wrap x filter
unsafe {
self.samplers.get(&(wrap, filter)).unwrap_unchecked()
}
} }
pub fn new(device: &ID3D12Device) -> error::Result<SamplerSet> { pub fn new(device: &ID3D12Device) -> error::Result<SamplerSet> {
let mut samplers = FxHashMap::default(); let mut samplers = FxHashMap::default();
@ -33,8 +39,9 @@ impl SamplerSet {
let mut heap = D3D12DescriptorHeap::new(device, 2 * wrap_modes.len())?; let mut heap = D3D12DescriptorHeap::new(device, 2 * wrap_modes.len())?;
for wrap_mode in wrap_modes { for wrap_mode in wrap_modes {
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
unsafe { unsafe {
let linear = heap.alloc_slot()?; let sampler = heap.alloc_slot()?;
device.CreateSampler( device.CreateSampler(
&D3D12_SAMPLER_DESC { &D3D12_SAMPLER_DESC {
Filter: FilterMode::Linear.into(), Filter: FilterMode::Linear.into(),
@ -48,31 +55,14 @@ impl SamplerSet {
MinLOD: -D3D12_FLOAT32_MAX, MinLOD: -D3D12_FLOAT32_MAX,
MaxLOD: D3D12_FLOAT32_MAX, MaxLOD: D3D12_FLOAT32_MAX,
}, },
*linear.deref().as_ref(), *sampler.deref().as_ref(),
); );
samplers.insert((*wrap_mode, *filter_mode), sampler);
let 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.deref().as_ref(),
);
samplers.insert((*wrap_mode, FilterMode::Linear), linear);
samplers.insert((*wrap_mode, FilterMode::Nearest), nearest);
} }
} }
assert_eq!(samplers.len(), wrap_modes.len() * 2);
Ok(SamplerSet { Ok(SamplerSet {
samplers, samplers,
_heap: heap, _heap: heap,

View file

@ -8,9 +8,13 @@ pub struct SamplerSet {
} }
impl SamplerSet { impl SamplerSet {
#[inline(always)]
pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> GLuint { pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> GLuint {
// eprintln!("{wrap}, {filter}, {mip}"); // SAFETY: the sampler set is complete for the matrix
*self.samplers.get(&(wrap, filter, mipmap)).unwrap() // wrap x filter x mipmap
unsafe {
*self.samplers.get(&(wrap, filter, mipmap)).unwrap_unchecked()
}
} }
fn make_sampler(sampler: GLuint, wrap: WrapMode, filter: FilterMode, mip: FilterMode) { fn make_sampler(sampler: GLuint, wrap: WrapMode, filter: FilterMode, mip: FilterMode) {
@ -36,62 +40,29 @@ impl SamplerSet {
WrapMode::MirroredRepeat, WrapMode::MirroredRepeat,
]; ];
for wrap_mode in wrap_modes { for wrap_mode in wrap_modes {
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
for mip_filter in &[FilterMode::Linear, FilterMode::Nearest] {
let mut sampler = 0;
unsafe { unsafe {
let mut linear_linear = 0; gl::GenSamplers(1, &mut sampler);
let mut linear_nearest = 0;
let mut nearest_nearest = 0;
let mut nearest_linear = 0;
gl::GenSamplers(1, &mut linear_linear);
gl::GenSamplers(1, &mut linear_nearest);
gl::GenSamplers(1, &mut nearest_linear);
gl::GenSamplers(1, &mut nearest_nearest);
SamplerSet::make_sampler( SamplerSet::make_sampler(
linear_linear, sampler,
*wrap_mode, *wrap_mode,
FilterMode::Linear, *filter_mode,
FilterMode::Linear, *mip_filter,
);
SamplerSet::make_sampler(
linear_nearest,
*wrap_mode,
FilterMode::Linear,
FilterMode::Nearest,
);
SamplerSet::make_sampler(
nearest_linear,
*wrap_mode,
FilterMode::Nearest,
FilterMode::Linear,
);
SamplerSet::make_sampler(
nearest_nearest,
*wrap_mode,
FilterMode::Nearest,
FilterMode::Nearest,
); );
samplers.insert( samplers.insert(
(*wrap_mode, FilterMode::Linear, FilterMode::Linear), (*wrap_mode, *filter_mode, *mip_filter),
linear_linear, sampler,
);
samplers.insert(
(*wrap_mode, FilterMode::Linear, FilterMode::Nearest),
linear_nearest,
);
samplers.insert(
(*wrap_mode, FilterMode::Nearest, FilterMode::Nearest),
nearest_nearest,
);
samplers.insert(
(*wrap_mode, FilterMode::Nearest, FilterMode::Linear),
nearest_linear,
); );
} }
} }
}
}
// assert all samplers were created.
assert_eq!(samplers.len(), wrap_modes.len() * 2 * 2);
SamplerSet { samplers } SamplerSet { samplers }
} }
} }

View file

@ -515,9 +515,11 @@ void main()
}, },
}; };
unsafe {
filter filter
.frame(&rendered, &viewport, framecount, None) .frame(&rendered, &viewport, framecount, None)
.unwrap(); .unwrap();
}
unsafe { unsafe {
// texture is done now. // texture is done now.

View file

@ -509,9 +509,11 @@ void main()
}, },
}; };
unsafe {
filter filter
.frame(&rendered, &viewport, framecount, None) .frame(&rendered, &viewport, framecount, None)
.unwrap(); .unwrap();
}
unsafe { unsafe {
// texture is done now. // texture is done now.

View file

@ -56,9 +56,14 @@ pub struct SamplerSet {
} }
impl SamplerSet { impl SamplerSet {
#[inline(always)]
pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> &VulkanSampler { pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> &VulkanSampler {
// eprintln!("{wrap}, {filter}, {mip}"); // eprintln!("{wrap}, {filter}, {mip}");
self.samplers.get(&(wrap, filter, mipmap)).unwrap() // SAFETY: the sampler set is complete for the matrix
// wrap x filter x mipmap
unsafe {
self.samplers.get(&(wrap, filter, mipmap)).unwrap_unchecked()
}
} }
pub fn new(device: &Arc<ash::Device>) -> error::Result<SamplerSet> { pub fn new(device: &Arc<ash::Device>) -> error::Result<SamplerSet> {
@ -70,25 +75,18 @@ impl SamplerSet {
WrapMode::MirroredRepeat, WrapMode::MirroredRepeat,
]; ];
for wrap_mode in wrap_modes { for wrap_mode in wrap_modes {
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
for mipmap_filter in &[FilterMode::Linear, FilterMode::Nearest] {
samplers.insert( samplers.insert(
(*wrap_mode, FilterMode::Linear, FilterMode::Linear), (*wrap_mode, *filter_mode, *mipmap_filter),
VulkanSampler::new(device, *wrap_mode, FilterMode::Linear, FilterMode::Linear)?, VulkanSampler::new(device, *wrap_mode, *filter_mode, *mipmap_filter)?,
);
samplers.insert(
(*wrap_mode, FilterMode::Linear, FilterMode::Nearest),
VulkanSampler::new(device, *wrap_mode, FilterMode::Linear, FilterMode::Nearest)?,
);
samplers.insert(
(*wrap_mode, FilterMode::Nearest, FilterMode::Nearest),
VulkanSampler::new(device, *wrap_mode, FilterMode::Nearest, FilterMode::Nearest)?,
);
samplers.insert(
(*wrap_mode, FilterMode::Nearest, FilterMode::Linear),
VulkanSampler::new(device, *wrap_mode, FilterMode::Nearest, FilterMode::Linear)?,
); );
} }
}
}
// assert all samplers were created.
assert_eq!(samplers.len(), wrap_modes.len() * 2 * 2);
Ok(SamplerSet { samplers }) Ok(SamplerSet { samplers })
} }
} }

View file

@ -1,14 +1,9 @@
use ash::vk; use ash::vk;
use gpu_allocator::vulkan::{Allocation, AllocationCreateDesc, AllocationScheme, Allocator}; use gpu_allocator::vulkan::{Allocation, AllocationCreateDesc, AllocationScheme, Allocator};
use gpu_allocator::MemoryLocation; use gpu_allocator::MemoryLocation;
use librashader_runtime::uniforms::UniformStorageAccess;
use parking_lot::RwLock; use parking_lot::RwLock;
use ash::prelude::VkResult; use ash::prelude::VkResult;
use std::ffi::c_void;
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
use std::ptr::NonNull;
use std::sync::Arc; use std::sync::Arc;
pub struct VulkanImageMemory { pub struct VulkanImageMemory {

View file

@ -4,21 +4,6 @@ use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc};
use parking_lot::RwLock; use parking_lot::RwLock;
use std::sync::Arc; use std::sync::Arc;
use librashader_reflect::reflect::semantics::BindingStage;
pub fn binding_stage_to_vulkan_stage(stage_mask: BindingStage) -> vk::ShaderStageFlags {
let mut mask = vk::ShaderStageFlags::default();
if stage_mask.contains(BindingStage::VERTEX) {
mask |= vk::ShaderStageFlags::VERTEX;
}
if stage_mask.contains(BindingStage::FRAGMENT) {
mask |= vk::ShaderStageFlags::FRAGMENT;
}
mask
}
#[inline(always)] #[inline(always)]
pub unsafe fn vulkan_image_layout_transition_levels( pub unsafe fn vulkan_image_layout_transition_levels(
device: &ash::Device, device: &ash::Device,