Move pixel buffer mapping to renderer gl

This commit is contained in:
2026-06-02 06:31:15 +02:00
parent 75dfc85978
commit 43e3a74c42
6 changed files with 66 additions and 23 deletions

View File

@@ -48,6 +48,10 @@ constexpr std::uint32_t gl_texture_wrap_t = 0x2803U;
constexpr std::uint32_t gl_texture_wrap_r = 0x8072U;
constexpr std::uint32_t gl_texture_border_color = 0x1004U;
constexpr std::uint32_t gl_clamp_to_edge = 0x812FU;
constexpr std::uint32_t gl_pixel_pack_buffer = 0x88EBU;
constexpr std::uint32_t gl_pixel_unpack_buffer = 0x88ECU;
constexpr std::uint32_t gl_stream_read = 0x88E1U;
constexpr std::uint32_t gl_map_read_bit = 0x0001U;
[[nodiscard]] bool contains(std::string_view text, std::string_view needle) noexcept
{
@@ -149,6 +153,26 @@ std::uint64_t readback_byte_count(
return static_cast<std::uint64_t>(width) * height * format.bytes_per_pixel;
}
std::uint32_t pixel_pack_buffer_target() noexcept
{
return gl_pixel_pack_buffer;
}
std::uint32_t pixel_unpack_buffer_target() noexcept
{
return gl_pixel_unpack_buffer;
}
std::uint32_t pixel_buffer_stream_read_usage() noexcept
{
return gl_stream_read;
}
std::uint32_t pixel_buffer_map_read_access() noexcept
{
return gl_map_read_bit;
}
const char* framebuffer_status_name(std::uint32_t status) noexcept
{
switch (status) {

View File

@@ -50,6 +50,10 @@ struct OpenGlReadbackFormat {
OpenGlReadbackFormat format,
std::uint32_t width,
std::uint32_t height) noexcept;
[[nodiscard]] std::uint32_t pixel_pack_buffer_target() noexcept;
[[nodiscard]] std::uint32_t pixel_unpack_buffer_target() noexcept;
[[nodiscard]] std::uint32_t pixel_buffer_stream_read_usage() noexcept;
[[nodiscard]] std::uint32_t pixel_buffer_map_read_access() noexcept;
[[nodiscard]] const char* framebuffer_status_name(std::uint32_t status) noexcept;
[[nodiscard]] std::uint32_t framebuffer_color_buffer_mask() noexcept;
[[nodiscard]] std::uint32_t framebuffer_blit_filter(bool linear) noexcept;

View File

@@ -493,17 +493,18 @@ bool PBO::create(RTT& rtt) noexcept
width = rtt.getWidth();
height = rtt.getHeight();
const auto readback = pp::renderer::gl::rgba8_readback_format();
const auto buffer_target = static_cast<GLenum>(pp::renderer::gl::pixel_pack_buffer_target());
rtt.bindFramebuffer();
glGenBuffers(1, &buffer_id);
glBindBuffer(GL_PIXEL_PACK_BUFFER, buffer_id);
glBindBuffer(buffer_target, buffer_id);
glBufferData(
GL_PIXEL_PACK_BUFFER,
buffer_target,
static_cast<GLsizeiptr>(pp::renderer::gl::readback_byte_count(
readback,
static_cast<std::uint32_t>(width),
static_cast<std::uint32_t>(height))),
0,
GL_STREAM_READ);
static_cast<GLenum>(pp::renderer::gl::pixel_buffer_stream_read_usage()));
glReadPixels(
0,
0,
@@ -512,7 +513,7 @@ bool PBO::create(RTT& rtt) noexcept
static_cast<GLenum>(readback.pixel_format),
static_cast<GLenum>(readback.component_type),
0);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glBindBuffer(buffer_target, 0);
rtt.unbindFramebuffer();
});
return true;
@@ -537,8 +538,9 @@ void PBO::destroy() noexcept
void PBO::bind_read() noexcept
{
App::I->render_task([this] {
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer_id);
bound_slot = GL_PIXEL_UNPACK_BUFFER;
const auto buffer_target = static_cast<GLenum>(pp::renderer::gl::pixel_unpack_buffer_target());
glBindBuffer(buffer_target, buffer_id);
bound_slot = buffer_target;
});
}
@@ -554,14 +556,15 @@ glm::uint8_t* PBO::map() noexcept
{
App::I->render_task([this] {
const auto readback = pp::renderer::gl::rgba8_readback_format();
glBindBuffer(GL_PIXEL_PACK_BUFFER, buffer_id);
mapped_ptr = (GLubyte*)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0,
const auto buffer_target = static_cast<GLenum>(pp::renderer::gl::pixel_pack_buffer_target());
glBindBuffer(buffer_target, buffer_id);
mapped_ptr = (GLubyte*)glMapBufferRange(buffer_target, 0,
static_cast<GLsizeiptr>(pp::renderer::gl::readback_byte_count(
readback,
static_cast<std::uint32_t>(width),
static_cast<std::uint32_t>(height))),
GL_MAP_READ_BIT);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
static_cast<GLbitfield>(pp::renderer::gl::pixel_buffer_map_read_access()));
glBindBuffer(buffer_target, 0);
});
return mapped_ptr;
}
@@ -569,8 +572,9 @@ glm::uint8_t* PBO::map() noexcept
void PBO::unmap() noexcept
{
App::I->render_task([this] {
glBindBuffer(GL_PIXEL_PACK_BUFFER, buffer_id);
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
const auto buffer_target = static_cast<GLenum>(pp::renderer::gl::pixel_pack_buffer_target());
glBindBuffer(buffer_target, buffer_id);
glUnmapBuffer(buffer_target);
glBindBuffer(buffer_target, 0);
});
}