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

Change MemoryDevice::map/unmap_memory #34

Merged
merged 2 commits into from
Jan 19, 2021
Merged

Change MemoryDevice::map/unmap_memory #34

merged 2 commits into from
Jan 19, 2021

Conversation

zakarumych
Copy link
Owner

Now those methods require &mut M
e.g. externally synchronized memory object

Fixes #33

Now those methods require &mut M
e.g. externally synchronized memory object

Fixes #33
Copy link
Contributor

@kvark kvark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, if this doesn't look wonderful, then I don't know what does!
Great work on addressing the API concern!
I noted a few things to improve, overall very happy with the code. Looking forward to see it released soon on crates :)

core::{
convert::TryFrom as _,
ptr::{copy_nonoverlapping, NonNull},
sync::atomic::{AtomicU8, Ordering::*},
// sync::atomic::{AtomicU8, Ordering::*},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line needs to be removed

@@ -158,37 +159,33 @@ impl<M> MemoryBlock<M> {
"`offset + size` is out of memory block bounds"
);

let ptr = match self.flavor {
MemoryBlockFlavor::Dedicated => {
let ptr = match &mut self.flavor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, in gfx/wgpu code we prefer to not match on references. It's your call here, so no objections, just fyi

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And use ref and ref mut in patterns? As I understand it, the semantics is the same. Just choice of style and MSRV

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. My preference is to make this explicit at the match pattern side, i.e. knowing the type being matched it's easier to see what is referenced, how deep, what's moved, or copied otherwise, based on those ref.

@@ -370,3 +318,21 @@ impl<M> MemoryBlock<M> {
self.props.contains(MemoryPropertyFlags::HOST_CACHED)
}
}

fn acquire_mapping(mapped: &mut bool) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could probably be merged with release_mapping into something like switch_mapping(mapped: &mut bool, to: bool) -> bool

"All blocks must be deallocated before cleanup"
);
device.deallocate_memory(chunk.memory);

match Arc::try_unwrap(chunk.memory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's redundant to call is_unique only to follow by try_unwrap. They do the same thing

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent was to panic in debug here and merely whining into traces in release

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can still do this without is_unique, can't you?

Err(_) => {
  debug_assert!(false, "All blocks must be deallocated before cleanup"),
  #[cfg(feature = "tracing")]
  tracing::error!("Cleanup before all memory blocks are deallocated");
}
``

if chunk.allocated == 0 {
drop(block);

if is_unique(&mut chunk.memory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could also do Arc::get_mut here and scrap is_unique completely

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arc::get_mut costs more

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting indeed. It does the atomics on the weak count, which you don't want to, reasonably.
You'd need to make it so the debug_assert code inside is_unique doesn't run in optimized builds. Otherwise, as the PR stands, it also calls get_mut internally, anyway.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, debug_assert code does not run in optimized builds by definition of debug_assert.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh it runs, it just doesn't panic. That's one of the weird things I stumbled upon back in the day.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. It doesn't run, but compiles.
debug_assert!(expr) expands to if cfg!(debug_assertions) { assert!(expr); }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, indeed! I got confused here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the type checks (i.e. using something that's under #[cfg(debug_assertions)] is not possible in there), not run-time, so that was my confusion. Thank you for correcting me!

@zakarumych zakarumych merged commit 915d0da into master Jan 19, 2021
bors bot added a commit to gfx-rs/wgpu that referenced this pull request Jan 20, 2021
1160: Update gpu-alloc and remove the associated locking r=kvark a=kvark

**Connections**
Takes advantage of zakarumych/gpu-alloc#34

**Description**
This PR removes the locking from our gpu-alloc wrapper.

**Testing**
Untested

Co-authored-by: Dzmitry Malyshau <[email protected]>
@zakarumych zakarumych deleted the map_unmap_mut branch September 3, 2021 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mutable self for memory mapping
2 participants