Route PBO readbacks through GL backend
This commit is contained in:
@@ -969,6 +969,147 @@ pp::foundation::Status restore_opengl_framebuffer_binding(
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::uint32_t> allocate_opengl_pixel_buffer(
|
||||
OpenGlPixelBufferAllocationDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.gen_buffers == nullptr) {
|
||||
return pp::foundation::Result<std::uint32_t>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL pixel-buffer allocation dispatch callback must not be null"));
|
||||
}
|
||||
|
||||
std::uint32_t buffer_id = 0U;
|
||||
dispatch.gen_buffers(1U, &buffer_id);
|
||||
if (buffer_id == 0U) {
|
||||
return pp::foundation::Result<std::uint32_t>::failure(
|
||||
pp::foundation::Status::out_of_range("OpenGL pixel-buffer allocation returned id 0"));
|
||||
}
|
||||
|
||||
return pp::foundation::Result<std::uint32_t>::success(buffer_id);
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::uint32_t> readback_opengl_framebuffer_to_pixel_buffer(
|
||||
std::int32_t width,
|
||||
std::int32_t height,
|
||||
OpenGlReadbackFormat format,
|
||||
OpenGlPixelBufferReadbackDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.gen_buffers == nullptr
|
||||
|| dispatch.bind_buffer == nullptr
|
||||
|| dispatch.buffer_data == nullptr
|
||||
|| dispatch.read_pixels == nullptr) {
|
||||
return pp::foundation::Result<std::uint32_t>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL pixel-buffer readback dispatch callbacks must not be null"));
|
||||
}
|
||||
|
||||
if (width <= 0
|
||||
|| height <= 0
|
||||
|| format.pixel_format == 0U
|
||||
|| format.component_type == 0U
|
||||
|| format.bytes_per_pixel == 0U) {
|
||||
return pp::foundation::Result<std::uint32_t>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL pixel-buffer readback parameters are invalid"));
|
||||
}
|
||||
|
||||
const auto buffer_id = allocate_opengl_pixel_buffer(OpenGlPixelBufferAllocationDispatch {
|
||||
.gen_buffers = dispatch.gen_buffers,
|
||||
});
|
||||
if (!buffer_id.ok()) {
|
||||
return buffer_id;
|
||||
}
|
||||
|
||||
const auto target = pixel_pack_buffer_target();
|
||||
dispatch.bind_buffer(target, buffer_id.value());
|
||||
dispatch.buffer_data(
|
||||
target,
|
||||
static_cast<std::intptr_t>(readback_byte_count(
|
||||
format,
|
||||
static_cast<std::uint32_t>(width),
|
||||
static_cast<std::uint32_t>(height))),
|
||||
nullptr,
|
||||
pixel_buffer_stream_read_usage());
|
||||
dispatch.read_pixels(0, 0, width, height, format.pixel_format, format.component_type, nullptr);
|
||||
dispatch.bind_buffer(target, 0U);
|
||||
|
||||
return pp::foundation::Result<std::uint32_t>::success(buffer_id.value());
|
||||
}
|
||||
|
||||
pp::foundation::Result<void*> map_opengl_pixel_buffer(
|
||||
std::uint32_t buffer_id,
|
||||
std::int32_t width,
|
||||
std::int32_t height,
|
||||
OpenGlReadbackFormat format,
|
||||
OpenGlPixelBufferMapDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.bind_buffer == nullptr || dispatch.map_buffer_range == nullptr) {
|
||||
return pp::foundation::Result<void*>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL pixel-buffer map dispatch callbacks must not be null"));
|
||||
}
|
||||
|
||||
if (buffer_id == 0U
|
||||
|| width <= 0
|
||||
|| height <= 0
|
||||
|| format.pixel_format == 0U
|
||||
|| format.component_type == 0U
|
||||
|| format.bytes_per_pixel == 0U) {
|
||||
return pp::foundation::Result<void*>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL pixel-buffer map parameters are invalid"));
|
||||
}
|
||||
|
||||
const auto target = pixel_pack_buffer_target();
|
||||
dispatch.bind_buffer(target, buffer_id);
|
||||
void* const mapped = dispatch.map_buffer_range(
|
||||
target,
|
||||
0,
|
||||
static_cast<std::intptr_t>(readback_byte_count(
|
||||
format,
|
||||
static_cast<std::uint32_t>(width),
|
||||
static_cast<std::uint32_t>(height))),
|
||||
pixel_buffer_map_read_access());
|
||||
dispatch.bind_buffer(target, 0U);
|
||||
|
||||
if (mapped == nullptr) {
|
||||
return pp::foundation::Result<void*>::failure(
|
||||
pp::foundation::Status::out_of_range("OpenGL pixel-buffer map returned null"));
|
||||
}
|
||||
|
||||
return pp::foundation::Result<void*>::success(mapped);
|
||||
}
|
||||
|
||||
pp::foundation::Status unmap_opengl_pixel_buffer(
|
||||
std::uint32_t buffer_id,
|
||||
OpenGlPixelBufferUnmapDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.bind_buffer == nullptr || dispatch.unmap_buffer == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL pixel-buffer unmap dispatch callbacks must not be null");
|
||||
}
|
||||
|
||||
if (buffer_id == 0U) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL pixel-buffer unmap buffer id is invalid");
|
||||
}
|
||||
|
||||
const auto target = pixel_pack_buffer_target();
|
||||
dispatch.bind_buffer(target, buffer_id);
|
||||
dispatch.unmap_buffer(target);
|
||||
dispatch.bind_buffer(target, 0U);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status delete_opengl_pixel_buffer(
|
||||
std::uint32_t buffer_id,
|
||||
OpenGlPixelBufferDeleteDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.delete_buffers == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL pixel-buffer delete dispatch callback must not be null");
|
||||
}
|
||||
|
||||
if (buffer_id == 0U) {
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
dispatch.delete_buffers(1U, &buffer_id);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::uint32_t> allocate_opengl_depth_renderbuffer(
|
||||
std::int32_t width,
|
||||
std::int32_t height,
|
||||
|
||||
@@ -398,6 +398,12 @@ using OpenGlBufferDataFn = void (*)(
|
||||
std::intptr_t byte_count,
|
||||
const void* data,
|
||||
std::uint32_t usage) noexcept;
|
||||
using OpenGlMapBufferRangeFn = void* (*)(
|
||||
std::uint32_t target,
|
||||
std::intptr_t offset,
|
||||
std::intptr_t byte_count,
|
||||
std::uint32_t access) noexcept;
|
||||
using OpenGlUnmapBufferFn = void (*)(std::uint32_t target) noexcept;
|
||||
using OpenGlBindVertexArrayFn = void (*)(std::uint32_t vertex_array) noexcept;
|
||||
using OpenGlEnableVertexAttribArrayFn = void (*)(std::uint32_t index) noexcept;
|
||||
using OpenGlVertexAttribPointerFn = void (*)(
|
||||
@@ -642,6 +648,31 @@ struct OpenGlFramebufferRestoreDispatch {
|
||||
OpenGlBindFramebufferFn bind_framebuffer = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlPixelBufferAllocationDispatch {
|
||||
OpenGlGenObjectsFn gen_buffers = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlPixelBufferReadbackDispatch {
|
||||
OpenGlGenObjectsFn gen_buffers = nullptr;
|
||||
OpenGlBindBufferFn bind_buffer = nullptr;
|
||||
OpenGlBufferDataFn buffer_data = nullptr;
|
||||
OpenGlReadPixelsFn read_pixels = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlPixelBufferMapDispatch {
|
||||
OpenGlBindBufferFn bind_buffer = nullptr;
|
||||
OpenGlMapBufferRangeFn map_buffer_range = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlPixelBufferUnmapDispatch {
|
||||
OpenGlBindBufferFn bind_buffer = nullptr;
|
||||
OpenGlUnmapBufferFn unmap_buffer = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlPixelBufferDeleteDispatch {
|
||||
OpenGlDeleteObjectsFn delete_buffers = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlDepthRenderbufferAllocationDispatch {
|
||||
OpenGlGenObjectsFn gen_renderbuffers = nullptr;
|
||||
OpenGlBindRenderbufferFn bind_renderbuffer = nullptr;
|
||||
@@ -864,6 +895,25 @@ struct OpenGlMeshDeleteDispatch {
|
||||
[[nodiscard]] pp::foundation::Status restore_opengl_framebuffer_binding(
|
||||
OpenGlFramebufferBindingState binding,
|
||||
OpenGlFramebufferRestoreDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Result<std::uint32_t> allocate_opengl_pixel_buffer(
|
||||
OpenGlPixelBufferAllocationDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Result<std::uint32_t> readback_opengl_framebuffer_to_pixel_buffer(
|
||||
std::int32_t width,
|
||||
std::int32_t height,
|
||||
OpenGlReadbackFormat format,
|
||||
OpenGlPixelBufferReadbackDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Result<void*> map_opengl_pixel_buffer(
|
||||
std::uint32_t buffer_id,
|
||||
std::int32_t width,
|
||||
std::int32_t height,
|
||||
OpenGlReadbackFormat format,
|
||||
OpenGlPixelBufferMapDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Status unmap_opengl_pixel_buffer(
|
||||
std::uint32_t buffer_id,
|
||||
OpenGlPixelBufferUnmapDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Status delete_opengl_pixel_buffer(
|
||||
std::uint32_t buffer_id,
|
||||
OpenGlPixelBufferDeleteDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Result<std::uint32_t> allocate_opengl_depth_renderbuffer(
|
||||
std::int32_t width,
|
||||
std::int32_t height,
|
||||
|
||||
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