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

Improved wgpu callbacks #3253

Merged
merged 7 commits into from
Aug 15, 2023
Merged

Improved wgpu callbacks #3253

merged 7 commits into from
Aug 15, 2023

Conversation

Wumpf
Copy link
Collaborator

@Wumpf Wumpf commented Aug 14, 2023

⚠️ BREAKING: Callbacks for egui_wgpu have been reworked.

  • Registering a callback now requires implementing egui_wgpu::CallbackTrait
  • new CallbackTrait::finish_prepare called after all prepare callbacks have been called
  • resources shared between callbacks are no longer required to be Send/Sync on wasm

I followed @MatrixDev's proposal fairly closely. Key differences:

  • only a single finish_prepare_callback that is not part of the callback trait. For convenience it still gets references to all callback objects. This technically makes the regular prepare callbacks redundant, but I figured it's convenient to have both around. I think @MatrixDev correctly describes the typical scenario as each individual prepare pushing to some form of system/manager and then processing the overall results in a `finish_prepare
  • kicked out TypeMap and replaced with integer hash map since:
    • no longer need to push objects in a singleton-style way. The new map is slightly harder to use but a lot more flexible
    • needed to do something to support non Sync/Send going forward anyways since on wasm wgpu resources aren't Send/Sync by default (since they contain references to JS objects)
    • one dependency less!

I ventured a little bit into trying to change egui's callback system on a more deeper level as hinted in the ticket, but I concluded the only way forward there would be to define separate callback data on the Shape (always sync+send, immutable, comparable etc.) and separately callback "systems". I think it should be easier to transition there with this PR in the future but this would be a much more far-reaching change.

@Wumpf Wumpf marked this pull request as ready for review August 14, 2023 21:30
@Wumpf
Copy link
Collaborator Author

Wumpf commented Aug 14, 2023

@MatrixDev, want to have a look if this fixes the issue for you? Doesn't address glow backend, but we can take that separately

@emilk emilk added the eframe Relates to epi and eframe label Aug 15, 2023
@@ -817,7 +817,7 @@ pub struct PaintCallback {
///
/// The rendering backend is also responsible for restoring any state, such as the bound shader
/// program, vertex array, etc.
pub callback: Arc<dyn Any + Sync + Send>,
pub callback: Arc<dyn Any + Send + Sync>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

accidental reordering of traits due to intra-pr-historical reasons ;)

@MatrixDev
Copy link

@MatrixDev, want to have a look if this fixes the issue for you? Doesn't address glow backend, but we can take that separately

Wow, this is some great work and it will fully cover my usecase.

The only thing I'm not sure about is Renderer::finish_prepare_callback. Don't get me wrong, it fits me 100% and is probably the most performant way to implement it. But having a single callback for all controls reduces ease of use for 3rd-party library controls (when they inevitably appear in the future). Instead of just calling extension function on UI users would also be required to provide manual implementation of Renderer::finish_prepare_callback which will group callbacks by controls and than call finish_prepare_callback for a specific control from a 3rd-party library.

@Wumpf
Copy link
Collaborator Author

Wumpf commented Aug 15, 2023

Thanks for you feedback! :)
Just talked it through with @emilk and we decided to bring back typemap and do the finish_prepare as you suggested, the 3d part library argument is a good one! 👍

@Wumpf Wumpf merged commit b896d64 into master Aug 15, 2023
34 checks passed
@Wumpf Wumpf deleted the improved-wgpu-callbacks branch August 15, 2023 15:17
Wumpf added a commit to rerun-io/rerun that referenced this pull request Aug 16, 2023
### What

* Depends on emilk/egui#3253
* allows for a bit nicer callback handling on our side. Still not
amazing but quite a bit better I reckon (no more locks and additional
ref counting on our side!). I was hoping to use the new, more flexible
`shared_paint_callback_resources` to store `ViewBuilder`, but it can't
be accessed without a handle to the eframe renderer.
The end goal would be to get rid of re_renderer's
`per_frame_data_helper`. This will likely be enabled with wgpu's
"Arcanization" effort as this will eliminate `wgpu::RenderPass`'s
lifetime dependency. In that case we could store a `Mutex<ViewBuilder>`
on `ReRendererCallback` 🤔
* For the forseeable future we'll have to enable
`fragile-send-sync-non-atomic-wasm` on wgpu (luckily egui itself no
longer has this restriction). The problem is that a lot of our
infrastructure is built around assuming `Send`/`Sync` whereas in
actuality webgpu rendering resources can't be shared accross threads
easily.
* Had to (discover and) work around
https://github.com/gfx-rs/naga/issues/2436

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/2980) (if
applicable)
* [x] Test WebGPU build

- [PR Build Summary](https://build.rerun.io/pr/2980)
- [Docs
preview](https://rerun.io/preview/pr%3Aandreas%2Fupdate-wgpu/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Aandreas%2Fupdate-wgpu/examples)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
eframe Relates to epi and eframe egui-wgpu
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Painter's CallbackFn redesign
3 participants