Skip to content

Delete OpenGL objects on regular drop instead of explicit destroy in glow example #7046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions crates/egui_demo_app/src/apps/custom3d_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Custom3d {

impl Custom3d {
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Option<Self> {
let gl = cc.gl.as_ref()?;
let gl = cc.gl.as_ref()?.clone();
Some(Self {
rotating_triangle: Arc::new(Mutex::new(RotatingTriangle::new(gl)?)),
angle: 0.0,
Expand Down Expand Up @@ -44,12 +44,6 @@ impl eframe::App for Custom3d {
});
});
}

fn on_exit(&mut self, gl: Option<&glow::Context>) {
if let Some(gl) = gl {
self.rotating_triangle.lock().destroy(gl);
}
}
}

impl Custom3d {
Expand All @@ -76,16 +70,17 @@ impl Custom3d {
}

struct RotatingTriangle {
gl: Arc<glow::Context>,
program: glow::Program,
vertex_array: glow::VertexArray,
}

#[expect(unsafe_code)] // we need unsafe code to use glow
impl RotatingTriangle {
fn new(gl: &glow::Context) -> Option<Self> {
fn new(gl: Arc<glow::Context>) -> Option<Self> {
use glow::HasContext as _;

let shader_version = egui_glow::ShaderVersion::get(gl);
let shader_version = egui_glow::ShaderVersion::get(&gl);

unsafe {
let program = gl.create_program().expect("Cannot create program");
Expand Down Expand Up @@ -176,20 +171,13 @@ impl RotatingTriangle {
.expect("Cannot create vertex array");

Some(Self {
gl,
program,
vertex_array,
})
}
}

fn destroy(&self, gl: &glow::Context) {
use glow::HasContext as _;
unsafe {
gl.delete_program(self.program);
gl.delete_vertex_array(self.vertex_array);
}
}

fn paint(&self, gl: &glow::Context, angle: f32) {
use glow::HasContext as _;
unsafe {
Expand All @@ -203,3 +191,13 @@ impl RotatingTriangle {
}
}
}

impl Drop for RotatingTriangle {
fn drop(&mut self) {
use glow::HasContext as _;
unsafe {
self.gl.delete_program(self.program);
self.gl.delete_vertex_array(self.vertex_array);
}
}
}
31 changes: 15 additions & 16 deletions examples/custom_3d_glow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ impl MyApp {
let gl = cc
.gl
.as_ref()
.expect("You need to run eframe with the glow backend");
.expect("You need to run eframe with the glow backend")
.clone();
Self {
rotating_triangle: Arc::new(Mutex::new(RotatingTriangle::new(gl))),
angle: 0.0,
Expand All @@ -58,12 +59,6 @@ impl eframe::App for MyApp {
ui.label("Drag to rotate!");
});
}

fn on_exit(&mut self, gl: Option<&glow::Context>) {
if let Some(gl) = gl {
self.rotating_triangle.lock().destroy(gl);
}
}
}

impl MyApp {
Expand All @@ -88,12 +83,13 @@ impl MyApp {
}

struct RotatingTriangle {
gl: Arc<glow::Context>,
program: glow::Program,
vertex_array: glow::VertexArray,
}

impl RotatingTriangle {
fn new(gl: &glow::Context) -> Self {
fn new(gl: Arc<glow::Context>) -> Self {
use glow::HasContext as _;

let shader_version = if cfg!(target_arch = "wasm32") {
Expand Down Expand Up @@ -175,20 +171,13 @@ impl RotatingTriangle {
.expect("Cannot create vertex array");

Self {
gl,
program,
vertex_array,
}
}
}

fn destroy(&self, gl: &glow::Context) {
use glow::HasContext as _;
unsafe {
gl.delete_program(self.program);
gl.delete_vertex_array(self.vertex_array);
}
}

fn paint(&self, gl: &glow::Context, angle: f32) {
use glow::HasContext as _;
unsafe {
Expand All @@ -202,3 +191,13 @@ impl RotatingTriangle {
}
}
}

impl Drop for RotatingTriangle {
fn drop(&mut self) {
use glow::HasContext as _;
unsafe {
self.gl.delete_program(self.program);
self.gl.delete_vertex_array(self.vertex_array);
}
}
}