1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:07:35 +00:00

LibGL: Remove image storage from MipMap

Images are stored on the device side. Texture2D and MipMap are now only
used to store imformation about the texture and reference to the device
image.
This commit is contained in:
Stephan Unverwerth 2021-12-23 13:52:19 +01:00 committed by Brian Gianforcaro
parent 4c944eaa41
commit 5e9d99474d
3 changed files with 34 additions and 114 deletions

View file

@ -7,43 +7,19 @@
#pragma once #pragma once
#include <AK/Vector.h>
#include <LibGL/GL/gl.h> #include <LibGL/GL/gl.h>
#include <LibGfx/Vector4.h>
namespace GL { namespace GL {
class MipMap { class MipMap {
public: public:
MipMap() = default;
~MipMap() = default;
void set_width(GLsizei width) { m_width = width; } void set_width(GLsizei width) { m_width = width; }
void set_height(GLsizei height) { m_height = height; } void set_height(GLsizei height) { m_height = height; }
GLsizei width() const { return m_width; } GLsizei width() const { return m_width; }
GLsizei height() const { return m_height; } GLsizei height() const { return m_height; }
Vector<u32>& pixel_data() { return m_pixel_data; }
const Vector<u32>& pixel_data() const { return m_pixel_data; }
FloatVector4 texel(unsigned x, unsigned y) const
{
if (x >= (unsigned)m_width || y >= (unsigned)m_height)
return { 0, 0, 0, 0 };
u32 texel = m_pixel_data.at(y * m_width + x);
return {
((texel >> 16) & 0xff) / 255.f,
((texel >> 8) & 0xff) / 255.f,
(texel & 0xff) / 255.f,
((texel >> 24) & 0xff) / 255.f
};
}
private: private:
GLsizei m_width { 0 }; GLsizei m_width { 0 };
GLsizei m_height { 0 }; GLsizei m_height { 0 };
Vector<u32> m_pixel_data;
}; };
} }

View file

