Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

The Context trait #281

Merged
merged 4 commits into from
Apr 27, 2020
Merged

The Context trait #281

merged 4 commits into from
Apr 27, 2020

Conversation

kvark
Copy link
Member

@kvark kvark commented Apr 26, 2020

The main motivation here is to avoid blocking the wgpu-core updates by wgpu-native. Instead, wgpu-native becomes a branch, and the dependency of wgpu-rs -> wgpu-native starts adhering to the contract/API of the standard webgpu-native headers.

The biggest change is the introduction of the Context trait. I recall us discussing 2 downsides to having this trait:

  1. inconvenient for the users to include. This is a non-issue here, since it's private.
  2. more code to maintain. This is less of an issue if we aim to have 3 backends.

What this gives in return is a well established contract with the backends. Unlike gfx-rs, the backend code is right here, a part of the crate, so the contract is only for internal use.

Fixes #156 : the "direct" implementation of it goes straight to wgpu-core. What this gives us is less overhead for command recording, since there is no longer an extra indirection on every command, and no heap allocation at the end of a render pass.

The downside of this PR is one extra Arc (with addref) per object.

This commit also has small improvements:

@kvark kvark requested a review from grovesNL April 26, 2020 05:05
Copy link
Contributor

@lachlansneff lachlansneff left a comment

Choose a reason for hiding this comment

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

I'm a little wary of all the Arc::clone calls everywhere, especially when compiling to wasm, since the browser backend will already be doing that internally.

}
}
}

impl Queue {
/// Submits a series of finished command buffers for execution.
pub fn submit(&self, command_buffers: &[CommandBuffer]) {
backend::queue_submit(&self.id, command_buffers);
pub fn submit<I: IntoIterator<Item = CommandBuffer>>(&self, command_buffers: I) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't this be inefficient for webgpu.h, since that takes a pointer to a buffer of command buffers? You'd have to allocate a Vec or similar to use that. Whereas, a slice could just be passed efficiently.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah, this is more flexible than a slice. You can always go from a slice to an iterator for free, but not the other way around. I.e. wgpu-native would do something like: command_buffer_slice.iter().cloned() for passing it here.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
);
}

trait Context: Sized {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the traits should be in a different file so it's easier to read this one.

Copy link
Member Author

Choose a reason for hiding this comment

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

It could be, yes. I find it convenient here, because it uses all the types in the proximity, and everything uses it. We can follow-up with moving it if we see fit, after the initial work is done.

@kvark
Copy link
Member Author

kvark commented Apr 26, 2020

@lachlansneff thank you for the review!

I'm a little wary of all the Arc::clone calls everywhere, especially when compiling to wasm, since the browser backend will already be doing that internally.

It's a fair thing to be concerned about. My understanding is that this is an extremely light overhead, since it only happens at object creation time, which is dominated by other factors.
Overall, I think we can have it, and there will not by any observable effect.

Main change here is the introduction of the Context trait.
The "direct" implementation of it goes straight to wgpu-core.

This commit also has small improvements:
- consuming command buffers on submit
- Instance type
- proper call to destructors
@kvark kvark changed the title Rewire the backend system to remove wgpu-native step The Context trait Apr 26, 2020
@kvark
Copy link
Member Author

kvark commented Apr 26, 2020

@grovesNL I think this is done. Please take a final (?) look.

Copy link
Collaborator

@grovesNL grovesNL left a comment

Choose a reason for hiding this comment

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

Looks great 👍

}

/// Creates a surface from a raw window handle.
pub unsafe fn create_surface<W: raw_window_handle::HasRawWindowHandle>(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could eventually avoid unsafe here later if we fully validate the handles internally

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd very much like that, but not sure if this is achievable via RawWindowHandle: after all, it's just a bunch of raw pointers. I.e. a winit-based API could be safe, but once we lower to the raw handles, safety zone is gone.

options: &crate::RequestAdapterOptions<'_>,
_backends: wgt::BackendBit,
) -> Self::RequestAdapterFuture {
//assert!(backends.contains(wgt::BackendBit::BROWSER_WEBGPU));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we skip this now?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's annoying/hard to get this check back in (but possible). I've added a detailed TODO here.

@kvark
Copy link
Member Author

kvark commented Apr 27, 2020

@grovesNL thank you for the quick review!
bors r=grovesNL

@bors bors bot merged commit 49640d2 into gfx-rs:master Apr 27, 2020
@kvark kvark deleted the core branch April 27, 2020 04:18
@kvark kvark mentioned this pull request May 6, 2020
kejor pushed a commit to kejor/wgpu-rs that referenced this pull request Nov 28, 2020
281: Force pipeline barriers between unordered usages r=grovesNL a=kvark

Fixes gfx-rs#272

Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
kejor pushed a commit to kejor/wgpu-rs that referenced this pull request Nov 28, 2020
302: Fixed pipeline barriers that are not transitions r=grovesNL a=kvark

The actual fix is a one-liner: `u.start != u.end` bit in `PendingTransition::record`. The case is relatively new - as of gfx-rs#281, which I haven't tested extensively.
The PR also improves our logging for further assistance with similar issues... but the most annoying piece is that I would find this much much earlier if I didn't ignore the result here: `let _ = state.change(...)`. Let it be the lesson to all of us :)

Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consume command buffers on submit Case for using wgpu-core
3 participants