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

View file

@ -10,9 +10,15 @@ pub struct SamplerSet {
}
impl SamplerSet {
#[inline(always)]
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> {
let mut samplers = FxHashMap::default();
let wrap_modes = &[
@ -22,49 +28,32 @@ impl SamplerSet {
WrapMode::MirroredRepeat,
];
for wrap_mode in wrap_modes {
unsafe {
let mut linear = None;
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,
},
Some(&mut linear),
)?;
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
unsafe {
let mut sampler = None;
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,
},
Some(&mut sampler),
)?;
assume_d3d11_init!(linear, "CreateSamplerState");
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);
assume_d3d11_init!(sampler, "CreateSamplerState");
samplers.insert((*wrap_mode, *filter_mode), sampler);
}
}
}
assert_eq!(samplers.len(), wrap_modes.len() * 2);
Ok(SamplerSet { samplers })
}
}

View file

@ -1,3 +1,4 @@
use std::iter::Filter;
use crate::descriptor_heap::{D3D12DescriptorHeap, D3D12DescriptorHeapSlot, SamplerPaletteHeap};
use crate::error;
use librashader_common::{FilterMode, WrapMode};
@ -14,12 +15,17 @@ pub struct SamplerSet {
}
impl SamplerSet {
#[inline(always)]
pub fn get(
&self,
wrap: WrapMode,
filter: FilterMode,
) -> &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> {
let mut samplers = FxHashMap::default();
@ -33,46 +39,30 @@ impl SamplerSet {
let mut heap = D3D12DescriptorHeap::new(device, 2 * wrap_modes.len())?;
for wrap_mode in wrap_modes {
unsafe {
let 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.deref().as_ref(),
);
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);
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);
}
}
}
assert_eq!(samplers.len(), wrap_modes.len() * 2);
Ok(SamplerSet {
samplers,
_heap: heap,

View file

@ -8,9 +8,13 @@ pub struct SamplerSet {
}
impl SamplerSet {
#[inline(always)]
pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> GLuint {
// 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()
}
}
fn make_sampler(sampler: GLuint, wrap: WrapMode, filter: FilterMode, mip: FilterMode) {
@ -36,62 +40,29 @@ impl SamplerSet {
WrapMode::MirroredRepeat,
];
for wrap_mode in wrap_modes {
unsafe {
let mut linear_linear = 0;
let mut linear_nearest = 0;
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
for mip_filter in &[FilterMode::Linear, FilterMode::Nearest] {
let mut sampler = 0;
unsafe {
gl::GenSamplers(1, &mut sampler);
SamplerSet::make_sampler(
sampler,
*wrap_mode,
*filter_mode,
*mip_filter,
);
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(
linear_linear,
*wrap_mode,
FilterMode::Linear,
FilterMode::Linear,
);
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(
(*wrap_mode, FilterMode::Linear, FilterMode::Linear),
linear_linear,
);
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,
);
samplers.insert(
(*wrap_mode, *filter_mode, *mip_filter),
sampler,
);
}
}
}
}
// assert all samplers were created.
assert_eq!(samplers.len(), wrap_modes.len() * 2 * 2);
SamplerSet { samplers }
}
}

View file

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

View file

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

View file

@ -56,9 +56,14 @@ pub struct SamplerSet {
}
impl SamplerSet {
#[inline(always)]
pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> &VulkanSampler {
// 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> {
@ -70,25 +75,18 @@ impl SamplerSet {
WrapMode::MirroredRepeat,
];
for wrap_mode in wrap_modes {
samplers.insert(
(*wrap_mode, FilterMode::Linear, FilterMode::Linear),
VulkanSampler::new(device, *wrap_mode, FilterMode::Linear, FilterMode::Linear)?,
);
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)?,
);
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
for mipmap_filter in &[FilterMode::Linear, FilterMode::Nearest] {
samplers.insert(
(*wrap_mode, *filter_mode, *mipmap_filter),
VulkanSampler::new(device, *wrap_mode, *filter_mode, *mipmap_filter)?,
);
}
}
}
// assert all samplers were created.
assert_eq!(samplers.len(), wrap_modes.len() * 2 * 2);
Ok(SamplerSet { samplers })
}
}

View file

@ -1,14 +1,9 @@
use ash::vk;
use gpu_allocator::vulkan::{Allocation, AllocationCreateDesc, AllocationScheme, Allocator};
use gpu_allocator::MemoryLocation;
use librashader_runtime::uniforms::UniformStorageAccess;
use parking_lot::RwLock;
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;
pub struct VulkanImageMemory {

View file

@ -4,21 +4,6 @@ use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc};
use parking_lot::RwLock;
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)]
pub unsafe fn vulkan_image_layout_transition_levels(
device: &ash::Device,