summaryrefslogtreecommitdiff
path: root/engine/src/renderer/opengl.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-05-28 19:58:28 +0200
committerHampusM <hampus@hampusmat.com>2026-05-28 19:58:28 +0200
commitea0e368379270cfbc10c28a1e63446a6f1c46c43 (patch)
tree0a36632da23bd78a6488331c6bea72cd05b2cf41 /engine/src/renderer/opengl.rs
parent038aead236e77f41e6d229510a5e25741a6815c6 (diff)
refactor(engine): adjust gl renderer to changes in opengl-bindings
Diffstat (limited to 'engine/src/renderer/opengl.rs')
-rw-r--r--engine/src/renderer/opengl.rs159
1 files changed, 46 insertions, 113 deletions
diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs
index e6163ab..e571595 100644
--- a/engine/src/renderer/opengl.rs
+++ b/engine/src/renderer/opengl.rs
@@ -58,9 +58,8 @@ use opengl_bindings::vertex_array::{
VertexArray,
};
use opengl_bindings::{
- ContextWithFns,
- CurrentContextWithFns,
MakeContextCurrentError as GlMakeContextCurrentError,
+ MaybeCurrentContextWithFns,
};
use raw_window_handle::WindowHandle;
use safer_ffi::layout::ReprC;
@@ -135,7 +134,7 @@ struct WindowGlConfig
#[derive(Sole, Default)]
struct GraphicsContext
{
- gl_context: Option<ContextWithFns>,
+ gl_context: Option<MaybeCurrentContextWithFns>,
surfaces: HashMap<SurfaceId, GraphicsContextSurface>,
shader_uniform_buffer_objs:
HashMap<RendererObjectId, HashMap<u32, opengl_bindings::buffer::Buffer<u8>>>,
@@ -328,18 +327,18 @@ fn init_window_graphics(
}) {
Ok(gl_context) => gl_context,
Err(err) => {
- tracing::error!("Failed to create GL context: {err:#?}");
+ tracing::error!("Failed to create GL context: {err}");
continue;
}
};
- let Ok(curr_gl_context) = gl_context.make_current(&window_surface) else {
- tracing::error!("Failed to make GL context current");
+ if let Err(err) = gl_context.make_current(&window_surface) {
+ tracing::error!("Failed to make GL context current: {err}");
continue;
};
if let Err(err) = gl_set_viewport(
- &curr_gl_context,
+ &gl_context,
&Vec2 { x: 0, y: 0 }.into(),
&window.inner_size().clone().into(),
) {
@@ -347,25 +346,25 @@ fn init_window_graphics(
}
set_enabled(
- &curr_gl_context,
+ &gl_context,
Capability::DepthTest,
graphics_props.depth_test,
);
set_enabled(
- &curr_gl_context,
+ &gl_context,
Capability::MultiSample,
graphics_props.multisampling_sample_cnt.is_some(),
);
if graphics_props.debug {
- enable(&curr_gl_context, Capability::DebugOutput);
- enable(&curr_gl_context, Capability::DebugOutputSynchronous);
+ enable(&gl_context, Capability::DebugOutput);
+ enable(&gl_context, Capability::DebugOutputSynchronous);
- set_debug_message_callback(&curr_gl_context, opengl_debug_message_cb);
+ set_debug_message_callback(&gl_context, opengl_debug_message_cb);
match set_debug_message_control(
- &curr_gl_context,
+ &gl_context,
None,
None,
None,
@@ -417,8 +416,6 @@ fn handle_commands(
return;
};
- let mut opt_curr_gl_ctx: Option<CurrentContextWithFns> = None;
-
let mut activated_gl_shader_program: Option<(RendererObjectId, GlShaderProgram)> =
None;
@@ -437,8 +434,6 @@ fn handle_commands(
};
if surface.window_surface.is_current(gl_context.context()) {
- opt_curr_gl_ctx = None;
-
if let Err(err) = gl_context.context().make_not_current_in_place() {
tracing::error!("Failed to make GL context not current: {err}");
}
@@ -452,23 +447,18 @@ fn handle_commands(
continue;
};
- let curr_gl_ctx = match gl_context.make_current(&surface.window_surface) {
- Ok(current_graphics_context) => current_graphics_context,
- Err(err) => {
- tracing::error!("Failed to make graphics context current: {err}");
- continue;
- }
- };
+ if let Err(err) = gl_context.make_current(&surface.window_surface) {
+ tracing::error!("Failed to make graphics context current: {err}");
+ continue;
+ }
if let Err(err) = gl_set_viewport(
- &curr_gl_ctx,
+ gl_context,
&Vec2 { x: 0, y: 0 }.into(),
&surface.size.into(),
) {
tracing::error!("Failed to set viewport: {err}");
}
-
- opt_curr_gl_ctx = Some(curr_gl_ctx);
}
RendererCommand::SetSurfaceSize(surface_id, new_surface_size) => {
let Some(surface) = surfaces.get_mut(&surface_id) else {
@@ -478,16 +468,12 @@ fn handle_commands(
surface.size = new_surface_size;
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- continue;
- };
-
if !surface.window_surface.is_current(gl_context.context()) {
continue;
}
if let Err(err) = gl_set_viewport(
- &curr_gl_ctx,
+ gl_context,
&Vec2 { x: 0, y: 0 }.into(),
&surface.size.into(),
) {
@@ -495,11 +481,6 @@ fn handle_commands(
}
}
RendererCommand::ClearBuffers(buffer_clear_mask) => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
let mut clear_mask = GlBufferClearMask::empty();
clear_mask.set(
@@ -517,7 +498,7 @@ fn handle_commands(
buffer_clear_mask.contains(BufferClearMask::STENCIL),
);
- clear_buffers(&curr_gl_ctx, clear_mask);
+ clear_buffers(gl_context, clear_mask);
}
RendererCommand::SwapBuffers(surface_id) => {
let Some(surface) = surfaces.get(&surface_id) else {
@@ -535,11 +516,6 @@ fn handle_commands(
shader_program_obj_id,
shader_program,
) => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
if object_store.contains_non_pending_with_id(&shader_program_obj_id) {
tracing::error!(
shader_program_object_id=?shader_program_obj_id,
@@ -552,7 +528,7 @@ fn handle_commands(
}
let gl_shader_program =
- match create_shader_program(&curr_gl_ctx, &shader_program) {
+ match create_shader_program(gl_context, &shader_program) {
Ok(gl_shader_program) => gl_shader_program,
Err(err) => {
tracing::error!("Failed to create shader program: {err}");
@@ -569,11 +545,6 @@ fn handle_commands(
);
}
RendererCommand::ActivateShader(shader_program_obj_id) => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
let Some(shader_program_obj) =
object_store.get_shader_program_obj(&shader_program_obj_id)
else {
@@ -584,17 +555,12 @@ fn handle_commands(
let gl_shader_program =
GlShaderProgram::from_raw(shader_program_obj.as_raw());
- gl_shader_program.activate(&curr_gl_ctx);
+ gl_shader_program.activate(gl_context);
activated_gl_shader_program =
Some((shader_program_obj_id, gl_shader_program));
}
RendererCommand::SetShaderBinding(binding_location, binding_value) => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
let Some((activated_gl_shader_program_obj_id, _)) =
&activated_gl_shader_program
else {
@@ -615,10 +581,8 @@ fn handle_commands(
let gl_texture = GlTexture::from_raw(texture_obj.as_raw());
- gl_texture.bind_to_texture_unit(
- curr_gl_ctx,
- binding_location.binding_index,
- );
+ gl_texture
+ .bind_to_texture_unit(gl_context, binding_location.binding_index);
// gl_shader_program.set_uniform_at_location(
// curr_gl_ctx,
@@ -640,11 +604,11 @@ fn handle_commands(
let uniform_buffer =
uniform_buffer_objs.entry(binding_index).or_insert_with(|| {
let uniform_buf =
- opengl_bindings::buffer::Buffer::<u8>::new(curr_gl_ctx);
+ opengl_bindings::buffer::Buffer::<u8>::new(gl_context);
uniform_buf
.init(
- curr_gl_ctx,
+ gl_context,
binding_location.binding_size,
opengl_bindings::buffer::Usage::Dynamic,
)
@@ -658,7 +622,7 @@ fn handle_commands(
// to be re-bound so that a uniform buffer from another shader isn't
// used
uniform_buffer.bind_to_indexed_target(
- curr_gl_ctx,
+ gl_context,
opengl_bindings::buffer::BindingTarget::UniformBuffer,
binding_index as u32,
);
@@ -667,7 +631,7 @@ fn handle_commands(
uniform_buffer
.store_at_byte_offset(
- curr_gl_ctx,
+ gl_context,
binding_location.byte_offset,
match binding_value {
ShaderBindingValue::Uint(ref value) => value.as_bytes(),
@@ -687,13 +651,8 @@ fn handle_commands(
.unwrap();
}
RendererCommand::CreateTexture(texture_asset) => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
if let Err(err) = create_texture_object(
- curr_gl_ctx,
+ gl_context,
&mut object_store,
&assets,
&texture_asset,
@@ -706,11 +665,6 @@ fn handle_commands(
mesh,
usage: mesh_usage,
} => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
if object_store.contains_non_pending_with_id(&mesh_object_id) {
tracing::error!(
mesh_object_id=?mesh_object_id,
@@ -773,7 +727,7 @@ fn handle_commands(
};
let graphics_mesh = match GraphicsMesh::new(
- &curr_gl_ctx,
+ gl_context,
&mesh,
mesh_usage,
&vertex_desc,
@@ -810,11 +764,6 @@ fn handle_commands(
mesh,
usage: mesh_usage,
} => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
let Some(mesh_graphics_ctx_obj_key) = object_store
.get_obj(&mesh_object_id)
.map(|obj| obj.as_raw())
@@ -851,16 +800,11 @@ fn handle_commands(
continue;
};
- if let Err(err) = graphics_mesh.update(curr_gl_ctx, &mesh, mesh_usage) {
+ if let Err(err) = graphics_mesh.update(gl_context, &mesh, mesh_usage) {
tracing::error!("Failed to update mesh: {err}");
}
}
RendererCommand::RemoveMesh(mesh_object_id) => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
let Some(mesh_graphics_ctx_obj_key) = object_store
.remove(&mesh_object_id)
.flatten()
@@ -896,14 +840,9 @@ fn handle_commands(
continue;
};
- graphics_mesh.destroy(curr_gl_ctx);
+ graphics_mesh.destroy(gl_context);
}
RendererCommand::DrawMesh(mesh_object_id, draw_mesh_opts) => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
let Some(mesh_graphics_ctx_obj_key) = object_store
.get_obj(&mesh_object_id)
.map(|obj| obj.as_raw())
@@ -954,8 +893,7 @@ fn handle_commands(
continue;
}
- if let Err(err) = draw_mesh(&curr_gl_ctx, graphics_mesh, &draw_mesh_opts)
- {
+ if let Err(err) = draw_mesh(gl_context, graphics_mesh, &draw_mesh_opts) {
tracing::error!("Failed to draw mesh: {err}");
};
}
@@ -963,16 +901,11 @@ fn handle_commands(
draw_props,
draw_props_update_flags,
) => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
if draw_props_update_flags
.contains(DrawPropertiesUpdateFlags::POLYGON_MODE_CONFIG)
{
opengl_bindings::misc::set_polygon_mode(
- &curr_gl_ctx,
+ gl_context,
draw_props.polygon_mode_config.face,
draw_props.polygon_mode_config.mode,
);
@@ -982,7 +915,7 @@ fn handle_commands(
.contains(DrawPropertiesUpdateFlags::BLENDING_ENABLED)
{
set_enabled(
- curr_gl_ctx,
+ gl_context,
Capability::Blend,
draw_props.blending_enabled,
);
@@ -992,7 +925,7 @@ fn handle_commands(
.contains(DrawPropertiesUpdateFlags::BLENDING_CONFIG)
{
gl_blending_configure(
- curr_gl_ctx,
+ gl_context,
GlBlendingConfig::default()
.with_source_factor(blending_factor_to_gl(
draw_props.blending_config.source_factor,
@@ -1010,7 +943,7 @@ fn handle_commands(
.contains(DrawPropertiesUpdateFlags::DEPTH_TEST_ENABLED)
{
set_enabled(
- curr_gl_ctx,
+ gl_context,
Capability::DepthTest,
draw_props.depth_test_enabled,
);
@@ -1020,7 +953,7 @@ fn handle_commands(
.contains(DrawPropertiesUpdateFlags::SCISSOR_TEST_ENABLED)
{
set_enabled(
- curr_gl_ctx,
+ gl_context,
Capability::ScissorTest,
draw_props.scissor_test_enabled,
);
@@ -1030,13 +963,13 @@ fn handle_commands(
.contains(DrawPropertiesUpdateFlags::SCISSOR_BOX)
{
gl_define_scissor_box(
- curr_gl_ctx,
+ gl_context,
draw_props.scissor_box.lower_left_corner_pos.into(),
draw_props
.scissor_box
.size
.unwrap_or_else(|| {
- let (_, viewport_size) = gl_get_viewport(curr_gl_ctx);
+ let (_, viewport_size) = gl_get_viewport(gl_context);
Dimens::<u16> {
width: viewport_size
@@ -1057,7 +990,7 @@ fn handle_commands(
.contains(DrawPropertiesUpdateFlags::FACE_CULLING_ENABLED)
{
set_enabled(
- curr_gl_ctx,
+ gl_context,
Capability::CullFace,
draw_props.face_culling_enabled,
);
@@ -1072,7 +1005,7 @@ fn create_gl_context(
graphics_props: &GraphicsProperties,
window_handle: WindowHandle<'_>,
surface: &GlutinSurface<GlutinWindowSurface>,
-) -> Result<ContextWithFns, CreateGlContextError>
+) -> Result<MaybeCurrentContextWithFns, CreateGlContextError>
{
let display = gl_config.display();
@@ -1086,7 +1019,7 @@ fn create_gl_context(
}
.map_err(CreateGlContextError::CreateGlutinContext)?;
- ContextWithFns::new(glutin_context, &surface)
+ MaybeCurrentContextWithFns::new(glutin_context, &surface)
.map_err(CreateGlContextError::MakeContextCurrent)
}
@@ -1102,7 +1035,7 @@ enum CreateGlContextError
#[tracing::instrument(skip_all)]
fn create_texture_object(
- curr_gl_ctx: &CurrentContextWithFns<'_>,
+ curr_gl_ctx: &MaybeCurrentContextWithFns,
renderer_object_store: &mut RendererObjectStore,
assets: &Assets,
texture_asset: &AssetHandle<Texture>,
@@ -1164,7 +1097,7 @@ fn create_texture_object(
}
fn draw_mesh(
- current_context: &CurrentContextWithFns<'_>,
+ current_context: &MaybeCurrentContextWithFns,
graphics_mesh: &GraphicsMesh,
opts: &DrawMeshOptions,
) -> Result<(), GlDrawError>
@@ -1194,7 +1127,7 @@ fn draw_mesh(
}
fn create_gl_texture(
- current_context: &CurrentContextWithFns<'_>,
+ current_context: &MaybeCurrentContextWithFns,
image: &Image,
texture_properties: &TextureProperties,
) -> Result<GlTexture, GlTextureGenerateError>
@@ -1238,7 +1171,7 @@ fn create_gl_texture(
}
fn create_shader_program(
- current_context: &CurrentContextWithFns<'_>,
+ current_context: &MaybeCurrentContextWithFns,
shader_program: &ShaderProgram,
) -> Result<GlShaderProgram, CreateShaderError>
{