From 301222ea27877db437a9c3dc0e2cbc1d0abad7c1 Mon Sep 17 00:00:00 2001 From: Kai Blaschke Date: Fri, 12 Jul 2024 13:29:19 +0200 Subject: [PATCH] Properly check for uninitialized texture in CopyTexture class. Before rendering the first frame, there is no output texture present in a preset, so we can't copy anything and should just skip that step. --- src/libprojectM/Renderer/CopyTexture.cpp | 5 ++++- src/libprojectM/Renderer/Texture.cpp | 11 ++++++++--- src/libprojectM/Renderer/Texture.hpp | 11 ++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/libprojectM/Renderer/CopyTexture.cpp b/src/libprojectM/Renderer/CopyTexture.cpp index 8e8ce336a..6a0c58605 100644 --- a/src/libprojectM/Renderer/CopyTexture.cpp +++ b/src/libprojectM/Renderer/CopyTexture.cpp @@ -157,7 +157,10 @@ void CopyTexture::Draw(const std::shared_ptr& originalTexture, co void CopyTexture::Draw(const std::shared_ptr& originalTexture, Framebuffer& framebuffer, int framebufferIndex, bool flipVertical, bool flipHorizontal) { - if (originalTexture == nullptr || framebuffer.GetColorAttachmentTexture(framebufferIndex, 0) == nullptr) + if (originalTexture == nullptr + || originalTexture->Empty() + || framebuffer.GetColorAttachmentTexture(framebufferIndex, 0) == nullptr + || framebuffer.GetColorAttachmentTexture(framebufferIndex, 0)->Empty()) { return; } diff --git a/src/libprojectM/Renderer/Texture.cpp b/src/libprojectM/Renderer/Texture.cpp index 47fb59b30..db0effa74 100644 --- a/src/libprojectM/Renderer/Texture.cpp +++ b/src/libprojectM/Renderer/Texture.cpp @@ -15,7 +15,7 @@ Texture::Texture(std::string name, const int width, const int height, const bool , m_format(GL_RGB) , m_type(GL_UNSIGNED_BYTE) { - CreateEmptyTexture(); + CreateNewTexture(); } Texture::Texture(std::string name, int width, int height, @@ -29,7 +29,7 @@ Texture::Texture(std::string name, int width, int height, , m_format(format) , m_type(type) { - CreateEmptyTexture(); + CreateNewTexture(); } Texture::Texture(std::string name, const GLuint texID, const GLenum target, @@ -99,7 +99,12 @@ auto Texture::IsUserTexture() const -> bool return m_isUserTexture; } -void Texture::CreateEmptyTexture() +auto Texture::Empty() const -> bool +{ + return m_textureId == 0; +} + +void Texture::CreateNewTexture() { glGenTextures(1, &m_textureId); glBindTexture(m_target, m_textureId); diff --git a/src/libprojectM/Renderer/Texture.hpp b/src/libprojectM/Renderer/Texture.hpp index 4577b1712..75fce1164 100644 --- a/src/libprojectM/Renderer/Texture.hpp +++ b/src/libprojectM/Renderer/Texture.hpp @@ -115,8 +115,17 @@ class Texture */ auto IsUserTexture() const -> bool; + /** + * @brief Returns true if the texture is empty/unallocated. + * @return true if the texture is not yet allocated (e.g. the ID 0), false if the object contains a valid texture. + */ + auto Empty() const -> bool; + private: - void CreateEmptyTexture(); + /** + * @brief Creates a new, blank texture with the given size. + */ + void CreateNewTexture(); GLuint m_textureId{0}; //!< The OpenGL texture name/ID. GLenum m_target{GL_NONE}; //!< The OpenGL texture target, e.g. GL_TEXTURE_2D.