From 397856089d01f3ed80c680661a5e121161368fff Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Wed, 16 Oct 2013 19:20:36 +0200 Subject: [PATCH] Tilemap: Optimize atlas assembly from mega surface tileset Use 'glPixelStorei()' parameters to upload sub images directly from the same surface instead of creating helper surfaces to upload from. --- src/gl-util.h | 18 ++++++++++++++++++ src/tilemap.cpp | 25 ++++--------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/gl-util.h b/src/gl-util.h index 0bca223..45aa0b9 100644 --- a/src/gl-util.h +++ b/src/gl-util.h @@ -313,6 +313,24 @@ typedef struct GenericBO IBO; #undef DEF_GL_ID +namespace PixelStore +{ + /* Setup a 'glSubTexImage2D()' call where the uploaded image + * itself is part of a bigger image in client memory */ + inline void setupSubImage(GLint imgWidth, GLint subX, GLint subY) + { + glPixelStorei(GL_UNPACK_ROW_LENGTH, imgWidth); + glPixelStorei(GL_UNPACK_SKIP_PIXELS, subX); + glPixelStorei(GL_UNPACK_SKIP_ROWS, subY); + } + + /* Reset all states set with 'setupSubImage()' */ + inline void reset() + { + setupSubImage(0, 0, 0); + } +} + /* Convenience struct wrapping a framebuffer * and a 2D texture as its target */ struct TEXFBO diff --git a/src/tilemap.cpp b/src/tilemap.cpp index 08d9109..c9a5d83 100644 --- a/src/tilemap.cpp +++ b/src/tilemap.cpp @@ -654,33 +654,16 @@ struct TilemapPrivate SDL_Surface *tsSurf = tileset->megaSurface(); - int bpp; - Uint32 rMask, gMask, bMask, aMask; - SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ABGR8888, - &bpp, &rMask, &gMask, &bMask, &aMask); - for (int i = 0; i < blits.count(); ++i) { TileAtlas::Blit &blitOp = blits[i]; - SDL_Surface *blitTemp = - SDL_CreateRGBSurface(0, tsLaneW, blitOp.h, bpp, rMask, gMask, bMask, aMask); + PixelStore::setupSubImage(tsSurf->w, blitOp.src.x, blitOp.src.y); - SDL_Rect tsRect; - tsRect.x = blitOp.src.x; - tsRect.y = blitOp.src.y; - tsRect.w = tsLaneW; - tsRect.h = blitOp.h; - - SDL_Rect tmpRect = tsRect; - tmpRect.x = tmpRect.y = 0; - - SDL_BlitSurface(tsSurf, &tsRect, blitTemp, &tmpRect); - - TEX::uploadSubImage(blitOp.dst.x, blitOp.dst.y, tsLaneW, blitOp.h, blitTemp->pixels, GL_RGBA); - - SDL_FreeSurface(blitTemp); + TEX::uploadSubImage(blitOp.dst.x, blitOp.dst.y, tsLaneW, blitOp.h, tsSurf->pixels, GL_RGBA); } + + PixelStore::reset(); } else {