From 8936491b847513f74dad5642187fd8778c556afd Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 4 Dec 2026 20:46:32 +0100 Subject: refactor(engine): have rendering surfaces made current implicitly by rendering backend --- engine/src/rendering/backend/opengl.rs | 104 +++++++++++++++++---------------- 1 file changed, 53 insertions(+), 51 deletions(-) (limited to 'engine/src/rendering/backend/opengl.rs') diff --git a/engine/src/rendering/backend/opengl.rs b/engine/src/rendering/backend/opengl.rs index 1dfdc15..d066b61 100644 --- a/engine/src/rendering/backend/opengl.rs +++ b/engine/src/rendering/backend/opengl.rs @@ -186,6 +186,7 @@ struct GraphicsContext { gl_context: Option, surfaces: HashMap, + current_surface_id: Option, backend_resources: BackendResourceStore, } @@ -735,17 +736,6 @@ fn init_window_graphics( continue; } - if let Err(err) = gl_set_viewport( - gl_context, - &Vec2 { x: 0, y: 0 }.into(), - &opengl_bindings::data_types::Dimens { - width: window.inner_size.width, - height: window.inner_size.height, - }, - ) { - tracing::error!("Failed to set viewport: {err}"); - } - set_enabled(gl_context, Capability::DepthTest, graphics_props.depth_test); set_enabled( @@ -824,6 +814,7 @@ fn handle_commands( let GraphicsContext { ref gl_context, ref mut surfaces, + ref mut current_surface_id, ref mut backend_resources, } = *graphics_ctx; @@ -848,7 +839,7 @@ fn handle_commands( continue; }; - if surface.window_surface.is_current(gl_context.context()) { + if *current_surface_id == Some(surface_id) { if let Err(err) = gl_context.context().make_not_current_in_place() { tracing::error!("Failed to make GL context not current: {err}"); } @@ -856,31 +847,11 @@ fn handle_commands( if let Err(err) = gl_context.make_current_surfaceless() { tracing::error!("Failed to make GL context current: {err}"); } - } - drop(surface); - } - Command::MakeCurrent(surface_id) => { - let Some(surface) = surfaces.get(&surface_id) else { - tracing::error!(surface_id=?surface_id, "Surface does not exist"); - continue; - }; - - if let Err(err) = gl_context.make_current(&surface.window_surface) { - tracing::error!("Failed to make graphics context current: {err}"); - continue; + *current_surface_id = None; } - if let Err(err) = gl_set_viewport( - gl_context, - &Vec2 { x: 0, y: 0 }.into(), - &opengl_bindings::data_types::Dimens { - width: surface.size.width, - height: surface.size.height, - }, - ) { - tracing::error!("Failed to set viewport: {err}"); - } + drop(surface); } Command::SetSurfaceSize(surface_id, new_surface_size) => { let Some(surface) = surfaces.get_mut(&surface_id) else { @@ -889,23 +860,24 @@ fn handle_commands( }; surface.size = new_surface_size; + } + Command::ClearSurfaceBuffers(surface_id, buffer_clear_mask) => { + if Some(surface_id) != *current_surface_id { + let Some(surface) = surfaces.get(&surface_id) else { + cold_path(); + tracing::error!(?surface_id, "Surface does not exist"); + continue; + }; - if !surface.window_surface.is_current(gl_context.context()) { - continue; - } + if let Err(err) = gl_context.make_current(&surface.window_surface) { + cold_path(); + tracing::error!("Failed to make graphics context current: {err}"); + continue; + } - if let Err(err) = gl_set_viewport( - gl_context, - &Vec2 { x: 0, y: 0 }.into(), - &opengl_bindings::data_types::Dimens { - width: surface.size.width, - height: surface.size.height, - }, - ) { - tracing::error!("Failed to set viewport: {err}"); + *current_surface_id = Some(surface_id); } - } - Command::ClearBuffers(buffer_clear_mask) => { + let mut clear_mask = GlBufferClearMask::empty(); clear_mask.set( @@ -927,7 +899,7 @@ fn handle_commands( } Command::SwapSurfaceBuffers(surface_id) => { let Some(surface) = surfaces.get(&surface_id) else { - tracing::error!(surface_id=?surface_id, "Surface does not exist"); + tracing::error!(?surface_id, "Surface does not exist"); continue; }; @@ -1210,7 +1182,7 @@ fn handle_commands( gl_context, ); } - Command::DrawMesh(object_id, draw_mesh_opts) => { + Command::DrawMesh(object_id, draw_mesh_opts, surface_id) => { let Some(resource) = backend_resources.get_resource_mut( object_store, object_id, @@ -1251,13 +1223,43 @@ fn handle_commands( gl_context, shader_program_vertex_desc, ) { - tracing::error!("Cannot draw mesh: {err}"); + tracing::error!("Updating mesh vertex attrs failed: {err}"); continue; } *vertex_attrs_updated_for_shader = Some(*shader_program_obj_id); } + let Some(surface) = surfaces.get(&surface_id) else { + cold_path(); + tracing::error!(?surface_id, "Surface does not exist"); + continue; + }; + + if Some(surface_id) != *current_surface_id { + if let Err(err) = gl_context.make_current(&surface.window_surface) { + cold_path(); + tracing::error!("Failed to make graphics context current: {err}"); + continue; + } + + *current_surface_id = Some(surface_id); + } + + // TODO: Only set the viewport if the surface have been made current or if + // the surface's size has changed. This might improve performance a bit + if let Err(err) = gl_set_viewport( + gl_context, + &Vec2 { x: 0, y: 0 }.into(), + &opengl_bindings::data_types::Dimens { + width: surface.size.width, + height: surface.size.height, + }, + ) { + cold_path(); + tracing::error!("Failed to set viewport: {err}"); + } + if let Err(err) = draw_mesh(gl_context, graphics_mesh, &draw_mesh_opts) { tracing::error!("Failed to draw mesh: {err}"); } -- cgit v1.2.3-18-g5258