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

WIP: Add egui for debugging purposes #232

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions maplibre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ cint = "0.3.1"
downcast-rs = "1.2.0"
smallvec = "1.9.0"

# egui
egui = "0.20.1"
egui-wgpu = "0.20.0"
egui_demo_lib = "0.20"

# Headless
png = { version = "0.17.5", optional = true }

Expand Down
55 changes: 42 additions & 13 deletions maplibre/src/render/debug_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ impl Node for DebugPassNode {
fn run(
&self,
_graph: &mut RenderGraphContext,
render_context: &mut RenderContext,
RenderContext {
command_encoder, ..
}: &mut RenderContext,
state: &RenderState,
) -> Result<(), NodeRunError> {
let Initialized(render_target) = &state.render_target else {
let RenderState {
surface,
mask_phase,
render_target,
egui_renderer,
egui_paint_jobs,
..
} = state;

let Initialized(render_target) = &render_target else {
return Ok(());
};

Expand All @@ -45,19 +56,37 @@ impl Node for DebugPassNode {
resolve_target: None,
};

let render_pass =
render_context
.command_encoder
.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(color_attachment)],
depth_stencil_attachment: None,
});
{
let mut render_pass = command_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(color_attachment.clone())],
depth_stencil_attachment: None,
});

// Debug outlines
let mut tracked_pass = TrackedRenderPass::new(render_pass);

for item in &mask_phase.items {
DrawDebugOutlines::render(state, item, &mut tracked_pass);
}
}

{
let Initialized(egui_renderer) = egui_renderer else { return Ok(()); };

let mut render_pass = command_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(color_attachment)],
depth_stencil_attachment: None,
});

let mut tracked_pass = TrackedRenderPass::new(render_pass);
let screen_descriptor = egui_wgpu::renderer::ScreenDescriptor {
size_in_pixels: [surface.size().width(), surface.size().height()],
pixels_per_point: 1.0,
};

for item in &state.mask_phase.items {
DrawDebugOutlines::render(state, item, &mut tracked_pass);
// Record all render passes
egui_renderer.render(&mut render_pass, &egui_paint_jobs, &screen_descriptor);
}

Ok(())
Expand Down
12 changes: 12 additions & 0 deletions maplibre/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ pub struct RenderState {

mask_phase: RenderPhase<TileShape>,
tile_phase: RenderPhase<(IndexEntry, TileShape)>,

// egui
egui_paint_jobs: Vec<egui::ClippedPrimitive>,
egui_textures: Vec<egui::TextureId>,
egui_renderer: Eventually<egui_wgpu::Renderer>,
egui_app: egui_demo_lib::DemoWindows,
egui_context: egui::Context,
}

impl RenderState {
Expand All @@ -113,6 +120,11 @@ impl RenderState {
surface,
mask_phase: Default::default(),
tile_phase: Default::default(),
egui_paint_jobs: Default::default(),
egui_textures: Default::default(),
egui_renderer: Default::default(),
egui_app: Default::default(),
egui_context: Default::default(),
}
}

Expand Down
46 changes: 46 additions & 0 deletions maplibre/src/render/stages/egui_cleanup_stage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
context::MapContext,
render::{eventually::Eventually::Initialized, RenderState, Renderer},
schedule::Stage,
};

#[derive(Default)]
pub struct EguiCleanupStage;

impl Stage for EguiCleanupStage {
#[tracing::instrument(name = "EguiCleanupStage", skip_all)]
fn run(
&mut self,
MapContext {
renderer:
Renderer {
device,
queue,
state:
RenderState {
surface,
mask_phase,
tile_phase,
tile_view_pattern,
buffer_pool,
egui_renderer,
egui_app,
egui_context,
egui_paint_jobs,
egui_textures,
..
},
..
},
..
}: &mut MapContext,
) {
let Initialized(egui_renderer) = egui_renderer else { return; };

for egui_texture in egui_textures.into_iter() {
egui_renderer.free_texture(egui_texture);
}

egui_textures.clear();
}
}
86 changes: 86 additions & 0 deletions maplibre/src/render/stages/egui_stage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use egui::RawInput;

use crate::{
context::MapContext,
render::{eventually::Eventually::Initialized, RenderState, Renderer},
schedule::Stage,
};

#[derive(Default)]
pub struct EguiStage;

impl Stage for EguiStage {
#[tracing::instrument(name = "EguiStage", skip_all)]
fn run(
&mut self,
MapContext {
renderer:
Renderer {
device,
queue,
state:
RenderState {
surface,
mask_phase,
tile_phase,
tile_view_pattern,
buffer_pool,
egui_renderer,
egui_app,
egui_context,
egui_paint_jobs,
egui_textures,
..
},
..
},
..
}: &mut MapContext,
) {
egui_renderer
.initialize(|| egui_wgpu::Renderer::new(&device, surface.surface_format(), None, 1));

let Initialized(egui_renderer) = egui_renderer else { return; };

// Draw the demo application.
let full_output = egui_context.run(RawInput::default(), |context| {
egui_app.ui(context);
});

let screen_descriptor = egui_wgpu::renderer::ScreenDescriptor {
size_in_pixels: [surface.size().width(), surface.size().height()],
pixels_per_point: 1.0,
};

let paint_jobs = egui_context.tessellate(full_output.shapes);

{
egui_textures.clear();
for (id, image_delta) in &full_output.textures_delta.set {
egui_renderer.update_texture(&device, &queue, *id, image_delta);
}

for id in &full_output.textures_delta.free {
egui_textures.push(*id);
}
}

{
let mut command_encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());

egui_renderer.update_buffers(
&device,
&queue,
&mut command_encoder,
&paint_jobs,
&screen_descriptor,
);

queue.submit(vec![command_encoder.finish()]);

egui_paint_jobs.clear();
egui_paint_jobs.extend(paint_jobs);
}
}
}
9 changes: 8 additions & 1 deletion maplibre/src/render/stages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ use crate::{
render::{
graph::RenderGraph,
stages::{
egui_cleanup_stage::EguiCleanupStage, egui_stage::EguiStage,
extract_stage::ExtractStage, phase_sort_stage::PhaseSortStage, queue_stage::QueueStage,
},
},
schedule::{Schedule, Stage, StageLabel},
};

mod egui_cleanup_stage;
mod egui_stage;
mod extract_stage;
mod graph_runner_stage;
mod phase_sort_stage;
Expand Down Expand Up @@ -56,12 +59,16 @@ multi_stage!(
PrepareStage,
resource: ResourceStage,
extract: ExtractStage,
upload: UploadStage
upload: UploadStage,
egui: EguiStage,
);

multi_stage!(CleanupStage, egui: EguiCleanupStage,);

pub fn register_default_render_stages(graph: RenderGraph, schedule: &mut Schedule) {
schedule.add_stage(RenderStageLabel::Prepare, PrepareStage::default());
schedule.add_stage(RenderStageLabel::Queue, QueueStage::default());
schedule.add_stage(RenderStageLabel::PhaseSort, PhaseSortStage::default());
schedule.add_stage(RenderStageLabel::Render, GraphRunnerStage::new(graph));
schedule.add_stage(RenderStageLabel::Cleanup, CleanupStage::default());
}
2 changes: 1 addition & 1 deletion maplibre/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Stage for NopStage {

#[macro_export]
macro_rules! multi_stage {
($multi_stage:ident, $($stage:ident: $stage_ty:ty),*) => {
($multi_stage:ident, $($stage:ident: $stage_ty:ty,)*) => {
pub struct $multi_stage {
$($stage: $stage_ty),*
}
Expand Down