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

LibGL: Better handling of texture targets and default textures

We were lacking support for default textures (i.e. calling
`glBindTexture` with a `texture` argument of `0`) which caused our
Quake2 port to render red screens whenever a video was playing. Every
texture unit is now initialized with a default 2D texture.

Additionally, we had this concept of a "currently bound target" on our
texture units which is not how OpenGL wants us to handle targets.
Calling `glBindTexture` should set the texture for the provided target
only, making it sort of an alias for future operations on the same
target.

Finally, `glDeleteTextures` should not remove the bound texture from
the target in the texture unit, but it should reset it to the default
texture.
This commit is contained in:
Jelle Raaijmakers 2022-03-08 12:16:56 +01:00 committed by Andreas Kling
parent c87d3521e4
commit c21a3b3029
7 changed files with 140 additions and 156 deletions

View file

@ -20,12 +20,12 @@ void Texture2D::upload_texture_data(GLuint lod, GLint internal_format, GLsizei w
mip.set_width(width);
mip.set_height(height);
// No pixel data was supplied leave the texture memory uninitialized.
m_internal_format = internal_format;
// No pixel data was supplied; leave the texture memory uninitialized.
if (pixels == nullptr)
return;
m_internal_format = internal_format;
replace_sub_texture_data(lod, 0, 0, width, height, format, type, pixels, pixels_per_row, byte_alignment);
}

View file

@ -1,32 +0,0 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGL/GL/gl.h>
#include <LibGL/Tex/TextureUnit.h>
namespace GL {
void TextureUnit::bind_texture_to_target(GLenum texture_target, const RefPtr<Texture>& texture)
{
if (!texture) {
m_texture_target_2d = nullptr;
m_currently_bound_target = GL_NONE;
m_currently_bound_texture = nullptr;
return;
}
switch (texture_target) {
case GL_TEXTURE_2D:
m_texture_target_2d = static_ptr_cast<Texture2D>(texture);
m_currently_bound_target = GL_TEXTURE_2D;
m_currently_bound_texture = texture;
break;
default:
VERIFY_NOT_REACHED();
}
}
}

View file

@ -1,13 +1,13 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/IntrusiveList.h>
#include <AK/OwnPtr.h>
#include <AK/RefPtr.h>
#include <LibGL/Tex/Texture2D.h>
namespace GL {
@ -16,13 +16,8 @@ class TextureUnit {
public:
TextureUnit() = default;
void bind_texture_to_target(GLenum texture_target, const RefPtr<Texture>& texture);
RefPtr<Texture2D>& bound_texture_2d() const { return m_texture_target_2d; }
RefPtr<Texture>& bound_texture() const { return m_currently_bound_texture; }
GLenum currently_bound_target() const { return m_currently_bound_target; }
bool is_bound() const { return !m_currently_bound_texture.is_null(); }
RefPtr<Texture2D> texture_2d_target_texture() const { return m_texture_2d_target_texture; }
void set_texture_2d_target_texture(RefPtr<Texture2D> const& texture) { m_texture_2d_target_texture = texture; }
void set_env_mode(GLenum mode) { m_env_mode = mode; }
GLenum env_mode() const { return m_env_mode; }
@ -37,11 +32,11 @@ public:
void set_texture_cube_map_enabled(bool texture_cube_map_enabled) { m_texture_cube_map_enabled = texture_cube_map_enabled; }
private:
mutable RefPtr<Texture2D> m_texture_target_2d { nullptr };
mutable RefPtr<Texture> m_currently_bound_texture { nullptr };
GLenum m_currently_bound_target;
GLenum m_env_mode { GL_MODULATE };
// Bound textures
RefPtr<Texture2D> m_texture_2d_target_texture {};
// Texturing state per unit, in increasing priority:
bool m_texture_1d_enabled { false };
bool m_texture_2d_enabled { false };