rt(vk): switch from an rwlock to a mutex for allocator

This commit is contained in:
chyyran 2024-02-28 18:04:33 -05:00 committed by Ronny Chan
parent 10ad2d927c
commit 752417f320
9 changed files with 32 additions and 32 deletions

View file

@ -4,7 +4,7 @@ use array_concat::concat_arrays;
use ash::vk; use ash::vk;
use gpu_allocator::vulkan::Allocator; use gpu_allocator::vulkan::Allocator;
use librashader_runtime::quad::{QuadType, VertexInput}; use librashader_runtime::quad::{QuadType, VertexInput};
use parking_lot::RwLock; use parking_lot::Mutex;
use std::sync::Arc; use std::sync::Arc;
const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [ const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
@ -54,7 +54,7 @@ pub struct DrawQuad {
impl DrawQuad { impl DrawQuad {
pub fn new( pub fn new(
device: &Arc<ash::Device>, device: &Arc<ash::Device>,
allocator: &Arc<RwLock<Allocator>>, allocator: &Arc<Mutex<Allocator>>,
) -> error::Result<DrawQuad> { ) -> error::Result<DrawQuad> {
let mut buffer = VulkanBuffer::new( let mut buffer = VulkanBuffer::new(
device, device,

View file

@ -26,7 +26,7 @@ use librashader_runtime::binding::BindingUtil;
use librashader_runtime::image::{Image, ImageError, UVDirection, BGRA8}; use librashader_runtime::image::{Image, ImageError, UVDirection, BGRA8};
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::QuadType;
use librashader_runtime::uniforms::UniformStorage; use librashader_runtime::uniforms::UniformStorage;
use parking_lot::RwLock; use parking_lot::Mutex;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::convert::Infallible; use std::convert::Infallible;
use std::path::Path; use std::path::Path;
@ -43,7 +43,7 @@ use rayon::prelude::*;
/// A Vulkan device and metadata that is required by the shader runtime. /// A Vulkan device and metadata that is required by the shader runtime.
pub struct VulkanObjects { pub struct VulkanObjects {
pub(crate) device: Arc<ash::Device>, pub(crate) device: Arc<ash::Device>,
pub(crate) alloc: Arc<RwLock<Allocator>>, pub(crate) alloc: Arc<Mutex<Allocator>>,
queue: vk::Queue, queue: vk::Queue,
// pub(crate) memory_properties: vk::PhysicalDeviceMemoryProperties, // pub(crate) memory_properties: vk::PhysicalDeviceMemoryProperties,
} }

View file

@ -4,7 +4,7 @@ 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 librashader_runtime::uniforms::UniformStorageAccess;
use parking_lot::RwLock; use parking_lot::Mutex;
use std::ffi::c_void; use std::ffi::c_void;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
@ -14,17 +14,17 @@ use std::sync::Arc;
pub struct VulkanImageMemory { pub struct VulkanImageMemory {
allocation: Option<Allocation>, allocation: Option<Allocation>,
allocator: Arc<RwLock<Allocator>>, allocator: Arc<Mutex<Allocator>>,
} }
impl VulkanImageMemory { impl VulkanImageMemory {
pub fn new( pub fn new(
device: &Arc<ash::Device>, device: &Arc<ash::Device>,
allocator: &Arc<RwLock<Allocator>>, allocator: &Arc<Mutex<Allocator>>,
requirements: vk::MemoryRequirements, requirements: vk::MemoryRequirements,
image: &vk::Image, image: &vk::Image,
) -> error::Result<VulkanImageMemory> { ) -> error::Result<VulkanImageMemory> {
let allocation = allocator.write().allocate(&AllocationCreateDesc { let allocation = allocator.lock().allocate(&AllocationCreateDesc {
name: "imagemem", name: "imagemem",
requirements, requirements,
location: MemoryLocation::GpuOnly, location: MemoryLocation::GpuOnly,
@ -46,7 +46,7 @@ impl Drop for VulkanImageMemory {
fn drop(&mut self) { fn drop(&mut self) {
let allocation = self.allocation.take(); let allocation = self.allocation.take();
if let Some(allocation) = allocation { if let Some(allocation) = allocation {
if let Err(e) = self.allocator.write().free(allocation) { if let Err(e) = self.allocator.lock().free(allocation) {
println!("librashader-runtime-vk: [warn] failed to deallocate image buffer {e}") println!("librashader-runtime-vk: [warn] failed to deallocate image buffer {e}")
} }
} }
@ -57,14 +57,14 @@ pub struct VulkanBuffer {
pub handle: vk::Buffer, pub handle: vk::Buffer,
device: Arc<ash::Device>, device: Arc<ash::Device>,
memory: Option<Allocation>, memory: Option<Allocation>,
allocator: Arc<RwLock<Allocator>>, allocator: Arc<Mutex<Allocator>>,
size: vk::DeviceSize, size: vk::DeviceSize,
} }
impl VulkanBuffer { impl VulkanBuffer {
pub fn new( pub fn new(
device: &Arc<ash::Device>, device: &Arc<ash::Device>,
allocator: &Arc<RwLock<Allocator>>, allocator: &Arc<Mutex<Allocator>>,
usage: vk::BufferUsageFlags, usage: vk::BufferUsageFlags,
size: usize, size: usize,
) -> error::Result<VulkanBuffer> { ) -> error::Result<VulkanBuffer> {
@ -77,7 +77,7 @@ impl VulkanBuffer {
let memory_reqs = device.get_buffer_memory_requirements(buffer); let memory_reqs = device.get_buffer_memory_requirements(buffer);
let alloc = allocator.write().allocate(&AllocationCreateDesc { let alloc = allocator.lock().allocate(&AllocationCreateDesc {
name: "buffer", name: "buffer",
requirements: memory_reqs, requirements: memory_reqs,
location: MemoryLocation::CpuToGpu, location: MemoryLocation::CpuToGpu,
@ -113,7 +113,7 @@ impl Drop for VulkanBuffer {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
if let Some(allocation) = self.memory.take() { if let Some(allocation) = self.memory.take() {
if let Err(e) = self.allocator.write().free(allocation) { if let Err(e) = self.allocator.lock().free(allocation) {
println!( println!(
"librashader-runtime-vk: [warn] failed to deallocate buffer memory {e}" "librashader-runtime-vk: [warn] failed to deallocate buffer memory {e}"
) )
@ -141,7 +141,7 @@ pub struct RawVulkanBuffer {
impl RawVulkanBuffer { impl RawVulkanBuffer {
pub fn new( pub fn new(
device: &Arc<ash::Device>, device: &Arc<ash::Device>,
allocator: &Arc<RwLock<Allocator>>, allocator: &Arc<Mutex<Allocator>>,
usage: vk::BufferUsageFlags, usage: vk::BufferUsageFlags,
size: usize, size: usize,
) -> error::Result<Self> { ) -> error::Result<Self> {

View file

@ -3,7 +3,7 @@ use crate::memory::VulkanImageMemory;
use crate::{error, util}; use crate::{error, util};
use ash::vk; use ash::vk;
use gpu_allocator::vulkan::Allocator; use gpu_allocator::vulkan::Allocator;
use parking_lot::RwLock; use parking_lot::Mutex;
use std::sync::Arc; use std::sync::Arc;
use crate::error::FilterChainError; use crate::error::FilterChainError;
@ -13,7 +13,7 @@ use librashader_runtime::scaling::{MipmapSize, ScaleFramebuffer, ViewportSize};
pub struct OwnedImage { pub struct OwnedImage {
pub device: Arc<ash::Device>, pub device: Arc<ash::Device>,
pub allocator: Arc<RwLock<Allocator>>, pub allocator: Arc<Mutex<Allocator>>,
pub image_view: vk::ImageView, pub image_view: vk::ImageView,
pub image: VulkanImage, pub image: VulkanImage,
pub memory: VulkanImageMemory, pub memory: VulkanImageMemory,
@ -33,7 +33,7 @@ pub struct OwnedImageLayout {
impl OwnedImage { impl OwnedImage {
fn new_internal( fn new_internal(
device: Arc<ash::Device>, device: Arc<ash::Device>,
alloc: &Arc<RwLock<Allocator>>, alloc: &Arc<Mutex<Allocator>>,
size: Size<u32>, size: Size<u32>,
mut format: ImageFormat, mut format: ImageFormat,
max_miplevels: u32, max_miplevels: u32,

View file

@ -2,7 +2,7 @@ use ash::vk;
use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc}; use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc};
use gpu_allocator::AllocationSizes; use gpu_allocator::AllocationSizes;
use parking_lot::RwLock; use parking_lot::Mutex;
use std::sync::Arc; use std::sync::Arc;
use crate::error; use crate::error;
@ -95,7 +95,7 @@ pub fn create_allocator(
device: ash::Device, device: ash::Device,
instance: ash::Instance, instance: ash::Instance,
physical_device: vk::PhysicalDevice, physical_device: vk::PhysicalDevice,
) -> error::Result<Arc<RwLock<Allocator>>> { ) -> error::Result<Arc<Mutex<Allocator>>> {
let alloc = Allocator::new(&AllocatorCreateDesc { let alloc = Allocator::new(&AllocatorCreateDesc {
instance, instance,
device, device,
@ -104,5 +104,5 @@ pub fn create_allocator(
buffer_device_address: false, buffer_device_address: false,
allocation_sizes: AllocationSizes::default(), allocation_sizes: AllocationSizes::default(),
})?; })?;
Ok(Arc::new(RwLock::new(alloc))) Ok(Arc::new(Mutex::new(alloc)))
} }

View file

@ -1,25 +1,25 @@
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 parking_lot::RwLock; use parking_lot::Mutex;
use ash::prelude::VkResult; use ash::prelude::VkResult;
use std::sync::Arc; use std::sync::Arc;
pub struct VulkanImageMemory { pub struct VulkanImageMemory {
allocation: Option<Allocation>, allocation: Option<Allocation>,
allocator: Arc<RwLock<Allocator>>, allocator: Arc<Mutex<Allocator>>,
} }
impl VulkanImageMemory { impl VulkanImageMemory {
pub fn new( pub fn new(
device: &Arc<ash::Device>, device: &Arc<ash::Device>,
allocator: &Arc<RwLock<Allocator>>, allocator: &Arc<Mutex<Allocator>>,
requirements: vk::MemoryRequirements, requirements: vk::MemoryRequirements,
image: &vk::Image, image: &vk::Image,
) -> VkResult<VulkanImageMemory> { ) -> VkResult<VulkanImageMemory> {
let allocation = allocator let allocation = allocator
.write() .lock()
.allocate(&AllocationCreateDesc { .allocate(&AllocationCreateDesc {
name: "imagemem", name: "imagemem",
requirements, requirements,
@ -43,7 +43,7 @@ impl Drop for VulkanImageMemory {
fn drop(&mut self) { fn drop(&mut self) {
let allocation = self.allocation.take(); let allocation = self.allocation.take();
if let Some(allocation) = allocation { if let Some(allocation) = allocation {
if let Err(e) = self.allocator.write().free(allocation) { if let Err(e) = self.allocator.lock().free(allocation) {
println!("librashader-runtime-vk: [warn] failed to deallocate image buffer {e}") println!("librashader-runtime-vk: [warn] failed to deallocate image buffer {e}")
} }
} }

View file

@ -26,8 +26,8 @@ use vulkan_base::VulkanBase;
use librashader_common::Viewport; use librashader_common::Viewport;
use librashader_runtime_vk::options::FrameOptionsVulkan; use librashader_runtime_vk::options::FrameOptionsVulkan;
use winit::event::{ElementState, Event, WindowEvent}; use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop, EventLoopBuilder}; use winit::event_loop::{EventLoop, EventLoopBuilder};
use winit::platform::windows::EventLoopBuilderExtWindows; use winit::platform::windows::EventLoopBuilderExtWindows;
// Constants // Constants

View file

@ -2,7 +2,7 @@ use ash::vk;
use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc}; use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc};
use gpu_allocator::AllocationSizes; use gpu_allocator::AllocationSizes;
use parking_lot::RwLock; use parking_lot::Mutex;
use std::sync::Arc; use std::sync::Arc;
#[inline(always)] #[inline(always)]
@ -50,7 +50,7 @@ pub fn create_allocator(
device: ash::Device, device: ash::Device,
instance: ash::Instance, instance: ash::Instance,
physical_device: vk::PhysicalDevice, physical_device: vk::PhysicalDevice,
) -> Arc<RwLock<Allocator>> { ) -> Arc<Mutex<Allocator>> {
let alloc = Allocator::new(&AllocatorCreateDesc { let alloc = Allocator::new(&AllocatorCreateDesc {
instance, instance,
device, device,
@ -60,5 +60,5 @@ pub fn create_allocator(
allocation_sizes: AllocationSizes::default(), allocation_sizes: AllocationSizes::default(),
}) })
.unwrap(); .unwrap();
Arc::new(RwLock::new(alloc)) Arc::new(Mutex::new(alloc))
} }

View file

@ -7,7 +7,7 @@ use ash::prelude::VkResult;
use gpu_allocator::vulkan::Allocator; use gpu_allocator::vulkan::Allocator;
use librashader_runtime_vk::error::FilterChainError; use librashader_runtime_vk::error::FilterChainError;
use librashader_runtime_vk::VulkanObjects; use librashader_runtime_vk::VulkanObjects;
use parking_lot::RwLock; use parking_lot::Mutex;
use std::ffi::CStr; use std::ffi::CStr;
use std::sync::Arc; use std::sync::Arc;
@ -22,7 +22,7 @@ pub struct VulkanBase {
// pub debug: VulkanDebug, // pub debug: VulkanDebug,
pub physical_device: vk::PhysicalDevice, pub physical_device: vk::PhysicalDevice,
pub mem_props: vk::PhysicalDeviceMemoryProperties, pub mem_props: vk::PhysicalDeviceMemoryProperties,
pub allocator: Arc<RwLock<Allocator>>, pub allocator: Arc<Mutex<Allocator>>,
} }
impl VulkanBase { impl VulkanBase {