Some refactoring of the OpenGL renderer and TextureDataManager.

This commit is contained in:
Leon Styhre 2022-10-11 18:11:36 +02:00
parent 11b035affa
commit 7e923cb9b6
6 changed files with 32 additions and 42 deletions

View file

@ -52,7 +52,7 @@ void Renderer::setIcon()
// Try creating SDL surface from logo data. // Try creating SDL surface from logo data.
SDL_Surface* logoSurface {SDL_CreateRGBSurfaceFrom( SDL_Surface* logoSurface {SDL_CreateRGBSurfaceFrom(
static_cast<void*>(rawData.data()), static_cast<int>(width), static_cast<int>(height), static_cast<void*>(rawData.data()), static_cast<int>(width), static_cast<int>(height),
32, static_cast<int>((width * 4)), rmask, gmask, bmask, amask)}; 32, static_cast<int>(width * 4), rmask, gmask, bmask, amask)};
if (logoSurface != nullptr) { if (logoSurface != nullptr) {
SDL_SetWindowIcon(mSDLWindow, logoSurface); SDL_SetWindowIcon(mSDLWindow, logoSurface);
@ -147,7 +147,7 @@ bool Renderer::createWindow()
if (mWindowWidth != displayMode.w || mWindowHeight != displayMode.h) if (mWindowWidth != displayMode.w || mWindowHeight != displayMode.h)
userResolution = true; userResolution = true;
unsigned int windowFlags; unsigned int windowFlags {0};
setup(); setup();
#if defined(_WIN64) #if defined(_WIN64)
@ -201,7 +201,7 @@ bool Renderer::createWindow()
// instead we simply indicate the physical pixel dimensions in parenthesis in the log // 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 // 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. // display so that the full application window is used for rendering.
int width = 0; int width {0};
SDL_GL_GetDrawableSize(mSDLWindow, &width, nullptr); SDL_GL_GetDrawableSize(mSDLWindow, &width, nullptr);
int scaleFactor = static_cast<int>(width / mWindowWidth); int scaleFactor = static_cast<int>(width / mWindowWidth);
@ -309,11 +309,11 @@ void Renderer::pushClipRect(const glm::ivec2& pos, const glm::ivec2& size)
box.h = sScreenHeight - box.y; box.h = sScreenHeight - box.y;
if (mScreenRotated) { if (mScreenRotated) {
box = Rect(mWindowWidth - mScreenOffsetX - box.x - box.w, box = Rect {mWindowWidth - mScreenOffsetX - box.x - box.w,
mWindowHeight - mScreenOffsetY - box.y - box.h, box.w, box.h); mWindowHeight - mScreenOffsetY - box.y - box.h, box.w, box.h};
} }
else { 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. // Make sure the box fits within mClipStack.top(), and clip further accordingly.
@ -349,7 +349,7 @@ void Renderer::popClipRect()
mClipStack.pop(); mClipStack.pop();
if (mClipStack.empty()) if (mClipStack.empty())
setScissor(Rect(0, 0, 0, 0)); setScissor(Rect {0, 0, 0, 0});
else else
setScissor(mClipStack.top()); setScissor(mClipStack.top());
} }
@ -360,7 +360,7 @@ void Renderer::drawRect(const float x,
const float h, const float h,
const unsigned int color, const unsigned int color,
const unsigned int colorEnd, const unsigned int colorEnd,
bool horizontalGradient, const bool horizontalGradient,
const float opacity, const float opacity,
const float dimming, const float dimming,
const BlendFactor srcBlendFactor, const BlendFactor srcBlendFactor,

View file

@ -146,7 +146,7 @@ public:
const float h, const float h,
const unsigned int color, const unsigned int color,
const unsigned int colorEnd, const unsigned int colorEnd,
bool horizontalGradient = false, const bool horizontalGradient = false,
const float opacity = 1.0, const float opacity = 1.0,
const float dimming = 1.0, const float dimming = 1.0,
const BlendFactor srcBlendFactor = BlendFactor::SRC_ALPHA, const BlendFactor srcBlendFactor = BlendFactor::SRC_ALPHA,

View file

@ -33,19 +33,13 @@ RendererOpenGL::RendererOpenGL() noexcept
{ {
} }
RendererOpenGL::~RendererOpenGL()
{
for (auto it = mShaderProgramVector.cbegin(); it != mShaderProgramVector.cend(); ++it)
delete *it;
}
RendererOpenGL* RendererOpenGL::getInstance() RendererOpenGL* RendererOpenGL::getInstance()
{ {
static RendererOpenGL instance; static RendererOpenGL instance;
return &instance; return &instance;
} }
ShaderOpenGL* RendererOpenGL::getShaderProgram(unsigned int shaderID) std::shared_ptr<ShaderOpenGL> RendererOpenGL::getShaderProgram(unsigned int shaderID)
{ {
unsigned int index {0}; unsigned int index {0};
@ -67,13 +61,13 @@ bool RendererOpenGL::loadShaders()
LOG(LogInfo) << "Loading shaders..."; LOG(LogInfo) << "Loading shaders...";
std::vector<std::string> shaderFiles; std::vector<std::string> shaderFiles;
shaderFiles.push_back(":/shaders/glsl/core.glsl"); shaderFiles.emplace_back(":/shaders/glsl/core.glsl");
shaderFiles.push_back(":/shaders/glsl/blur_horizontal.glsl"); shaderFiles.emplace_back(":/shaders/glsl/blur_horizontal.glsl");
shaderFiles.push_back(":/shaders/glsl/blur_vertical.glsl"); shaderFiles.emplace_back(":/shaders/glsl/blur_vertical.glsl");
shaderFiles.push_back(":/shaders/glsl/scanlines.glsl"); shaderFiles.emplace_back(":/shaders/glsl/scanlines.glsl");
for (auto it = shaderFiles.cbegin(); it != shaderFiles.cend(); ++it) { for (auto it = shaderFiles.cbegin(); it != shaderFiles.cend(); ++it) {
ShaderOpenGL* loadShader = new ShaderOpenGL(); auto loadShader = std::make_shared<ShaderOpenGL>();
loadShader->loadShaderFile(*it, GL_VERTEX_SHADER); loadShader->loadShaderFile(*it, GL_VERTEX_SHADER);
loadShader->loadShaderFile(*it, GL_FRAGMENT_SHADER); loadShader->loadShaderFile(*it, GL_FRAGMENT_SHADER);
@ -83,7 +77,7 @@ bool RendererOpenGL::loadShaders()
return false; return false;
} }
mShaderProgramVector.push_back(loadShader); mShaderProgramVector.emplace_back(std::move(loadShader));
} }
return true; return true;
@ -576,7 +570,7 @@ void RendererOpenGL::shaderPostprocessing(unsigned int shaders,
for (size_t i = 0; i < shaderList.size(); ++i) { for (size_t i = 0; i < shaderList.size(); ++i) {
vertices->shaders = shaderList[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 // For the blur shaders there is an optional variable to set the number of passes
// to execute, which proportionally affects the blur amount. // to execute, which proportionally affects the blur amount.
if (shaderList[i] == Renderer::Shader::BLUR_HORIZONTAL || if (shaderList[i] == Renderer::Shader::BLUR_HORIZONTAL ||

View file

@ -20,15 +20,14 @@
#include <SDL2/SDL_opengl.h> #include <SDL2/SDL_opengl.h>
#endif #endif
#include <memory>
class RendererOpenGL : public Renderer class RendererOpenGL : public Renderer
{ {
public: public:
RendererOpenGL() noexcept;
~RendererOpenGL();
static RendererOpenGL* getInstance(); static RendererOpenGL* getInstance();
ShaderOpenGL* getShaderProgram(unsigned int shaderID); std::shared_ptr<ShaderOpenGL> getShaderProgram(unsigned int shaderID);
bool loadShaders() override; bool loadShaders() override;
GLenum convertBlendFactor(const BlendFactor BlendFactor); GLenum convertBlendFactor(const BlendFactor BlendFactor);
@ -73,7 +72,9 @@ public:
unsigned char* textureRGBA = nullptr) override; unsigned char* textureRGBA = nullptr) override;
private: private:
std::vector<ShaderOpenGL*> mShaderProgramVector; RendererOpenGL() noexcept;
std::vector<std::shared_ptr<ShaderOpenGL>> mShaderProgramVector;
GLuint mShaderFBO1; GLuint mShaderFBO1;
GLuint mShaderFBO2; GLuint mShaderFBO2;
GLuint mVertexBuffer1; GLuint mVertexBuffer1;
@ -83,14 +84,16 @@ private:
GLuint mWhiteTexture; GLuint mWhiteTexture;
GLuint mPostProcTexture1; GLuint mPostProcTexture1;
GLuint mPostProcTexture2; GLuint mPostProcTexture2;
ShaderOpenGL* mCoreShader; std::shared_ptr<ShaderOpenGL> mCoreShader;
ShaderOpenGL* mBlurHorizontalShader; std::shared_ptr<ShaderOpenGL> mBlurHorizontalShader;
ShaderOpenGL* mBlurVerticalShader; std::shared_ptr<ShaderOpenGL> mBlurVerticalShader;
ShaderOpenGL* mScanlinelShader; std::shared_ptr<ShaderOpenGL> mScanlinelShader;
ShaderOpenGL* mLastShader; std::shared_ptr<ShaderOpenGL> mLastShader;
int mMajorGLVersion; int mMajorGLVersion;
int mMinorGLVersion; int mMinorGLVersion;
friend Renderer;
}; };
#endif // ES_CORE_RENDERER_RENDERER_OPENGL_H #endif // ES_CORE_RENDERER_RENDERER_OPENGL_H

View file

@ -24,13 +24,7 @@ TextureDataManager::TextureDataManager()
data[i * 4 + 3] = 0; data[i * 4 + 3] = 0;
} }
mBlank->initFromRGBA(data, 5, 5); mBlank->initFromRGBA(data, 5, 5);
mLoader = new TextureLoader; mLoader = std::make_unique<TextureLoader>();
}
TextureDataManager::~TextureDataManager()
{
// Delete TextureLoader object when destroyed.
delete mLoader;
} }
std::shared_ptr<TextureData> TextureDataManager::add(const TextureResource* key, bool tiled) std::shared_ptr<TextureData> TextureDataManager::add(const TextureResource* key, bool tiled)

View file

@ -63,7 +63,6 @@ class TextureDataManager
{ {
public: public:
TextureDataManager(); TextureDataManager();
~TextureDataManager();
std::shared_ptr<TextureData> add(const TextureResource* key, bool tiled); std::shared_ptr<TextureData> add(const TextureResource* key, bool tiled);
@ -90,7 +89,7 @@ private:
std::map<const TextureResource*, std::list<std::shared_ptr<TextureData>>::const_iterator> std::map<const TextureResource*, std::list<std::shared_ptr<TextureData>>::const_iterator>
mTextureLookup; mTextureLookup;
std::shared_ptr<TextureData> mBlank; std::shared_ptr<TextureData> mBlank;
TextureLoader* mLoader; std::unique_ptr<TextureLoader> mLoader;
}; };
#endif // ES_CORE_RESOURCES_TEXTURE_DATA_MANAGER_H #endif // ES_CORE_RESOURCES_TEXTURE_DATA_MANAGER_H