Skip to main content

dear_imgui_glow/
lib.rs

1//! Glow (OpenGL) renderer for Dear ImGui
2//!
3//! This crate provides a Glow-based renderer for Dear ImGui, allowing you to
4//! render Dear ImGui interfaces using the Glow OpenGL abstraction.
5//!
6//! # Features
7//!
8//! - **Basic rendering**: Render Dear ImGui draw data using OpenGL
9//! - **Texture support**: Handle font textures and user textures
10//! - **Multi-viewport support**: Support for multiple windows (feature-gated)
11//! - **OpenGL compatibility**: Support for OpenGL 2.1+ and OpenGL ES 2.0+
12//!
13//! # Example
14//!
15//! ```rust,no_run
16//! use dear_imgui_rs::Context;
17//! use dear_imgui_glow::GlowRenderer;
18//! use glow::HasContext;
19//!
20//! // Initialize your OpenGL context and Dear ImGui context
21//! let gl = unsafe { glow::Context::from_loader_function(|s| {
22//!     // Your OpenGL loader function
23//!     std::ptr::null()
24//! }) };
25//! let mut imgui = Context::create();
26//!
27//! // Create the renderer (simple usage)
28//! let mut renderer = GlowRenderer::new(gl, &mut imgui).unwrap();
29//!
30//! // In your render loop:
31//! // imgui.new_frame();
32//! // ... build your UI ...
33//! // let draw_data = imgui.render();
34//! // renderer.render(draw_data).unwrap();
35//! ```
36
37// Re-export glow to make it easier for users to use the correct version.
38pub use glow;
39use glow::{Context, HasContext};
40
41mod error;
42mod renderer;
43mod shaders;
44mod state;
45mod texture;
46mod versions;
47
48pub use error::*;
49pub use renderer::*;
50pub use texture::*;
51pub use versions::*;
52
53// Re-export multi-viewport support if enabled
54#[cfg(feature = "multi-viewport")]
55pub use renderer::multi_viewport;
56
57pub type GlBuffer = <Context as HasContext>::Buffer;
58pub type GlTexture = <Context as HasContext>::Texture;
59pub type GlVertexArray = <Context as HasContext>::VertexArray;
60pub type GlProgram = <Context as HasContext>::Program;
61pub type GlUniformLocation = <Context as HasContext>::UniformLocation;
62
63/// Convert a DrawVert slice to a byte slice
64///
65/// Safety notes:
66/// - This intentionally does **not** accept arbitrary `T` to avoid accidentally
67///   reading padding bytes from Rust-side structs (which could be uninitialized).
68/// - `DrawVert` is a `#[repr(C)]` layout-compatible vertex type with no padding
69///   (verified via the size check below).
70#[inline]
71fn draw_verts_as_bytes(slice: &[dear_imgui_rs::render::DrawVert]) -> &[u8] {
72    const _: [(); 20] = [(); std::mem::size_of::<dear_imgui_rs::render::DrawVert>()];
73    unsafe { std::slice::from_raw_parts(slice.as_ptr().cast::<u8>(), std::mem::size_of_val(slice)) }
74}
75
76/// Convert a DrawIdx slice to a byte slice.
77#[inline]
78fn draw_indices_as_bytes(slice: &[dear_imgui_rs::render::DrawIdx]) -> &[u8] {
79    const _: [(); 2] = [(); std::mem::size_of::<dear_imgui_rs::render::DrawIdx>()];
80    unsafe { std::slice::from_raw_parts(slice.as_ptr().cast::<u8>(), std::mem::size_of_val(slice)) }
81}
82
83/// Debug message helper for OpenGL debugging
84#[cfg(feature = "debug_message_insert_support")]
85fn gl_debug_message(gl: &Context, message: &str) {
86    unsafe {
87        gl.debug_message_insert(
88            glow::DEBUG_SOURCE_APPLICATION,
89            glow::DEBUG_TYPE_MARKER,
90            0,
91            glow::DEBUG_SEVERITY_NOTIFICATION,
92            message,
93        );
94    }
95}
96
97#[cfg(not(feature = "debug_message_insert_support"))]
98fn gl_debug_message(_gl: &Context, _message: &str) {
99    // No-op when debug messages are not supported
100}