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 std::collections::VecDeque;
use std::path::Path;
use std::sync::Arc;
use crate::options::{FilterChainOptionsVulkan, FrameOptionsVulkan};
/// A Vulkan device and metadata that is required by the shader runtime.
pub struct VulkanObjects {
pub(crate) device: ash::Device,
pub(crate) device: Arc<ash::Device>,
pub(crate) memory_properties: vk::PhysicalDeviceMemoryProperties,
queue: vk::Queue,
pipeline_cache: vk::PipelineCache,
@ -77,7 +78,7 @@ impl TryFrom<VulkanInstance> for VulkanObjects {
instance.get_physical_device_memory_properties(vulkan.physical_device);
Ok(VulkanObjects {
device,
device: Arc::new(device),
queue,
pipeline_cache,
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;
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 {
let device = value.2;
@ -136,7 +137,7 @@ pub(crate) struct FilterCommon {
pub feedback_inputs: Box<[Option<InputImage>]>,
pub history_textures: Box<[Option<InputImage>]>,
pub config: FilterMutable,
pub device: ash::Device,
pub device: Arc<ash::Device>,
}
/// Contains residual intermediate `VkImageView` and `VkImage` objects created

View file

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

View file

@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::filter_chain::VulkanObjects;
use crate::texture::VulkanImage;
use crate::{error, util};
@ -8,7 +9,7 @@ use librashader_common::Size;
pub(crate) struct OutputImage {
pub size: Size<u32>,
pub image_view: vk::ImageView,
device: ash::Device,
device: Arc<ash::Device>,
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::vulkan_base::VulkanBase;
use ash::prelude::VkResult;
@ -5,7 +6,7 @@ use ash::vk;
pub struct VulkanCommandPool {
pool: vk::CommandPool,
device: ash::Device,
device: Arc<ash::Device>,
pub buffers: Vec<vk::CommandBuffer>,
}

View file

@ -6,6 +6,7 @@ use ash::prelude::VkResult;
use ash::vk;
use ash::vk::{Extent3D, Handle};
use std::ffi::CStr;
use std::sync::Arc;
pub struct VulkanSwapchain {
pub swapchain: vk::SwapchainKHR,
@ -18,7 +19,7 @@ pub struct VulkanSwapchain {
pub render_images: Vec<(vk::Image, VulkanImageMemory)>,
pub render_image_views: Vec<vk::ImageView>,
device: ash::Device,
device: Arc<ash::Device>,
}
impl VulkanSwapchain {
@ -112,7 +113,7 @@ impl VulkanSwapchain {
&base.mem_props,
mem_reqs.memory_type_bits,
vk::MemoryPropertyFlags::DEVICE_LOCAL,
))
).unwrap())
.build();
// 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 ash::prelude::VkResult;
use std::ffi::{CStr, CString};
use std::sync::Arc;
const WINDOW_TITLE: &'static [u8] = b"librashader Vulkan\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 entry: ash::Entry,
pub instance: ash::Instance,
pub device: ash::Device,
pub device: Arc<ash::Device>,
pub graphics_queue: vk::Queue,
pub debug: VulkanDebug,
pub physical_device: vk::PhysicalDevice,
@ -65,7 +66,7 @@ impl VulkanBase {
Ok(VulkanBase {
entry,
instance,
device,
device: Arc::new(device),
graphics_queue: queue,
physical_device,
mem_props,

View file

@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::error;
use ash::vk;
use librashader_common::{FilterMode, WrapMode};
@ -5,12 +6,12 @@ use rustc_hash::FxHashMap;
pub struct VulkanSampler {
pub handle: vk::Sampler,
device: ash::Device,
device: Arc<ash::Device>,
}
impl VulkanSampler {
pub fn new(
device: &ash::Device,
device: &Arc<ash::Device>,
wrap: WrapMode,
filter: FilterMode,
mipmap: FilterMode,
@ -61,7 +62,7 @@ impl SamplerSet {
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 wrap_modes = &[
WrapMode::ClampToBorder,

View file

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

View file

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

View file

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