From 7e923cb9b60a58b60bc600e4199d0d407d99794f Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 11 Oct 2022 18:11:36 +0200 Subject: [PATCH] Some refactoring of the OpenGL renderer and TextureDataManager. --- es-core/src/renderers/Renderer.cpp | 16 +++++++------- es-core/src/renderers/Renderer.h | 2 +- es-core/src/renderers/RendererOpenGL.cpp | 22 +++++++------------ es-core/src/renderers/RendererOpenGL.h | 23 +++++++++++--------- es-core/src/resources/TextureDataManager.cpp | 8 +------ es-core/src/resources/TextureDataManager.h | 3 +-- 6 files changed, 32 insertions(+), 42 deletions(-) diff --git a/es-core/src/renderers/Renderer.cpp b/es-core/src/renderers/Renderer.cpp index d02c07f95..ef2da3ef7 100644 --- a/es-core/src/renderers/Renderer.cpp +++ b/es-core/src/renderers/Renderer.cpp @@ -52,7 +52,7 @@ void Renderer::setIcon() // Try creating SDL surface from logo data. SDL_Surface* logoSurface {SDL_CreateRGBSurfaceFrom( static_cast(rawData.data()), static_cast(width), static_cast(height), - 32, static_cast((width * 4)), rmask, gmask, bmask, amask)}; + 32, static_cast(width * 4), rmask, gmask, bmask, amask)}; if (logoSurface != nullptr) { SDL_SetWindowIcon(mSDLWindow, logoSurface); @@ -147,7 +147,7 @@ bool Renderer::createWindow() if (mWindowWidth != displayMode.w || mWindowHeight != displayMode.h) userResolution = true; - unsigned int windowFlags; + unsigned int windowFlags {0}; setup(); #if defined(_WIN64) @@ -201,7 +201,7 @@ bool Renderer::createWindow() // instead we simply indicate the physical pixel dimensions in parenthesis in the log // file and make sure to double the window and screen sizes in case of a high DPI // display so that the full application window is used for rendering. - int width = 0; + int width {0}; SDL_GL_GetDrawableSize(mSDLWindow, &width, nullptr); int scaleFactor = static_cast(width / mWindowWidth); @@ -309,11 +309,11 @@ void Renderer::pushClipRect(const glm::ivec2& pos, const glm::ivec2& size) box.h = sScreenHeight - box.y; if (mScreenRotated) { - box = Rect(mWindowWidth - mScreenOffsetX - box.x - box.w, - mWindowHeight - mScreenOffsetY - box.y - box.h, box.w, box.h); + box = Rect {mWindowWidth - mScreenOffsetX - box.x - box.w, + mWindowHeight - mScreenOffsetY - box.y - box.h, box.w, box.h}; } else { - box = Rect(mScreenOffsetX + box.x, mScreenOffsetY + box.y, box.w, box.h); + box = Rect {mScreenOffsetX + box.x, mScreenOffsetY + box.y, box.w, box.h}; } // Make sure the box fits within mClipStack.top(), and clip further accordingly. @@ -349,7 +349,7 @@ void Renderer::popClipRect() mClipStack.pop(); if (mClipStack.empty()) - setScissor(Rect(0, 0, 0, 0)); + setScissor(Rect {0, 0, 0, 0}); else setScissor(mClipStack.top()); } @@ -360,7 +360,7 @@ void Renderer::drawRect(const float x, const float h, const unsigned int color, const unsigned int colorEnd, - bool horizontalGradient, + const bool horizontalGradient, const float opacity, const float dimming, const BlendFactor srcBlendFactor, diff --git a/es-core/src/renderers/Renderer.h b/es-core/src/renderers/Renderer.h index cc763a43c..c07962ef9 100644 --- a/es-core/src/renderers/Renderer.h +++ b/es-core/src/renderers/Renderer.h @@ -146,7 +146,7 @@ public: const float h, const unsigned int color, const unsigned int colorEnd, - bool horizontalGradient = false, + const bool horizontalGradient = false, const float opacity = 1.0, const float dimming = 1.0, const BlendFactor srcBlendFactor = BlendFactor::SRC_ALPHA, diff --git a/es-core/src/renderers/RendererOpenGL.cpp b/es-core/src/renderers/RendererOpenGL.cpp index b26bc6935..2dcce598a 100644 --- a/es-core/src/renderers/RendererOpenGL.cpp +++ b/es-core/src/renderers/RendererOpenGL.cpp @@ -33,19 +33,13 @@ RendererOpenGL::RendererOpenGL() noexcept { } -RendererOpenGL::~RendererOpenGL() -{ - for (auto it = mShaderProgramVector.cbegin(); it != mShaderProgramVector.cend(); ++it) - delete *it; -} - RendererOpenGL* RendererOpenGL::getInstance() { static RendererOpenGL instance; return &instance; } -ShaderOpenGL* RendererOpenGL::getShaderProgram(unsigned int shaderID) +std::shared_ptr RendererOpenGL::getShaderProgram(unsigned int shaderID) { unsigned int index {0}; @@ -67,13 +61,13 @@ bool RendererOpenGL::loadShaders() LOG(LogInfo) << "Loading shaders..."; std::vector shaderFiles; - shaderFiles.push_back(":/shaders/glsl/core.glsl"); - shaderFiles.push_back(":/shaders/glsl/blur_horizontal.glsl"); - shaderFiles.push_back(":/shaders/glsl/blur_vertical.glsl"); - shaderFiles.push_back(":/shaders/glsl/scanlines.glsl"); + shaderFiles.emplace_back(":/shaders/glsl/core.glsl"); + shaderFiles.emplace_back(":/shaders/glsl/blur_horizontal.glsl"); + shaderFiles.emplace_back(":/shaders/glsl/blur_vertical.glsl"); + shaderFiles.emplace_back(":/shaders/glsl/scanlines.glsl"); for (auto it = shaderFiles.cbegin(); it != shaderFiles.cend(); ++it) { - ShaderOpenGL* loadShader = new ShaderOpenGL(); + auto loadShader = std::make_shared(); loadShader->loadShaderFile(*it, GL_VERTEX_SHADER); loadShader->loadShaderFile(*it, GL_FRAGMENT_SHADER); @@ -83,7 +77,7 @@ bool RendererOpenGL::loadShaders() return false; } - mShaderProgramVector.push_back(loadShader); + mShaderProgramVector.emplace_back(std::move(loadShader)); } return true; @@ -576,7 +570,7 @@ void RendererOpenGL::shaderPostprocessing(unsigned int shaders, for (size_t i = 0; i < shaderList.size(); ++i) { vertices->shaders = shaderList[i]; - int shaderPasses = 1; + int shaderPasses {1}; // For the blur shaders there is an optional variable to set the number of passes // to execute, which proportionally affects the blur amount. if (shaderList[i] == Renderer::Shader::BLUR_HORIZONTAL || diff --git a/es-core/src/renderers/RendererOpenGL.h b/es-core/src/renderers/RendererOpenGL.h index 5cfaaa06a..d6574e0dc 100644 --- a/es-core/src/renderers/RendererOpenGL.h +++ b/es-core/src/renderers/RendererOpenGL.h @@ -20,15 +20,14 @@ #include #endif +#include + class RendererOpenGL : public Renderer { public: - RendererOpenGL() noexcept; - ~RendererOpenGL(); - static RendererOpenGL* getInstance(); - ShaderOpenGL* getShaderProgram(unsigned int shaderID); + std::shared_ptr getShaderProgram(unsigned int shaderID); bool loadShaders() override; GLenum convertBlendFactor(const BlendFactor BlendFactor); @@ -73,7 +72,9 @@ public: unsigned char* textureRGBA = nullptr) override; private: - std::vector mShaderProgramVector; + RendererOpenGL() noexcept; + + std::vector> mShaderProgramVector; GLuint mShaderFBO1; GLuint mShaderFBO2; GLuint mVertexBuffer1; @@ -83,14 +84,16 @@ private: GLuint mWhiteTexture; GLuint mPostProcTexture1; GLuint mPostProcTexture2; - ShaderOpenGL* mCoreShader; - ShaderOpenGL* mBlurHorizontalShader; - ShaderOpenGL* mBlurVerticalShader; - ShaderOpenGL* mScanlinelShader; - ShaderOpenGL* mLastShader; + std::shared_ptr mCoreShader; + std::shared_ptr mBlurHorizontalShader; + std::shared_ptr mBlurVerticalShader; + std::shared_ptr mScanlinelShader; + std::shared_ptr mLastShader; int mMajorGLVersion; int mMinorGLVersion; + + friend Renderer; }; #endif // ES_CORE_RENDERER_RENDERER_OPENGL_H diff --git a/es-core/src/resources/TextureDataManager.cpp b/es-core/src/resources/TextureDataManager.cpp index f91d11319..eeb1f5533 100644 --- a/es-core/src/resources/TextureDataManager.cpp +++ b/es-core/src/resources/TextureDataManager.cpp @@ -24,13 +24,7 @@ TextureDataManager::TextureDataManager() data[i * 4 + 3] = 0; } mBlank->initFromRGBA(data, 5, 5); - mLoader = new TextureLoader; -} - -TextureDataManager::~TextureDataManager() -{ - // Delete TextureLoader object when destroyed. - delete mLoader; + mLoader = std::make_unique(); } std::shared_ptr TextureDataManager::add(const TextureResource* key, bool tiled) diff --git a/es-core/src/resources/TextureDataManager.h b/es-core/src/resources/TextureDataManager.h index d1a1b03ef..a235314c9 100644 --- a/es-core/src/resources/TextureDataManager.h +++ b/es-core/src/resources/TextureDataManager.h @@ -63,7 +63,6 @@ class TextureDataManager { public: TextureDataManager(); - ~TextureDataManager(); std::shared_ptr add(const TextureResource* key, bool tiled); @@ -90,7 +89,7 @@ private: std::map>::const_iterator> mTextureLookup; std::shared_ptr mBlank; - TextureLoader* mLoader; + std::unique_ptr mLoader; }; #endif // ES_CORE_RESOURCES_TEXTURE_DATA_MANAGER_H