LibGL: Simplify Texture2D reading; add support for RGB565

This commit is contained in:
Jelle Raaijmakers 2021-12-02 02:17:05 +01:00 committed by Andreas Kling
parent 12c4491cca
commit 212334eaef
Notes: sideshowbarker 2024-07-17 22:50:07 +09:00
2 changed files with 58 additions and 59 deletions

View file

@ -851,7 +851,7 @@ void SoftwareGLContext::gl_tex_image_2d(GLenum target, GLint level, GLint intern
// We only support symbolic constants for now // We only support symbolic constants for now
RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA), GL_INVALID_ENUM); RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA), GL_INVALID_ENUM);
RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE, GL_INVALID_VALUE); RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE);
RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE); RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE); RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
// Check if width and height are a power of 2 // Check if width and height are a power of 2
@ -873,7 +873,8 @@ void SoftwareGLContext::gl_tex_sub_image_2d(GLenum target, GLint level, GLint xo
RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION); RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
// We only support symbolic constants for now // We only support symbolic constants for now
RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE, GL_INVALID_VALUE); RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_RGB), GL_INVALID_VALUE);
RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE);
RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE); RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE); RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);

View file

@ -34,79 +34,77 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse
{ {
auto& mip = m_mipmaps[lod]; auto& mip = m_mipmaps[lod];
// FIXME: We currently only support GL_UNSIGNED_BYTE pixel data // FIXME: We currently only support GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5 pixel data
VERIFY(type == GL_UNSIGNED_BYTE); VERIFY(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5);
VERIFY(xoffset >= 0 && yoffset >= 0 && xoffset + width <= mip.width() && yoffset + height <= mip.height()); VERIFY(xoffset >= 0 && yoffset >= 0 && xoffset + width <= mip.width() && yoffset + height <= mip.height());
VERIFY(pixels_per_row == 0 || pixels_per_row >= xoffset + width); VERIFY(pixels_per_row == 0 || pixels_per_row >= xoffset + width);
u8 pixel_size_bytes;
switch (type) {
case GL_UNSIGNED_BYTE:
pixel_size_bytes = (format == GL_RGBA || format == GL_BGRA) ? 4 : 3;
break;
case GL_UNSIGNED_SHORT_5_6_5:
pixel_size_bytes = sizeof(u16);
break;
default:
VERIFY_NOT_REACHED();
}
// Calculate row offset at end to fit alignment // Calculate row offset at end to fit alignment
int const physical_width = pixels_per_row > 0 ? pixels_per_row : width; int const physical_width = pixels_per_row > 0 ? pixels_per_row : width;
u8 const component_size_bytes = sizeof(u8); size_t const physical_width_bytes = physical_width * pixel_size_bytes;
u8 const component_count = (format == GL_RGBA || format == GL_BGRA) ? 4 : 3; size_t const row_remainder_bytes = (physical_width - width) * pixel_size_bytes
size_t const physical_width_bytes = physical_width * component_count * component_size_bytes;
size_t const row_remainder_bytes = (physical_width - width) * component_count * component_size_bytes
+ (byte_alignment - physical_width_bytes % byte_alignment) % byte_alignment; + (byte_alignment - physical_width_bytes % byte_alignment) % byte_alignment;
u8 const* pixel_byte_array = reinterpret_cast<u8 const*>(pixels); u8 const* pixel_byte_array = reinterpret_cast<u8 const*>(pixels);
if (format == GL_RGBA) { auto get_next_pixel = [type, format](u8 const** pixels) -> u32 {
for (auto y = yoffset; y < yoffset + height; y++) { // Split bytes up into RGBA components
for (auto x = xoffset; x < xoffset + width; x++) { u8 c1, c2, c3, c4;
u32 r = *pixel_byte_array++; switch (type) {
u32 g = *pixel_byte_array++; case GL_UNSIGNED_BYTE:
u32 b = *pixel_byte_array++; c1 = *((*pixels)++);
u32 a = *pixel_byte_array++; c2 = *((*pixels)++);
c3 = *((*pixels)++);
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b); if (format == GL_RGBA || format == GL_BGRA) {
mip.pixel_data()[y * mip.width() + x] = pixel; c4 = *((*pixels)++);
} else {
c4 = 255;
} }
break;
pixel_byte_array += row_remainder_bytes; case GL_UNSIGNED_SHORT_5_6_5: {
u16 const s = *reinterpret_cast<u16 const*>((*pixels) += 2);
c1 = (s & 0xf800) >> 8;
c2 = (s & 0x7e0) >> 3;
c3 = (s & 0x1f) << 3;
c4 = 255;
break;
} }
} else if (format == GL_BGRA) { default:
for (auto y = yoffset; y < yoffset + height; y++) { VERIFY_NOT_REACHED();
for (auto x = xoffset; x < xoffset + width; x++) {
u32 b = *pixel_byte_array++;
u32 g = *pixel_byte_array++;
u32 r = *pixel_byte_array++;
u32 a = *pixel_byte_array++;
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
mip.pixel_data()[y * mip.width() + x] = pixel;
}
pixel_byte_array += row_remainder_bytes;
} }
} else if (format == GL_BGR) {
for (auto y = yoffset; y < yoffset + height; y++) {
for (auto x = xoffset; x < xoffset + width; x++) {
u32 b = *pixel_byte_array++;
u32 g = *pixel_byte_array++;
u32 r = *pixel_byte_array++;
u32 a = 255;
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b); // Reorder components into BGRA pixel
mip.pixel_data()[y * mip.width() + x] = pixel; switch (format) {
} case GL_BGR:
case GL_BGRA:
pixel_byte_array += row_remainder_bytes; return ((c4 << 24) | (c3 << 16) | (c2 << 8) | c1);
case GL_RGB:
case GL_RGBA:
return ((c4 << 24) | (c1 << 16) | (c2 << 8) | c3);
default:
VERIFY_NOT_REACHED();
} }
} else if (format == GL_RGB) { };
for (auto y = yoffset; y < yoffset + height; y++) {
for (auto x = xoffset; x < xoffset + width; x++) {
u32 r = *pixel_byte_array++;
u32 g = *pixel_byte_array++;
u32 b = *pixel_byte_array++;
u32 a = 255;
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b); for (auto y = yoffset; y < yoffset + height; y++) {
mip.pixel_data()[y * mip.width() + x] = pixel; for (auto x = xoffset; x < xoffset + width; x++) {
} u32 pixel = get_next_pixel(&pixel_byte_array);
mip.pixel_data()[y * mip.width() + x] = pixel;
pixel_byte_array += row_remainder_bytes;
} }
} else {
VERIFY_NOT_REACHED(); pixel_byte_array += row_remainder_bytes;
} }
} }