Skip to content

Commit

Permalink
Fixed bug where I was constantly adding render nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed Sep 18, 2023
1 parent f282811 commit ed2b2bf
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ eyre = "0.6"
tracing = "0.1"
bevy_egui = "0.21"
egui-wgpu = "0.22"
egui = "0.22"
bevy-inspector-egui = "0.19"
wgpu = "0.16"
bevy_flycam = { git = "https://github.com/sburris0/bevy_flycam", branch = "bevy_0.11" }
Expand Down
1 change: 1 addition & 0 deletions skills/egui-texture/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ bevy_egui.workspace = true
bevy_flycam.workspace = true
color-eyre.workspace = true
egui-wgpu.workspace = true
egui.workspace = true
wgpu.workspace = true
42 changes: 19 additions & 23 deletions skills/egui-texture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fn main() -> Result<()> {
brightness: 1.,
})
.add_systems(Startup, setup)
.add_systems(Update, ui_example_system)
.add_systems(Update, update_egui_ui);
.add_systems(Update, ui_example_system);
// .add_systems(Update, update_egui_ui);

let Ok(render_app) = app.get_sub_app_mut(bevy::render::RenderApp) else {
panic!("The render plugin should have added this subapp");
Expand All @@ -33,8 +33,6 @@ fn main() -> Result<()> {
pub struct EguiContext {
output_texture: Handle<Image>,
ctx: egui::Context,
egui_output: egui::FullOutput,
clipped_primitives: Vec<egui::ClippedPrimitive>,
}

fn setup(
Expand All @@ -55,8 +53,6 @@ fn setup(
EguiContext {
output_texture,
ctx: egui::Context::default(),
egui_output: default(),
clipped_primitives: Vec::new(),
}
};

Expand All @@ -80,20 +76,20 @@ fn ui_example_system(mut contexts: EguiContexts) {
});
}

/// Performs the egui ui draw, updates textures, and generates the primitives.
///
/// Still need to actually perform the render encoder commands
fn update_egui_ui(
mut q: Query<&mut EguiContext>,
mut redraw: EventWriter<bevy::window::RequestRedraw>,
) {
for mut egui_ctx in q.iter_mut() {
let mut egui_output = egui_ctx.ctx.run(egui::RawInput::default(), |ctx| {
egui::Window::new("my window").show(ctx, |ui| ui.label("foobar"));
});
let shapes = std::mem::take(&mut egui_output.shapes);
egui_ctx.egui_output = egui_output;
egui_ctx.clipped_primitives = egui_ctx.ctx.tessellate(shapes);
redraw.send(bevy::window::RequestRedraw)
}
}
// /// Performs the egui ui draw, updates textures, and generates the primitives.
// ///
// /// Still need to actually perform the render encoder commands
// fn update_egui_ui(
// mut q: Query<&mut EguiContext>,
// mut redraw: EventWriter<bevy::window::RequestRedraw>,
// ) {
// for mut egui_ctx in q.iter_mut() {
// let mut egui_output = egui_ctx.ctx.run(egui::RawInput::default(), |ctx| {
// egui::Window::new("my window").show(ctx, |ui| ui.label("foobar"));
// });
// let shapes = std::mem::take(&mut egui_output.shapes);
// egui_ctx.egui_output = egui_output;
// egui_ctx.clipped_primitives = egui_ctx.ctx.tessellate(shapes);
// redraw.send(bevy::window::RequestRedraw)
// }
// }
22 changes: 15 additions & 7 deletions skills/egui-texture/src/render_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Mutex;

use bevy::{prelude::*, render::render_asset::RenderAssets};
use std::sync::Mutex;

use crate::EguiContext;

Expand Down Expand Up @@ -39,16 +38,22 @@ impl bevy::render::render_graph::Node for EguiNode {
};

let mut renderer = self.renderer.lock().unwrap();

// TODO: Eventually I'll move this to a separate user defined system.
let egui_output = self.egui_ctx.ctx.run(egui::RawInput::default(), |ctx| {
egui::Window::new("my window").show(ctx, |ui| ui.label("foobar"));
});
// TODO: Handle textures to delete
for (tid, delta) in self.egui_ctx.egui_output.textures_delta.set.iter() {
for (tid, delta) in egui_output.textures_delta.set.iter() {
renderer.update_texture(device, queue, *tid, delta);
}
let clipped_primitives = self.egui_ctx.ctx.tessellate(egui_output.shapes);

renderer.update_buffers(
device,
queue,
encoder,
&self.egui_ctx.clipped_primitives,
&clipped_primitives,
&screen_descriptor,
);

Expand All @@ -73,16 +78,19 @@ impl bevy::render::render_graph::Node for EguiNode {

renderer.render(
&mut egui_render_pass,
&self.egui_ctx.clipped_primitives,
&clipped_primitives,
&screen_descriptor,
);
//
// drop(egui_render_pass);
// let commands = encoder.finish();
// tuple.1.submit([commands]);
// queue.submit([commands]);
// error!("After submit");
// // output.present();
// todo!()
static COUNT: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
// if COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 2 {
// panic!("end");
// }
Ok(())
}
}
6 changes: 3 additions & 3 deletions skills/egui-texture/src/render_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::render_node::EguiNode;
use crate::EguiContext;

pub fn add_render_node(
q: Extract<Query<(Entity, &EguiContext)>>,
q: Extract<Query<(Entity, &EguiContext), Added<EguiContext>>>,
// q: Extract<Query<&EguiContext>>,
textures: Extract<Res<Assets<Image>>>,
mut render_graph: ResMut<RenderGraph>,
device: Res<bevy::render::renderer::RenderDevice>,
mut commands: Commands,
) {
error!("add_render_node");
for (entity, egui_ctx) in q.iter() {
info!("adding render node for {entity:?}");
let output_texture_format = textures
.get(&egui_ctx.output_texture)
.expect("Should have found the matching `Image`")
Expand Down

0 comments on commit ed2b2bf

Please sign in to comment.