Route canvas layer GL state through backend

This commit is contained in:
2026-06-04 22:28:49 +02:00
parent d55f26d637
commit 5aa07b2953
7 changed files with 186 additions and 16 deletions

View File

@@ -1,12 +1,154 @@
#include "pch.h"
#include "canvas_layer.h"
#include "app.h"
#include "log.h"
#include "renderer_gl/opengl_capabilities.h"
#include "rtt.h"
#include "util.h"
uint32_t Layer::s_count = 0;
namespace {
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_active_texture(std::uint32_t texture_unit) noexcept
{
glActiveTexture(static_cast<GLenum>(texture_unit));
}
void set_opengl_clear_color(float r, float g, float b, float a) noexcept
{
glClearColor(r, g, b, a);
}
void clear_opengl_buffer(std::uint32_t mask) noexcept
{
glClear(static_cast<GLbitfield>(mask));
}
void copy_opengl_tex_sub_image_2d(
std::uint32_t target,
std::int32_t level,
std::int32_t destination_x,
std::int32_t destination_y,
std::int32_t source_x,
std::int32_t source_y,
std::int32_t width,
std::int32_t height) noexcept
{
glCopyTexSubImage2D(
static_cast<GLenum>(target),
static_cast<GLint>(level),
static_cast<GLint>(destination_x),
static_cast<GLint>(destination_y),
static_cast<GLint>(source_x),
static_cast<GLint>(source_y),
static_cast<GLsizei>(width),
static_cast<GLsizei>(height));
}
void apply_layer_viewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height)
{
const auto status = pp::renderer::gl::apply_opengl_viewport(
pp::renderer::gl::OpenGlViewportRect {
.x = x,
.y = y,
.width = width,
.height = height,
},
pp::renderer::gl::OpenGlViewportDispatch {
.viewport = set_opengl_viewport,
});
if (!status.ok())
LOG("Layer viewport dispatch failed because: %s", status.message);
}
void apply_layer_capability(std::uint32_t state, bool enabled)
{
const auto status = pp::renderer::gl::apply_opengl_capability(
state,
enabled,
pp::renderer::gl::OpenGlCapabilityDispatch {
.enable = enable_opengl_state,
.disable = disable_opengl_state,
});
if (!status.ok())
LOG("Layer capability dispatch failed because: %s", status.message);
}
void set_layer_active_texture_unit(std::uint32_t unit_index)
{
const auto status = pp::renderer::gl::activate_opengl_texture_unit(
unit_index,
pp::renderer::gl::OpenGlActiveTextureDispatch {
.active_texture = set_opengl_active_texture,
});
if (!status.ok())
LOG("Layer active texture dispatch failed because: %s", status.message);
}
void copy_layer_framebuffer_to_texture(
std::uint32_t texture_target,
std::int32_t destination_x,
std::int32_t destination_y,
std::int32_t source_x,
std::int32_t source_y,
std::int32_t width,
std::int32_t height)
{
const auto status = pp::renderer::gl::copy_opengl_framebuffer_to_texture_2d(
pp::renderer::gl::OpenGlTexture2DFramebufferCopy {
.texture_target = texture_target,
.destination_x = destination_x,
.destination_y = destination_y,
.source_x = source_x,
.source_y = source_y,
.width = width,
.height = height,
},
pp::renderer::gl::OpenGlTexture2DFramebufferCopyDispatch {
.copy_tex_sub_image_2d = copy_opengl_tex_sub_image_2d,
});
if (!status.ok())
LOG("Layer framebuffer-to-texture copy dispatch failed because: %s", status.message);
}
void clear_layer_color_buffer(const glm::vec4& color)
{
const auto status = pp::renderer::gl::clear_opengl_render_target(
pp::renderer::gl::OpenGlDefaultClear {
.color = { color.r, color.g, color.b, color.a },
.mask = pp::renderer::gl::framebuffer_color_buffer_mask(),
},
pp::renderer::gl::OpenGlClearDispatch {
.clear_color = set_opengl_clear_color,
.clear = clear_opengl_buffer,
});
if (!status.ok())
LOG("Layer clear dispatch failed because: %s", status.message);
}
void restore_layer_clear_color(const GLfloat* color)
{
set_opengl_clear_color(color[0], color[1], color[2], color[3]);
}
}
RTT& Layer::rtt(int i, int frame /*= -1*/)
{
if (frame == -1)
@@ -46,7 +188,7 @@ TextureCube Layer::gen_cube()
{
ret.bind();
rtt(i).bindFramebuffer();
glCopyTexSubImage2D(pp::renderer::gl::cube_face_texture_target(i), 0, 0, 0, 0, 0, w, w);
copy_layer_framebuffer_to_texture(pp::renderer::gl::cube_face_texture_target(i), 0, 0, 0, 0, w, w);
rtt(i).unbindFramebuffer();
});
}
@@ -72,15 +214,15 @@ Texture2D Layer::gen_equirect(glm::ivec2 size /*= { 0, 0 }*/)
latlong.create(size.x * 4, size.y * 2);
ret.create(size.x * 4, size.y * 2);
glDisable(pp::renderer::gl::blend_state());
apply_layer_capability(pp::renderer::gl::blend_state(), false);
latlong.bindFramebuffer();
//latlong.clear({ 0, 1, 1, 1 });
glViewport(0, 0, latlong.getWidth(), latlong.getHeight());
apply_layer_viewport(0, 0, latlong.getWidth(), latlong.getHeight());
glActiveTexture(pp::renderer::gl::active_texture_unit(0U));
set_layer_active_texture_unit(0U);
glBindTexture(pp::renderer::gl::texture_cube_map_target(), cube.m_cubetex_id);
ShaderManager::use(kShader::Equirect);
@@ -117,12 +259,12 @@ PBO Layer::gen_equirect_pbo(glm::ivec2 size /*= { 0, 0 }*/)
App::I->render_task([&]
{
glDisable(pp::renderer::gl::blend_state());
apply_layer_capability(pp::renderer::gl::blend_state(), false);
latlong.bindFramebuffer();
glViewport(0, 0, latlong.getWidth(), latlong.getHeight());
glActiveTexture(pp::renderer::gl::active_texture_unit(0U));
apply_layer_viewport(0, 0, latlong.getWidth(), latlong.getHeight());
set_layer_active_texture_unit(0U);
glBindTexture(pp::renderer::gl::texture_cube_map_target(), cube.m_cubetex_id);
ShaderManager::use(kShader::Equirect);
@@ -461,14 +603,13 @@ void LayerFrame::clear(const glm::vec4& c)
// push clear color state
GLfloat cc[4];
glGetFloatv(pp::renderer::gl::color_clear_value_query(), cc);
glClearColor(c.r, c.g, c.b, c.a);
bool erase = (c.a == 0.f);
for (int i = 0; i < 6; i++)
{
m_rtt[i].bindFramebuffer();
glClear(pp::renderer::gl::framebuffer_color_buffer_mask());
clear_layer_color_buffer(c);
m_rtt[i].unbindFramebuffer();
if (erase)
@@ -484,7 +625,7 @@ void LayerFrame::clear(const glm::vec4& c)
}
// restore clear color state
glClearColor(cc[0], cc[1], cc[2], cc[3]);
restore_layer_clear_color(cc);
});
}

View File

@@ -844,8 +844,9 @@ pp::foundation::Status copy_opengl_framebuffer_to_texture_2d(
return pp::foundation::Status::success();
}
const auto texture_target = copy.texture_target == 0U ? texture_2d_target() : copy.texture_target;
dispatch.copy_tex_sub_image_2d(
texture_2d_target(),
texture_target,
copy.level,
copy.destination_x,
copy.destination_y,

View File

@@ -150,6 +150,7 @@ struct OpenGlTexture2DUpdate {
};
struct OpenGlTexture2DFramebufferCopy {
std::uint32_t texture_target = 0;
std::int32_t level = 0;
std::int32_t destination_x = 0;
std::int32_t destination_y = 0;