Route PBO readbacks through GL backend
This commit is contained in:
137
src/rtt.cpp
137
src/rtt.cpp
@@ -78,6 +78,52 @@ void read_opengl_pixels(
|
||||
pixels);
|
||||
}
|
||||
|
||||
void gen_opengl_buffers(std::uint32_t count, std::uint32_t* ids) noexcept
|
||||
{
|
||||
glGenBuffers(static_cast<GLsizei>(count), reinterpret_cast<GLuint*>(ids));
|
||||
}
|
||||
|
||||
void delete_opengl_buffers(std::uint32_t count, const std::uint32_t* ids) noexcept
|
||||
{
|
||||
glDeleteBuffers(static_cast<GLsizei>(count), reinterpret_cast<const GLuint*>(ids));
|
||||
}
|
||||
|
||||
void bind_opengl_buffer(std::uint32_t target, std::uint32_t buffer) noexcept
|
||||
{
|
||||
glBindBuffer(static_cast<GLenum>(target), static_cast<GLuint>(buffer));
|
||||
}
|
||||
|
||||
void set_opengl_buffer_data(
|
||||
std::uint32_t target,
|
||||
std::intptr_t byte_count,
|
||||
const void* data,
|
||||
std::uint32_t usage) noexcept
|
||||
{
|
||||
glBufferData(
|
||||
static_cast<GLenum>(target),
|
||||
static_cast<GLsizeiptr>(byte_count),
|
||||
data,
|
||||
static_cast<GLenum>(usage));
|
||||
}
|
||||
|
||||
void* map_opengl_buffer_range(
|
||||
std::uint32_t target,
|
||||
std::intptr_t offset,
|
||||
std::intptr_t byte_count,
|
||||
std::uint32_t access) noexcept
|
||||
{
|
||||
return glMapBufferRange(
|
||||
static_cast<GLenum>(target),
|
||||
static_cast<GLintptr>(offset),
|
||||
static_cast<GLsizeiptr>(byte_count),
|
||||
static_cast<GLbitfield>(access));
|
||||
}
|
||||
|
||||
void unmap_opengl_buffer(std::uint32_t target) noexcept
|
||||
{
|
||||
glUnmapBuffer(static_cast<GLenum>(target));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RTT& RTT::operator=(RTT&& other)
|
||||
@@ -673,7 +719,16 @@ PBO::~PBO() noexcept
|
||||
bool PBO::create() noexcept
|
||||
{
|
||||
App::I->render_task([this] {
|
||||
glGenBuffers(1, &buffer_id);
|
||||
const auto result = pp::renderer::gl::allocate_opengl_pixel_buffer(
|
||||
pp::renderer::gl::OpenGlPixelBufferAllocationDispatch {
|
||||
.gen_buffers = gen_opengl_buffers,
|
||||
});
|
||||
if (!result.ok()) {
|
||||
LOG("%s", result.status().message);
|
||||
buffer_id = 0U;
|
||||
return;
|
||||
}
|
||||
buffer_id = result.value();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@@ -683,29 +738,24 @@ bool PBO::create(RTT& rtt) noexcept
|
||||
App::I->render_task([this, &rtt] {
|
||||
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(buffer_target, buffer_id);
|
||||
glBufferData(
|
||||
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,
|
||||
static_cast<GLenum>(pp::renderer::gl::pixel_buffer_stream_read_usage()));
|
||||
glReadPixels(
|
||||
0,
|
||||
0,
|
||||
const auto result = pp::renderer::gl::readback_opengl_framebuffer_to_pixel_buffer(
|
||||
width,
|
||||
height,
|
||||
static_cast<GLenum>(readback.pixel_format),
|
||||
static_cast<GLenum>(readback.component_type),
|
||||
0);
|
||||
glBindBuffer(buffer_target, 0);
|
||||
pp::renderer::gl::rgba8_readback_format(),
|
||||
pp::renderer::gl::OpenGlPixelBufferReadbackDispatch {
|
||||
.gen_buffers = gen_opengl_buffers,
|
||||
.bind_buffer = bind_opengl_buffer,
|
||||
.buffer_data = set_opengl_buffer_data,
|
||||
.read_pixels = read_opengl_pixels,
|
||||
});
|
||||
rtt.unbindFramebuffer();
|
||||
if (!result.ok()) {
|
||||
LOG("%s", result.status().message);
|
||||
buffer_id = 0U;
|
||||
return;
|
||||
}
|
||||
buffer_id = result.value();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@@ -715,7 +765,14 @@ void PBO::destroy() noexcept
|
||||
if (buffer_id)
|
||||
{
|
||||
App::I->render_task_async([id=buffer_id] {
|
||||
glDeleteBuffers(1, &id);
|
||||
const auto status = pp::renderer::gl::delete_opengl_pixel_buffer(
|
||||
id,
|
||||
pp::renderer::gl::OpenGlPixelBufferDeleteDispatch {
|
||||
.delete_buffers = delete_opengl_buffers,
|
||||
});
|
||||
if (!status.ok()) {
|
||||
LOG("%s", status.message);
|
||||
}
|
||||
});
|
||||
buffer_id = 0;
|
||||
bound_slot = 0;
|
||||
@@ -746,16 +803,21 @@ void PBO::unbind() noexcept
|
||||
glm::uint8_t* PBO::map() noexcept
|
||||
{
|
||||
App::I->render_task([this] {
|
||||
const auto readback = pp::renderer::gl::rgba8_readback_format();
|
||||
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))),
|
||||
static_cast<GLbitfield>(pp::renderer::gl::pixel_buffer_map_read_access()));
|
||||
glBindBuffer(buffer_target, 0);
|
||||
const auto result = pp::renderer::gl::map_opengl_pixel_buffer(
|
||||
buffer_id,
|
||||
width,
|
||||
height,
|
||||
pp::renderer::gl::rgba8_readback_format(),
|
||||
pp::renderer::gl::OpenGlPixelBufferMapDispatch {
|
||||
.bind_buffer = bind_opengl_buffer,
|
||||
.map_buffer_range = map_opengl_buffer_range,
|
||||
});
|
||||
if (!result.ok()) {
|
||||
LOG("%s", result.status().message);
|
||||
mapped_ptr = nullptr;
|
||||
return;
|
||||
}
|
||||
mapped_ptr = static_cast<glm::uint8_t*>(result.value());
|
||||
});
|
||||
return mapped_ptr;
|
||||
}
|
||||
@@ -763,9 +825,14 @@ glm::uint8_t* PBO::map() noexcept
|
||||
void PBO::unmap() noexcept
|
||||
{
|
||||
App::I->render_task([this] {
|
||||
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);
|
||||
const auto status = pp::renderer::gl::unmap_opengl_pixel_buffer(
|
||||
buffer_id,
|
||||
pp::renderer::gl::OpenGlPixelBufferUnmapDispatch {
|
||||
.bind_buffer = bind_opengl_buffer,
|
||||
.unmap_buffer = unmap_opengl_buffer,
|
||||
});
|
||||
if (!status.ok()) {
|
||||
LOG("%s", status.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user