Skip to content

Commit

Permalink
Puffin UI
Browse files Browse the repository at this point in the history
  • Loading branch information
vojd committed Sep 7, 2023
1 parent 0469874 commit 3f16044
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 6 deletions.
85 changes: 85 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ glutin-winit = "0.3.0"
log = "0.4"
macros = { path = "components/macros", version = "0.1" }
memoffset = "0.9"
puffin = "0.16"
puffin_egui = "0.22"
# required by glutin to access the raw window handle from the &Window
raw-window-handle = "0.5.0"
regex = "1.9"
Expand Down
1 change: 1 addition & 0 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn handle_actions(
shader_service: &mut ShaderService,
control_flow: &mut ControlFlow,
) {
puffin::profile_function!();
for action in actions.drain(..) {
match action {
Action::AppExit => {
Expand Down
15 changes: 14 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ impl App {
log::debug!("MainLoop: Start");

while app_state.is_running {
puffin::profile_scope!("Main loop");
puffin::GlobalProfiler::lock().new_frame();

let _ = shader_service.run(gl.as_ref());
app_state.shader_error = shader_service.last_error.clone();

Expand All @@ -76,6 +79,7 @@ impl App {
// TODO: No unwrap on the window object

let _repaint_after = ui.run(app_window.window.as_ref().unwrap(), |egui_ctx| {
puffin::profile_scope!("UI: update");
egui::TopBottomPanel::top("view_top").show(egui_ctx, |ui| {
top_bar(ui, app_state, &mut actions, &shader_service);
});
Expand All @@ -95,23 +99,32 @@ impl App {
});
});
}

puffin_egui::profiler_window(egui_ctx);
});

puffin::profile_scope!("Handle: Events");
handle_events(&event, control_flow, &mut ui, app_state, &mut actions);

puffin::profile_scope!("Handle: Events");
handle_actions(&mut actions, app_state, &mut shader_service, control_flow);
});

// Render the OpenGL scene
renderer.draw(app_state, &shader_service);
{
puffin::profile_scope!("Renderer::draw");
renderer.draw(app_state, &shader_service);
}

// Render UI on top of OpenGL scene
if app_state.ui_visible && app_window.window.is_some() {
puffin::profile_scope!("UI: paint");
if let Some(window) = &app_window.window {
ui.paint(window);
}
}

puffin::profile_scope!("Swap buffers");
app_window.swap_buffers();

app_state.timer.stop();
Expand Down
3 changes: 2 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ pub fn handle_events<T>(
app_state: &mut AppState,
actions: &mut Vec<Action>,
) {
*control_flow = ControlFlow::Poll;
puffin::profile_function!();

*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent { event, .. } => {
match event {
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ fn create_new_default_shader(path: PathBuf) -> std::io::Result<u64> {
fn main() -> anyhow::Result<(), anyhow::Error> {
SimpleLogger::new().init().unwrap();

puffin::set_scopes_on(true);

// Parse command line arguments using `structopt`
let config = AppConfig::parse();
let mut app = App::from_config(config.clone());
Expand Down
1 change: 1 addition & 0 deletions src/render/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl Renderer {
}

pub fn draw(&self, state: &mut AppState, shader_service: &ShaderService) {
puffin::profile_function!();
let gl = self.gl.clone();
unsafe {
gl.bind_vertex_array(Some(self.vertex_array));
Expand Down
7 changes: 5 additions & 2 deletions src/shader/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl ShaderService {
/// It is basically the same as watching for file changes and the
/// reload the shaders whenever that happens.
pub fn run(&mut self, gl: &glow::Context) -> Result<(), ShaderError> {
puffin::profile_function!();

// pull file updates from the channel
if let Some(recv) = &self.receiver {
if let Ok(changed_path_buf) = recv.try_recv() {
Expand All @@ -79,9 +81,9 @@ impl ShaderService {
if shader.ready_to_compile {
match shader.try_to_compile() {
Ok(_) => {
log::debug!("Shader compiled");
shader.find_shader_uniforms(gl);
shader.bind_uniform_locations(gl);
self.last_error = None;
log::info!("Shader compiled");
}
Err(e) => {
self.last_error = Some(e.clone());
Expand All @@ -90,6 +92,7 @@ impl ShaderService {
}
}
}

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/shader/skuggbox_shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl SkuggboxShader {
}

/// Detected uniforms in the shader source
pub fn find_shader_uniforms(&mut self, gl: &glow::Context) {
pub fn bind_uniform_locations(&mut self, gl: &glow::Context) {
if let Some(program) = self.program {
self.locations = unsafe { ShaderProgram::uniform_locations(gl, program) };
}
Expand Down
11 changes: 10 additions & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use glutin::context::{
PossiblyCurrentContext, Version,
};
use glutin::display::{GetGlDisplay, GlDisplay};
use glutin::surface::{GlSurface, Surface, WindowSurface};
use glutin::surface::{GlSurface, Surface, SwapInterval, WindowSurface};
use std::ffi::CString;
use std::num::NonZeroU32;
use std::sync::Arc;

use glutin_winit::{DisplayBuilder, GlWindow};
Expand Down Expand Up @@ -126,6 +127,14 @@ impl AppWindow {
.make_current(&gl_surface)
.unwrap();

// Try to enable vsync
log::info!("Window: Enabling vsync");
if let Err(err) = gl_surface
.set_swap_interval(&gl_context, SwapInterval::Wait(NonZeroU32::new(1).unwrap()))
{
log::error!("Window: {:?}", err);
}

let gl_display = gl_config.display();
let gl = unsafe {
Context::from_loader_function(|symbol| {
Expand Down

0 comments on commit 3f16044

Please sign in to comment.