Route GL state snapshot through renderer GL

This commit is contained in:
2026-06-03 06:15:51 +02:00
parent 3e15b2f46c
commit 9971b2b7f2
6 changed files with 516 additions and 37 deletions

View File

@@ -5,6 +5,70 @@
#include "app.h"
#include "renderer_gl/opengl_capabilities.h"
namespace {
std::uint8_t is_opengl_enabled(std::uint32_t state) noexcept
{
return static_cast<std::uint8_t>(glIsEnabled(static_cast<GLenum>(state)));
}
void query_opengl_integer(std::uint32_t name, std::int32_t* value) noexcept
{
glGetIntegerv(static_cast<GLenum>(name), reinterpret_cast<GLint*>(value));
}
void query_opengl_float(std::uint32_t name, float* value) noexcept
{
glGetFloatv(static_cast<GLenum>(name), value);
}
void set_opengl_active_texture(std::uint32_t texture_unit) noexcept
{
glActiveTexture(static_cast<GLenum>(texture_unit));
}
void enable_opengl_state(std::uint32_t state) noexcept
{
glEnable(static_cast<GLenum>(state));
}
void disable_opengl_state(std::uint32_t state) noexcept
{
glDisable(static_cast<GLenum>(state));
}
void set_opengl_viewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height) noexcept
{
glViewport(static_cast<GLint>(x), static_cast<GLint>(y), static_cast<GLsizei>(width), static_cast<GLsizei>(height));
}
void set_opengl_clear_color(float r, float g, float b, float a) noexcept
{
glClearColor(r, g, b, a);
}
void bind_opengl_framebuffer(std::uint32_t target, std::uint32_t framebuffer) noexcept
{
glBindFramebuffer(static_cast<GLenum>(target), static_cast<GLuint>(framebuffer));
}
void use_opengl_program(std::uint32_t program) noexcept
{
glUseProgram(static_cast<GLuint>(program));
}
void bind_opengl_texture(std::uint32_t target, std::uint32_t texture) noexcept
{
glBindTexture(static_cast<GLenum>(target), static_cast<GLuint>(texture));
}
void bind_opengl_sampler(std::uint32_t unit, std::uint32_t sampler) noexcept
{
glBindSampler(static_cast<GLuint>(unit), static_cast<GLuint>(sampler));
}
}
template<>
std::vector<vertex_t> poly_remove_duplicate<vertex_t>(const std::vector<vertex_t>& v, const float tollerance)
{
@@ -727,41 +791,73 @@ void parallel_for(size_t nb_elements, std::function<void(size_t i)> functor, boo
void gl_state::save()
{
assert(App::I->is_render_thread());
blend = glIsEnabled(pp::renderer::gl::blend_state());
depth_test = glIsEnabled(pp::renderer::gl::depth_test_state());
scissor_test = glIsEnabled(pp::renderer::gl::scissor_test_state());
glGetIntegerv(pp::renderer::gl::viewport_query(), vp);
glGetFloatv(pp::renderer::gl::color_clear_value_query(), cc);
glGetIntegerv(pp::renderer::gl::current_program_query(), &program);
glGetIntegerv(pp::renderer::gl::draw_framebuffer_binding_query(), &fbd);
glGetIntegerv(pp::renderer::gl::read_framebuffer_binding_query(), &fbr);
glGetIntegerv(pp::renderer::gl::active_texture_query(), &active_tex);
glGetIntegerv(pp::renderer::gl::texture_binding_cube_map_query(), &cube);
for (int i = 0; i < 10; ++i)
{
glActiveTexture(pp::renderer::gl::active_texture_unit(static_cast<std::uint32_t>(i)));
glGetIntegerv(pp::renderer::gl::texture_binding_2d_query(), tex + i);
glGetIntegerv(pp::renderer::gl::sampler_binding_query(), sampler + i);
const auto snapshot = pp::renderer::gl::snapshot_opengl_state(
pp::renderer::gl::OpenGlStateSnapshotDispatch {
.is_enabled = is_opengl_enabled,
.get_integer = query_opengl_integer,
.get_float = query_opengl_float,
.active_texture = set_opengl_active_texture,
});
if (!snapshot.ok()) {
LOG("OpenGL state snapshot failed: %s", snapshot.status().message);
return;
}
const auto& state = snapshot.value();
blend = static_cast<GLboolean>(state.blend_enabled);
depth_test = static_cast<GLboolean>(state.depth_test_enabled);
scissor_test = static_cast<GLboolean>(state.scissor_test_enabled);
for (std::size_t i = 0; i < state.viewport.size(); ++i) {
vp[i] = static_cast<GLint>(state.viewport[i]);
cc[i] = state.clear_color[i];
}
for (std::size_t i = 0; i < state.texture_2d_bindings.size(); ++i) {
tex[i] = static_cast<GLint>(state.texture_2d_bindings[i]);
sampler[i] = static_cast<GLint>(state.sampler_bindings[i]);
}
cube = static_cast<GLint>(state.cube_map_binding);
program = static_cast<GLint>(state.program);
fbd = static_cast<GLint>(state.draw_framebuffer);
fbr = static_cast<GLint>(state.read_framebuffer);
active_tex = static_cast<GLint>(state.active_texture);
}
void gl_state::restore()
{
assert(App::I->is_render_thread());
blend ? glEnable(pp::renderer::gl::blend_state()) : glDisable(pp::renderer::gl::blend_state());
depth_test ? glEnable(pp::renderer::gl::depth_test_state()) : glDisable(pp::renderer::gl::depth_test_state());
scissor_test ? glEnable(pp::renderer::gl::scissor_test_state()) : glDisable(pp::renderer::gl::scissor_test_state());
glViewport(vp[0], vp[1], vp[2], vp[3]);
glClearColor(cc[0], cc[1], cc[2], cc[3]);
glBindFramebuffer(pp::renderer::gl::draw_framebuffer_target(), fbd);
glBindFramebuffer(pp::renderer::gl::read_framebuffer_target(), fbr);
glUseProgram(program);
for (int i = 0; i < 10; ++i)
{
glActiveTexture(pp::renderer::gl::active_texture_unit(static_cast<std::uint32_t>(i)));
glBindTexture(pp::renderer::gl::texture_2d_target(), tex[i]);
glBindSampler(i, sampler[i]);
pp::renderer::gl::OpenGlSavedState state {};
state.blend_enabled = blend;
state.depth_test_enabled = depth_test;
state.scissor_test_enabled = scissor_test;
for (std::size_t i = 0; i < state.viewport.size(); ++i) {
state.viewport[i] = static_cast<std::int32_t>(vp[i]);
state.clear_color[i] = cc[i];
}
glActiveTexture(active_tex);
glBindTexture(pp::renderer::gl::texture_cube_map_target(), cube);
for (std::size_t i = 0; i < state.texture_2d_bindings.size(); ++i) {
state.texture_2d_bindings[i] = static_cast<std::int32_t>(tex[i]);
state.sampler_bindings[i] = static_cast<std::int32_t>(sampler[i]);
}
state.cube_map_binding = static_cast<std::int32_t>(cube);
state.program = static_cast<std::int32_t>(program);
state.draw_framebuffer = static_cast<std::int32_t>(fbd);
state.read_framebuffer = static_cast<std::int32_t>(fbr);
state.active_texture = static_cast<std::int32_t>(active_tex);
const auto status = pp::renderer::gl::restore_opengl_state(
state,
pp::renderer::gl::OpenGlStateRestoreDispatch {
.enable = enable_opengl_state,
.disable = disable_opengl_state,
.viewport = set_opengl_viewport,
.clear_color = set_opengl_clear_color,
.bind_framebuffer = bind_opengl_framebuffer,
.use_program = use_opengl_program,
.active_texture = set_opengl_active_texture,
.bind_texture = bind_opengl_texture,
.bind_sampler = bind_opengl_sampler,
});
if (!status.ok())
LOG("OpenGL state restore failed: %s", status.message);
}