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 +++++++++++++++++---------------- engine/src/rendering/main_pipeline.rs | 16 ++--- 2 files changed, 62 insertions(+), 58 deletions(-) (limited to 'engine/src/rendering') 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}"); } diff --git a/engine/src/rendering/main_pipeline.rs b/engine/src/rendering/main_pipeline.rs index 0695800..f8b3e00 100644 --- a/engine/src/rendering/main_pipeline.rs +++ b/engine/src/rendering/main_pipeline.rs @@ -52,6 +52,7 @@ use crate::rendering::{ RenderPasses, RgbaTextureDataType, Surface, + SurfaceId, TargetWindow, TextureCreation, TexturePixelDataFormat, @@ -214,10 +215,6 @@ pub fn add_passes( )?; for (window, window_surface) in &window_query { - render_pass - .commands - .push(Command::MakeCurrent(window_surface.id)); - add_set_3d_shader_camera_bindings( render_pass, (&camera, &camera_transform), @@ -233,9 +230,10 @@ pub fn add_passes( matches!(scene_sky_box_state, Ok(Some(_)) | Err(_)), ); - render_pass - .commands - .push(Command::ClearBuffers(buf_clear_mask)); + render_pass.commands.push(Command::ClearSurfaceBuffers( + window_surface.id, + buf_clear_mask, + )); for (mesh, material, material_flags, transform, draw_flags) in &renderable_query { if assets.get(&mesh.asset).is_none() { @@ -296,6 +294,7 @@ pub fn add_passes( render_pass.commands.push(Command::DrawMesh( ObjectId::Asset(mesh.asset.id()), DrawMeshOptions::default(), + window_surface.id, )); if draw_flags.as_deref().is_some_and(|draw_flags| { @@ -318,6 +317,7 @@ pub fn add_passes( &camera, &camera_transform, &window, + window_surface.id, shaders.sky_box_shader_obj_id, shaders.sky_box_shader, &backend_properties, @@ -795,6 +795,7 @@ fn add_sky_box_commands( camera: &Camera, camera_transform: &Transform, window: &Window, + surface_id: SurfaceId, sky_box_shader_obj_id: ObjectId, sky_box_shader_program: &ShaderProgram, backend_properties: &BackendProperties, @@ -866,6 +867,7 @@ fn add_sky_box_commands( render_pass.commands.push(Command::DrawMesh( sky_box_state.mesh_object_id, DrawMeshOptions::default(), + surface_id, )); Ok(()) -- cgit v1.2.3-18-g5258