Skip to content

Commit

Permalink
Misc clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
attackgoat committed Feb 21, 2024
1 parent b5883ec commit 84adb90
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 134 deletions.
2 changes: 2 additions & 0 deletions contrib/rel-mgmt/check
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cargo fmt --manifest-path examples/vr/Cargo.toml && diff || fail "Unformatted ru

# Rust code errors
cargo check --all-targets
cargo check --all-targets --features parking_lot
cargo check --manifest-path contrib/screen-13-egui/Cargo.toml --all-targets --all-features
cargo check --manifest-path contrib/screen-13-fx/Cargo.toml --all-targets --all-features
cargo check --manifest-path contrib/screen-13-hot/Cargo.toml --all-targets --all-features
Expand All @@ -36,6 +37,7 @@ cargo check --manifest-path examples/vr/Cargo.toml --all-targets --all-features

# Rust code lints
cargo clippy --all-targets
cargo clippy --all-targets --features parking_lot
cargo clippy --manifest-path contrib/screen-13-egui/Cargo.toml --all-targets --all-features
cargo clippy --manifest-path contrib/screen-13-fx/Cargo.toml --all-targets --all-features
cargo clippy --manifest-path contrib/screen-13-hot/Cargo.toml --all-targets --all-features
Expand Down
3 changes: 1 addition & 2 deletions src/driver/accel_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ impl AccelerationStructure {
static TLS: RefCell<Tls> = Default::default();
}

TLS.with(|tls| {
let mut tls = tls.borrow_mut();
TLS.with_borrow_mut(|tls| {
tls.geometries.clear();
tls.max_primitive_counts.clear();

Expand Down
20 changes: 8 additions & 12 deletions src/driver/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
log::warn,
std::{
fmt::{Debug, Formatter},
mem::ManuallyDrop,
ops::{Deref, Range},
sync::{
atomic::{AtomicU8, Ordering},
Expand Down Expand Up @@ -51,7 +52,7 @@ use {
/// [deref]: core::ops::Deref
/// [fully qualified syntax]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name
pub struct Buffer {
allocation: Option<Allocation>,
allocation: ManuallyDrop<Allocation>,
buffer: vk::Buffer,
device: Arc<Device>,

Expand Down Expand Up @@ -120,7 +121,7 @@ impl Buffer {
profiling::scope!("allocate");

#[cfg_attr(not(feature = "parking_lot"), allow(unused_mut))]
let mut allocator = device.allocator.as_ref().unwrap().lock();
let mut allocator = device.allocator.lock();

#[cfg(not(feature = "parking_lot"))]
let mut allocator = allocator.unwrap();
Expand Down Expand Up @@ -152,7 +153,7 @@ impl Buffer {
};

Ok(Self {
allocation: Some(allocation),
allocation: ManuallyDrop::new(allocation),
buffer,
device,
info,
Expand Down Expand Up @@ -350,7 +351,7 @@ impl Buffer {
"Buffer is not mappable - create using mappable flag"
);

&this.allocation.as_ref().unwrap().mapped_slice().unwrap()[0..this.info.size as usize]
&this.allocation.mapped_slice().unwrap()[0..this.info.size as usize]
}

/// Returns a mapped mutable slice.
Expand Down Expand Up @@ -388,12 +389,7 @@ impl Buffer {
"Buffer is not mappable - create using mappable flag"
);

&mut this
.allocation
.as_mut()
.unwrap()
.mapped_slice_mut()
.unwrap()[0..this.info.size as usize]
&mut this.allocation.mapped_slice_mut().unwrap()[0..this.info.size as usize]
}
}

Expand Down Expand Up @@ -426,12 +422,12 @@ impl Drop for Buffer {
profiling::scope!("deallocate");

#[cfg_attr(not(feature = "parking_lot"), allow(unused_mut))]
let mut allocator = self.device.allocator.as_ref().unwrap().lock();
let mut allocator = self.device.allocator.lock();

#[cfg(not(feature = "parking_lot"))]
let mut allocator = allocator.unwrap();

allocator.free(self.allocation.take().unwrap())
allocator.free(unsafe { ManuallyDrop::take(&mut self.allocation) })
}
.unwrap_or_else(|_| warn!("Unable to free buffer allocation"));

Expand Down
18 changes: 10 additions & 8 deletions src/driver/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use {
ffi::CStr,
fmt::{Debug, Formatter},
iter::{empty, repeat},
mem::forget,
mem::{forget, ManuallyDrop},
ops::Deref,
thread::panicking,
time::Instant,
Expand All @@ -36,7 +36,7 @@ pub type SelectPhysicalDeviceFn = dyn FnOnce(&[PhysicalDevice]) -> usize;
pub struct Device {
pub(crate) accel_struct_ext: Option<khr::AccelerationStructure>,

pub(super) allocator: Option<Mutex<Allocator>>,
pub(super) allocator: ManuallyDrop<Mutex<Allocator>>,

device: ash::Device,

Expand Down Expand Up @@ -310,7 +310,7 @@ impl Device {

Ok(Self {
accel_struct_ext,
allocator: Some(Mutex::new(allocator)),
allocator: ManuallyDrop::new(Mutex::new(allocator)),
device,
instance,
physical_device,
Expand Down Expand Up @@ -435,20 +435,22 @@ impl Drop for Device {
fn drop(&mut self) {
if panicking() {
// When panicking we don't want the GPU allocator to complain about leaks
forget(self.allocator.take().unwrap());
unsafe {
forget(ManuallyDrop::take(&mut self.allocator));
}

return;
}

// trace!("drop");

let res = unsafe { self.device.device_wait_idle() };

if let Err(err) = res {
if let Err(err) = unsafe { self.device.device_wait_idle() } {
warn!("device_wait_idle() failed: {err}");
}

self.allocator.take().unwrap();
unsafe {
ManuallyDrop::drop(&mut self.allocator);
}

unsafe {
self.device.destroy_device(None);
Expand Down
4 changes: 2 additions & 2 deletions src/driver/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Image {
profiling::scope!("allocate");

#[cfg_attr(not(feature = "parking_lot"), allow(unused_mut))]
let mut allocator = device.allocator.as_ref().unwrap().lock();
let mut allocator = device.allocator.lock();

#[cfg(not(feature = "parking_lot"))]
let mut allocator = allocator.unwrap();
Expand Down Expand Up @@ -264,7 +264,7 @@ impl Image {
profiling::scope!("deallocate");

#[cfg_attr(not(feature = "parking_lot"), allow(unused_mut))]
let mut allocator = this.device.allocator.as_ref().unwrap().lock();
let mut allocator = this.device.allocator.lock();

#[cfg(not(feature = "parking_lot"))]
let mut allocator = allocator.unwrap();
Expand Down
Loading

0 comments on commit 84adb90

Please sign in to comment.