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

LibGL+LibGPU+LibSoftGPU: Implement GL_GENERATE_MIPMAP

We can now generate texture mipmaps on the fly if the client requests
it. This fixes the missing textures in our PrBoom+ port.
This commit is contained in:
Jelle Raaijmakers 2022-09-04 22:18:16 +02:00 committed by Linus Groh
parent dda5987684
commit 1540c56e6c
10 changed files with 148 additions and 82 deletions

View file

@ -1,25 +0,0 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGL/GL/gl.h>
namespace GL {
class MipMap {
public:
void set_width(GLsizei width) { m_width = width; }
void set_height(GLsizei height) { m_height = height; }
GLsizei width() const { return m_width; }
GLsizei height() const { return m_height; }
private:
GLsizei m_width { 0 };
GLsizei m_height { 0 };
};
}

View file

@ -21,6 +21,7 @@ public:
virtual bool is_texture_3d() const { return false; }
virtual bool is_cube_map() const { return false; }
RefPtr<GPU::Image> device_image() const { return m_device_image; }
RefPtr<GPU::Image> device_image() { return m_device_image; }
void set_device_image(RefPtr<GPU::Image> image) { m_device_image = image; }

View file

@ -19,20 +19,15 @@ void Texture2D::download_texture_data(GLuint lod, GPU::ImageDataLayout output_la
void Texture2D::upload_texture_data(GLuint lod, GLenum internal_format, GPU::ImageDataLayout input_layout, GLvoid const* pixels)
{
// 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..
auto& mip = m_mipmaps[lod];
m_internal_format = internal_format;
mip.set_width(input_layout.selection.width);
mip.set_height(input_layout.selection.height);
// No pixel data was supplied; leave the texture memory uninitialized.
if (pixels == nullptr)
return;
replace_sub_texture_data(lod, input_layout, { 0, 0, 0 }, pixels);
if (lod == 0 && m_generate_mipmaps)
device_image()->regenerate_mipmaps();
}
void Texture2D::replace_sub_texture_data(GLuint lod, GPU::ImageDataLayout input_layout, Vector3<i32> const& output_offset, GLvoid const* pixels)
@ -42,7 +37,18 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GPU::ImageDataLayout input_
// once used for rendering for the first time.
VERIFY(!device_image().is_null());
device_image()->write_texels(0, lod, output_offset, pixels, input_layout);
device_image()->write_texels(lod, output_offset, pixels, input_layout);
if (lod == 0 && m_generate_mipmaps)
device_image()->regenerate_mipmaps();
}
void Texture2D::set_generate_mipmaps(bool generate_mipmaps)
{
if (m_generate_mipmaps == generate_mipmaps)
return;
m_generate_mipmaps = generate_mipmaps;
if (generate_mipmaps && !device_image().is_null())
device_image()->regenerate_mipmaps();
}
}

View file

@ -10,10 +10,8 @@
#include "Texture.h"
#include <AK/Array.h>
#include <AK/IntegralMath.h>
#include <LibGL/GL/gl.h>
#include <LibGL/Tex/MipMap.h>
#include <LibGL/Tex/Sampler2D.h>
#include <LibGPU/ImageDataLayout.h>
@ -31,24 +29,17 @@ public:
void upload_texture_data(GLuint lod, GLenum internal_format, GPU::ImageDataLayout input_layout, GLvoid const* pixels);
void replace_sub_texture_data(GLuint lod, GPU::ImageDataLayout input_layout, Vector3<i32> const& output_offset, GLvoid const* pixels);
MipMap const& mipmap(unsigned lod) const
{
if (lod >= m_mipmaps.size())
return m_mipmaps.last();
return m_mipmaps.at(lod);
}
void set_generate_mipmaps(bool generate_mipmaps);
GLenum internal_format() const { return m_internal_format; }
Sampler2D const& sampler() const { return m_sampler; }
Sampler2D& sampler() { return m_sampler; }
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 : m_mipmaps.at(level).height(); }
int width_at_lod(unsigned level) const { return static_cast<int>(device_image()->width_at_level(level)); }
int height_at_lod(unsigned level) const { return static_cast<int>(device_image()->height_at_level(level)); }
int depth_at_lod(unsigned level) const { return static_cast<int>(device_image()->depth_at_level(level)); }
private:
// FIXME: Mipmaps are currently unused, but we have the plumbing for it at least
Array<MipMap, LOG2_MAX_TEXTURE_SIZE> m_mipmaps;
bool m_generate_mipmaps { false };
GLenum m_internal_format;
Sampler2D m_sampler;
};