2021-05-13 22:18:17 +10:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
2021-08-11 19:57:22 +02:00
|
|
|
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
|
2021-05-13 22:18:17 +10:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Format.h>
|
|
|
|
|
#include <LibGL/GL/gl.h>
|
2021-05-29 23:34:40 +02:00
|
|
|
#include <LibGL/Tex/Texture2D.h>
|
2021-05-13 22:18:17 +10:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
namespace GL {
|
|
|
|
|
|
2021-08-21 14:49:27 +02:00
|
|
|
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)
|
2021-05-13 22:18:17 +10:00
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
// checks here to see if we support them; the program will simply fail to compile..
|
|
|
|
|
|
2021-08-18 13:25:48 +02:00
|
|
|
auto& mip = m_mipmaps[lod];
|
|
|
|
|
mip.set_width(width);
|
|
|
|
|
mip.set_height(height);
|
|
|
|
|
|
|
|
|
|
// No pixel data was supplied. Just allocate texture memory and leave it uninitialized.
|
2021-05-13 22:18:17 +10:00
|
|
|
if (pixels == nullptr) {
|
2021-08-18 13:25:48 +02:00
|
|
|
mip.pixel_data().resize(width * height);
|
|
|
|
|
return;
|
2021-05-13 22:18:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_internal_format = internal_format;
|
|
|
|
|
|
|
|
|
|
const u8* pixel_byte_array = reinterpret_cast<const u8*>(pixels);
|
|
|
|
|
|
2021-08-18 13:20:00 +02:00
|
|
|
mip.pixel_data().clear();
|
|
|
|
|
if (format == GL_RGBA) {
|
2021-08-21 14:49:27 +02:00
|
|
|
for (auto y = 0; y < height; y++) {
|
|
|
|
|
for (auto x = 0; x < 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pixels_per_row > 0) {
|
|
|
|
|
pixel_byte_array += (pixels_per_row - width) * 4;
|
|
|
|
|
}
|
2021-05-13 22:18:17 +10:00
|
|
|
}
|
2021-08-18 13:20:00 +02:00
|
|
|
} else if (format == GL_BGRA) {
|
2021-08-21 14:49:27 +02:00
|
|
|
for (auto y = 0; y < height; y++) {
|
|
|
|
|
for (auto x = 0; x < 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pixels_per_row > 0) {
|
|
|
|
|
pixel_byte_array += (pixels_per_row - width) * 4;
|
|
|
|
|
}
|
2021-05-13 22:18:17 +10:00
|
|
|
}
|
2021-08-18 13:20:00 +02:00
|
|
|
} else if (format == GL_BGR) {
|
2021-08-21 14:49:27 +02:00
|
|
|
for (auto y = 0; y < height; y++) {
|
|
|
|
|
for (auto x = 0; x < 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pixels_per_row > 0) {
|
|
|
|
|
pixel_byte_array += (pixels_per_row - width) * 3;
|
|
|
|
|
}
|
2021-05-13 22:18:17 +10:00
|
|
|
}
|
2021-08-18 13:20:00 +02:00
|
|
|
} else if (format == GL_RGB) {
|
2021-08-21 14:49:27 +02:00
|
|
|
for (auto y = 0; y < height; y++) {
|
|
|
|
|
for (auto x = 0; x < 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pixels_per_row > 0) {
|
|
|
|
|
pixel_byte_array += (pixels_per_row - width) * 3;
|
|
|
|
|
}
|
2021-05-13 22:18:17 +10:00
|
|
|
}
|
2021-08-18 13:20:00 +02:00
|
|
|
} else {
|
2021-05-13 22:18:17 +10:00
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 19:57:22 +02:00
|
|
|
MipMap const& Texture2D::mipmap(unsigned lod) const
|
2021-05-13 22:18:17 +10:00
|
|
|
{
|
2021-08-11 19:57:22 +02:00
|
|
|
if (lod >= m_mipmaps.size())
|
|
|
|
|
return m_mipmaps.at(m_mipmaps.size() - 1);
|
2021-05-13 22:18:17 +10:00
|
|
|
|
2021-08-11 19:57:22 +02:00
|
|
|
return m_mipmaps.at(lod);
|
2021-05-13 22:18:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|