From 7c03d93c367b842c464dca30e121bc4c20547c36 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 1 Jan 2022 16:44:19 +0000 Subject: Generic solution for glGen/glDel arrays, then tidy-up the uses --- gfx/models/mesh.cpp | 3 +- gfx/models/mesh.h | 3 +- gfx/models/texture.cpp | 10 ++----- gfx/models/texture.h | 10 ++----- lib/glArrays.cpp | 21 ++++++++++++++ lib/glArrays.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/glBuffers.cpp | 13 --------- lib/glBuffers.h | 64 ------------------------------------------ lib/glVertexArrays.cpp | 13 --------- lib/glVertexArrays.h | 64 ------------------------------------------ test/Jamfile.jam | 1 + test/test-lib.cpp | 50 +++++++++++++++++++++++++++++++++ ui/icon.cpp | 10 ++----- ui/icon.h | 10 ++----- ui/iconButton.cpp | 3 +- ui/iconButton.h | 3 +- ui/text.cpp | 3 +- ui/text.h | 3 +- 18 files changed, 164 insertions(+), 196 deletions(-) create mode 100644 lib/glArrays.cpp create mode 100644 lib/glArrays.h delete mode 100644 lib/glBuffers.cpp delete mode 100644 lib/glBuffers.h delete mode 100644 lib/glVertexArrays.cpp delete mode 100644 lib/glVertexArrays.h create mode 100644 test/test-lib.cpp diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index 09c2872..f13e242 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -1,6 +1,5 @@ #include "mesh.h" -#include "glBuffers.h" -#include "glVertexArrays.h" +#include "glArrays.h" #include "vertex.hpp" #include diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h index 4e6cec8..9b74f48 100644 --- a/gfx/models/mesh.h +++ b/gfx/models/mesh.h @@ -2,8 +2,7 @@ #define MESH_INCLUDED_H #include -#include -#include +#include #include #include diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index cd275e8..612f0a0 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -1,4 +1,6 @@ #include "texture.h" +#include "glArrays.h" +#include #include #include #include @@ -6,11 +8,10 @@ Cache Texture::cachedTexture; -Texture::Texture(const std::filesystem::path & fileName) : m_texture {} +Texture::Texture(const std::filesystem::path & fileName) { const Image tex {Resource::mapPath(fileName).c_str(), STBI_rgb_alpha}; - glGenTextures(1, &m_texture); glBindTexture(GL_TEXTURE_2D, m_texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -22,11 +23,6 @@ Texture::Texture(const std::filesystem::path & fileName) : m_texture {} GL_RGBA, GL_UNSIGNED_BYTE, tex.data.data()); } -Texture::~Texture() -{ - glDeleteTextures(1, &m_texture); -} - void Texture::Bind() const { diff --git a/gfx/models/texture.h b/gfx/models/texture.h index 8bbba85..6dbe9af 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -1,9 +1,8 @@ #ifndef TEXTURE_H #define TEXTURE_H -#include #include -#include +#include template class Cache; @@ -11,17 +10,12 @@ class Texture { public: explicit Texture(const std::filesystem::path & fileName); - virtual ~Texture(); - - NO_COPY(Texture); - NO_MOVE(Texture); - static Cache cachedTexture; void Bind() const; private: - GLuint m_texture; + glTexture m_texture; }; #endif diff --git a/lib/glArrays.cpp b/lib/glArrays.cpp new file mode 100644 index 0000000..7c5b2ea --- /dev/null +++ b/lib/glArrays.cpp @@ -0,0 +1,21 @@ +#include "glArrays.h" +#include + +// Base +static_assert(!std::is_default_constructible_v>); +static_assert(!std::is_copy_constructible_v>); +static_assert(!std::is_copy_assignable_v>); +static_assert(std::is_nothrow_move_constructible_v>); +static_assert(std::is_nothrow_move_assignable_v>); + +// Specialisations (glBuffer is an example of the typedef) +static_assert(std::is_nothrow_default_constructible_v); +static_assert(!std::is_trivially_default_constructible_v); +static_assert(std::is_nothrow_destructible_v); +static_assert(!std::is_trivially_destructible_v); +static_assert(std::is_default_constructible_v); +static_assert(!std::is_copy_constructible_v); +static_assert(!std::is_copy_assignable_v); +static_assert(std::is_nothrow_move_constructible_v); +static_assert(std::is_nothrow_move_assignable_v); +static_assert(sizeof(glBuffer) == sizeof(GLuint)); diff --git a/lib/glArrays.h b/lib/glArrays.h new file mode 100644 index 0000000..ed05eeb --- /dev/null +++ b/lib/glArrays.h @@ -0,0 +1,76 @@ +#pragma once + +#include +#include // IWYU pragma: keep +#include +#include +#include + +template class glArraysBase { + static_assert(N > 0); + +public: + ~glArraysBase() = default; + NO_COPY(glArraysBase); + CUSTOM_MOVE(glArraysBase); + + // NOLINTNEXTLINE(hicpp-explicit-conversions) + inline operator GLuint() const + { + static_assert(N == 1, "Implicit cast only if N == 1"); + return ids.front(); + } + + inline auto + operator[](size_t n) const + { + return ids[n]; + } + + constexpr static auto size {N}; + +protected: + glArraysBase() noexcept = default; + std::array ids {}; +}; + +template inline glArraysBase::glArraysBase(glArraysBase && src) noexcept : ids {src.ids} +{ + std::fill(src.ids.begin(), src.ids.end(), 0); +} + +template +inline glArraysBase & +glArraysBase::operator=(glArraysBase && src) noexcept +{ + ids = src.ids; + std::fill(src.ids.begin(), src.ids.end(), 0); + return *this; +} + +template class glArrays : public glArraysBase { +public: + using glArraysBase::glArraysBase; + using glArraysBase::operator=; + + DEFAULT_MOVE_COPY(glArrays); + + inline glArrays() noexcept + { + (*Gen)(N, this->ids.data()); + } + + inline ~glArrays() noexcept + { + if (this->ids.front()) { + (*Del)(N, this->ids.data()); + } + } +}; + +template using glVertexArrays = glArrays; +using glVertexArray = glVertexArrays<1>; +template using glBuffers = glArrays; +using glBuffer = glBuffers<1>; +template using glTextures = glArrays; +using glTexture = glTextures<1>; diff --git a/lib/glBuffers.cpp b/lib/glBuffers.cpp deleted file mode 100644 index cb5ab2a..0000000 --- a/lib/glBuffers.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "glBuffers.h" - -void -glBuffersBase::gen(GLsizei n, GLuint * ids) -{ - glGenBuffers(n, ids); -} - -void -glBuffersBase::del(GLsizei n, const GLuint * ids) -{ - glDeleteBuffers(n, ids); -} diff --git a/lib/glBuffers.h b/lib/glBuffers.h deleted file mode 100644 index e2c09df..0000000 --- a/lib/glBuffers.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef GLBUFFERS_H -#define GLBUFFERS_H - -#include -#include // IWYU pragma: keep -#include -#include -#include - -class glBuffersBase { -protected: - static void gen(GLsizei, GLuint *); - static void del(GLsizei, const GLuint *); -}; - -template class glBuffers : glBuffersBase { -public: - glBuffers() - { - gen(N, ids.data()); - } - - ~glBuffers() - { - del(N, ids.data()); - } - - NO_COPY(glBuffers); - CUSTOM_MOVE(glBuffers); - - // NOLINTNEXTLINE(hicpp-explicit-conversions) - operator GLuint() const - { - static_assert(N == 1, "Implicit cast only if N == 1"); - return ids.front(); - } - - auto - operator[](size_t n) const - { - return ids[n]; - } - -private: - std::array ids {}; -}; - -template glBuffers::glBuffers(glBuffers && src) noexcept : ids {src.ids} -{ - std::fill(src.ids.begin(), src.ids.end(), -1); -} - -template -glBuffers & -glBuffers::operator=(glBuffers && src) noexcept -{ - ids = src.ids; - std::fill(src.ids.begin(), src.ids.end(), -1); - return *this; -} - -using glBuffer = glBuffers<1>; - -#endif diff --git a/lib/glVertexArrays.cpp b/lib/glVertexArrays.cpp deleted file mode 100644 index 372b49b..0000000 --- a/lib/glVertexArrays.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "glVertexArrays.h" - -void -glVertexArraysBase::gen(GLsizei n, GLuint * ids) -{ - glGenVertexArrays(n, ids); -} - -void -glVertexArraysBase::del(GLsizei n, const GLuint * ids) -{ - glDeleteVertexArrays(n, ids); -} diff --git a/lib/glVertexArrays.h b/lib/glVertexArrays.h deleted file mode 100644 index 803206a..0000000 --- a/lib/glVertexArrays.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef GLVERTEXARRAYS_H -#define GLVERTEXARRAYS_H - -#include -#include // IWYU pragma: keep -#include -#include -#include - -class glVertexArraysBase { -protected: - static void gen(GLsizei, GLuint *); - static void del(GLsizei, const GLuint *); -}; - -template class glVertexArrays : glVertexArraysBase { -public: - glVertexArrays() - { - gen(N, ids.data()); - } - - ~glVertexArrays() - { - del(N, ids.data()); - } - - NO_COPY(glVertexArrays); - CUSTOM_MOVE(glVertexArrays); - - // NOLINTNEXTLINE(hicpp-explicit-conversions) - operator GLuint() const - { - static_assert(N == 1, "Implicit cast only if N == 1"); - return ids.front(); - } - - auto - operator[](size_t n) const - { - return ids[n]; - } - -private: - std::array ids {}; -}; - -template glVertexArrays::glVertexArrays(glVertexArrays && src) noexcept : ids {src.ids} -{ - std::fill(src.ids.begin(), src.ids.end(), -1); -} - -template -glVertexArrays & -glVertexArrays::operator=(glVertexArrays && src) noexcept -{ - ids = src.ids; - std::fill(src.ids.begin(), src.ids.end(), -1); - return *this; -} - -using glVertexArray = glVertexArrays<1>; - -#endif diff --git a/test/Jamfile.jam b/test/Jamfile.jam index f8987d0..e487044 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -22,6 +22,7 @@ project : requirements run test-collection.cpp ; run test-obj.cpp ; run test-maths.cpp ; +run test-lib.cpp ; run test-network.cpp ; run test-persistence.cpp : -- : [ sequence.insertion-sort [ glob fixtures/json/*.json fixtures/json/bad/*.json ] ] ; run test-text.cpp ; diff --git a/test/test-lib.cpp b/test/test-lib.cpp new file mode 100644 index 0000000..e464f2f --- /dev/null +++ b/test/test-lib.cpp @@ -0,0 +1,50 @@ +#define BOOST_TEST_MODULE test_lib + +#include "test-helpers.hpp" +#include +#include + +#include +#include +#include + +std::set active; +void +generator(GLsizei n, GLuint * out) +{ + static GLuint next {1}; + + while (n--) { + active.insert(next); + *out++ = next++; + } +} + +void +deleter(GLsizei n, GLuint * in) +{ + BOOST_CHECK(std::all_of(in, in + n, [](GLuint id) { + return active.erase(id) > 0; + })); +} + +using TestArray = glArrays<5, &generator, &deleter>; + +BOOST_AUTO_TEST_CASE(generate_and_delete) +{ + { + TestArray a; + } + BOOST_CHECK(active.empty()); +} + +BOOST_AUTO_TEST_CASE(generate_move_and_delete) +{ + { + TestArray a; + BOOST_CHECK_EQUAL(TestArray::size, active.size()); + TestArray b {std::move(a)}; + BOOST_CHECK_EQUAL(TestArray::size, active.size()); + } + BOOST_CHECK(active.empty()); +} diff --git a/ui/icon.cpp b/ui/icon.cpp index 371410a..657e953 100644 --- a/ui/icon.cpp +++ b/ui/icon.cpp @@ -1,4 +1,6 @@ #include "icon.h" +#include "glArrays.h" +#include #include #include #include @@ -7,9 +9,8 @@ Icon::Icon(const std::filesystem::path & fileName) : Icon {Image {Resource::mapP { } -Icon::Icon(const Image & tex) : size {tex.width, tex.height}, m_texture {} +Icon::Icon(const Image & tex) : size {tex.width, tex.height} { - glGenTextures(1, &m_texture); glBindTexture(GL_TEXTURE_2D, m_texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); @@ -21,11 +22,6 @@ Icon::Icon(const Image & tex) : size {tex.width, tex.height}, m_texture {} GL_RGBA, GL_UNSIGNED_BYTE, tex.data.data()); } -Icon::~Icon() -{ - glDeleteTextures(1, &m_texture); -} - void Icon::Bind() const { diff --git a/ui/icon.h b/ui/icon.h index cc6df87..2650218 100644 --- a/ui/icon.h +++ b/ui/icon.h @@ -1,10 +1,9 @@ #ifndef ICON_H #define ICON_H -#include #include +#include #include -#include class Image; @@ -13,16 +12,11 @@ public: explicit Icon(const std::filesystem::path & fileName); explicit Icon(const Image & image); - virtual ~Icon(); - - NO_COPY(Icon); - NO_MOVE(Icon); - void Bind() const; const glm::vec2 size; private: - GLuint m_texture; + glTexture m_texture; }; #endif diff --git a/ui/iconButton.cpp b/ui/iconButton.cpp index c97896f..f74b3fc 100644 --- a/ui/iconButton.cpp +++ b/ui/iconButton.cpp @@ -1,6 +1,5 @@ #include "iconButton.h" -#include "glBuffers.h" -#include "glVertexArrays.h" +#include "glArrays.h" #include "ui/icon.h" #include "ui/uiComponent.h" #include diff --git a/ui/iconButton.h b/ui/iconButton.h index 76e3f1b..53ada74 100644 --- a/ui/iconButton.h +++ b/ui/iconButton.h @@ -3,8 +3,7 @@ #include "icon.h" #include "uiComponent.h" -#include -#include +#include #include #include diff --git a/ui/text.cpp b/ui/text.cpp index be696c3..2acfb7e 100644 --- a/ui/text.cpp +++ b/ui/text.cpp @@ -3,8 +3,7 @@ #include "gfx/gl/uiShader.h" #include "uiComponent.h" #include -#include -#include +#include #include #include #include diff --git a/ui/text.h b/ui/text.h index 81122de..cbe49de 100644 --- a/ui/text.h +++ b/ui/text.h @@ -2,8 +2,7 @@ #include "uiComponent.h" #include -#include -#include +#include #include #include #include -- cgit v1.2.3