Skip to content

Commit

Permalink
Merge pull request #8 from BillyDM/glow
Browse files Browse the repository at this point in the history
update to egui 0.19, use official glow painter
  • Loading branch information
Billy Messenger authored Sep 21, 2022
2 parents b7436c0 + 2617a66 commit 9abe392
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 841 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ readme = "README.md"

[features]
default = ["opengl"]
opengl = ["raw-gl-context", "gl"]
opengl = ["raw-gl-context", "egui_glow"]

[dependencies]
egui = "0.15"
egui = "0.19"
egui_glow = { version = "0.19", optional = true }
raw-gl-context = { version = "0.1", optional = true }
gl = { version = "0.14", optional = true }
keyboard-types = { version = "0.5", default-features = false }
baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "f6e99e9aa6f5aeb6b721cb05e4d882a51d995909" }
raw-window-handle = "0.3"
copypasta = "0.7.1"
raw-window-handle = "0.4"
copypasta = "0.8"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A [`baseview`] backend for [`egui`].

```rust
use baseview::{Size, WindowOpenOptions, WindowScalePolicy};
use egui::CtxRef;
use egui::Context;
use egui_baseview::{EguiWindow, Queue, RenderSettings, Settings};

fn main() {
Expand All @@ -33,10 +33,10 @@ fn main() {
state,
// Called once before the first frame. Allows you to do setup code and to
// call `ctx.set_fonts()`. Optional.
|_egui_ctx: &CtxRef, _queue: &mut Queue, _state: &mut State| {},
|_egui_ctx: &Context, _queue: &mut Queue, _state: &mut State| {},
// Called before each frame. Here you should update the state of your
// application and build the UI.
|egui_ctx: &CtxRef, _queue: &mut Queue, state: &mut State| {
|egui_ctx: &Context, _queue: &mut Queue, state: &mut State| {
egui::Window::new("egui-baseview simple demo").show(&egui_ctx, |ui| {
ui.heading("My Egui Application");
ui.horizontal(|ui| {
Expand Down
6 changes: 3 additions & 3 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use baseview::{Size, WindowOpenOptions, WindowScalePolicy};
use egui::CtxRef;
use egui::Context;
use egui_baseview::{EguiWindow, Queue, RenderSettings, Settings};

fn main() {
Expand All @@ -17,8 +17,8 @@ fn main() {
EguiWindow::open_blocking(
settings,
state,
|_egui_ctx: &CtxRef, _queue: &mut Queue, _state: &mut ()| {},
|egui_ctx: &CtxRef, _queue: &mut Queue, _state: &mut ()| {
|_egui_ctx: &Context, _queue: &mut Queue, _state: &mut ()| {},
|egui_ctx: &Context, _queue: &mut Queue, _state: &mut ()| {
egui::Window::new("egui-baseview hello world").show(&egui_ctx, |ui| {
ui.label("Hello World!");
});
Expand Down
6 changes: 3 additions & 3 deletions examples/simple_demo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use baseview::{Size, WindowOpenOptions, WindowScalePolicy};
use egui::CtxRef;
use egui::Context;
use egui_baseview::{EguiWindow, Queue, RenderSettings, Settings};

fn main() {
Expand All @@ -19,10 +19,10 @@ fn main() {
state,
// Called once before the first frame. Allows you to do setup code and to
// call `ctx.set_fonts()`. Optional.
|_egui_ctx: &CtxRef, _queue: &mut Queue, _state: &mut State| {},
|_egui_ctx: &Context, _queue: &mut Queue, _state: &mut State| {},
// Called before each frame. Here you should update the state of your
// application and build the UI.
|egui_ctx: &CtxRef, queue: &mut Queue, state: &mut State| {
|egui_ctx: &Context, queue: &mut Queue, state: &mut State| {
egui::Window::new("egui-baseview simple demo").show(&egui_ctx, |ui| {
ui.heading("My Egui Application");
ui.horizontal(|ui| {
Expand Down
77 changes: 49 additions & 28 deletions src/renderer/opengl_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,83 @@
use baseview::Window;
use egui::{Color32, Rgba};
use egui_glow::Painter;
use raw_gl_context::GlContext;
use std::sync::Arc;

pub use raw_gl_context::GlConfig as RenderSettings;

mod painter;
use painter::Painter;

pub struct Renderer {
context: GlContext,
glow_context: Arc<egui_glow::glow::Context>,
painter: Painter,
}

impl Renderer {
pub fn new(window: &Window, render_settings: RenderSettings, canvas_size: (u32, u32)) -> Self {
pub fn new(window: &Window, render_settings: RenderSettings) -> Self {
let context = GlContext::create(window, render_settings).unwrap();

context.make_current();

gl::load_with(|s| context.get_proc_address(s) as _);
let glow_context = Arc::new(unsafe {
egui_glow::glow::Context::from_loader_function(|s| context.get_proc_address(s))
});

let painter = Painter::new(canvas_size.0, canvas_size.1);
let painter = egui_glow::Painter::new(Arc::clone(&glow_context), None, "")
.map_err(|error| {
eprintln!("error occurred in initializing painter:\n{}", error);
})
.unwrap();

context.make_not_current();

Self { context, painter }
Self {
context,
glow_context,
painter,
}
}

pub fn render(
&mut self,
bg_color: Rgba,
clipped_meshes: Vec<egui::ClippedMesh>,
egui_texture: &egui::Texture,
bg_color: egui::Rgba,
canvas_width: u32,
canvas_height: u32,
pixels_per_point: f32,
egui_ctx: &mut egui::Context,
shapes: &mut Vec<egui::epaint::ClippedShape>,
textures_delta: &mut egui::TexturesDelta,
) {
let shapes = std::mem::take(shapes);
let mut textures_delta = std::mem::take(textures_delta);

self.context.make_current();

unsafe {
use egui_glow::glow::HasContext as _;
self.glow_context
.clear_color(bg_color.r(), bg_color.g(), bg_color.b(), bg_color.a());
self.glow_context.clear(egui_glow::glow::COLOR_BUFFER_BIT);
}

for (id, image_delta) in textures_delta.set {
self.painter.set_texture(id, &image_delta);
}

let clipped_primitives = egui_ctx.tessellate(shapes);
let dimensions: [u32; 2] = [canvas_width, canvas_height];

self.painter
.paint_meshes(bg_color, clipped_meshes, egui_texture, pixels_per_point);
.paint_primitives(dimensions, pixels_per_point, &clipped_primitives);

for id in textures_delta.free.drain(..) {
self.painter.free_texture(id);
}

self.context.swap_buffers();
self.context.make_not_current();
}
}

pub fn new_user_texture(
&mut self,
size: (usize, usize),
srgba_pixels: &[Color32],
filtering: bool,
) -> egui::TextureId {
self.painter.new_user_texture(size, srgba_pixels, filtering)
}

pub fn update_user_texture_data(&mut self, texture_id: egui::TextureId, pixels: &[Color32]) {
self.painter.update_user_texture_data(texture_id, pixels)
}

pub fn update_window_size(&mut self, width: u32, height: u32) {
self.painter.set_canvas_size(width, height);
impl Drop for Renderer {
fn drop(&mut self) {
self.painter.destroy()
}
}
Loading

0 comments on commit 9abe392

Please sign in to comment.