From 8ed244f6faee3ccd5520269033f5a17bd5c0c467 Mon Sep 17 00:00:00 2001 From: chyyran Date: Thu, 16 Feb 2023 18:19:38 -0500 Subject: [PATCH] rt: clean up sampler sets --- librashader-runtime-d3d11/src/filter_chain.rs | 2 - librashader-runtime-d3d11/src/samplers.rs | 69 +++++++--------- librashader-runtime-d3d12/src/samplers.rs | 66 +++++++--------- librashader-runtime-gl/src/samplers.rs | 79 ++++++------------- .../tests/hello_triangle/gl3.rs | 8 +- .../tests/hello_triangle/gl46.rs | 8 +- librashader-runtime-vk/src/samplers.rs | 34 ++++---- .../tests/hello_triangle/memory.rs | 5 -- .../tests/hello_triangle/util.rs | 15 ---- 9 files changed, 108 insertions(+), 178 deletions(-) diff --git a/librashader-runtime-d3d11/src/filter_chain.rs b/librashader-runtime-d3d11/src/filter_chain.rs index cba373b..f9535a1 100644 --- a/librashader-runtime-d3d11/src/filter_chain.rs +++ b/librashader-runtime-d3d11/src/filter_chain.rs @@ -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, diff --git a/librashader-runtime-d3d11/src/samplers.rs b/librashader-runtime-d3d11/src/samplers.rs index 6a479ed..9ef4afe 100644 --- a/librashader-runtime-d3d11/src/samplers.rs +++ b/librashader-runtime-d3d11/src/samplers.rs @@ -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 { 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 }) } } diff --git a/librashader-runtime-d3d12/src/samplers.rs b/librashader-runtime-d3d12/src/samplers.rs index 4751ace..cf5b9c4 100644 --- a/librashader-runtime-d3d12/src/samplers.rs +++ b/librashader-runtime-d3d12/src/samplers.rs @@ -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 { - 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 { 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, diff --git a/librashader-runtime-gl/src/samplers.rs b/librashader-runtime-gl/src/samplers.rs index ca53b74..2cddc59 100644 --- a/librashader-runtime-gl/src/samplers.rs +++ b/librashader-runtime-gl/src/samplers.rs @@ -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 } } } diff --git a/librashader-runtime-gl/tests/hello_triangle/gl3.rs b/librashader-runtime-gl/tests/hello_triangle/gl3.rs index ee2c5fb..d086bc0 100644 --- a/librashader-runtime-gl/tests/hello_triangle/gl3.rs +++ b/librashader-runtime-gl/tests/hello_triangle/gl3.rs @@ -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. diff --git a/librashader-runtime-gl/tests/hello_triangle/gl46.rs b/librashader-runtime-gl/tests/hello_triangle/gl46.rs index e8c7ec8..fab9a75 100644 --- a/librashader-runtime-gl/tests/hello_triangle/gl46.rs +++ b/librashader-runtime-gl/tests/hello_triangle/gl46.rs @@ -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. diff --git a/librashader-runtime-vk/src/samplers.rs b/librashader-runtime-vk/src/samplers.rs index 12bd258..6102594 100644 --- a/librashader-runtime-vk/src/samplers.rs +++ b/librashader-runtime-vk/src/samplers.rs @@ -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) -> error::Result { @@ -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 }) } } diff --git a/librashader-runtime-vk/tests/hello_triangle/memory.rs b/librashader-runtime-vk/tests/hello_triangle/memory.rs index ad12194..1783f88 100644 --- a/librashader-runtime-vk/tests/hello_triangle/memory.rs +++ b/librashader-runtime-vk/tests/hello_triangle/memory.rs @@ -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 { diff --git a/librashader-runtime-vk/tests/hello_triangle/util.rs b/librashader-runtime-vk/tests/hello_triangle/util.rs index f3f3b5b..077c750 100644 --- a/librashader-runtime-vk/tests/hello_triangle/util.rs +++ b/librashader-runtime-vk/tests/hello_triangle/util.rs @@ -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,