Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

GLVideoDriver: Implemented GLPaletteManager class to share palette te…

…xtures between sprites (greatly reduces memory usage)
  • Loading branch information...
commit d8b4a87f9079cf77cf8c4e6a735bc3e3f51d208e 1 parent b140695
@BehoIder BehoIder authored lynxlynxlynx committed
View
83 gemrb/plugins/SDLVideo/GLPaletteManager.cpp
@@ -0,0 +1,83 @@
+#ifdef USE_GL
+#include <GL/glew.h>
+#else
+#include <GLES2/GL2.h>
+#include <GLES2/GL2ext.h>
+#endif
+
+#include "GLPaletteManager.h"
+#include "Palette.h"
+
+using namespace GemRB;
+
+std::map<PaletteKey, PaletteValue> GLPaletteManager::textures;
+
+GLuint GLPaletteManager::CreatePaletteTexture(Palette* palette, unsigned int colorKey)
+{
+ PaletteKey key = std::make_pair(palette, colorKey);
+ if (GLPaletteManager::textures.find(key) == GLPaletteManager::textures.end())
+ {
+ GLuint texture;
+ // not found, we need to create it
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ Color* colors = new Color[256];
+ memcpy(colors, palette->col, sizeof(Color)*256);
+ for(unsigned int i=0; i<256; i++)
+ {
+ if(colors[i].a == 0)
+ {
+ colors[i].a = 0xFF;
+ }
+ if(i == colorKey) colors[i].a = 0;
+ }
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) colors);
+ delete[] colors;
+ PaletteValue value = std::make_pair(texture, 1);
+ textures.insert(std::make_pair(key, value));
+ }
+ else
+ {
+ textures[key].second++;
+ }
+ return textures[key].first;
+}
+
+void GLPaletteManager::RemovePaletteTexture(Palette* palette, unsigned int colorKey)
+{
+ std::pair<Palette*, unsigned int> key = std::make_pair(palette, colorKey);
+ if (textures.find(key) == textures.end())
+ {
+ // nothing found
+ }
+ else
+ {
+ PaletteValue value = textures[key];
+ if (value.second > 1) value.second --;
+ else
+ {
+ glDeleteTextures(1, &(value.first));
+ textures.erase(key);
+ }
+ }
+
+}
+
+void GLPaletteManager::Clear()
+{
+ for(std::map<std::pair<Palette*, unsigned int>, PaletteValue>::iterator it = textures.begin(); it != textures.end(); ++it)
+ {
+ glDeleteTextures(1, &(textures[it->first].first));
+ }
+ textures.clear();
+}
+
+
+
+
View
24 gemrb/plugins/SDLVideo/GLPaletteManager.h
@@ -0,0 +1,24 @@
+#ifndef GLPALETTEMANAGER_H
+#define GLPALETTEMANAGER_H
+
+#include <map>
+
+namespace GemRB
+{
+ class Palette;
+
+ typedef std::pair<Palette*, unsigned int> PaletteKey;
+ typedef std::pair<GLuint, unsigned int> PaletteValue;
+
+ class GLPaletteManager
+ {
+ private:
+ static std::map<PaletteKey, PaletteValue> textures;
+ public:
+ static GLuint CreatePaletteTexture(Palette* palette, unsigned int colorKey);
+ static void RemovePaletteTexture(Palette* palette, unsigned int colorKey);
+ static void Clear();
+ };
+}
+
+#endif
View
38 gemrb/plugins/SDLVideo/GLTextureSprite2D.cpp
@@ -7,6 +7,7 @@
#include "SDLVideo.h"
#include "GLTextureSprite2D.h"
+#include "GLPaletteManager.h"
using namespace GemRB;
@@ -35,9 +36,10 @@ GLTextureSprite2D::GLTextureSprite2D (int Width, int Height, int Bpp, void* pixe
GLTextureSprite2D::~GLTextureSprite2D()
{
- glDeleteTextures(1, &glTexture);
- glDeleteTextures(1, &glPaletteTexture);
- glDeleteTextures(1, &glMaskTexture);
+ if (glTexture != 0) glDeleteTextures(1, &glTexture);
+ if (glMaskTexture != 0) glDeleteTextures(1, &glMaskTexture);
+ if (IsPaletted() && glPaletteTexture != 0)
+ GLPaletteManager::RemovePaletteTexture(currentPalette, colorKeyIndex);
}
GLTextureSprite2D::GLTextureSprite2D(const GLTextureSprite2D &obj) : Sprite2D(obj)
@@ -59,7 +61,6 @@ GLTextureSprite2D* GLTextureSprite2D::copy() const
return new GLTextureSprite2D(*this);
}
-
void GLTextureSprite2D::SetPalette(Palette *pal)
{
if(!IsPaletted() || currentPalette == pal) return;
@@ -71,8 +72,8 @@ void GLTextureSprite2D::SetPalette(Palette *pal)
{
currentPalette->release();
}
+ GLPaletteManager::RemovePaletteTexture(currentPalette, colorKeyIndex);
currentPalette = pal;
- glDeleteTextures(1, &glPaletteTexture);
glPaletteTexture = 0;
}
@@ -88,8 +89,8 @@ void GLTextureSprite2D::SetColorKey(ieDword index)
colorKeyIndex = index;
if(IsPaletted())
{
- glDeleteTextures(1, &glPaletteTexture);
glDeleteTextures(1, &glMaskTexture);
+ GLPaletteManager::RemovePaletteTexture(currentPalette, colorKeyIndex);
glPaletteTexture = 0;
glMaskTexture = 0;
}
@@ -115,7 +116,7 @@ Color GLTextureSprite2D::GetPixel(unsigned short x, unsigned short y) const
void GLTextureSprite2D::createGlTexture()
{
- glDeleteTextures(1, &glTexture);
+ if (glTexture != 0) glDeleteTextures(1, &glTexture);
glGenTextures(1, &glTexture);
glBindTexture(GL_TEXTURE_2D, glTexture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@@ -151,28 +152,7 @@ void GLTextureSprite2D::createGlTexture()
void GLTextureSprite2D::createGlTextureForPalette()
{
- glDeleteTextures(1, &glPaletteTexture);
- glGenTextures(1, &glPaletteTexture);
- glBindTexture(GL_TEXTURE_2D, glPaletteTexture);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- Color* colors = new Color[256];
- memcpy(colors, currentPalette->col, sizeof(Color)*256);
- for(unsigned int i=0; i<256; i++)
- {
- if(colors[i].a == 0)
- {
- colors[i].a = 0xFF;
- }
- if(i == colorKeyIndex)
- colors[i].a = 0;
- }
- glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
- glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) colors);
- delete[] colors;
+ glPaletteTexture = GLPaletteManager::CreatePaletteTexture(currentPalette, colorKeyIndex);
}
void GLTextureSprite2D::createGLMaskTexture()
View
2  gemrb/plugins/SDLVideo/GLTextureSprite2D.h
@@ -30,7 +30,7 @@ namespace GemRB
Color GetPixel(unsigned short x, unsigned short y) const;
ieDword GetColorKey() const { return colorKeyIndex; }
void SetColorKey(ieDword);
- bool IsPaletted() const { return Bpp != 32; }
+ bool IsPaletted() const { return Bpp == 8; }
GLTextureSprite2D (int Width, int Height, int Bpp, void* pixels, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0);
~GLTextureSprite2D();
GLTextureSprite2D(const GLTextureSprite2D &obj);
View
2  gemrb/plugins/SDLVideo/SDL20GLVideo.cpp
@@ -15,6 +15,7 @@
#include "Interface.h"
#include "Game.h" // for GetGlobalTint
#include "GLTextureSprite2D.h"
+#include "GLPaletteManager.h"
#include "Shader.h"
#include "Matrix.h"
@@ -136,6 +137,7 @@ GLVideoDriver::~GLVideoDriver()
glDeleteProgram(programPalGrayed);
glDeleteProgram(programPalSepia);
glDeleteProgram(programRect);
+ GLPaletteManager::Clear();
SDL_GL_DeleteContext(context);
}
Please sign in to comment.
Something went wrong with that request. Please try again.