Route retained readbacks through GL backend

This commit is contained in:
2026-06-04 23:20:06 +02:00
parent 9190e9053a
commit 2a4698e9f6
5 changed files with 88 additions and 34 deletions

View File

@@ -36,6 +36,36 @@ void set_opengl_viewport(std::int32_t x, std::int32_t y, std::int32_t width, std
glViewport(static_cast<GLint>(x), static_cast<GLint>(y), static_cast<GLsizei>(width), static_cast<GLsizei>(height));
}
void get_opengl_integer(std::uint32_t name, std::int32_t* values) noexcept
{
static_assert(sizeof(GLint) == sizeof(std::int32_t));
glGetIntegerv(static_cast<GLenum>(name), reinterpret_cast<GLint*>(values));
}
void read_opengl_pixels(
std::int32_t x,
std::int32_t y,
std::int32_t width,
std::int32_t height,
std::uint32_t pixel_format,
std::uint32_t component_type,
void* pixels) noexcept
{
glReadPixels(
static_cast<GLint>(x),
static_cast<GLint>(y),
static_cast<GLsizei>(width),
static_cast<GLsizei>(height),
static_cast<GLenum>(pixel_format),
static_cast<GLenum>(component_type),
pixels);
}
void bind_opengl_framebuffer(std::uint32_t target, std::uint32_t framebuffer) noexcept
{
glBindFramebuffer(static_cast<GLenum>(target), static_cast<GLuint>(framebuffer));
}
void set_canvas_mode_active_texture_unit(std::uint32_t unit_index)
{
const auto status = pp::renderer::gl::activate_opengl_texture_unit(
@@ -76,6 +106,34 @@ void apply_canvas_mode_viewport(std::int32_t x, std::int32_t y, std::int32_t wid
LOG("Canvas mode viewport dispatch failed because: %s", status.message);
}
std::uint32_t query_canvas_mode_read_framebuffer()
{
std::int32_t read_framebuffer = 0;
get_opengl_integer(pp::renderer::gl::read_framebuffer_binding_query(), &read_framebuffer);
return static_cast<std::uint32_t>(read_framebuffer);
}
void read_canvas_mode_pixel(std::int32_t x, std::int32_t y, glm::u8vec4& pixel)
{
const auto status = pp::renderer::gl::readback_opengl_framebuffer(
pp::renderer::gl::OpenGlFramebufferReadback {
.framebuffer = query_canvas_mode_read_framebuffer(),
.x = x,
.y = y,
.width = 1,
.height = 1,
.format = pp::renderer::gl::rgba8_readback_format(),
.pixels = &pixel,
},
pp::renderer::gl::OpenGlFramebufferReadbackDispatch {
.get_integer = get_opengl_integer,
.bind_framebuffer = bind_opengl_framebuffer,
.read_pixels = read_opengl_pixels,
});
if (!status.ok())
LOG("Canvas mode pixel readback dispatch failed because: %s", status.message);
}
}
void CanvasModeBasicCamera::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
@@ -343,19 +401,17 @@ void CanvasModePen::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const
}
}
glm::u8vec4 pixel;
GLint fb_width = App::I->width;
GLint fb_height = App::I->height;
std::int32_t fb_width = static_cast<std::int32_t>(App::I->width);
std::int32_t fb_height = static_cast<std::int32_t>(App::I->height);
if (node->m_density != 1.f)
{
fb_width = node->m_rtt.getWidth();
fb_height = node->m_rtt.getHeight();
}
glReadPixels((pos.x / App::I->width) * fb_width,
((App::I->height - pos.y - 1) / App::I->height) * fb_height,
1, 1,
pp::renderer::gl::rgba_pixel_format(),
pp::renderer::gl::unsigned_byte_component_type(),
&pixel);
read_canvas_mode_pixel(
static_cast<std::int32_t>((pos.x / App::I->width) * fb_width),
static_cast<std::int32_t>(((App::I->height - pos.y - 1) / App::I->height) * fb_height),
pixel);
bool outline = glm::min(tip_scale.x, tip_scale.y) < 20 || m_resizing ? false : m_draw_outline;
ShaderManager::u_int(kShaderUniform::DrawOutline, outline);
ShaderManager::u_vec4(kShaderUniform::Col, outline ? glm::vec4(1.f - glm::vec3(pixel) / 255.f, 1.f) : tip_color);