vk: avoid cloning the whole ash::Device object by using an Arc

This commit is contained in:
chyyran 2023-01-14 15:06:43 -05:00
parent 2e2713a21b
commit e652f0bb1d
10 changed files with 31 additions and 21 deletions

View file

@ -26,11 +26,12 @@ use librashader_runtime::uniforms::UniformStorage;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::path::Path; use std::path::Path;
use std::sync::Arc;
use crate::options::{FilterChainOptionsVulkan, FrameOptionsVulkan}; use crate::options::{FilterChainOptionsVulkan, FrameOptionsVulkan};
/// 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: ash::Device, pub(crate) device: Arc<ash::Device>,
pub(crate) memory_properties: vk::PhysicalDeviceMemoryProperties, pub(crate) memory_properties: vk::PhysicalDeviceMemoryProperties,
queue: vk::Queue, queue: vk::Queue,
pipeline_cache: vk::PipelineCache, pipeline_cache: vk::PipelineCache,
@ -77,7 +78,7 @@ impl TryFrom<VulkanInstance> for VulkanObjects {
instance.get_physical_device_memory_properties(vulkan.physical_device); instance.get_physical_device_memory_properties(vulkan.physical_device);
Ok(VulkanObjects { Ok(VulkanObjects {
device, device: Arc::new(device),
queue, queue,
pipeline_cache, pipeline_cache,
memory_properties, memory_properties,
@ -87,10 +88,10 @@ impl TryFrom<VulkanInstance> for VulkanObjects {
} }
} }
impl TryFrom<(vk::PhysicalDevice, ash::Instance, ash::Device)> for VulkanObjects { impl TryFrom<(vk::PhysicalDevice, ash::Instance, Arc<ash::Device>)> for VulkanObjects {
type Error = FilterChainError; type Error = FilterChainError;
fn try_from(value: (vk::PhysicalDevice, ash::Instance, ash::Device)) -> error::Result<Self> { fn try_from(value: (vk::PhysicalDevice, ash::Instance, Arc<ash::Device>)) -> error::Result<Self> {
unsafe { unsafe {
let device = value.2; let device = value.2;
@ -136,7 +137,7 @@ pub(crate) struct FilterCommon {
pub feedback_inputs: Box<[Option<InputImage>]>, pub feedback_inputs: Box<[Option<InputImage>]>,
pub history_textures: Box<[Option<InputImage>]>, pub history_textures: Box<[Option<InputImage>]>,
pub config: FilterMutable, pub config: FilterMutable,
pub device: ash::Device, pub device: Arc<ash::Device>,
} }
/// Contains residual intermediate `VkImageView` and `VkImage` objects created /// Contains residual intermediate `VkImageView` and `VkImage` objects created

View file

@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::{error, VulkanImage}; use crate::{error, VulkanImage};
use crate::filter_chain::FilterCommon; use crate::filter_chain::FilterCommon;
use crate::render_target::RenderTarget; use crate::render_target::RenderTarget;
@ -17,7 +18,7 @@ use librashader_runtime::uniforms::{UniformStorage, UniformStorageAccess};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
pub struct FilterPass { pub struct FilterPass {
pub device: ash::Device, pub device: Arc<ash::Device>,
pub reflection: ShaderReflection, pub reflection: ShaderReflection,
// pub(crate) compiled: ShaderCompilerOutput<Vec<u32>>, // pub(crate) compiled: ShaderCompilerOutput<Vec<u32>>,
pub(crate) uniform_storage: UniformStorage, pub(crate) uniform_storage: UniformStorage,

View file

@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::filter_chain::VulkanObjects; use crate::filter_chain::VulkanObjects;
use crate::texture::VulkanImage; use crate::texture::VulkanImage;
use crate::{error, util}; use crate::{error, util};
@ -8,7 +9,7 @@ use librashader_common::Size;
pub(crate) struct OutputImage { pub(crate) struct OutputImage {
pub size: Size<u32>, pub size: Size<u32>,
pub image_view: vk::ImageView, pub image_view: vk::ImageView,
device: ash::Device, device: Arc<ash::Device>,
image: vk::Image, image: vk::Image,
} }

View file

@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::hello_triangle::physicaldevice::find_queue_family; use crate::hello_triangle::physicaldevice::find_queue_family;
use crate::hello_triangle::vulkan_base::VulkanBase; use crate::hello_triangle::vulkan_base::VulkanBase;
use ash::prelude::VkResult; use ash::prelude::VkResult;
@ -5,7 +6,7 @@ use ash::vk;
pub struct VulkanCommandPool { pub struct VulkanCommandPool {
pool: vk::CommandPool, pool: vk::CommandPool,
device: ash::Device, device: Arc<ash::Device>,
pub buffers: Vec<vk::CommandBuffer>, pub buffers: Vec<vk::CommandBuffer>,
} }

View file

@ -6,6 +6,7 @@ use ash::prelude::VkResult;
use ash::vk; use ash::vk;
use ash::vk::{Extent3D, Handle}; use ash::vk::{Extent3D, Handle};
use std::ffi::CStr; use std::ffi::CStr;
use std::sync::Arc;
pub struct VulkanSwapchain { pub struct VulkanSwapchain {
pub swapchain: vk::SwapchainKHR, pub swapchain: vk::SwapchainKHR,
@ -18,7 +19,7 @@ pub struct VulkanSwapchain {
pub render_images: Vec<(vk::Image, VulkanImageMemory)>, pub render_images: Vec<(vk::Image, VulkanImageMemory)>,
pub render_image_views: Vec<vk::ImageView>, pub render_image_views: Vec<vk::ImageView>,
device: ash::Device, device: Arc<ash::Device>,
} }
impl VulkanSwapchain { impl VulkanSwapchain {
@ -112,7 +113,7 @@ impl VulkanSwapchain {
&base.mem_props, &base.mem_props,
mem_reqs.memory_type_bits, mem_reqs.memory_type_bits,
vk::MemoryPropertyFlags::DEVICE_LOCAL, vk::MemoryPropertyFlags::DEVICE_LOCAL,
)) ).unwrap())
.build(); .build();
// todo: optimize by reusing existing memory. // todo: optimize by reusing existing memory.

View file

@ -9,6 +9,7 @@ use crate::hello_triangle::physicaldevice::{find_queue_family, pick_physical_dev
use crate::hello_triangle::surface::VulkanSurface; use crate::hello_triangle::surface::VulkanSurface;
use ash::prelude::VkResult; use ash::prelude::VkResult;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::sync::Arc;
const WINDOW_TITLE: &'static [u8] = b"librashader Vulkan\0"; const WINDOW_TITLE: &'static [u8] = b"librashader Vulkan\0";
const KHRONOS_VALIDATION: &'static [u8] = b"VK_LAYER_KHRONOS_validation\0"; const KHRONOS_VALIDATION: &'static [u8] = b"VK_LAYER_KHRONOS_validation\0";
@ -16,7 +17,7 @@ const KHRONOS_VALIDATION: &'static [u8] = b"VK_LAYER_KHRONOS_validation\0";
pub struct VulkanBase { pub struct VulkanBase {
pub entry: ash::Entry, pub entry: ash::Entry,
pub instance: ash::Instance, pub instance: ash::Instance,
pub device: ash::Device, pub device: Arc<ash::Device>,
pub graphics_queue: vk::Queue, pub graphics_queue: vk::Queue,
pub debug: VulkanDebug, pub debug: VulkanDebug,
pub physical_device: vk::PhysicalDevice, pub physical_device: vk::PhysicalDevice,
@ -65,7 +66,7 @@ impl VulkanBase {
Ok(VulkanBase { Ok(VulkanBase {
entry, entry,
instance, instance,
device, device: Arc::new(device),
graphics_queue: queue, graphics_queue: queue,
physical_device, physical_device,
mem_props, mem_props,

View file

@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::error; use crate::error;
use ash::vk; use ash::vk;
use librashader_common::{FilterMode, WrapMode}; use librashader_common::{FilterMode, WrapMode};
@ -5,12 +6,12 @@ use rustc_hash::FxHashMap;
pub struct VulkanSampler { pub struct VulkanSampler {
pub handle: vk::Sampler, pub handle: vk::Sampler,
device: ash::Device, device: Arc<ash::Device>,
} }
impl VulkanSampler { impl VulkanSampler {
pub fn new( pub fn new(
device: &ash::Device, device: &Arc<ash::Device>,
wrap: WrapMode, wrap: WrapMode,
filter: FilterMode, filter: FilterMode,
mipmap: FilterMode, mipmap: FilterMode,
@ -61,7 +62,7 @@ impl SamplerSet {
self.samplers.get(&(wrap, filter, mipmap)).unwrap() self.samplers.get(&(wrap, filter, mipmap)).unwrap()
} }
pub fn new(device: &ash::Device) -> error::Result<SamplerSet> { pub fn new(device: &Arc<ash::Device>) -> error::Result<SamplerSet> {
let mut samplers = FxHashMap::default(); let mut samplers = FxHashMap::default();
let wrap_modes = &[ let wrap_modes = &[
WrapMode::ClampToBorder, WrapMode::ClampToBorder,

View file

@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::filter_chain::VulkanObjects; use crate::filter_chain::VulkanObjects;
use crate::util::find_vulkan_memory_type; use crate::util::find_vulkan_memory_type;
use crate::vulkan_primitives::VulkanImageMemory; use crate::vulkan_primitives::VulkanImageMemory;
@ -9,7 +10,7 @@ use librashader_presets::Scale2D;
use librashader_runtime::scaling::{MipmapSize, ViewportSize}; use librashader_runtime::scaling::{MipmapSize, ViewportSize};
pub struct OwnedImage { pub struct OwnedImage {
pub device: ash::Device, pub device: Arc<ash::Device>,
pub mem_props: vk::PhysicalDeviceMemoryProperties, pub mem_props: vk::PhysicalDeviceMemoryProperties,
pub image_view: vk::ImageView, pub image_view: vk::ImageView,
pub image: VulkanImage, pub image: VulkanImage,
@ -28,7 +29,7 @@ pub struct OwnedImageLayout {
impl OwnedImage { impl OwnedImage {
fn new_internal( fn new_internal(
device: ash::Device, device: Arc<ash::Device>,
mem_props: vk::PhysicalDeviceMemoryProperties, mem_props: vk::PhysicalDeviceMemoryProperties,
size: Size<u32>, size: Size<u32>,
mut format: ImageFormat, mut format: ImageFormat,

View file

@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::error; use crate::error;
use crate::vulkan_primitives::VulkanBuffer; use crate::vulkan_primitives::VulkanBuffer;
use ash::vk; use ash::vk;
@ -6,12 +7,12 @@ use librashader_runtime::uniforms::UniformStorageAccess;
pub struct VkUboRing { pub struct VkUboRing {
ring: BoxRingBuffer<VulkanBuffer>, ring: BoxRingBuffer<VulkanBuffer>,
device: ash::Device, device: Arc<ash::Device>,
} }
impl VkUboRing { impl VkUboRing {
pub fn new( pub fn new(
device: &ash::Device, device: &Arc<ash::Device>,
mem_props: &vk::PhysicalDeviceMemoryProperties, mem_props: &vk::PhysicalDeviceMemoryProperties,
ring_size: usize, ring_size: usize,
buffer_size: usize, buffer_size: usize,

View file

@ -1,15 +1,16 @@
use crate::{error, util}; use crate::{error, util};
use ash::vk; use ash::vk;
use std::ffi::c_void; use std::ffi::c_void;
use std::sync::Arc;
pub struct VulkanImageMemory { pub struct VulkanImageMemory {
pub handle: vk::DeviceMemory, pub handle: vk::DeviceMemory,
device: ash::Device, device: Arc<ash::Device>,
} }
impl VulkanImageMemory { impl VulkanImageMemory {
pub fn new( pub fn new(
device: &ash::Device, device: &Arc<ash::Device>,
alloc: &vk::MemoryAllocateInfo, alloc: &vk::MemoryAllocateInfo,
) -> error::Result<VulkanImageMemory> { ) -> error::Result<VulkanImageMemory> {
unsafe { unsafe {