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,
|
||||
|
||||
Reference in New Issue
Block a user