LibGL: Refactor TextureNameAllocator to a more general NameAllocator
This functionality can also be used for other types of objects.
This commit is contained in:
parent
9a0bb8212a
commit
892006218a
Notes:
sideshowbarker
2024-07-17 04:03:33 +09:00
Author: https://github.com/cflip Commit: https://github.com/SerenityOS/serenity/commit/892006218a Pull-request: https://github.com/SerenityOS/serenity/pull/16061 Reviewed-by: https://github.com/gmta ✅
7 changed files with 67 additions and 61 deletions
|
@ -8,8 +8,8 @@ set(SOURCES
|
|||
Lighting.cpp
|
||||
List.cpp
|
||||
Matrix.cpp
|
||||
NameAllocator.cpp
|
||||
Stencil.cpp
|
||||
Tex/NameAllocator.cpp
|
||||
Tex/Texture2D.cpp
|
||||
Texture.cpp
|
||||
Vertex.cpp
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
#include <AK/Tuple.h>
|
||||
#include <AK/Variant.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibGL/Tex/NameAllocator.h>
|
||||
#include <LibGL/NameAllocator.h>
|
||||
#include <LibGL/Tex/Texture.h>
|
||||
#include <LibGL/Tex/TextureUnit.h>
|
||||
#include <LibGPU/Device.h>
|
||||
|
@ -373,7 +373,7 @@ private:
|
|||
return static_cast<T*>(default_texture.value());
|
||||
}
|
||||
|
||||
TextureNameAllocator m_name_allocator;
|
||||
NameAllocator m_texture_name_allocator;
|
||||
HashMap<GLuint, RefPtr<Texture>> m_allocated_textures;
|
||||
HashMap<GLenum, RefPtr<Texture>> m_default_textures;
|
||||
Vector<TextureUnit> m_texture_units;
|
||||
|
|
35
Userland/Libraries/LibGL/NameAllocator.cpp
Normal file
35
Userland/Libraries/LibGL/NameAllocator.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGL/NameAllocator.h>
|
||||
|
||||
namespace GL {
|
||||
|
||||
void NameAllocator::allocate(GLsizei count, GLuint* names)
|
||||
{
|
||||
for (auto i = 0; i < count; ++i) {
|
||||
if (!m_free_names.is_empty()) {
|
||||
names[i] = m_free_names.top();
|
||||
m_free_names.pop();
|
||||
} else {
|
||||
// We're out of free previously allocated names. Let's allocate a new contiguous amount from the
|
||||
// last known texture name
|
||||
names[i] = m_last_id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NameAllocator::free(GLuint name)
|
||||
{
|
||||
m_free_names.push(name);
|
||||
}
|
||||
|
||||
bool NameAllocator::has_allocated_name(GLuint name) const
|
||||
{
|
||||
return name < m_last_id && !m_free_names.contains_slow(name);
|
||||
}
|
||||
|
||||
}
|
27
Userland/Libraries/LibGL/NameAllocator.h
Normal file
27
Userland/Libraries/LibGL/NameAllocator.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Stack.h>
|
||||
#include <LibGL/GL/gl.h>
|
||||
|
||||
namespace GL {
|
||||
|
||||
class NameAllocator {
|
||||
public:
|
||||
NameAllocator() = default;
|
||||
|
||||
void allocate(GLsizei count, GLuint* names);
|
||||
void free(GLuint name);
|
||||
bool has_allocated_name(GLuint name) const;
|
||||
|
||||
private:
|
||||
Stack<GLuint, 512> m_free_names;
|
||||
GLuint m_last_id { 1 };
|
||||
};
|
||||
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGL/Tex/NameAllocator.h>
|
||||
|
||||
namespace GL {
|
||||
|
||||
void TextureNameAllocator::allocate(GLsizei count, GLuint* textures)
|
||||
{
|
||||
for (auto i = 0; i < count; ++i) {
|
||||
if (!m_free_texture_names.is_empty()) {
|
||||
textures[i] = m_free_texture_names.top();
|
||||
m_free_texture_names.pop();
|
||||
} else {
|
||||
// We're out of free previously allocated names. Let's allocate a new contiguous amount from the
|
||||
// last known texture name
|
||||
textures[i] = m_last_texture_id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextureNameAllocator::free(GLuint texture)
|
||||
{
|
||||
m_free_texture_names.push(texture);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Stack.h>
|
||||
#include <LibGL/GL/gl.h>
|
||||
|
||||
namespace GL {
|
||||
|
||||
class TextureNameAllocator {
|
||||
public:
|
||||
TextureNameAllocator() = default;
|
||||
|
||||
void allocate(GLsizei count, GLuint* textures);
|
||||
void free(GLuint texture);
|
||||
|
||||
private:
|
||||
Stack<GLuint, 512> m_free_texture_names;
|
||||
GLuint m_last_texture_id { 1 };
|
||||
};
|
||||
|
||||
}
|
|
@ -190,7 +190,7 @@ void GLContext::gl_delete_textures(GLsizei n, GLuint const* textures)
|
|||
if (texture_object == m_allocated_textures.end() || texture_object->value.is_null())
|
||||
continue;
|
||||
|
||||
m_name_allocator.free(name);
|
||||
m_texture_name_allocator.free(name);
|
||||
|
||||
auto texture = texture_object->value;
|
||||
|
||||
|
@ -211,7 +211,7 @@ void GLContext::gl_gen_textures(GLsizei n, GLuint* textures)
|
|||
RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
|
||||
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
|
||||
|
||||
m_name_allocator.allocate(n, textures);
|
||||
m_texture_name_allocator.allocate(n, textures);
|
||||
|
||||
// Initialize all texture names with a nullptr
|
||||
for (auto i = 0; i < n; ++i) {
|
||||
|
|
Loading…
Add table
Reference in a new issue