diff --git a/Userland/Libraries/LibGL/CMakeLists.txt b/Userland/Libraries/LibGL/CMakeLists.txt index 133f87229c8..3c9bb667253 100644 --- a/Userland/Libraries/LibGL/CMakeLists.txt +++ b/Userland/Libraries/LibGL/CMakeLists.txt @@ -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 diff --git a/Userland/Libraries/LibGL/GLContext.h b/Userland/Libraries/LibGL/GLContext.h index bcac8232ffe..f76ed24ef57 100644 --- a/Userland/Libraries/LibGL/GLContext.h +++ b/Userland/Libraries/LibGL/GLContext.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -373,7 +373,7 @@ private: return static_cast(default_texture.value()); } - TextureNameAllocator m_name_allocator; + NameAllocator m_texture_name_allocator; HashMap> m_allocated_textures; HashMap> m_default_textures; Vector m_texture_units; diff --git a/Userland/Libraries/LibGL/NameAllocator.cpp b/Userland/Libraries/LibGL/NameAllocator.cpp new file mode 100644 index 00000000000..ccd9350e5a8 --- /dev/null +++ b/Userland/Libraries/LibGL/NameAllocator.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +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); +} + +} diff --git a/Userland/Libraries/LibGL/NameAllocator.h b/Userland/Libraries/LibGL/NameAllocator.h new file mode 100644 index 00000000000..186f211b3fd --- /dev/null +++ b/Userland/Libraries/LibGL/NameAllocator.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +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 m_free_names; + GLuint m_last_id { 1 }; +}; + +} diff --git a/Userland/Libraries/LibGL/Tex/NameAllocator.cpp b/Userland/Libraries/LibGL/Tex/NameAllocator.cpp deleted file mode 100644 index f1cd1be7580..00000000000 --- a/Userland/Libraries/LibGL/Tex/NameAllocator.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2021, Jesse Buhagiar - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include - -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); -} - -} diff --git a/Userland/Libraries/LibGL/Tex/NameAllocator.h b/Userland/Libraries/LibGL/Tex/NameAllocator.h deleted file mode 100644 index c40a88476b6..00000000000 --- a/Userland/Libraries/LibGL/Tex/NameAllocator.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2021, Jesse Buhagiar - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include - -namespace GL { - -class TextureNameAllocator { -public: - TextureNameAllocator() = default; - - void allocate(GLsizei count, GLuint* textures); - void free(GLuint texture); - -private: - Stack m_free_texture_names; - GLuint m_last_texture_id { 1 }; -}; - -} diff --git a/Userland/Libraries/LibGL/Texture.cpp b/Userland/Libraries/LibGL/Texture.cpp index 300183d3140..880cf2cd426 100644 --- a/Userland/Libraries/LibGL/Texture.cpp +++ b/Userland/Libraries/LibGL/Texture.cpp @@ -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) {