Add user-defined errors for render functions (#196)

- Fixes #189
This commit is contained in:
Jay Oster 2021-09-04 09:00:25 -07:00 committed by GitHub
parent 0f8b1abe87
commit db00a67c60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 10 deletions

View file

@ -60,6 +60,8 @@ fn main() -> Result<(), Error> {
time += 0.01;
noise_renderer.render(encoder, render_target, context.scaling_renderer.clip_rect());
Ok(())
});
if render_result

View file

@ -67,8 +67,9 @@ fn main() -> Result<(), Error> {
context.scaling_renderer.render(encoder, render_target);
// Render egui
gui.render(encoder, render_target, context)
.expect("egui render error");
gui.render(encoder, render_target, context)?;
Ok(())
});
// Basic error handling

View file

@ -70,8 +70,9 @@ fn main() -> Result<(), Error> {
context.scaling_renderer.render(encoder, render_target);
// Render Dear ImGui
gui.render(&window, encoder, render_target, context)
.expect("gui.render() failed");
gui.render(&window, encoder, render_target, context)?;
Ok(())
});
// Basic error handling

View file

@ -115,6 +115,9 @@ pub enum Error {
/// Equivalent to [`wgpu::SurfaceError`]
#[error("The GPU failed to acquire a surface frame.")]
Surface(wgpu::SurfaceError),
/// User-defined error from custom render function
#[error("User-defined error.")]
UserDefined(#[from] Box<dyn std::error::Error>),
}
impl<'win, W: HasRawWindowHandle> SurfaceTexture<'win, W> {
@ -296,12 +299,14 @@ impl Pixels {
/// }
///
/// // Draw it to the `SurfaceTexture`
/// pixels.render();
/// pixels.render()?;
/// # Ok::<(), pixels::Error>(())
/// ```
pub fn render(&mut self) -> Result<(), Error> {
self.render_with(|encoder, render_target, context| {
context.scaling_renderer.render(encoder, render_target);
Ok(())
})
}
@ -312,9 +317,14 @@ impl Pixels {
/// which you can use to render to the screen, and a [`PixelsContext`] with all of the internal
/// `wgpu` context.
///
/// The render function must return a `Result`. This allows fallible render functions to be
/// handled gracefully. The boxed `Error` will be made available in the [`Error::UserDefined`]
/// variant returned by `render_with()`.
///
/// # Errors
///
/// Returns an error when [`wgpu::Surface::get_current_frame`] fails.
/// Returns an error when either [`wgpu::Surface::get_current_frame`] or the provided render
/// function fails.
///
/// # Example
///
@ -337,12 +347,17 @@ impl Pixels {
/// pixels.render_with(|encoder, render_target, context| {
/// context.scaling_renderer.render(encoder, render_target);
/// // etc...
/// });
/// Ok(())
/// })?;
/// # Ok::<(), pixels::Error>(())
/// ```
pub fn render_with<F>(&mut self, render_function: F) -> Result<(), Error>
where
F: FnOnce(&mut wgpu::CommandEncoder, &wgpu::TextureView, &PixelsContext),
F: FnOnce(
&mut wgpu::CommandEncoder,
&wgpu::TextureView,
&PixelsContext,
) -> Result<(), Box<dyn std::error::Error>>,
{
let frame = self
.context
@ -389,8 +404,8 @@ impl Pixels {
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
// Call the users render function.
(render_function)(&mut encoder, &view, &self.context);
// Call the user's render function.
(render_function)(&mut encoder, &view, &self.context)?;
self.context.queue.submit(Some(encoder.finish()));
Ok(())