diff --git a/imgui-glow-renderer/src/lib.rs b/imgui-glow-renderer/src/lib.rs index 7e50040..96070b3 100644 --- a/imgui-glow-renderer/src/lib.rs +++ b/imgui-glow-renderer/src/lib.rs @@ -45,41 +45,10 @@ use std::{borrow::Cow, error::Error, fmt::Display, mem::size_of}; use imgui::internal::RawWrapper; use crate::versions::{GlVersion, GlslVersion}; +use glow::{Context, HasContext}; pub mod versions; -// TODO: document all the things! - -// This may be generics overkill, but I have to assume there's some reason that -// `glow` hid the functions of `[glow::Context]` behind the `[glow::Context]` -// trait. The specified types required here are necessary because otherwise -// required initialisations or conversions in this crate can't be performed. In -// any case, the OpenGL standard specifies these types, so there should be no -// loss of generality. -pub trait Gl: - glow::HasContext< - Texture = glow::Texture, - UniformLocation = glow::UniformLocation, - Program = glow::Program, - Buffer = glow::Buffer, - Sampler = glow::Sampler, - VertexArray = glow::VertexArray, -> -{ -} -impl< - G: glow::HasContext< - Texture = glow::Texture, - UniformLocation = glow::UniformLocation, - Program = glow::Program, - Buffer = glow::Buffer, - Sampler = glow::Sampler, - VertexArray = glow::VertexArray, - >, - > Gl for G -{ -} - /// Renderer which owns the OpenGL context and handles textures itself. Also /// converts all output colors to sRGB for display. Useful for simple applications, /// but more complicated applications may prefer to use `[Renderer]`, or even @@ -87,17 +56,20 @@ impl< /// /// OpenGL context is still available to the rest of the application through /// the `[gl_context]` method. -pub struct AutoRenderer { - gl: G, +pub struct AutoRenderer { + gl: glow::Context, texture_map: SimpleTextureMap, - renderer: Renderer, + renderer: Renderer, } -impl AutoRenderer { +impl AutoRenderer { /// # Errors /// Any error initialising the OpenGL objects (including shaders) will /// result in an error. - pub fn initialize(gl: G, imgui_context: &mut imgui::Context) -> Result { + pub fn initialize( + gl: glow::Context, + imgui_context: &mut imgui::Context, + ) -> Result { let mut texture_map = SimpleTextureMap::default(); let renderer = Renderer::initialize(&gl, imgui_context, &mut texture_map, true)?; Ok(Self { @@ -110,7 +82,7 @@ impl AutoRenderer { /// Note: no need to provide a `mut` version of this, as all methods on /// `[glow::HasContext]` are immutable. #[inline] - pub fn gl_context(&self) -> &G { + pub fn gl_context(&self) -> &glow::Context { &self.gl } @@ -125,7 +97,7 @@ impl AutoRenderer { } #[inline] - pub fn renderer(&self) -> &Renderer { + pub fn renderer(&self) -> &Renderer { &self.renderer } @@ -138,7 +110,7 @@ impl AutoRenderer { } } -impl Drop for AutoRenderer { +impl Drop for AutoRenderer { fn drop(&mut self) { self.renderer.destroy(&self.gl); } @@ -146,20 +118,20 @@ impl Drop for AutoRenderer { /// Main renderer. Borrows the OpenGL context and [texture map](TextureMap) /// when required. -pub struct Renderer { - shaders: Shaders, +pub struct Renderer { + shaders: Shaders, state_backup: GlStateBackup, - pub vbo_handle: G::Buffer, - pub ebo_handle: G::Buffer, - pub font_atlas_texture: G::Texture, + pub vbo_handle: ::Buffer, + pub ebo_handle: ::Buffer, + pub font_atlas_texture: ::Texture, #[cfg(feature = "bind_vertex_array_support")] - pub vertex_array_object: G::VertexArray, + pub vertex_array_object: ::VertexArray, pub gl_version: GlVersion, pub has_clip_origin_support: bool, pub is_destroyed: bool, } -impl Renderer { +impl Renderer { /// Create the renderer, initialising OpenGL objects and shaders. /// /// `output_srgb` controls whether the shader outputs sRGB colors, or linear @@ -182,7 +154,7 @@ impl Renderer { /// Any error initialising the OpenGL objects (including shaders) will /// result in an error. pub fn initialize( - gl: &G, + gl: &Context, imgui_context: &mut imgui::Context, texture_map: &mut T, output_srgb: bool, @@ -249,7 +221,7 @@ impl Renderer { /// This must be called before being dropped to properly free OpenGL /// resources. - pub fn destroy(&mut self, gl: &G) { + pub fn destroy(&mut self, gl: &Context) { if self.is_destroyed { return; } @@ -279,7 +251,7 @@ impl Renderer { /// however) pub fn render( &mut self, - gl: &G, + gl: &Context, texture_map: &T, draw_data: &imgui::DrawData, ) -> Result<(), RenderError> { @@ -351,7 +323,7 @@ impl Renderer { /// result in an error. pub fn set_up_render_state( &mut self, - gl: &G, + gl: &Context, draw_data: &imgui::DrawData, fb_width: f32, fb_height: f32, @@ -469,7 +441,7 @@ impl Renderer { #[allow(clippy::too_many_arguments)] fn render_elements( &self, - gl: &G, + gl: &Context, texture_map: &T, element_count: usize, element_params: imgui::DrawCmdParams, @@ -561,9 +533,15 @@ impl Renderer { /// Then `[gl_texture]` can be called to find the OpenGL texture corresponding to /// that `[imgui::TextureId]`. pub trait TextureMap { - fn register(&mut self, gl_texture: glow::Texture) -> Option; + fn register( + &mut self, + gl_texture: ::Texture, + ) -> Option; - fn gl_texture(&self, imgui_texture: imgui::TextureId) -> Option; + fn gl_texture( + &self, + imgui_texture: imgui::TextureId, + ) -> Option<::Texture>; } /// Texture map where the imgui texture ID is simply numerically equal to the @@ -639,18 +617,18 @@ pub struct GlStateBackup { } impl GlStateBackup { - fn pre_init(&mut self, gl: &G) { + fn pre_init(&mut self, gl: &Context) { self.texture = unsafe { gl.get_parameter_i32(glow::TEXTURE_BINDING_2D) }; } - fn post_init(&mut self, gl: &G) { + fn post_init(&mut self, gl: &Context) { #[allow(clippy::cast_sign_loss)] unsafe { gl.bind_texture(glow::TEXTURE_2D, Some(self.texture as _)); } } - fn pre_render(&mut self, gl: &G, gl_version: GlVersion) { + fn pre_render(&mut self, gl: &Context, gl_version: GlVersion) { #[allow(clippy::cast_sign_loss)] unsafe { self.active_texture = gl.get_parameter_i32(glow::ACTIVE_TEXTURE); @@ -701,7 +679,7 @@ impl GlStateBackup { } } - fn post_render(&mut self, gl: &G, _gl_version: GlVersion) { + fn post_render(&mut self, gl: &Context, _gl_version: GlVersion) { #![allow(clippy::cast_sign_loss)] unsafe { gl.use_program(Some(self.program as _)); @@ -782,17 +760,17 @@ impl GlStateBackup { /// Parses `GL_VERSION` and `GL_SHADING_LANGUAGE_VERSION` at runtime in order to /// generate shaders which should work on a wide variety of modern devices /// (GL >= 3.3 and GLES >= 2.0 are expected to work). -struct Shaders { - program: G::Program, - texture_uniform_location: G::UniformLocation, - matrix_uniform_location: G::UniformLocation, +struct Shaders { + program: ::Program, + texture_uniform_location: ::UniformLocation, + matrix_uniform_location: ::UniformLocation, position_attribute_index: u32, uv_attribute_index: u32, color_attribute_index: u32, } -impl Shaders { - fn new(gl: &G, gl_version: GlVersion, output_srgb: bool) -> Result { +impl Shaders { + fn new(gl: &Context, gl_version: GlVersion, output_srgb: bool) -> Result { let (vertex_source, fragment_source) = Self::get_shader_sources(gl, gl_version, output_srgb)?; @@ -859,7 +837,7 @@ impl Shaders { } fn get_shader_sources( - gl: &G, + gl: &Context, gl_version: GlVersion, output_srgb: bool, ) -> Result<(String, String), ShaderError> { @@ -1055,11 +1033,11 @@ impl From for InitError { pub type RenderError = String; -fn prepare_font_atlas( - gl: &G, +fn prepare_font_atlas( + gl: &Context, mut fonts: imgui::FontAtlasRefMut, texture_map: &mut T, -) -> Result { +) -> Result<::Texture, InitError> { #![allow(clippy::cast_possible_wrap)] let atlas_texture = fonts.build_rgba32_texture();