Skip to content

Commit

Permalink
Apply More Code Review Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed May 27, 2022
1 parent d5d8cab commit f79b273
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct CreationContext<'s> {
/// you might want to use later from a [`egui::PaintCallback`].
#[cfg(feature = "glow")]
pub gl: Option<std::sync::Arc<glow::Context>>,

/// Can be used to manage GPU resources for custom rendering with WGPU using
/// [`egui::PaintCallback`]s.
#[cfg(feature = "wgpu")]
Expand Down
28 changes: 25 additions & 3 deletions egui-wgpu/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ use wgpu::util::DeviceExt as _;

/// A callback function that can be used to compose an [`egui::PaintCallback`] for custom WGPU
/// rendering.
///
/// The callback is composed of two functions: `prepare` and `paint`.
///
/// `prepare` is called every frame before `paint`, and can use the passed-in [`wgpu::Device`] and
/// [`wgpu::Buffer`] to allocate or modify GPU resources such as buffers.
///
/// `paint` is called after `prepare` and is given access to the the [`wgpu::RenderPass`] so that it
/// can issue draw commands.
///
/// The final argument of both the `prepare` and `paint` callbacks is a the
/// [`paint_callback_resources`][crate::renderer::RenderPass::paint_callback_resources].
/// `paint_callback_resources` has the same lifetime as the Egui render pass, so it can be used to
/// store buffers, pipelines, and other information that needs to be accessed during the render
/// pass.
///
/// # Example
///
/// See the [custom3d_wgpu] demo source for a detailed usage example.
///
/// [custom3d_wgpu]:
/// https://github.com/emilk/egui/blob/master/egui_demo_app/src/apps/custom3d_wgpu.rs
pub struct CallbackFn {
prepare: Box<PrepareCallback>,
paint: Box<PaintCallback>,
Expand All @@ -32,6 +53,7 @@ impl CallbackFn {
Self::default()
}

/// Set the prepare callback
pub fn prepare<F>(mut self, prepare: F) -> Self
where
F: Fn(&wgpu::Device, &wgpu::Queue, &mut TypeMap) + Sync + Send + 'static,
Expand All @@ -40,6 +62,7 @@ impl CallbackFn {
self
}

/// Set the paint callback
pub fn paint<F>(mut self, paint: F) -> Self
where
F: for<'a, 'b> Fn(PaintCallbackInfo, &'a mut wgpu::RenderPass<'b>, &'b TypeMap)
Expand Down Expand Up @@ -375,6 +398,7 @@ impl RenderPass {
let cbfn = if let Some(c) = callback.callback.downcast_ref::<CallbackFn>() {
c
} else {
// We already warned in the `prepare` callback
continue;
};

Expand Down Expand Up @@ -539,7 +563,6 @@ impl RenderPass {
};
}

/// Should be called before `execute()`.
pub fn free_texture(&mut self, id: &egui::TextureId) {
self.textures.remove(id);
}
Expand Down Expand Up @@ -682,8 +705,7 @@ impl RenderPass {
let cbfn = if let Some(c) = callback.callback.downcast_ref::<CallbackFn>() {
c
} else {
// TODO: Should we warn in the console about this, or provide some other way to
// debug ignored paint callbacks?
tracing::warn!("Unknown paint callback: expected `egui_gpu::CallbackFn`");
continue;
};

Expand Down
10 changes: 7 additions & 3 deletions egui-wgpu/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,6 @@ impl<'a> Painter<'a> {
for (id, image_delta) in &textures_delta.set {
rpass.update_texture(&render_state.device, &render_state.queue, *id, image_delta);
}
for id in &textures_delta.free {
rpass.free_texture(id);
}

rpass.update_buffers(
&render_state.device,
Expand All @@ -290,6 +287,13 @@ impl<'a> Painter<'a> {
}),
);

{
let mut rpass = render_state.egui_rpass.write();
for id in &textures_delta.free {
rpass.free_texture(id);
}
}

// Submit the commands.
render_state.queue.submit(std::iter::once(encoder.finish()));

Expand Down
4 changes: 2 additions & 2 deletions egui_demo_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ chrono = { version = "0.4", features = ["js-sys", "wasmbind"] }
eframe = { version = "0.18.0", path = "../eframe", default-features = false }
egui = { version = "0.18.0", path = "../egui", features = ["extra_debug_asserts"] }
egui_demo_lib = { version = "0.18.0", path = "../egui_demo_lib", features = ["chrono"] }
bytemuck = { version = "1.9.1", optional = true }
pollster = { version = "0.2.5", optional = true }

# Optional dependencies:

bytemuck = { version = "1.9.1", optional = true }
egui_extras = { version = "0.18.0", optional = true, path = "../egui_extras" }

# feature "http":
Expand All @@ -56,6 +55,7 @@ image = { version = "0.24", optional = true, default-features = false, features
"png",
] }
poll-promise = { version = "0.1", optional = true, default-features = false }
pollster = { version = "0.2.5", optional = true }

# feature "persistence":
serde = { version = "1", optional = true, features = ["derive"] }
Expand Down
13 changes: 12 additions & 1 deletion egui_glow/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ pub struct Painter {
destroyed: bool,
}

// TODO: Documentation
/// A callback function that can be used to compose an [`egui::PaintCallback`] for custom rendering
/// with [`glow`].
///
/// The callback is passed, the [`egui::PaintCallbackInfo`] and the [`Painter`] which can be used to
/// access the OpenGL context.
///
/// # Example
///
/// See the [custom3d_glow] demo source for a detailed usage example.
///
/// [custom3d_glow]:
/// https://github.com/emilk/egui/blob/master/egui_demo_app/src/apps/custom3d_wgpu.rs
pub struct CallbackFn {
f: Box<dyn Fn(PaintCallbackInfo, &Painter) + Sync + Send>,
}
Expand Down

0 comments on commit f79b273

Please sign in to comment.