From 5c338b18fafdd5ad348b168693993c6aebe6e029 Mon Sep 17 00:00:00 2001 From: Tsuki Date: Tue, 3 Sep 2024 16:05:22 +0800 Subject: [PATCH] add debug layer --- radar-g/src/widgets/render/imp.rs | 60 ++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/radar-g/src/widgets/render/imp.rs b/radar-g/src/widgets/render/imp.rs index 7b4057f..6e1b412 100644 --- a/radar-g/src/widgets/render/imp.rs +++ b/radar-g/src/widgets/render/imp.rs @@ -15,6 +15,7 @@ use gtk::subclass::prelude::*; use slippy_map_tiles::Tile; use std::cell::{Cell, RefCell}; use std::collections::HashMap; +use std::ffi::CStr; use std::num::NonZeroU32; use std::rc::Rc; use std::sync::{Arc, Mutex}; @@ -203,7 +204,30 @@ impl Render { static LOAD_FN: fn(&str) -> *const std::ffi::c_void = |s| epoxy::get_proc_addr(s) as *const _; use femtovg::renderer::OpenGl; - let ctx = glow::Context::from_loader_function(LOAD_FN); + let mut ctx = glow::Context::from_loader_function(LOAD_FN); + + let version = unsafe { ctx.get_parameter_string(glow::VERSION) }; + info!("OpenGL Version: {}", version); + + if ctx.supported_extensions().contains("GL_ARB_debug_output") { + unsafe { + ctx.debug_message_callback(|source, _type, id, severity, message| { + println!( + "GL ARB Debug Message: Source: {:?}, ID: {}, Severity: {:?}, Message: {}", + source, + id, + severity, + message + ); + }); + ctx.enable(glow::DEBUG_OUTPUT); + ctx.enable(glow::DEBUG_OUTPUT_SYNCHRONOUS); + } + } else { + info!("GL_ARB_debug_output not supported"); + } + + // enable_opengl_debugging(&mut ctx); let id = NonZeroU32::new(ctx.get_parameter_i32(glow::DRAW_FRAMEBUFFER_BINDING) as u32) @@ -273,3 +297,37 @@ impl Render { (x, h as f32 - y) } } + +fn gl_debug_output( + source: u32, + _type: u32, + id: u32, + severity: u32, + _length: i32, + message: *const i8, + _user_param: *mut std::ffi::c_void, +) { + unsafe { + let message = CStr::from_ptr(message); + println!( + "GL Debug Message: Source: {:?}, ID: {}, Severity: {:?}, Message: {}", + source, + id, + severity, + message.to_string_lossy() + ); + } +} + +fn enable_opengl_debugging(gl: &mut glow::Context) { + unsafe { + gl.enable(glow::DEBUG_OUTPUT); + gl.enable(glow::DEBUG_OUTPUT_SYNCHRONOUS); // 确保错误信息立即显示 + gl.debug_message_callback(|source, _type, id, severity, message| { + println!( + "GL Debug Message: Source: {:?}, ID: {}, Severity: {:?}, Message: {}", + source, id, severity, message + ); + }); + } +}