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

Make draw data creation no longer require a mutable re_renderer context #4422

Merged
merged 3 commits into from
Dec 5, 2023

Conversation

Wumpf
Copy link
Member

@Wumpf Wumpf commented Dec 4, 2023

What

Make draw data (this is in essence resource submission to GPU! e.g. make pointcloud ready to render) an operation that doesn't require a mutable renderer context.

Two pieces to this:

  • FileResolver resolve needs to be non-mut, this turned out to be trivial
  • Renderer::get_or_create had to be streamlined. Renderer creation is now done directly on the context and holding a renderer implies holding a read lock. Since Renderers are added very rarely, this isn't much of a limitation!

This causes a large trickle of removing &mut and gets us almost to having a non-mutable render context reference on the Viewer context!

Checklist


Part of series towards more multithreading in the viewer!

@Wumpf Wumpf added 🔺 re_renderer affects re_renderer itself 🚜 refactor Change the code, not the functionality include in changelog labels Dec 4, 2023
@Wumpf Wumpf force-pushed the andreas/re_renderer/interior-mutable-static-resource-pool branch from 41c718e to 2e754ba Compare December 4, 2023 10:59
@Wumpf Wumpf force-pushed the andreas/re_renderer/non-mut-drawdata-creation branch 2 times, most recently from 3d84895 to 0cd103f Compare December 4, 2023 20:51
crates/re_renderer/src/context.rs Show resolved Hide resolved
crates/re_renderer/src/context.rs Outdated Show resolved Hide resolved
Comment on lines 414 to 419
{
self.renderers.write().get_or_create::<_, R>(
&self.shared_renderer_data,
&self.gpu_resources,
&self.device,
&self.resolver,
);
}
// Release write lock again and only take a read lock.
// safe to unwrap since we just created it.
parking_lot::RwLockReadGuard::map(self.renderers.read(), |r| r.get::<R>().unwrap())
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't need unwrap. get_or_create should just return the gotten or created thing

Suggested change
{
self.renderers.write().get_or_create::<_, R>(
&self.shared_renderer_data,
&self.gpu_resources,
&self.device,
&self.resolver,
);
}
// Release write lock again and only take a read lock.
// safe to unwrap since we just created it.
parking_lot::RwLockReadGuard::map(self.renderers.read(), |r| r.get::<R>().unwrap())
self.renderers.write().get_or_create::<_, R>(
&self.shared_renderer_data,
&self.gpu_resources,
&self.device,
&self.resolver,
)

Copy link
Member Author

Choose a reason for hiding this comment

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

the problem with that is that we have to release the write lock first! The result of get_or_create implies keeping the write lock and we don't want to do that.

