Route RTT blit readback through renderer GL
This commit is contained in:
220
src/rtt.cpp
220
src/rtt.cpp
@@ -44,6 +44,60 @@ namespace {
|
||||
return static_cast<GLenum>(pp::renderer::gl::read_framebuffer_binding_query());
|
||||
}
|
||||
|
||||
void query_opengl_integer(std::uint32_t name, std::int32_t* value) noexcept
|
||||
{
|
||||
glGetIntegerv(static_cast<GLenum>(name), reinterpret_cast<GLint*>(value));
|
||||
}
|
||||
|
||||
void bind_opengl_framebuffer(std::uint32_t target, std::uint32_t framebuffer) noexcept
|
||||
{
|
||||
glBindFramebuffer(static_cast<GLenum>(target), static_cast<GLuint>(framebuffer));
|
||||
}
|
||||
|
||||
void blit_opengl_framebuffer(
|
||||
std::int32_t source_x0,
|
||||
std::int32_t source_y0,
|
||||
std::int32_t source_x1,
|
||||
std::int32_t source_y1,
|
||||
std::int32_t destination_x0,
|
||||
std::int32_t destination_y0,
|
||||
std::int32_t destination_x1,
|
||||
std::int32_t destination_y1,
|
||||
std::uint32_t mask,
|
||||
std::uint32_t filter) noexcept
|
||||
{
|
||||
glBlitFramebuffer(
|
||||
static_cast<GLint>(source_x0),
|
||||
static_cast<GLint>(source_y0),
|
||||
static_cast<GLint>(source_x1),
|
||||
static_cast<GLint>(source_y1),
|
||||
static_cast<GLint>(destination_x0),
|
||||
static_cast<GLint>(destination_y0),
|
||||
static_cast<GLint>(destination_x1),
|
||||
static_cast<GLint>(destination_y1),
|
||||
static_cast<GLbitfield>(mask),
|
||||
static_cast<GLenum>(filter));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RTT& RTT::operator=(RTT&& other)
|
||||
@@ -112,9 +166,6 @@ bool RTT::resize(int width, int height)
|
||||
|
||||
App::I->render_task([&]
|
||||
{
|
||||
glGetIntegerv(draw_framebuffer_binding_query(), &oldDFboID);
|
||||
glGetIntegerv(read_framebuffer_binding_query(), &oldRFboID);
|
||||
|
||||
ret = new_rtt.create(width, height, -1, int_fmt, rboID != 0);
|
||||
if (!ret)
|
||||
{
|
||||
@@ -122,22 +173,28 @@ bool RTT::resize(int width, int height)
|
||||
return;
|
||||
}
|
||||
|
||||
glBindFramebuffer(draw_framebuffer_target(), new_rtt.fboID);
|
||||
glBindFramebuffer(read_framebuffer_target(), fboID);
|
||||
glBlitFramebuffer(
|
||||
0,
|
||||
0,
|
||||
w,
|
||||
h,
|
||||
0,
|
||||
0,
|
||||
new_rtt.w,
|
||||
new_rtt.h,
|
||||
static_cast<GLbitfield>(pp::renderer::gl::framebuffer_color_buffer_mask()),
|
||||
static_cast<GLenum>(pp::renderer::gl::framebuffer_blit_filter(true)));
|
||||
|
||||
glBindFramebuffer(draw_framebuffer_target(), oldDFboID);
|
||||
glBindFramebuffer(read_framebuffer_target(), oldRFboID);
|
||||
const auto status = pp::renderer::gl::blit_opengl_framebuffer(
|
||||
pp::renderer::gl::OpenGlFramebufferBlit {
|
||||
.source_framebuffer = static_cast<std::uint32_t>(fboID),
|
||||
.destination_framebuffer = static_cast<std::uint32_t>(new_rtt.fboID),
|
||||
.source_rect = pp::renderer::gl::OpenGlFramebufferRect { .x1 = w, .y1 = h },
|
||||
.destination_rect = pp::renderer::gl::OpenGlFramebufferRect {
|
||||
.x1 = new_rtt.w,
|
||||
.y1 = new_rtt.h,
|
||||
},
|
||||
.mask = pp::renderer::gl::framebuffer_color_buffer_mask(),
|
||||
.filter = pp::renderer::gl::framebuffer_blit_filter(true),
|
||||
},
|
||||
pp::renderer::gl::OpenGlFramebufferBlitDispatch {
|
||||
.get_integer = query_opengl_integer,
|
||||
.bind_framebuffer = bind_opengl_framebuffer,
|
||||
.blit_framebuffer = blit_opengl_framebuffer,
|
||||
});
|
||||
if (!status.ok()) {
|
||||
LOG("RTT::resize blit failed because: %s", status.message);
|
||||
ret = false;
|
||||
return;
|
||||
}
|
||||
|
||||
destroy();
|
||||
});
|
||||
@@ -188,19 +245,22 @@ void RTT::copy(const RTT & source)
|
||||
return;
|
||||
App::I->render_task([&]
|
||||
{
|
||||
GLint old_draw = 0;
|
||||
GLint old_read = 0;
|
||||
glGetIntegerv(draw_framebuffer_binding_query(), &old_draw);
|
||||
glGetIntegerv(read_framebuffer_binding_query(), &old_read);
|
||||
|
||||
glBindFramebuffer(draw_framebuffer_target(), fboID);
|
||||
glBindFramebuffer(read_framebuffer_target(), source.fboID);
|
||||
glBlitFramebuffer(0, 0, source.w, source.h, 0, 0, w, h,
|
||||
static_cast<GLbitfield>(pp::renderer::gl::framebuffer_color_buffer_mask()),
|
||||
static_cast<GLenum>(pp::renderer::gl::framebuffer_blit_filter(true)));
|
||||
|
||||
glBindFramebuffer(draw_framebuffer_target(), old_draw);
|
||||
glBindFramebuffer(read_framebuffer_target(), old_read);
|
||||
const auto status = pp::renderer::gl::blit_opengl_framebuffer(
|
||||
pp::renderer::gl::OpenGlFramebufferBlit {
|
||||
.source_framebuffer = static_cast<std::uint32_t>(source.fboID),
|
||||
.destination_framebuffer = static_cast<std::uint32_t>(fboID),
|
||||
.source_rect = pp::renderer::gl::OpenGlFramebufferRect { .x1 = source.w, .y1 = source.h },
|
||||
.destination_rect = pp::renderer::gl::OpenGlFramebufferRect { .x1 = w, .y1 = h },
|
||||
.mask = pp::renderer::gl::framebuffer_color_buffer_mask(),
|
||||
.filter = pp::renderer::gl::framebuffer_blit_filter(true),
|
||||
},
|
||||
pp::renderer::gl::OpenGlFramebufferBlitDispatch {
|
||||
.get_integer = query_opengl_integer,
|
||||
.bind_framebuffer = bind_opengl_framebuffer,
|
||||
.blit_framebuffer = blit_opengl_framebuffer,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("RTT::copy blit failed because: %s", status.message);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -211,20 +271,32 @@ void RTT::copy(const RTT& source, const glm::vec4& rect)
|
||||
App::I->render_task([&]
|
||||
{
|
||||
auto r = rect_intersection(rect, { 0, 0, w, h });
|
||||
|
||||
GLint old_draw = 0;
|
||||
GLint old_read = 0;
|
||||
glGetIntegerv(draw_framebuffer_binding_query(), &old_draw);
|
||||
glGetIntegerv(read_framebuffer_binding_query(), &old_read);
|
||||
|
||||
glBindFramebuffer(draw_framebuffer_target(), fboID);
|
||||
glBindFramebuffer(read_framebuffer_target(), source.fboID);
|
||||
glBlitFramebuffer(r.x, r.y, r.z, r.w, r.x, r.y, r.z, r.w,
|
||||
static_cast<GLbitfield>(pp::renderer::gl::framebuffer_color_buffer_mask()),
|
||||
static_cast<GLenum>(pp::renderer::gl::framebuffer_blit_filter(false)));
|
||||
|
||||
glBindFramebuffer(draw_framebuffer_target(), old_draw);
|
||||
glBindFramebuffer(read_framebuffer_target(), old_read);
|
||||
const auto status = pp::renderer::gl::blit_opengl_framebuffer(
|
||||
pp::renderer::gl::OpenGlFramebufferBlit {
|
||||
.source_framebuffer = static_cast<std::uint32_t>(source.fboID),
|
||||
.destination_framebuffer = static_cast<std::uint32_t>(fboID),
|
||||
.source_rect = pp::renderer::gl::OpenGlFramebufferRect {
|
||||
.x0 = static_cast<std::int32_t>(r.x),
|
||||
.y0 = static_cast<std::int32_t>(r.y),
|
||||
.x1 = static_cast<std::int32_t>(r.z),
|
||||
.y1 = static_cast<std::int32_t>(r.w),
|
||||
},
|
||||
.destination_rect = pp::renderer::gl::OpenGlFramebufferRect {
|
||||
.x0 = static_cast<std::int32_t>(r.x),
|
||||
.y0 = static_cast<std::int32_t>(r.y),
|
||||
.x1 = static_cast<std::int32_t>(r.z),
|
||||
.y1 = static_cast<std::int32_t>(r.w),
|
||||
},
|
||||
.mask = pp::renderer::gl::framebuffer_color_buffer_mask(),
|
||||
.filter = pp::renderer::gl::framebuffer_blit_filter(false),
|
||||
},
|
||||
pp::renderer::gl::OpenGlFramebufferBlitDispatch {
|
||||
.get_integer = query_opengl_integer,
|
||||
.bind_framebuffer = bind_opengl_framebuffer,
|
||||
.blit_framebuffer = blit_opengl_framebuffer,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("RTT::copy region blit failed because: %s", status.message);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -314,7 +386,7 @@ bool RTT::create(int width, int height, int tex/* = -1*/, GLint internal_format,
|
||||
}
|
||||
|
||||
GLint oldFboID;
|
||||
glGetIntegerv(draw_framebuffer_binding_query(), &oldFboID);
|
||||
glGetIntegerv(static_cast<GLenum>(pp::renderer::gl::draw_framebuffer_binding_query()), &oldFboID);
|
||||
|
||||
// Create a framebuffer object
|
||||
glGenFramebuffers(1, &fboID);
|
||||
@@ -440,18 +512,21 @@ uint8_t* RTT::readTextureData(uint8_t* buffer) const noexcept
|
||||
App::I->render_task([&]
|
||||
{
|
||||
const auto readback = pp::renderer::gl::rgba8_readback_format();
|
||||
GLint old;
|
||||
glGetIntegerv(read_framebuffer_binding_query(), &old);
|
||||
glBindFramebuffer(read_framebuffer_target(), fboID);
|
||||
glReadPixels(
|
||||
0,
|
||||
0,
|
||||
w,
|
||||
h,
|
||||
static_cast<GLenum>(readback.pixel_format),
|
||||
static_cast<GLenum>(readback.component_type),
|
||||
buffer);
|
||||
glBindFramebuffer(read_framebuffer_target(), old);
|
||||
const auto status = pp::renderer::gl::readback_opengl_framebuffer(
|
||||
pp::renderer::gl::OpenGlFramebufferReadback {
|
||||
.framebuffer = static_cast<std::uint32_t>(fboID),
|
||||
.width = w,
|
||||
.height = h,
|
||||
.format = readback,
|
||||
.pixels = buffer,
|
||||
},
|
||||
pp::renderer::gl::OpenGlFramebufferReadbackDispatch {
|
||||
.get_integer = query_opengl_integer,
|
||||
.bind_framebuffer = bind_opengl_framebuffer,
|
||||
.read_pixels = read_opengl_pixels,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("RTT::readTextureData() failed because: %s", status.message);
|
||||
});
|
||||
return buffer;
|
||||
}
|
||||
@@ -465,18 +540,21 @@ float* RTT::readTextureDataFloat(float* buffer) const noexcept
|
||||
App::I->render_task([&]
|
||||
{
|
||||
const auto readback = pp::renderer::gl::rgba32f_readback_format();
|
||||
GLint old;
|
||||
glGetIntegerv(read_framebuffer_binding_query(), &old);
|
||||
glBindFramebuffer(read_framebuffer_target(), fboID);
|
||||
glReadPixels(
|
||||
0,
|
||||
0,
|
||||
w,
|
||||
h,
|
||||
static_cast<GLenum>(readback.pixel_format),
|
||||
static_cast<GLenum>(readback.component_type),
|
||||
buffer);
|
||||
glBindFramebuffer(read_framebuffer_target(), old);
|
||||
const auto status = pp::renderer::gl::readback_opengl_framebuffer(
|
||||
pp::renderer::gl::OpenGlFramebufferReadback {
|
||||
.framebuffer = static_cast<std::uint32_t>(fboID),
|
||||
.width = w,
|
||||
.height = h,
|
||||
.format = readback,
|
||||
.pixels = buffer,
|
||||
},
|
||||
pp::renderer::gl::OpenGlFramebufferReadbackDispatch {
|
||||
.get_integer = query_opengl_integer,
|
||||
.bind_framebuffer = bind_opengl_framebuffer,
|
||||
.read_pixels = read_opengl_pixels,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("RTT::readTextureDataFloat() failed because: %s", status.message);
|
||||
});
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user