Share retained texture dispatch bridge

This commit is contained in:
2026-06-05 14:29:59 +02:00
parent df21d673dd
commit 421f2713db
6 changed files with 306 additions and 250 deletions

View File

@@ -3,78 +3,13 @@
#include "texture.h"
#include "util.h"
#include "app.h"
#include "legacy_gl_texture_dispatch.h"
#include "renderer_gl/opengl_capabilities.h"
#include <cstdint>
namespace {
void gen_opengl_textures(std::uint32_t count, std::uint32_t* ids) noexcept
{
glGenTextures(static_cast<GLsizei>(count), reinterpret_cast<GLuint*>(ids));
}
void delete_opengl_textures(std::uint32_t count, const std::uint32_t* ids) noexcept
{
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));
@@ -227,24 +162,19 @@ bool TextureCube::create(int resolution) noexcept
destroy();
m_resolution = resolution;
const auto format = pp::renderer::gl::texture_format_for_channel_count(4U);
const auto texture = pp::renderer::gl::allocate_opengl_texture_cube(
const auto texture = pp::legacy::gl_texture::allocate_texture_cube(
pp::renderer::gl::OpenGlTextureCubeAllocation {
.resolution = m_resolution,
.internal_format = static_cast<std::int32_t>(format.internal_format),
.pixel_format = format.pixel_format,
.component_type = pp::renderer::gl::unsigned_byte_component_type(),
},
pp::renderer::gl::OpenGlTextureCubeAllocationDispatch {
.gen_textures = gen_opengl_textures,
.bind_texture = bind_opengl_texture,
.tex_image_2d = upload_opengl_texture_2d,
});
if (!texture.ok())
"TextureCube::create()");
if (texture == 0U)
{
LOG("TextureCube::create() failed because: %s", texture.status().message);
return;
}
m_cubetex_id = static_cast<GLuint>(texture.value());
m_cubetex_id = static_cast<GLuint>(texture);
});
return m_cubetex_id != 0;
}
@@ -259,13 +189,9 @@ void TextureCube::destroy() noexcept
for (std::size_t i = 0U; i < f.size(); ++i)
texture_ids[i] = static_cast<std::uint32_t>(f[i]);
texture_ids[f.size()] = static_cast<std::uint32_t>(id);
const auto status = pp::renderer::gl::delete_opengl_texture_objects(
pp::legacy::gl_texture::delete_texture_objects(
texture_ids,
pp::renderer::gl::OpenGlTexture2DDeleteDispatch {
.delete_textures = delete_opengl_textures,
});
if (!status.ok())
LOG("TextureCube::destroy() failed because: %s", status.message);
"TextureCube::destroy()");
});
m_cubetex_id = 0;
m_faces.fill(0);
@@ -276,13 +202,9 @@ void TextureCube::destroy() noexcept
void TextureCube::bind() const noexcept
{
assert(App::I->is_render_thread());
const auto status = pp::renderer::gl::bind_opengl_texture_cube(
pp::legacy::gl_texture::bind_texture_cube(
static_cast<std::uint32_t>(m_cubetex_id),
pp::renderer::gl::OpenGlTexture2DBindDispatch {
.bind_texture = bind_opengl_texture,
});
if (!status.ok())
LOG("TextureCube::bind() failed because: %s", status.message);
"TextureCube::bind()");
}
bool TextureManager::load(const char* path, bool generate_mipmaps)
@@ -344,7 +266,7 @@ Image Texture2D::get_image() const noexcept
.pixels = ret.m_data.get(),
},
pp::renderer::gl::OpenGlTexture2DReadbackDispatch {
.bind_texture = bind_opengl_texture,
.bind_texture = pp::legacy::gl_texture::bind_opengl_texture,
.gen_framebuffers = gen_opengl_framebuffers,
.get_integer = query_opengl_integer,
.bind_framebuffer = bind_opengl_framebuffer,
@@ -413,7 +335,7 @@ bool Texture2D::create(int width, int height, GLint internal_format, GLint forma
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(
const auto texture = pp::legacy::gl_texture::allocate_texture_2d(
pp::renderer::gl::OpenGlTexture2DAllocation {
.width = width,
.height = height,
@@ -422,13 +344,8 @@ bool Texture2D::create(int width, int height, GLint internal_format, GLint forma
.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);
"Texture2D::create()");
if (texture == 0U) {
return;
}
@@ -436,7 +353,7 @@ bool Texture2D::create(int width, int height, GLint internal_format, GLint forma
m_height = height;
m_format = format;
m_iformat = internal_format;
m_tex = static_cast<GLuint>(texture.value());
m_tex = static_cast<GLuint>(texture);
});
return true;
}
@@ -458,14 +375,9 @@ void Texture2D::create_mipmaps()
{
App::I->render_task([this]
{
const auto status = pp::renderer::gl::generate_opengl_texture_2d_mipmaps(
if (!pp::legacy::gl_texture::generate_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);
"Texture2D::create_mipmaps()")) {
return;
}
has_mips = true;
@@ -515,13 +427,9 @@ void Texture2D::destroy()
{
App::I->render_task_async([id = m_tex]
{
const auto status = pp::renderer::gl::delete_opengl_texture_2d(
pp::legacy::gl_texture::delete_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);
"Texture2D::destroy()");
});
m_tex = 0;
}
@@ -530,32 +438,24 @@ void Texture2D::destroy()
void Texture2D::bind() const
{
assert(App::I->is_render_thread());
const auto status = pp::renderer::gl::bind_opengl_texture_2d(
pp::legacy::gl_texture::bind_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);
"Texture2D::bind()");
}
void Texture2D::unbind() const
{
assert(App::I->is_render_thread());
const auto status = pp::renderer::gl::bind_opengl_texture_2d(
pp::legacy::gl_texture::bind_texture_2d(
0U,
pp::renderer::gl::OpenGlTexture2DBindDispatch {
.bind_texture = bind_opengl_texture,
});
if (!status.ok())
LOG("Texture2D::unbind() failed because: %s", status.message);
"Texture2D::unbind()");
}
void Texture2D::update(const uint8_t* data)
{
App::I->render_task([this, data]
{
const auto status = pp::renderer::gl::update_opengl_texture_2d(
pp::legacy::gl_texture::update_texture_2d(
pp::renderer::gl::OpenGlTexture2DUpdate {
.texture_id = static_cast<std::uint32_t>(m_tex),
.width = m_width,
@@ -564,12 +464,7 @@ void Texture2D::update(const uint8_t* data)
.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);
"Texture2D::update()");
});
}