-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose type erased draw data that can be consumed directly (#2300)
<!-- Open the PR up as a draft until you feel it is ready for a proper review. Do not make PR:s from your own `main` branch, as that makes it difficult for reviewers to add their own fixes. Add any improvements to the branch as new commits to make it easier for reviewers to follow the progress. All commits will be squashed to a single commit once the PR is merged into `main`. Make sure you mention any issues that this PR closes in the description, as well as any other related issues. To get an auto-generated PR description you can put "copilot:summary" or "copilot:walkthrough" anywhere. --> ### What Split out of ongoing scene traitification refactor: The `SceneElement` trait should be able to emit draw data directly and this smaller refactor allows that. This happens to avoid a lot of copy operations we had and makes use of DrawData safer as it enforces the "single frame" assumption a bit better. ### 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) * [ ] I've included a screenshot or gif (if applicable) <!-- This line will get updated when the PR build summary job finishes. --> PR Build Summary: https://build.rerun.io/pr/2300 <!-- pr-link-docs:start --> Docs preview: https://rerun.io/preview/c0c317e/docs <!-- pr-link-docs:end -->
- Loading branch information
Showing
12 changed files
with
105 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::{ | ||
draw_phases::DrawPhase, | ||
renderer::{DrawData, Renderer}, | ||
RenderContext, | ||
}; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum QueueableDrawDataError { | ||
#[error("Failed to retrieve renderer of type {0}")] | ||
FailedToRetrieveRenderer(&'static str), | ||
|
||
#[error("Mismatching draw data type, expected {0}")] | ||
UnexpectedDrawDataType(&'static str), | ||
|
||
#[error(transparent)] | ||
DrawError(#[from] anyhow::Error), | ||
} | ||
|
||
type DrawFn = dyn for<'a, 'b> Fn( | ||
&'b RenderContext, | ||
DrawPhase, | ||
&'a mut wgpu::RenderPass<'b>, | ||
&'b dyn std::any::Any, | ||
) -> Result<(), QueueableDrawDataError> | ||
+ Sync | ||
+ Send; | ||
|
||
/// Type erased draw data that can be submitted directly to the view builder. | ||
pub struct QueueableDrawData { | ||
pub(crate) draw_func: Box<DrawFn>, | ||
pub(crate) draw_data: Box<dyn std::any::Any + std::marker::Send + std::marker::Sync>, | ||
pub(crate) renderer_name: &'static str, | ||
pub(crate) participated_phases: &'static [DrawPhase], | ||
} | ||
|
||
impl<D: DrawData + Sync + Send + 'static> From<D> for QueueableDrawData { | ||
fn from(draw_data: D) -> Self { | ||
QueueableDrawData { | ||
draw_func: Box::new(move |ctx, phase, pass, draw_data| { | ||
let renderers = ctx.renderers.read(); | ||
let renderer = renderers.get::<D::Renderer>().ok_or( | ||
QueueableDrawDataError::FailedToRetrieveRenderer(std::any::type_name::< | ||
D::Renderer, | ||
>()), | ||
)?; | ||
let draw_data = draw_data.downcast_ref::<D>().ok_or( | ||
QueueableDrawDataError::UnexpectedDrawDataType(std::any::type_name::<D>()), | ||
)?; | ||
renderer | ||
.draw(&ctx.gpu_resources, phase, pass, draw_data) | ||
.map_err(QueueableDrawDataError::from) | ||
}), | ||
draw_data: Box::new(draw_data), | ||
renderer_name: std::any::type_name::<D::Renderer>(), | ||
participated_phases: D::Renderer::participated_phases(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters