1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:47:34 +00:00

LibGL: Implement glTexSubImage2D

This commit is contained in:
Stephan Unverwerth 2021-08-31 22:35:52 +02:00 committed by Ali Mohammad Pur
parent 1aed453d8c
commit e7d3483618
7 changed files with 63 additions and 19 deletions

View file

@ -12,7 +12,7 @@
namespace GL {
void Texture2D::upload_texture_data(GLenum, GLint lod, GLint internal_format, GLsizei width, GLsizei height, GLint, GLenum format, GLenum, const GLvoid* pixels, size_t pixels_per_row)
void Texture2D::upload_texture_data(GLuint lod, GLint internal_format, GLsizei width, GLsizei height, GLint, GLenum format, GLenum type, const GLvoid* pixels, size_t pixels_per_row)
{
// NOTE: Some target, format, and internal formats are currently unsupported.
// Considering we control this library, and `gl.h` itself, we don't need to add any
@ -21,28 +21,39 @@ void Texture2D::upload_texture_data(GLenum, GLint lod, GLint internal_format, GL
auto& mip = m_mipmaps[lod];
mip.set_width(width);
mip.set_height(height);
mip.pixel_data().resize(width * height);
// No pixel data was supplied. Just allocate texture memory and leave it uninitialized.
// No pixel data was supplied leave the texture memory uninitialized.
if (pixels == nullptr) {
mip.pixel_data().resize(width * height);
return;
}
m_internal_format = internal_format;
replace_sub_texture_data(lod, 0, 0, width, height, format, type, pixels, pixels_per_row);
}
void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* pixels, size_t pixels_per_row)
{
auto& mip = m_mipmaps[lod];
// FIXME: We currently only support GL_UNSIGNED_BYTE pixel data
VERIFY(type == GL_UNSIGNED_BYTE);
VERIFY(lod < m_mipmaps.size());
VERIFY(xoffset >= 0 && yoffset >= 0 && xoffset + width <= mip.width() && yoffset + height <= mip.height());
const u8* pixel_byte_array = reinterpret_cast<const u8*>(pixels);
mip.pixel_data().clear();
if (format == GL_RGBA) {
for (auto y = 0; y < height; y++) {
for (auto x = 0; x < width; x++) {
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 = *pixel_byte_array++;
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
mip.pixel_data().append(pixel);
mip.pixel_data()[y * mip.width() + x] = pixel;
}
if (pixels_per_row > 0) {
@ -50,15 +61,15 @@ void Texture2D::upload_texture_data(GLenum, GLint lod, GLint internal_format, GL
}
}
} else if (format == GL_BGRA) {
for (auto y = 0; y < height; y++) {
for (auto x = 0; x < width; x++) {
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 = *pixel_byte_array++;
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
mip.pixel_data().append(pixel);
mip.pixel_data()[y * mip.width() + x] = pixel;
}
if (pixels_per_row > 0) {
@ -66,15 +77,15 @@ void Texture2D::upload_texture_data(GLenum, GLint lod, GLint internal_format, GL
}
}
} else if (format == GL_BGR) {
for (auto y = 0; y < height; y++) {
for (auto x = 0; x < width; x++) {
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);
mip.pixel_data().append(pixel);
mip.pixel_data()[y * mip.width() + x] = pixel;
}
if (pixels_per_row > 0) {
@ -82,15 +93,15 @@ void Texture2D::upload_texture_data(GLenum, GLint lod, GLint internal_format, GL
}
}
} else if (format == GL_RGB) {
for (auto y = 0; y < height; y++) {
for (auto x = 0; x < width; x++) {
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);
mip.pixel_data().append(pixel);
mip.pixel_data()[y * mip.width() + x] = pixel;
}
if (pixels_per_row > 0) {