Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store Vulkan device as a reference to avoid huge struct sizes #259

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/vulkan-buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn main() {
// Setting up the allocator
let mut allocator = Allocator::new(&AllocatorCreateDesc {
instance: instance.clone(),
device: device.clone(),
device: &device,
physical_device: pdevice,
debug_settings: Default::default(),
buffer_device_address: false,
Expand Down
26 changes: 13 additions & 13 deletions src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ unsafe impl Send for SendSyncPtr {}
// `mapped_ptr` themselves.
unsafe impl Sync for SendSyncPtr {}

pub struct AllocatorCreateDesc {
pub struct AllocatorCreateDesc<'a> {
pub instance: ash::Instance,
pub device: ash::Device,
pub device: &'a ash::Device,
pub physical_device: vk::PhysicalDevice,
pub debug_settings: AllocatorDebugSettings,
pub buffer_device_address: bool,
Expand Down Expand Up @@ -674,23 +674,23 @@ impl MemoryType {
}
}

pub struct Allocator {
pub struct Allocator<'a> {
pub(crate) memory_types: Vec<MemoryType>,
pub(crate) memory_heaps: Vec<vk::MemoryHeap>,
device: ash::Device,
device: &'a ash::Device,
pub(crate) buffer_image_granularity: u64,
pub(crate) debug_settings: AllocatorDebugSettings,
allocation_sizes: AllocationSizes,
}

impl fmt::Debug for Allocator {
impl fmt::Debug for Allocator<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.generate_report().fmt(f)
}
}

impl Allocator {
pub fn new(desc: &AllocatorCreateDesc) -> Result<Self> {
impl<'a> Allocator<'a> {
pub fn new(desc: &AllocatorCreateDesc<'a>) -> Result<Self> {
if desc.physical_device == vk::PhysicalDevice::null() {
return Err(AllocationError::InvalidAllocatorCreateDesc(
"AllocatorCreateDesc field `physical_device` is null.".into(),
Expand Down Expand Up @@ -754,7 +754,7 @@ impl Allocator {
Ok(Self {
memory_types,
memory_heaps,
device: desc.device.clone(),
device: desc.device,
buffer_image_granularity: granularity,
debug_settings: desc.debug_settings,
allocation_sizes: desc.allocation_sizes,
Expand Down Expand Up @@ -827,7 +827,7 @@ impl Allocator {
Err(AllocationError::OutOfMemory)
} else {
memory_type.allocate(
&self.device,
self.device,
desc,
self.buffer_image_granularity,
backtrace.clone(),
Expand All @@ -849,7 +849,7 @@ impl Allocator {
};

self.memory_types[memory_type_index].allocate(
&self.device,
self.device,
desc,
self.buffer_image_granularity,
backtrace,
Expand Down Expand Up @@ -877,7 +877,7 @@ impl Allocator {
return Ok(());
}

self.memory_types[allocation.memory_type_index].free(allocation, &self.device)?;
self.memory_types[allocation.memory_type_index].free(allocation, self.device)?;

Ok(())
}
Expand Down Expand Up @@ -955,7 +955,7 @@ impl Allocator {
}
}

impl Drop for Allocator {
impl Drop for Allocator<'_> {
fn drop(&mut self) {
if self.debug_settings.log_leaks_on_shutdown {
self.report_memory_leaks(Level::Warn);
Expand All @@ -966,7 +966,7 @@ impl Drop for Allocator {
for mem_block in mem_type.memory_blocks.iter_mut() {
let block = mem_block.take();
if let Some(block) = block {
block.destroy(&self.device);
block.destroy(self.device);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/vulkan/visualizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl AllocatorVisualizer {
self.color_scheme = color_scheme;
}

pub fn render_memory_block_ui(&mut self, ui: &mut egui::Ui, alloc: &Allocator) {
pub fn render_memory_block_ui(&mut self, ui: &mut egui::Ui, alloc: &Allocator<'_>) {
ui.label(format!(
"buffer image granularity: {:?}",
alloc.buffer_image_granularity
Expand Down Expand Up @@ -145,7 +145,7 @@ impl AllocatorVisualizer {
pub fn render_memory_block_window(
&mut self,
ctx: &egui::Context,
allocator: &Allocator,
allocator: &Allocator<'_>,
open: &mut bool,
) {
egui::Window::new("Allocator Memory Blocks")
Expand All @@ -156,7 +156,7 @@ impl AllocatorVisualizer {
pub fn render_memory_block_visualization_windows(
&mut self,
ctx: &egui::Context,
allocator: &Allocator,
allocator: &Allocator<'_>,
) {
// Draw each window.
let color_scheme = &self.color_scheme;
Expand Down Expand Up @@ -200,7 +200,7 @@ impl AllocatorVisualizer {
});
}

pub fn render_breakdown_ui(&mut self, ui: &mut egui::Ui, allocator: &Allocator) {
pub fn render_breakdown_ui(&mut self, ui: &mut egui::Ui, allocator: &Allocator<'_>) {
render_allocation_reports_ui(
ui,
&mut self.breakdown_settings,
Expand All @@ -216,7 +216,7 @@ impl AllocatorVisualizer {
pub fn render_breakdown_window(
&mut self,
ctx: &egui::Context,
allocator: &Allocator,
allocator: &Allocator<'_>,
open: &mut bool,
) {
egui::Window::new("Allocator Breakdown")
Expand Down