Route Texture2D through renderer GL
This commit is contained in:
277
src/texture.cpp
277
src/texture.cpp
@@ -9,24 +9,134 @@
|
||||
|
||||
namespace {
|
||||
|
||||
[[nodiscard]] GLenum texture_2d_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::texture_2d_target());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum texture_cube_map_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::texture_cube_map_target());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum framebuffer_target() noexcept
|
||||
void gen_opengl_textures(std::uint32_t count, std::uint32_t* ids) noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::framebuffer_target());
|
||||
glGenTextures(static_cast<GLsizei>(count), reinterpret_cast<GLuint*>(ids));
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum draw_framebuffer_binding_query() noexcept
|
||||
void delete_opengl_textures(std::uint32_t count, const std::uint32_t* ids) noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::draw_framebuffer_binding_query());
|
||||
glDeleteTextures(static_cast<GLsizei>(count), reinterpret_cast<const GLuint*>(ids));
|
||||
}
|
||||
|
||||
void bind_opengl_texture(std::uint32_t target, std::uint32_t texture) noexcept
|
||||
{
|
||||
glBindTexture(static_cast<GLenum>(target), static_cast<GLuint>(texture));
|
||||
}
|
||||
|
||||
void upload_opengl_texture_2d(
|
||||
std::uint32_t target,
|
||||
std::int32_t level,
|
||||
std::int32_t internal_format,
|
||||
std::int32_t width,
|
||||
std::int32_t height,
|
||||
std::int32_t border,
|
||||
std::uint32_t pixel_format,
|
||||
std::uint32_t component_type,
|
||||
const void* data) noexcept
|
||||
{
|
||||
glTexImage2D(
|
||||
static_cast<GLenum>(target),
|
||||
static_cast<GLint>(level),
|
||||
static_cast<GLint>(internal_format),
|
||||
static_cast<GLsizei>(width),
|
||||
static_cast<GLsizei>(height),
|
||||
static_cast<GLint>(border),
|
||||
static_cast<GLenum>(pixel_format),
|
||||
static_cast<GLenum>(component_type),
|
||||
data);
|
||||
}
|
||||
|
||||
void update_opengl_texture_2d_region(
|
||||
std::uint32_t target,
|
||||
std::int32_t level,
|
||||
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,
|
||||
const void* data) noexcept
|
||||
{
|
||||
glTexSubImage2D(
|
||||
static_cast<GLenum>(target),
|
||||
static_cast<GLint>(level),
|
||||
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),
|
||||
data);
|
||||
}
|
||||
|
||||
void generate_opengl_mipmap(std::uint32_t target) noexcept
|
||||
{
|
||||
glGenerateMipmap(static_cast<GLenum>(target));
|
||||
}
|
||||
|
||||
void gen_opengl_framebuffers(std::uint32_t count, std::uint32_t* ids) noexcept
|
||||
{
|
||||
glGenFramebuffers(static_cast<GLsizei>(count), reinterpret_cast<GLuint*>(ids));
|
||||
}
|
||||
|
||||
void delete_opengl_framebuffers(std::uint32_t count, const std::uint32_t* ids) noexcept
|
||||
{
|
||||
glDeleteFramebuffers(static_cast<GLsizei>(count), reinterpret_cast<const GLuint*>(ids));
|
||||
}
|
||||
|
||||
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 attach_opengl_framebuffer_texture_2d(
|
||||
std::uint32_t target,
|
||||
std::uint32_t attachment,
|
||||
std::uint32_t texture_target,
|
||||
std::uint32_t texture,
|
||||
std::int32_t level) noexcept
|
||||
{
|
||||
glFramebufferTexture2D(
|
||||
static_cast<GLenum>(target),
|
||||
static_cast<GLenum>(attachment),
|
||||
static_cast<GLenum>(texture_target),
|
||||
static_cast<GLuint>(texture),
|
||||
static_cast<GLint>(level));
|
||||
}
|
||||
|
||||
std::uint32_t check_opengl_framebuffer_status(std::uint32_t target) noexcept
|
||||
{
|
||||
return static_cast<std::uint32_t>(glCheckFramebufferStatus(static_cast<GLenum>(target)));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -162,44 +272,33 @@ Image Texture2D::get_image() const noexcept
|
||||
ret.create(m_width, m_height);
|
||||
App::I->render_task([&]
|
||||
{
|
||||
bind();
|
||||
|
||||
GLuint fboID;
|
||||
glGenFramebuffers(1, &fboID);
|
||||
if (fboID == 0)
|
||||
const auto readback = pp::renderer::gl::readback_opengl_texture_2d(
|
||||
pp::renderer::gl::OpenGlTexture2DReadback {
|
||||
.texture_id = static_cast<std::uint32_t>(m_tex),
|
||||
.width = m_width,
|
||||
.height = m_height,
|
||||
.format = pp::renderer::gl::rgba8_readback_format(),
|
||||
.pixels = ret.m_data.get(),
|
||||
},
|
||||
pp::renderer::gl::OpenGlTexture2DReadbackDispatch {
|
||||
.bind_texture = bind_opengl_texture,
|
||||
.gen_framebuffers = gen_opengl_framebuffers,
|
||||
.get_integer = query_opengl_integer,
|
||||
.bind_framebuffer = bind_opengl_framebuffer,
|
||||
.framebuffer_texture_2d = attach_opengl_framebuffer_texture_2d,
|
||||
.check_framebuffer_status = check_opengl_framebuffer_status,
|
||||
.read_pixels = read_opengl_pixels,
|
||||
.delete_framebuffers = delete_opengl_framebuffers,
|
||||
});
|
||||
if (!readback.ok()) {
|
||||
LOG("Texture2D::get_image() failed because: %s", readback.status().message);
|
||||
return;
|
||||
|
||||
GLint oldFboID;
|
||||
glGetIntegerv(draw_framebuffer_binding_query(), &oldFboID);
|
||||
|
||||
glBindFramebuffer(framebuffer_target(), fboID);
|
||||
glFramebufferTexture2D(
|
||||
framebuffer_target(),
|
||||
static_cast<GLenum>(pp::renderer::gl::framebuffer_color_attachment()),
|
||||
texture_2d_target(),
|
||||
m_tex,
|
||||
0);
|
||||
GLenum status = glCheckFramebufferStatus(framebuffer_target());
|
||||
if (status == static_cast<GLenum>(pp::renderer::gl::framebuffer_complete_status()))
|
||||
{
|
||||
const auto readback = pp::renderer::gl::rgba8_readback_format();
|
||||
glReadPixels(
|
||||
0,
|
||||
0,
|
||||
m_width,
|
||||
m_height,
|
||||
static_cast<GLenum>(readback.pixel_format),
|
||||
static_cast<GLenum>(readback.component_type),
|
||||
ret.m_data.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (!readback.value().pixels_read) {
|
||||
LOG("Texture2D::get_image() failed because: %s",
|
||||
pp::renderer::gl::framebuffer_status_name(static_cast<std::uint32_t>(status)));
|
||||
pp::renderer::gl::framebuffer_status_name(readback.value().framebuffer_status));
|
||||
}
|
||||
|
||||
glBindFramebuffer(framebuffer_target(), oldFboID);
|
||||
glDeleteFramebuffers(1, &fboID);
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
@@ -249,17 +348,32 @@ bool Texture2D::create(int width, int height, GLint internal_format, GLint forma
|
||||
App::I->render_task([=]
|
||||
{
|
||||
destroy();
|
||||
const auto component_type = pp::renderer::gl::texture_upload_type_for_internal_format(
|
||||
static_cast<std::uint32_t>(internal_format));
|
||||
const auto texture = pp::renderer::gl::allocate_opengl_texture_2d(
|
||||
pp::renderer::gl::OpenGlTexture2DAllocation {
|
||||
.width = width,
|
||||
.height = height,
|
||||
.internal_format = internal_format,
|
||||
.pixel_format = static_cast<std::uint32_t>(format),
|
||||
.component_type = component_type,
|
||||
.data = data,
|
||||
},
|
||||
pp::renderer::gl::OpenGlTexture2DAllocationDispatch {
|
||||
.gen_textures = gen_opengl_textures,
|
||||
.bind_texture = bind_opengl_texture,
|
||||
.tex_image_2d = upload_opengl_texture_2d,
|
||||
});
|
||||
if (!texture.ok()) {
|
||||
LOG("Texture2D::create() failed because: %s", texture.status().message);
|
||||
return;
|
||||
}
|
||||
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_format = format;
|
||||
m_iformat = internal_format;
|
||||
glGenTextures(1, &m_tex);
|
||||
//LOG("TEX create %d", m_tex);
|
||||
bind();
|
||||
const auto ifmt = static_cast<GLenum>(
|
||||
pp::renderer::gl::texture_upload_type_for_internal_format(static_cast<std::uint32_t>(internal_format)));
|
||||
glTexImage2D(texture_2d_target(), 0, internal_format, width, height, 0, format, ifmt, data);
|
||||
unbind();
|
||||
m_tex = static_cast<GLuint>(texture.value());
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@@ -281,9 +395,16 @@ void Texture2D::create_mipmaps()
|
||||
{
|
||||
App::I->render_task([this]
|
||||
{
|
||||
bind();
|
||||
glGenerateMipmap(texture_2d_target());
|
||||
unbind();
|
||||
const auto status = pp::renderer::gl::generate_opengl_texture_2d_mipmaps(
|
||||
static_cast<std::uint32_t>(m_tex),
|
||||
pp::renderer::gl::OpenGlTexture2DMipmapDispatch {
|
||||
.bind_texture = bind_opengl_texture,
|
||||
.generate_mipmap = generate_opengl_mipmap,
|
||||
});
|
||||
if (!status.ok()) {
|
||||
LOG("Texture2D::create_mipmaps() failed because: %s", status.message);
|
||||
return;
|
||||
}
|
||||
has_mips = true;
|
||||
});
|
||||
}
|
||||
@@ -331,7 +452,13 @@ void Texture2D::destroy()
|
||||
{
|
||||
App::I->render_task_async([id = m_tex]
|
||||
{
|
||||
glDeleteTextures(1, &id);
|
||||
const auto status = pp::renderer::gl::delete_opengl_texture_2d(
|
||||
static_cast<std::uint32_t>(id),
|
||||
pp::renderer::gl::OpenGlTexture2DDeleteDispatch {
|
||||
.delete_textures = delete_opengl_textures,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Texture2D::destroy() failed because: %s", status.message);
|
||||
});
|
||||
m_tex = 0;
|
||||
}
|
||||
@@ -340,30 +467,46 @@ void Texture2D::destroy()
|
||||
void Texture2D::bind() const
|
||||
{
|
||||
assert(App::I->is_render_thread());
|
||||
glBindTexture(texture_2d_target(), m_tex);
|
||||
const auto status = pp::renderer::gl::bind_opengl_texture_2d(
|
||||
static_cast<std::uint32_t>(m_tex),
|
||||
pp::renderer::gl::OpenGlTexture2DBindDispatch {
|
||||
.bind_texture = bind_opengl_texture,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Texture2D::bind() failed because: %s", status.message);
|
||||
}
|
||||
|
||||
void Texture2D::unbind() const
|
||||
{
|
||||
assert(App::I->is_render_thread());
|
||||
glBindTexture(texture_2d_target(), 0);
|
||||
const auto status = pp::renderer::gl::bind_opengl_texture_2d(
|
||||
0U,
|
||||
pp::renderer::gl::OpenGlTexture2DBindDispatch {
|
||||
.bind_texture = bind_opengl_texture,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Texture2D::unbind() failed because: %s", status.message);
|
||||
}
|
||||
|
||||
void Texture2D::update(const uint8_t* data)
|
||||
{
|
||||
App::I->render_task([this, data]
|
||||
{
|
||||
bind();
|
||||
glTexSubImage2D(
|
||||
texture_2d_target(),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
m_width,
|
||||
m_height,
|
||||
m_format,
|
||||
static_cast<GLenum>(pp::renderer::gl::unsigned_byte_component_type()),
|
||||
data);
|
||||
const auto status = pp::renderer::gl::update_opengl_texture_2d(
|
||||
pp::renderer::gl::OpenGlTexture2DUpdate {
|
||||
.texture_id = static_cast<std::uint32_t>(m_tex),
|
||||
.width = m_width,
|
||||
.height = m_height,
|
||||
.pixel_format = static_cast<std::uint32_t>(m_format),
|
||||
.component_type = pp::renderer::gl::unsigned_byte_component_type(),
|
||||
.data = data,
|
||||
},
|
||||
pp::renderer::gl::OpenGlTexture2DUpdateDispatch {
|
||||
.bind_texture = bind_opengl_texture,
|
||||
.tex_sub_image_2d = update_opengl_texture_2d_region,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Texture2D::update() failed because: %s", status.message);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user