Route RTT blit readback through renderer GL
This commit is contained in:
@@ -608,6 +608,82 @@ pp::foundation::Result<OpenGlTexture2DReadbackResult> readback_opengl_texture_2d
|
||||
return pp::foundation::Result<OpenGlTexture2DReadbackResult>::success(result);
|
||||
}
|
||||
|
||||
pp::foundation::Status blit_opengl_framebuffer(
|
||||
OpenGlFramebufferBlit blit,
|
||||
OpenGlFramebufferBlitDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.get_integer == nullptr
|
||||
|| dispatch.bind_framebuffer == nullptr
|
||||
|| dispatch.blit_framebuffer == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL framebuffer blit dispatch callbacks must not be null");
|
||||
}
|
||||
|
||||
if (blit.source_rect.x1 <= blit.source_rect.x0
|
||||
|| blit.source_rect.y1 <= blit.source_rect.y0
|
||||
|| blit.destination_rect.x1 <= blit.destination_rect.x0
|
||||
|| blit.destination_rect.y1 <= blit.destination_rect.y0
|
||||
|| blit.mask == 0U
|
||||
|| blit.filter == 0U) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL framebuffer blit parameters are invalid");
|
||||
}
|
||||
|
||||
std::int32_t previous_draw_framebuffer = 0;
|
||||
std::int32_t previous_read_framebuffer = 0;
|
||||
dispatch.get_integer(draw_framebuffer_binding_query(), &previous_draw_framebuffer);
|
||||
dispatch.get_integer(read_framebuffer_binding_query(), &previous_read_framebuffer);
|
||||
dispatch.bind_framebuffer(draw_framebuffer_target(), blit.destination_framebuffer);
|
||||
dispatch.bind_framebuffer(read_framebuffer_target(), blit.source_framebuffer);
|
||||
dispatch.blit_framebuffer(
|
||||
blit.source_rect.x0,
|
||||
blit.source_rect.y0,
|
||||
blit.source_rect.x1,
|
||||
blit.source_rect.y1,
|
||||
blit.destination_rect.x0,
|
||||
blit.destination_rect.y0,
|
||||
blit.destination_rect.x1,
|
||||
blit.destination_rect.y1,
|
||||
blit.mask,
|
||||
blit.filter);
|
||||
dispatch.bind_framebuffer(draw_framebuffer_target(), static_cast<std::uint32_t>(previous_draw_framebuffer));
|
||||
dispatch.bind_framebuffer(read_framebuffer_target(), static_cast<std::uint32_t>(previous_read_framebuffer));
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status readback_opengl_framebuffer(
|
||||
OpenGlFramebufferReadback readback,
|
||||
OpenGlFramebufferReadbackDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.get_integer == nullptr
|
||||
|| dispatch.bind_framebuffer == nullptr
|
||||
|| dispatch.read_pixels == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument(
|
||||
"OpenGL framebuffer readback dispatch callbacks must not be null");
|
||||
}
|
||||
|
||||
if (readback.width <= 0
|
||||
|| readback.height <= 0
|
||||
|| readback.format.pixel_format == 0U
|
||||
|| readback.format.component_type == 0U
|
||||
|| readback.format.bytes_per_pixel == 0U
|
||||
|| readback.pixels == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL framebuffer readback parameters are invalid");
|
||||
}
|
||||
|
||||
std::int32_t previous_read_framebuffer = 0;
|
||||
dispatch.get_integer(read_framebuffer_binding_query(), &previous_read_framebuffer);
|
||||
dispatch.bind_framebuffer(read_framebuffer_target(), readback.framebuffer);
|
||||
dispatch.read_pixels(
|
||||
readback.x,
|
||||
readback.y,
|
||||
readback.width,
|
||||
readback.height,
|
||||
readback.format.pixel_format,
|
||||
readback.format.component_type,
|
||||
readback.pixels);
|
||||
dispatch.bind_framebuffer(read_framebuffer_target(), static_cast<std::uint32_t>(previous_read_framebuffer));
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
std::uint32_t extension_count_query() noexcept
|
||||
{
|
||||
return gl_num_extensions;
|
||||
|
||||
@@ -141,6 +141,32 @@ struct OpenGlTexture2DReadbackResult {
|
||||
bool pixels_read = false;
|
||||
};
|
||||
|
||||
struct OpenGlFramebufferRect {
|
||||
std::int32_t x0 = 0;
|
||||
std::int32_t y0 = 0;
|
||||
std::int32_t x1 = 0;
|
||||
std::int32_t y1 = 0;
|
||||
};
|
||||
|
||||
struct OpenGlFramebufferBlit {
|
||||
std::uint32_t source_framebuffer = 0;
|
||||
std::uint32_t destination_framebuffer = 0;
|
||||
OpenGlFramebufferRect source_rect;
|
||||
OpenGlFramebufferRect destination_rect;
|
||||
std::uint32_t mask = 0;
|
||||
std::uint32_t filter = 0;
|
||||
};
|
||||
|
||||
struct OpenGlFramebufferReadback {
|
||||
std::uint32_t framebuffer = 0;
|
||||
std::int32_t x = 0;
|
||||
std::int32_t y = 0;
|
||||
std::int32_t width = 0;
|
||||
std::int32_t height = 0;
|
||||
OpenGlReadbackFormat format;
|
||||
void* pixels = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlWindowsWglContextConfig {
|
||||
std::array<std::int32_t, 9> context_attributes {};
|
||||
std::array<std::int32_t, 15> pixel_format_attributes {};
|
||||
@@ -223,6 +249,17 @@ using OpenGlReadPixelsFn = void (*)(
|
||||
std::uint32_t pixel_format,
|
||||
std::uint32_t component_type,
|
||||
void* pixels) noexcept;
|
||||
using OpenGlBlitFramebufferFn = void (*)(
|
||||
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;
|
||||
|
||||
struct OpenGlStateDispatch {
|
||||
OpenGlCapabilityFn enable = nullptr;
|
||||
@@ -332,6 +369,18 @@ struct OpenGlTexture2DReadbackDispatch {
|
||||
OpenGlDeleteObjectsFn delete_framebuffers = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlFramebufferBlitDispatch {
|
||||
OpenGlGetIntegerFn get_integer = nullptr;
|
||||
OpenGlBindFramebufferFn bind_framebuffer = nullptr;
|
||||
OpenGlBlitFramebufferFn blit_framebuffer = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlFramebufferReadbackDispatch {
|
||||
OpenGlGetIntegerFn get_integer = nullptr;
|
||||
OpenGlBindFramebufferFn bind_framebuffer = nullptr;
|
||||
OpenGlReadPixelsFn read_pixels = nullptr;
|
||||
};
|
||||
|
||||
[[nodiscard]] OpenGlCapabilities detect_opengl_capabilities(
|
||||
std::span<const std::string_view> extensions,
|
||||
OpenGlRuntime runtime) noexcept;
|
||||
@@ -382,6 +431,12 @@ struct OpenGlTexture2DReadbackDispatch {
|
||||
[[nodiscard]] pp::foundation::Result<OpenGlTexture2DReadbackResult> readback_opengl_texture_2d(
|
||||
OpenGlTexture2DReadback readback,
|
||||
OpenGlTexture2DReadbackDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Status blit_opengl_framebuffer(
|
||||
OpenGlFramebufferBlit blit,
|
||||
OpenGlFramebufferBlitDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Status readback_opengl_framebuffer(
|
||||
OpenGlFramebufferReadback readback,
|
||||
OpenGlFramebufferReadbackDispatch dispatch) noexcept;
|
||||
|
||||
[[nodiscard]] std::uint32_t extension_count_query() noexcept;
|
||||
[[nodiscard]] std::uint32_t extension_string_name() noexcept;
|
||||
|
||||
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