From da329909f93597970afd169cee28ece3bee7127b Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 15 Oct 2023 00:06:07 +0200 Subject: feat(engine): add logging OpenGL debug messages --- engine/src/renderer/mod.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'engine/src/renderer') diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 4ed3c0a..cc1631f 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -3,6 +3,8 @@ use std::process::abort; use glfw::WindowSize; +#[cfg(feature = "debug")] +use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType}; use crate::opengl::shader::Program as ShaderProgram; use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; use crate::opengl::vertex_buffer::{BufferUsage, VertexBuffer}; @@ -28,6 +30,22 @@ pub fn initialize(window: &glfw::Window) -> Result<(), Error> } }); + #[cfg(feature = "debug")] + { + use crate::opengl::debug::{ + enable_debug_output, + set_debug_message_callback, + set_debug_message_control, + MessageIdsAction, + }; + + enable_debug_output(); + + set_debug_message_callback(opengl_debug_message_cb); + + set_debug_message_control(None, None, None, &[], MessageIdsAction::Disable); + } + let window_size = window.size().map_err(Error::GetWindowSizeFailed)?; set_viewport(&Vec2 { x: 0, y: 0 }, &window_size); @@ -35,6 +53,37 @@ pub fn initialize(window: &glfw::Window) -> Result<(), Error> Ok(()) } +#[cfg(feature = "debug")] +#[tracing::instrument(skip_all)] +fn opengl_debug_message_cb( + source: MessageSource, + ty: MessageType, + id: u32, + severity: MessageSeverity, + message: &str, +) +{ + use tracing::{event, Level}; + + macro_rules! create_event { + ($level: expr) => { + event!($level, ?source, ?ty, id, ?severity, message); + }; + } + + match ty { + MessageType::Error => { + create_event!(Level::ERROR); + } + MessageType::Other => { + create_event!(Level::INFO); + } + _ => { + create_event!(Level::WARN); + } + }; +} + pub fn render<'renderable>(renderables: impl IntoIterator) { clear_buffers(BufferClearMask::COLOR); -- cgit v1.2.3-18-g5258