summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGL/Tex/Texture2D.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibGL/Tex/Texture2D.cpp')
-rw-r--r--Userland/Libraries/LibGL/Tex/Texture2D.cpp38
1 files changed, 18 insertions, 20 deletions
diff --git a/Userland/Libraries/LibGL/Tex/Texture2D.cpp b/Userland/Libraries/LibGL/Tex/Texture2D.cpp
index 86e58e2784..9300b7a98e 100644
--- a/Userland/Libraries/LibGL/Tex/Texture2D.cpp
+++ b/Userland/Libraries/LibGL/Tex/Texture2D.cpp
@@ -5,14 +5,12 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/Format.h>
#include <LibGL/GL/gl.h>
#include <LibGL/Tex/Texture2D.h>
-#include <string.h>
namespace GL {
-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)
+void Texture2D::upload_texture_data(GLuint lod, GLint internal_format, GLsizei width, GLsizei height, GLint, GLenum format, GLenum type, const GLvoid* pixels, GLsizei pixels_per_row, u8 byte_alignment)
{
// 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
@@ -24,16 +22,15 @@ void Texture2D::upload_texture_data(GLuint lod, GLint internal_format, GLsizei w
mip.pixel_data().resize(width * height);
// No pixel data was supplied leave the texture memory uninitialized.
- if (pixels == nullptr) {
+ if (pixels == nullptr)
return;
- }
m_internal_format = internal_format;
- replace_sub_texture_data(lod, 0, 0, width, height, format, type, pixels, pixels_per_row);
+ replace_sub_texture_data(lod, 0, 0, width, height, format, type, pixels, pixels_per_row, byte_alignment);
}
-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)
+void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* pixels, GLsizei pixels_per_row, u8 byte_alignment)
{
auto& mip = m_mipmaps[lod];
@@ -41,8 +38,17 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse
VERIFY(type == GL_UNSIGNED_BYTE);
VERIFY(lod < m_mipmaps.size());
VERIFY(xoffset >= 0 && yoffset >= 0 && xoffset + width <= mip.width() && yoffset + height <= mip.height());
+ VERIFY(pixels_per_row == 0 || pixels_per_row >= xoffset + width);
+
+ // Calculate row offset at end to fit alignment
+ int const physical_width = pixels_per_row > 0 ? pixels_per_row : width;
+ u8 const component_size_bytes = sizeof(u8);
+ u8 const component_count = (format == GL_RGBA || format == GL_BGRA) ? 4 : 3;
+ 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;
- const u8* pixel_byte_array = reinterpret_cast<const u8*>(pixels);
+ u8 const* pixel_byte_array = reinterpret_cast<u8 const*>(pixels);
if (format == GL_RGBA) {
for (auto y = yoffset; y < yoffset + height; y++) {
@@ -56,9 +62,7 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse
mip.pixel_data()[y * mip.width() + x] = pixel;
}
- if (pixels_per_row > 0) {
- pixel_byte_array += (pixels_per_row - width) * 4;
- }
+ pixel_byte_array += row_remainder_bytes;
}
} else if (format == GL_BGRA) {
for (auto y = yoffset; y < yoffset + height; y++) {
@@ -72,9 +76,7 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse
mip.pixel_data()[y * mip.width() + x] = pixel;
}
- if (pixels_per_row > 0) {
- pixel_byte_array += (pixels_per_row - width) * 4;
- }
+ pixel_byte_array += row_remainder_bytes;
}
} else if (format == GL_BGR) {
for (auto y = yoffset; y < yoffset + height; y++) {
@@ -88,9 +90,7 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse
mip.pixel_data()[y * mip.width() + x] = pixel;
}
- if (pixels_per_row > 0) {
- pixel_byte_array += (pixels_per_row - width) * 3;
- }
+ pixel_byte_array += row_remainder_bytes;
}
} else if (format == GL_RGB) {
for (auto y = yoffset; y < yoffset + height; y++) {
@@ -104,9 +104,7 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse
mip.pixel_data()[y * mip.width() + x] = pixel;
}
- if (pixels_per_row > 0) {
- pixel_byte_array += (pixels_per_row - width) * 3;
- }
+ pixel_byte_array += row_remainder_bytes;
}
} else {
VERIFY_NOT_REACHED();