Skip to content

Conversation

@sagudev
Copy link
Collaborator

@sagudev sagudev commented Aug 4, 2025

Connections
Continuation of #7764
Related issue: #6974

Description
Currently, BufferView* is generic over a lifetime, but the lifetime is only used for a reference to a buffer. Since buffers are now Cloneable, it make sense to abandon this and instead have BufferView* "own" a Buffer.

This makes it easier to pass around BufferView*.

Testing
Tested via existing tests.

Squash or Rebase?

Squash

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

@sagudev sagudev requested a review from a team as a code owner August 4, 2025 04:40
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using BufferSlice instead of &BufferSlice would be less diff, but less flexible (user would need to clone BufferSlice in some cases).

@sagudev sagudev mentioned this pull request Aug 4, 2025
6 tasks
@sagudev sagudev requested a review from kpreid August 4, 2025 04:53
Copy link
Collaborator

@kpreid kpreid left a comment

Choose a reason for hiding this comment

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

The thing I really like about this change is that BufferView[Mut] (the one that gives access to mapped buffers, not just designating a part of a buffer) now has no lifetime, so it can be used in more ways. (But that could also be done on its own without modifying BufferSlice.)

The thing I don't like is that every render pass user has to add more &s, and their .slice() calls now perform a completely unnecessary reference count increment and decrement. Is there any chance it would be possible to arrange so that the set_vertex_buffer() etc. will actually take ownership of the slice's reference count and take advantage of that internally for their own resource tracking purposes? Or are there too many layers in the way? If this were possible, it would keep the syntax for users simpler and be a net performance improvement.

@sagudev
Copy link
Collaborator Author

sagudev commented Aug 4, 2025

The thing I don't like is that every render pass user has to add more &s, and their .slice() calls now perform a completely unnecessary reference count increment and decrement. Is there any chance it would be possible to arrange so that the set_vertex_buffer() etc. will actually take ownership of the slice's reference count and take advantage of that internally for their own resource tracking purposes? Or are there too many layers in the way? If this were possible, it would keep the syntax for users simpler and be a net performance improvement.

It might be possible, but that would be more involved as inner method takes &DispathcBuffer anyway.

The thing I really like about this change is that BufferView[Mut] (the one that gives access to mapped buffers, not just designating a part of a buffer) now has no lifetime, so it can be used in more ways. (But that could also be done on its own without modifying BufferSlice.)

I just check and I only need BufferView without lifetimes, so I went with this approach.

Signed-off-by: sagudev <[email protected]>
Copy link
Member

@Wumpf Wumpf left a comment

Choose a reason for hiding this comment

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

yay, finally!
This will fix some awful unsafe code I maintain in a different project! :)

@Wumpf Wumpf merged commit f04b3b0 into gfx-rs:trunk Aug 10, 2025
40 checks passed
sharmajai pushed a commit to sharmajai/wgpu that referenced this pull request Oct 12, 2025
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.

3 participants