Move OpenGL image format mapping

This commit is contained in:
2026-06-01 17:48:30 +02:00
parent 2754df9f46
commit 2e0ebd0e13
6 changed files with 81 additions and 9 deletions

View File

@@ -7,6 +7,14 @@ namespace {
constexpr std::uint32_t gl_unsigned_byte = 0x1401U;
constexpr std::uint32_t gl_float = 0x1406U;
constexpr std::uint32_t gl_half_float = 0x140BU;
constexpr std::uint32_t gl_red = 0x1903U;
constexpr std::uint32_t gl_rgb = 0x1907U;
constexpr std::uint32_t gl_rgba = 0x1908U;
constexpr std::uint32_t gl_rg = 0x8227U;
constexpr std::uint32_t gl_r8 = 0x8229U;
constexpr std::uint32_t gl_rg8 = 0x822BU;
constexpr std::uint32_t gl_rgb8 = 0x8051U;
constexpr std::uint32_t gl_rgba8 = 0x8058U;
constexpr std::uint32_t gl_rgba32f = 0x8814U;
constexpr std::uint32_t gl_rgba16f = 0x881AU;
@@ -68,4 +76,20 @@ std::uint32_t texture_upload_type_for_internal_format(std::uint32_t internal_for
}
}
OpenGlPixelFormat texture_format_for_channel_count(std::uint32_t channel_count) noexcept
{
switch (channel_count) {
case 1:
return OpenGlPixelFormat { .internal_format = gl_r8, .pixel_format = gl_red, .channel_count = 1 };
case 2:
return OpenGlPixelFormat { .internal_format = gl_rg8, .pixel_format = gl_rg, .channel_count = 2 };
case 3:
return OpenGlPixelFormat { .internal_format = gl_rgb8, .pixel_format = gl_rgb, .channel_count = 3 };
case 4:
return OpenGlPixelFormat { .internal_format = gl_rgba8, .pixel_format = gl_rgba, .channel_count = 4 };
default:
return OpenGlPixelFormat {};
}
}
}

View File

@@ -20,10 +20,17 @@ struct OpenGlCapabilities {
bool float16_textures = false;
};
struct OpenGlPixelFormat {
std::uint32_t internal_format = 0;
std::uint32_t pixel_format = 0;
std::uint32_t channel_count = 0;
};
[[nodiscard]] OpenGlCapabilities detect_opengl_capabilities(
std::span<const std::string_view> extensions,
OpenGlRuntime runtime) noexcept;
[[nodiscard]] std::uint32_t texture_upload_type_for_internal_format(std::uint32_t internal_format) noexcept;
[[nodiscard]] OpenGlPixelFormat texture_format_for_channel_count(std::uint32_t channel_count) noexcept;
}

View File

@@ -188,9 +188,16 @@ bool Texture2D::create(int width, int height, GLint internal_format, GLint forma
}
bool Texture2D::create(const Image& img)
{
static GLint formats[] = { GL_RED, GL_RG, GL_RGB, GL_RGBA };
static GLint iformats[] = { GL_R8, GL_RG8, GL_RGB8, GL_RGBA8 };
return create(img.width, img.height, iformats[img.comp - 1], formats[img.comp - 1], img.data());
const auto format = pp::renderer::gl::texture_format_for_channel_count(static_cast<std::uint32_t>(img.comp));
if (format.channel_count == 0U)
return false;
return create(
img.width,
img.height,
static_cast<GLint>(format.internal_format),
static_cast<GLint>(format.pixel_format),
img.data());
}
void Texture2D::create_mipmaps()