summaryrefslogtreecommitdiff
path: root/engine/src/renderer/opengl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/renderer/opengl.rs')
-rw-r--r--engine/src/renderer/opengl.rs82
1 files changed, 56 insertions, 26 deletions
diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs
index 4892893..378a89d 100644
--- a/engine/src/renderer/opengl.rs
+++ b/engine/src/renderer/opengl.rs
@@ -136,7 +136,7 @@ struct WindowGlConfig
struct GraphicsContext
{
gl_context: Option<ContextWithFns>,
- surfaces: HashMap<SurfaceId, GlutinSurface<GlutinWindowSurface>>,
+ surfaces: HashMap<SurfaceId, GraphicsContextSurface>,
shader_uniform_buffer_objs:
HashMap<RendererObjectId, HashMap<u32, opengl_bindings::buffer::Buffer<u8>>>,
objects: HashMap<RendererObjectRawValue, GraphicsContextObject>,
@@ -144,6 +144,13 @@ struct GraphicsContext
}
#[derive(Debug)]
+struct GraphicsContextSurface
+{
+ window_surface: GlutinSurface<GlutinWindowSurface>,
+ size: Dimens<u32>,
+}
+
+#[derive(Debug)]
enum GraphicsContextObject
{
Mesh
@@ -291,7 +298,7 @@ fn init_window_graphics(
continue;
};
- let surface = match unsafe {
+ let window_surface = match unsafe {
display.create_window_surface(
&window_gl_config.gl_config,
&glutin::surface::SurfaceAttributesBuilder::<
@@ -304,7 +311,7 @@ fn init_window_graphics(
),
)
} {
- Ok(surface) => surface,
+ Ok(window_surface) => window_surface,
Err(err) => {
tracing::error!("Failed to create window surface: {err}");
continue;
@@ -316,7 +323,7 @@ fn init_window_graphics(
&window_gl_config.gl_config,
&graphics_props,
window_handle,
- &surface,
+ &window_surface,
)
}) {
Ok(gl_context) => gl_context,
@@ -326,7 +333,7 @@ fn init_window_graphics(
}
};
- let Ok(curr_gl_context) = gl_context.make_current(&surface) else {
+ let Ok(curr_gl_context) = gl_context.make_current(&window_surface) else {
tracing::error!("Failed to make GL context current");
continue;
};
@@ -379,7 +386,13 @@ fn init_window_graphics(
actions.add_components(window_ent_id, (SurfaceSpec { id: surface_id },));
- graphics_ctx.surfaces.insert(surface_id, surface);
+ graphics_ctx.surfaces.insert(
+ surface_id,
+ GraphicsContextSurface {
+ window_surface,
+ size: window.inner_size().clone(),
+ },
+ );
}
}
@@ -423,7 +436,7 @@ fn handle_commands(
continue;
};
- if surface.is_current(gl_context.context()) {
+ 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() {
@@ -439,7 +452,7 @@ fn handle_commands(
continue;
};
- let curr_gl_ctx = match gl_context.make_current(surface) {
+ 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}");
@@ -447,8 +460,40 @@ fn handle_commands(
}
};
+ if let Err(err) = gl_set_viewport(
+ &curr_gl_ctx,
+ &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 {
+ tracing::error!(surface_id=?surface_id, "Surface does not exist");
+ continue;
+ };
+
+ 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,
+ &Vec2 { x: 0, y: 0 }.into(),
+ &surface.size.into(),
+ ) {
+ tracing::error!("Failed to set viewport: {err}");
+ }
+ }
RendererCommand::ClearBuffers(buffer_clear_mask) => {
let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
tracing::error!("No GL context is current");
@@ -480,7 +525,9 @@ fn handle_commands(
continue;
};
- if let Err(err) = surface.swap_buffers(gl_context.context()) {
+ if let Err(err) =
+ surface.window_surface.swap_buffers(gl_context.context())
+ {
tracing::error!("Failed to swap buffers: {err}");
}
}
@@ -1016,23 +1063,6 @@ fn handle_commands(
);
}
}
- RendererCommand::SetViewport {
- size: viewport_size,
- position: viewport_position,
- } => {
- let Some(curr_gl_ctx) = &opt_curr_gl_ctx else {
- tracing::error!("No GL context is current");
- continue;
- };
-
- if let Err(err) = gl_set_viewport(
- curr_gl_ctx,
- &viewport_position.into(),
- &viewport_size.into(),
- ) {
- tracing::error!("Failed to set viewport: {err}");
- }
- }
}
}
}