@ -19,7 +19,6 @@ void Texture2D::upload_texture_data(GLuint lod, GLint internal_format, GLsizei w
auto& mip = m_mipmaps[lod]; auto& mip = m_mipmaps[lod];
mip.set_width(width); mip.set_width(width);
mip.set_height(height); mip.set_height(height);
mip.pixel_data().resize(width * height);
// No pixel data was supplied leave the texture memory uninitialized. // No pixel data was supplied leave the texture memory uninitialized.
if (pixels == nullptr) if (pixels == nullptr)
@ -39,6 +38,12 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse
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);
// FIXME: We currently depend on the first glTexImage2D call to attach an image to mipmap level 0, which initializes the GPU image
// Ideally we would create separate GPU images for each level and merge them into a final image
// once used for rendering for the first time.
if (device_image().is_null())
return;
u8 pixel_size_bytes; u8 pixel_size_bytes;
switch (type) { switch (type) {
case GL_UNSIGNED_BYTE: case GL_UNSIGNED_BYTE:
@ -54,91 +59,38 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse
// 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;
size_t const physical_width_bytes = physical_width * pixel_size_bytes; size_t const physical_width_bytes = physical_width * pixel_size_bytes;
size_t const row_remainder_bytes = (physical_width - width) * pixel_size_bytes
+ (byte_alignment - physical_width_bytes % byte_alignment) % byte_alignment;
u8 const* pixel_byte_array = reinterpret_cast<u8 const*>(pixels); SoftGPU::ImageDataLayout layout;
layout.column_stride = pixel_size_bytes;
layout.row_stride = physical_width_bytes + (byte_alignment - physical_width_bytes % byte_alignment) % byte_alignment;
layout.depth_stride = 0;
auto get_next_pixel = [type, format](u8 const** pixels) -> u32 { if (type == GL_UNSIGNED_SHORT_5_6_5) {
// Split bytes up into RGBA components layout.format = SoftGPU::ImageFormat::RGB565;
u8 c1, c2, c3, c4; } else if (type == GL_UNSIGNED_BYTE) {
switch (type) { if (format == GL_RGB)
case GL_UNSIGNED_BYTE: layout.format = SoftGPU::ImageFormat::RGB888;
c1 = *((*pixels)++); else if (format == GL_BGR)
c2 = *((*pixels)++); layout.format = SoftGPU::ImageFormat::BGR888;
c3 = *((*pixels)++); else if (format == GL_RGBA)
if (format == GL_RGBA || format == GL_BGRA) { layout.format = SoftGPU::ImageFormat::RGBA8888;
c4 = *((*pixels)++); else if (format == GL_BGRA)
} else { layout.format = SoftGPU::ImageFormat::BGRA8888;
c4 = 255; }
}
break;
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;
}
default:
VERIFY_NOT_REACHED();
}
// Reorder components into BGRA pixel Vector3<unsigned> offset {
switch (format) { static_cast<unsigned>(xoffset),
case GL_BGR: static_cast<unsigned>(yoffset),
case GL_BGRA: 0
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();
}
}; };
for (auto y = yoffset; y < yoffset + height; y++) { Vector3<unsigned> size {
for (auto x = xoffset; x < xoffset + width; x++) { static_cast<unsigned>(width),
u32 pixel = get_next_pixel(&pixel_byte_array); static_cast<unsigned>(height),
mip.pixel_data()[y * mip.width() + x] = pixel; 1
} };
pixel_byte_array += row_remainder_bytes; device_image()->write_texels(0, lod, offset, size, pixels, layout);
}
if (!device_image().is_null()) {
SoftGPU::ImageDataLayout layout;
layout.column_stride = pixel_size_bytes;
layout.row_stride = physical_width_bytes + (byte_alignment - physical_width_bytes % byte_alignment) % byte_alignment;
layout.depth_stride = 0;
if (type == GL_UNSIGNED_SHORT_5_6_5) {
layout.format = SoftGPU::ImageFormat::RGB565;
} else if (type == GL_UNSIGNED_BYTE) {
if (format == GL_RGB)
layout.format = SoftGPU::ImageFormat::RGB888;
else if (format == GL_BGR)
layout.format = SoftGPU::ImageFormat::BGR888;
else if (format == GL_RGBA)
layout.format = SoftGPU::ImageFormat::RGBA8888;
else if (format == GL_BGRA)
layout.format = SoftGPU::ImageFormat::BGRA8888;
}
Vector3<unsigned> offset {
static_cast<unsigned>(xoffset),
static_cast<unsigned>(yoffset),
0
};
Vector3<unsigned> size {
static_cast<unsigned>(width),
static_cast<unsigned>(height),
1
};
device_image()->write_texels(0, lod, offset, size, pixels, layout);
}
} }
} }

View file

@ -44,16 +44,8 @@ public:
Sampler2D const& sampler() const { return m_sampler; } Sampler2D const& sampler() const { return m_sampler; }
Sampler2D& sampler() { return m_sampler; } Sampler2D& sampler() { return m_sampler; }
int width_at_lod(unsigned level) const { return (level >= m_mipmaps.size()) ? 0 : max(1, m_mipmaps.at(level).width() >> level); } int width_at_lod(unsigned level) const { return (level >= m_mipmaps.size()) ? 0 : m_mipmaps.at(level).width(); }
int height_at_lod(unsigned level) const { return (level >= m_mipmaps.size()) ? 0 : max(1, m_mipmaps.at(level).height() >> level); } int height_at_lod(unsigned level) const { return (level >= m_mipmaps.size()) ? 0 : m_mipmaps.at(level).height(); }
private:
template<typename TCallback>
void swizzle(Vector<u32>& pixels, TCallback&& callback)
{
for (auto& pixel : pixels)
pixel = callback(pixel);
}
private: private:
// FIXME: Mipmaps are currently unused, but we have the plumbing for it at least // FIXME: Mipmaps are currently unused, but we have the plumbing for it at least