Move texture2d mapping to renderer gl
This commit is contained in:
@@ -7,6 +7,25 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace {
|
||||
|
||||
[[nodiscard]] GLenum texture_2d_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::texture_2d_target());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum framebuffer_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::framebuffer_target());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum draw_framebuffer_binding_query() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::draw_framebuffer_binding_query());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::map<uint16_t, Texture2D> TextureManager::m_textures;
|
||||
std::array<int, 6> TextureCube::m_faces_map {
|
||||
static_cast<int>(pp::renderer::gl::cube_face_texture_target(0U)),
|
||||
@@ -125,21 +144,35 @@ Image Texture2D::get_image() const noexcept
|
||||
return;
|
||||
|
||||
GLint oldFboID;
|
||||
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldFboID);
|
||||
glGetIntegerv(draw_framebuffer_binding_query(), &oldFboID);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_tex, 0);
|
||||
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
if (status == GL_FRAMEBUFFER_COMPLETE)
|
||||
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()))
|
||||
{
|
||||
glReadPixels(0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, ret.m_data.get());
|
||||
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
|
||||
{
|
||||
LOG("Texture2D::get_image() failed because framebuffer status = %d", (int)status);
|
||||
LOG("Texture2D::get_image() failed because: %s",
|
||||
pp::renderer::gl::framebuffer_status_name(static_cast<std::uint32_t>(status)));
|
||||
}
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, oldFboID);
|
||||
glBindFramebuffer(framebuffer_target(), oldFboID);
|
||||
glDeleteFramebuffers(1, &fboID);
|
||||
});
|
||||
return ret;
|
||||
@@ -181,7 +214,7 @@ bool Texture2D::create(int width, int height, GLint internal_format, GLint forma
|
||||
bind();
|
||||
const auto ifmt = static_cast<GLenum>(
|
||||
pp::renderer::gl::texture_upload_type_for_internal_format(static_cast<std::uint32_t>(internal_format)));
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, ifmt, data);
|
||||
glTexImage2D(texture_2d_target(), 0, internal_format, width, height, 0, format, ifmt, data);
|
||||
unbind();
|
||||
});
|
||||
return true;
|
||||
@@ -205,7 +238,7 @@ void Texture2D::create_mipmaps()
|
||||
App::I->render_task([this]
|
||||
{
|
||||
bind();
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glGenerateMipmap(texture_2d_target());
|
||||
unbind();
|
||||
has_mips = true;
|
||||
});
|
||||
@@ -253,13 +286,13 @@ void Texture2D::destroy()
|
||||
void Texture2D::bind() const
|
||||
{
|
||||
assert(App::I->is_render_thread());
|
||||
glBindTexture(GL_TEXTURE_2D, m_tex);
|
||||
glBindTexture(texture_2d_target(), m_tex);
|
||||
}
|
||||
|
||||
void Texture2D::unbind() const
|
||||
{
|
||||
assert(App::I->is_render_thread());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindTexture(texture_2d_target(), 0);
|
||||
}
|
||||
|
||||
void Texture2D::update(const uint8_t* data)
|
||||
@@ -267,7 +300,16 @@ void Texture2D::update(const uint8_t* data)
|
||||
App::I->render_task([this, data]
|
||||
{
|
||||
bind();
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, m_format, GL_UNSIGNED_BYTE, data);
|
||||
glTexSubImage2D(
|
||||
texture_2d_target(),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
m_width,
|
||||
m_height,
|
||||
m_format,
|
||||
static_cast<GLenum>(pp::renderer::gl::unsigned_byte_component_type()),
|
||||
data);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user