From efea8856ed1738da0a37149fbbb4669522a8a1ba Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 20 Oct 2019 09:06:26 +0200 Subject: [PATCH] Clean up OpenGL engine. Code de-duplication and improved namespacing. --- .../engines/opengl/ApplyPaletteShader.h | 2 +- .../engines/opengl/ApplyTransparencyShader.h | 3 +- .../drawing/engines/opengl/DrawCommands.h | 10 +- .../drawing/engines/opengl/DrawRectShader.h | 1 - .../drawing/engines/opengl/GLSLTypes.h | 200 +++++++----------- .../drawing/engines/opengl/TextureCache.cpp | 14 +- .../drawing/engines/opengl/TextureCache.h | 22 +- 7 files changed, 107 insertions(+), 145 deletions(-) diff --git a/src/openrct2-ui/drawing/engines/opengl/ApplyPaletteShader.h b/src/openrct2-ui/drawing/engines/opengl/ApplyPaletteShader.h index e0677a667a..31c33dfbc8 100644 --- a/src/openrct2-ui/drawing/engines/opengl/ApplyPaletteShader.h +++ b/src/openrct2-ui/drawing/engines/opengl/ApplyPaletteShader.h @@ -28,7 +28,7 @@ public: ApplyPaletteShader(); ~ApplyPaletteShader() override; - void SetTexture(GLuint texture); + static void SetTexture(GLuint texture); void SetPalette(const vec4* glPalette); void Draw(); diff --git a/src/openrct2-ui/drawing/engines/opengl/ApplyTransparencyShader.h b/src/openrct2-ui/drawing/engines/opengl/ApplyTransparencyShader.h index c61c33b3ef..840bec9ea7 100644 --- a/src/openrct2-ui/drawing/engines/opengl/ApplyTransparencyShader.h +++ b/src/openrct2-ui/drawing/engines/opengl/ApplyTransparencyShader.h @@ -31,7 +31,8 @@ public: ApplyTransparencyShader(); ~ApplyTransparencyShader() override; - void SetTextures(GLuint opaqueTex, GLuint opaqueDepth, GLuint transparentTex, GLuint transparentDepth, GLuint paletteTex); + static void SetTextures( + GLuint opaqueTex, GLuint opaqueDepth, GLuint transparentTex, GLuint transparentDepth, GLuint paletteTex); void Draw(); private: diff --git a/src/openrct2-ui/drawing/engines/opengl/DrawCommands.h b/src/openrct2-ui/drawing/engines/opengl/DrawCommands.h index 464c37a0f3..4bc78b13b2 100644 --- a/src/openrct2-ui/drawing/engines/opengl/DrawCommands.h +++ b/src/openrct2-ui/drawing/engines/opengl/DrawCommands.h @@ -27,7 +27,7 @@ public: : _numInstances(0) { } - bool empty() const + [[nodiscard]] bool empty() const { return _numInstances == 0; } @@ -51,7 +51,7 @@ public: } return _instances[_numInstances++] = value; } - size_t size() const + [[nodiscard]] size_t size() const { return _numInstances; } @@ -114,9 +114,9 @@ struct DrawRectCommand enum { - FLAG_NO_TEXTURE = (1 << 2), - FLAG_MASK = (1 << 3), - FLAG_CROSS_HATCH = (1 << 4), + FLAG_NO_TEXTURE = (1U << 2U), + FLAG_MASK = (1U << 3U), + FLAG_CROSS_HATCH = (1U << 4U), }; }; diff --git a/src/openrct2-ui/drawing/engines/opengl/DrawRectShader.h b/src/openrct2-ui/drawing/engines/opengl/DrawRectShader.h index 1d5f2dc081..473e2f985b 100644 --- a/src/openrct2-ui/drawing/engines/opengl/DrawRectShader.h +++ b/src/openrct2-ui/drawing/engines/opengl/DrawRectShader.h @@ -14,7 +14,6 @@ #include "OpenGLShaderProgram.h" #include -#include class DrawRectShader final : public OpenGLShaderProgram { diff --git a/src/openrct2-ui/drawing/engines/opengl/GLSLTypes.h b/src/openrct2-ui/drawing/engines/opengl/GLSLTypes.h index 8a730ae853..fce694e46a 100644 --- a/src/openrct2-ui/drawing/engines/opengl/GLSLTypes.h +++ b/src/openrct2-ui/drawing/engines/opengl/GLSLTypes.h @@ -15,136 +15,98 @@ #pragma pack(push, 1) -struct ivec2 +namespace detail { - union + template struct vec2 { - GLint x; - GLint s; - GLint r; - }; - union - { - GLint y; - GLint t; - GLint g; - }; -}; + using ValueType = T_; -struct vec2 -{ - union - { - GLfloat x; - GLfloat s; - GLfloat r; + union + { + ValueType x; + ValueType s; + ValueType r; + }; + union + { + ValueType y; + ValueType t; + ValueType g; + }; }; - union - { - GLfloat y; - GLfloat t; - GLfloat g; - }; -}; -struct ivec3 -{ - union - { - GLint x; - GLint s; - GLint r; - }; - union - { - GLint y; - GLint t; - GLint g; - }; - union - { - GLint z; - GLint p; - GLint b; - }; -}; + template struct vec2; + template struct vec2; -struct vec3f -{ - union + template struct vec3 { - GLfloat x; - GLfloat s; - GLfloat r; - }; - union - { - GLfloat y; - GLfloat t; - GLfloat g; - }; - union - { - GLfloat z; - GLfloat p; - GLfloat b; - }; -}; + using ValueType = T_; -struct ivec4 -{ - union - { - GLint x; - GLint s; - GLint r; + union + { + ValueType x; + ValueType s; + ValueType r; + }; + union + { + ValueType y; + ValueType t; + ValueType g; + }; + union + { + ValueType z; + ValueType p; + ValueType b; + }; }; - union - { - GLint y; - GLint t; - GLint g; - }; - union - { - GLint z; - GLint p; - GLint b; - }; - union - { - GLint w; - GLint q; - GLint a; - }; -}; -struct vec4 -{ - union + template struct vec3; + template struct vec3; + + template struct vec4 { - GLfloat x; - GLfloat s; - GLfloat r; + using ValueType = T_; + + union + { + ValueType x; + ValueType s; + ValueType r; + }; + union + { + ValueType y; + ValueType t; + ValueType g; + }; + union + { + ValueType z; + ValueType p; + ValueType b; + }; + union + { + ValueType w; + ValueType q; + ValueType a; + }; }; - union - { - GLfloat y; - GLfloat t; - GLfloat g; - }; - union - { - GLfloat z; - GLfloat p; - GLfloat b; - }; - union - { - GLfloat w; - GLfloat q; - GLfloat a; - }; -}; + + template struct vec4; + template struct vec4; + +} // namespace detail + +using vec2 = detail::vec2; +using ivec2 = detail::vec2; + +using vec3 = detail::vec3; +using ivec3 = detail::vec3; + +using vec4 = detail::vec4; +using ivec4 = detail::vec4; #pragma pack(pop) diff --git a/src/openrct2-ui/drawing/engines/opengl/TextureCache.cpp b/src/openrct2-ui/drawing/engines/opengl/TextureCache.cpp index 16dee7ba1e..4905af8a7c 100644 --- a/src/openrct2-ui/drawing/engines/opengl/TextureCache.cpp +++ b/src/openrct2-ui/drawing/engines/opengl/TextureCache.cpp @@ -65,7 +65,7 @@ BasicTextureInfo TextureCache::GetOrLoadImageTexture(uint32_t image) { uint32_t index; - image &= 0x7FFFF; + image &= 0x7FFFFUL; // Try to read cached texture first. { @@ -97,7 +97,7 @@ BasicTextureInfo TextureCache::GetOrLoadImageTexture(uint32_t image) BasicTextureInfo TextureCache::GetOrLoadGlyphTexture(uint32_t image, uint8_t* palette) { - GlyphId glyphId; + GlyphId glyphId{}; glyphId.Image = image; // Try to read cached texture first. @@ -203,7 +203,7 @@ void TextureCache::EnlargeAtlasesTexture(GLuint newEntries) } // Initial capacity will be 12 which covers most cases of a fully visible park. - _atlasesTextureCapacity = (_atlasesTextureCapacity + 6) << 1; + _atlasesTextureCapacity = (_atlasesTextureCapacity + 6) << 1UL; } glBindTexture(GL_TEXTURE_2D_ARRAY, _atlasesTexture); @@ -275,8 +275,8 @@ AtlasTextureInfo TextureCache::AllocateImage(int32_t imageWidth, int32_t imageHe throw std::runtime_error("more texture atlases required, but device limit reached!"); } - int32_t atlasIndex = (int32_t)_atlases.size(); - int32_t atlasSize = (int32_t)powf(2, (float)Atlas::CalculateImageSizeOrder(imageWidth, imageHeight)); + auto atlasIndex = static_cast(_atlases.size()); + int32_t atlasSize = powf(2, (float)Atlas::CalculateImageSizeOrder(imageWidth, imageHeight)); # ifdef DEBUG log_verbose("new texture atlas #%d (size %d) allocated", atlasIndex, atlasSize); @@ -294,7 +294,7 @@ AtlasTextureInfo TextureCache::AllocateImage(int32_t imageWidth, int32_t imageHe rct_drawpixelinfo TextureCache::GetImageAsDPI(uint32_t image, uint32_t tertiaryColour) { - auto g1Element = gfx_get_g1_element(image & 0x7FFFF); + auto g1Element = gfx_get_g1_element(image & 0x7FFFFUL); int32_t width = g1Element->width; int32_t height = g1Element->height; @@ -305,7 +305,7 @@ rct_drawpixelinfo TextureCache::GetImageAsDPI(uint32_t image, uint32_t tertiaryC rct_drawpixelinfo TextureCache::GetGlyphAsDPI(uint32_t image, uint8_t* palette) { - auto g1Element = gfx_get_g1_element(image & 0x7FFFF); + auto g1Element = gfx_get_g1_element(image & 0x7FFFFUL); int32_t width = g1Element->width; int32_t height = g1Element->height; diff --git a/src/openrct2-ui/drawing/engines/opengl/TextureCache.h b/src/openrct2-ui/drawing/engines/opengl/TextureCache.h index c8fc9d584c..851d035094 100644 --- a/src/openrct2-ui/drawing/engines/opengl/TextureCache.h +++ b/src/openrct2-ui/drawing/engines/opengl/TextureCache.h @@ -35,8 +35,8 @@ struct GlyphId size_t operator()(const GlyphId& k) const { size_t hash = k.Image * 7; - hash += (k.Palette & 0xFFFFFFFF) * 13; - hash += (k.Palette >> 32) * 23; + hash += (k.Palette & 0xFFFFFFFFUL) * 13; + hash += (k.Palette >> 32UL) * 23; return hash; } }; @@ -111,14 +111,14 @@ public: AtlasTextureInfo Allocate(int32_t actualWidth, int32_t actualHeight) { - assert(_freeSlots.size() > 0); + assert(!_freeSlots.empty()); GLuint slot = _freeSlots.back(); _freeSlots.pop_back(); auto bounds = GetSlotCoordinates(slot, actualWidth, actualHeight); - AtlasTextureInfo info; + AtlasTextureInfo info{}; info.index = _index; info.slot = slot; info.bounds = bounds; @@ -136,15 +136,15 @@ public: // Checks if specified image would be tightly packed in this atlas // by checking if it is within the right power of 2 range - bool IsImageSuitable(int32_t actualWidth, int32_t actualHeight) const + [[nodiscard]] bool IsImageSuitable(int32_t actualWidth, int32_t actualHeight) const { int32_t imageOrder = CalculateImageSizeOrder(actualWidth, actualHeight); - int32_t atlasOrder = (int32_t)log2(_imageSize); + int32_t atlasOrder = log2(_imageSize); return imageOrder == atlasOrder; } - int32_t GetFreeSlots() const + [[nodiscard]] int32_t GetFreeSlots() const { return (int32_t)_freeSlots.size(); } @@ -162,7 +162,7 @@ public: } private: - ivec4 GetSlotCoordinates(GLuint slot, int32_t actualWidth, int32_t actualHeight) const + [[nodiscard]] ivec4 GetSlotCoordinates(GLuint slot, int32_t actualWidth, int32_t actualHeight) const { int32_t row = slot / _cols; int32_t col = slot % _cols; @@ -175,7 +175,7 @@ private: }; } - vec4 NormalizeCoordinates(const ivec4& coords) const + [[nodiscard]] vec4 NormalizeCoordinates(const ivec4& coords) const { return vec4{ coords.x / (float)_atlasWidth, @@ -231,8 +231,8 @@ private: AtlasTextureInfo LoadImageTexture(uint32_t image); AtlasTextureInfo LoadGlyphTexture(uint32_t image, uint8_t* palette); AtlasTextureInfo AllocateImage(int32_t imageWidth, int32_t imageHeight); - rct_drawpixelinfo GetImageAsDPI(uint32_t image, uint32_t tertiaryColour); - rct_drawpixelinfo GetGlyphAsDPI(uint32_t image, uint8_t* palette); + static rct_drawpixelinfo GetImageAsDPI(uint32_t image, uint32_t tertiaryColour); + static rct_drawpixelinfo GetGlyphAsDPI(uint32_t image, uint8_t* palette); void FreeTextures(); static rct_drawpixelinfo CreateDPI(int32_t width, int32_t height);