Skip to content

Commit

Permalink
Convert to much faster (Big O) 2D bin packing algorithm
Browse files Browse the repository at this point in the history
MaxRects time increases exponentially, after adding 18000 items MaxRects takes 1000 times longer than Skyline. This is unacceptable with ~110,000 sprites. Skyline packing efficiency is pretty good too
  • Loading branch information
grantramsay committed Mar 18, 2019
1 parent 8ce51e6 commit efadd7e
Show file tree
Hide file tree
Showing 10 changed files with 1,343 additions and 620 deletions.
6 changes: 4 additions & 2 deletions components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ set(RenderFiles
render/misc.h
render/atlastexture.cpp
render/atlastexture.h
../extern/RectangleBinPack/MaxRectsBinPack.cpp
../extern/RectangleBinPack/MaxRectsBinPack.h
../extern/RectangleBinPack/GuillotineBinPack.cpp
../extern/RectangleBinPack/GuillotineBinPack.h
../extern/RectangleBinPack/Rect.cpp
../extern/RectangleBinPack/Rect.h
../extern/RectangleBinPack/SkylineBinPack.cpp
../extern/RectangleBinPack/SkylineBinPack.h
)
add_library(Render ${RenderFiles})
target_link_libraries(Render PUBLIC Cel Levels nuklear SDL2::SDL2 SDL_image::SDL_image ${OPENGL_LIBRARIES})
Expand Down
6 changes: 3 additions & 3 deletions components/render/atlastexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* be missing if the array texture was allocated with > 1 depth (layers)... */

#include "atlastexture.h"
#include "../../extern/RectangleBinPack/MaxRectsBinPack.h"
#include "../../extern/RectangleBinPack/SkylineBinPack.h"

#include <boost/make_unique.hpp>
#include <misc/assert.h>
Expand Down Expand Up @@ -35,7 +35,7 @@ namespace Render
glGenTextures(mTextureLayers, &mTextureArrayIds[0]);

for (int32_t layer = 0; layer < mTextureLayers; layer++)
mBinPacker.push_back(boost::make_unique<rbp::MaxRectsBinPack>(mTextureWidth, mTextureHeight, false));
mBinPacker.push_back(boost::make_unique<rbp::SkylineBinPack>(mTextureWidth, mTextureHeight, false, false));

GLuint fbo;
glGenFramebuffers(1, &fbo);
Expand Down Expand Up @@ -106,7 +106,7 @@ namespace Render
int32_t layer;
for (layer = 0; layer < mTextureLayers; layer++)
{
packedPos = mBinPacker[layer]->Insert(paddedWidth, paddedHeight, rbp::MaxRectsBinPack::RectBestAreaFit);
packedPos = mBinPacker[layer]->Insert(paddedWidth, paddedHeight, rbp::SkylineBinPack::LevelMinWasteFit);
if (packedPos.height != 0)
break;
}
Expand Down
4 changes: 2 additions & 2 deletions components/render/atlastexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace rbp
{
class MaxRectsBinPack;
class SkylineBinPack;
}

namespace Render
Expand Down Expand Up @@ -50,6 +50,6 @@ namespace Render
GLint mTextureLayers;
AtlasTextureLookupMap mLookupMap;
size_t mNextTextureId = 1;
std::vector<std::unique_ptr<rbp::MaxRectsBinPack>> mBinPacker;
std::vector<std::unique_ptr<rbp::SkylineBinPack>> mBinPacker;
};
}
2 changes: 1 addition & 1 deletion components/render/sdl2backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// clang-format off
#include <misc/disablewarn.h>
#include "../../extern/jo_gif/jo_gif.cpp"
#include "../../extern/RectangleBinPack/MaxRectsBinPack.h"
#include "../../extern/RectangleBinPack/SkylineBinPack.h"
#include <misc/enablewarn.h>
// clang-format on

Expand Down
Loading

0 comments on commit efadd7e

Please sign in to comment.