crates/re_renderer/src/renderer/generic_skybox.rs Outdated Show resolved Hide resolved
crates/re_renderer/src/renderer/test_triangle.rs Outdated Show resolved Hide resolved
crates/re_renderer/src/context.rs Outdated Show resolved Hide resolved
Base automatically changed from andreas/re_renderer/interior-mutable-static-resource-pool to main December 5, 2023 12:36
Wumpf added a commit that referenced this pull request Dec 5, 2023
…peline/Shader/Layouts/etc.) (#4421)

### What

* Prerequisite for #1325
* Followed by #4422

The dynamic resource pool (textures, buffers, bindgroups) was already
interior mutable. The static resource pool (mostly append only pool)
however, still required mutable access. This PR addresses this.

The actual tricky part is the implications this has on the draw step:
`wgpu::RenderPass<'a>` expects all passed in references to live for
`'a`. This isn't much of a problem we have with resources from the
dynamic resource pool since there the handles are actually Arcs to the
underlying wgpu resources. However we can't do this with RenderPipelines
(the only relevant static resource for this case) though since we want
to be able to swap out what the handle points to for shader reloading.
This means we need to hold the lock read lock on render pipelines for
the entirety of the pass recording.
This is quite nice and easy for our ViewBuilder draws (== individual
space views), but hard for the "compositing step" where we draw into
egui's render pass. This is solved by taking out all render pipelines of
the lock and putting them back in at the start of the frame.
Once wgpu lifts this restrictions we can simplify things a bit!

(to be continued with follow-up PRs!)


### 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 the web demo (if applicable):
  * Full build: [app.rerun.io](https://app.rerun.io/pr/4421/index.html)
* Partial build:
[app.rerun.io](https://app.rerun.io/pr/4421/index.html?manifest_url=https://app.rerun.io/version)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4421)
- [Docs
preview](https://rerun.io/preview/8e4b0d88d9d01df8121a84850b251c1840494695/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/8e4b0d88d9d01df8121a84850b251c1840494695/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)


---
Part of series towards more multithreading in the viewer!
* #4387
* #4404
* #4389
* You are here ➡️ #4421
* #4422
* #4430
@Wumpf Wumpf force-pushed the andreas/re_renderer/non-mut-drawdata-creation branch from 0cd103f to 5a1ad48 Compare December 5, 2023 15:29
@Wumpf Wumpf merged commit eab8244 into main Dec 5, 2023
40 checks passed
@Wumpf Wumpf deleted the andreas/re_renderer/non-mut-drawdata-creation branch December 5, 2023 16:08
Wumpf added a commit that referenced this pull request Dec 5, 2023
### What

* Prerequisite for #1325
* Follow-up of #4422

Makes `ViewBuilder`'s draw method no longer require a mutable render
context, thus no longer requiring a mutable frame state on the render
context (which previously held the mutable viewbuilder!), thus finally
lifting the need to store mutable references to the render context from
the viewer context!

Having a non-mut draw method on ViewBuilder is _mostly_ straight forward
at this point. The main hurdle are render phases that so far consumed
themselves on drawing because of readback textures (which are still
self-consuming for good reasons).

### 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 the web demo (if applicable):
  * Full build: [app.rerun.io](https://app.rerun.io/pr/4430/index.html)
* Partial build:
[app.rerun.io](https://app.rerun.io/pr/4430/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
- Useful for quick testing when changes do not affect examples in any
way
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4430)
- [Docs
preview](https://rerun.io/preview/965d71c3723e74947e2d063372d51d9ae89cc9dd/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/965d71c3723e74947e2d063372d51d9ae89cc9dd/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)


---
Part of series towards more multithreading in the viewer!
* #4387
* #4404
* #4389
* #4421
* #4422 
* You are here ➡️ #4430
@Wumpf Wumpf mentioned this pull request Dec 5, 2023
4 tasks
Wumpf added a commit that referenced this pull request Dec 5, 2023
### What

* Prerequisite for #1325
* Follow-up of #4430 

What it says on the tin!
No additional review required, just waiting for rust ci to be sure :)

### 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 the web demo (if applicable):
  * Full build: [app.rerun.io](https://app.rerun.io/pr/4430/index.html)
* Partial build:
[app.rerun.io](https://app.rerun.io/pr/4430/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
- Useful for quick testing when changes do not affect examples in any
way
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4430)
- [Docs
preview](https://rerun.io/preview/cc13e9becf363d9f611cfc59c61b41d1135ef411/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/cc13e9becf363d9f611cfc59c61b41d1135ef411/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)


---
Part of series towards more multithreading in the viewer!
* #4387
* #4404
* #4389
* #4421
* #4422 
* #4430
* You are here ➡️  #4438
@Wumpf Wumpf mentioned this pull request Dec 7, 2023
4 tasks
Wumpf added a commit that referenced this pull request Dec 11, 2023
### What

* Fixes #1325 

Two levels of parallism actually:
* All context & part systems for a single space view each run in
parallel
* All space views now in parallel first setup their query and then kick
of their (parallized!) system execution

This gives us major speedups whenever:
* There is several heavy systems in a scene (e.g. a lot of points and a
lot of arrows)
* There is several space views

As such this is a very fundamental & widely applicable parallelization!


![image](https://github.com/rerun-io/rerun/assets/1220815/14bc6ca8-abb0-480b-91dc-2da51253dd92)
(a rather silly "points with arrows" scene duplicated a bunch of times.
Note that we still don't have any caching, so all these space views
might as well be animated individually!)

<img width="1421" alt="image"
src="https://github.com/rerun-io/rerun/assets/1220815/57e13e15-9d8f-4a30-bf74-d8174676986d">
(busy rayon worker threads!)


Related things that are not parallel yet:
* ui methods per space view (run one by one)
* draw call recording (done as part of the ui method, but we could
likely do this in parallel, only joining on these tasks when egui wants
to queue up the command buffers)
* processing _within_ a system depends on the system at hand. Today,
only point cloud has some parallization


### 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 the web demo (if applicable):
  * Full build: [app.rerun.io](https://app.rerun.io/pr/4460/index.html)
* Partial build:
[app.rerun.io](https://app.rerun.io/pr/4460/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
- Useful for quick testing when changes do not affect examples in any
way
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4460)
- [Docs
preview](https://rerun.io/preview/cb7e9a9902bb9923ade41271ccc43ed01a2a4d33/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/cb7e9a9902bb9923ade41271ccc43ed01a2a4d33/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

---
End of a series on refactors leading to this parallization:
* #4387
* #4404
* #4389
* #4421
* #4422 
* #4430
* #4438
* #4460

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
include in changelog 🔺 re_renderer affects re_renderer itself 🚜 refactor Change the code, not the functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants