Skip to content

Commit

Permalink
Release colormap & font before window destroy
Browse files Browse the repository at this point in the history
Also cleaned up colormap_impl class implementation
  • Loading branch information
9prady9 committed Dec 17, 2018
1 parent 5ff12c8 commit d2f4bc7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 89 deletions.
4 changes: 3 additions & 1 deletion examples/cpu/histogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void kernel(Bitmap& bmp);

void populateBins(Bitmap& bmp, int *hist_array, const unsigned nbins, float *hist_cols);

int main(void)
int main(int argc, char* argv[])
{
Bitmap bmp = createBitmap(IMGW, IMGH);
/*
Expand Down Expand Up @@ -86,6 +86,8 @@ int main(void)
createGLBuffer(&handles[1], hist.vertices(), FORGE_VERTEX_BUFFER);
createGLBuffer(&handles[2], hist.colors(), FORGE_VERTEX_BUFFER);

wnd.setColorMap((fg_color_map)(argc == 2 ? atoi(argv[1]) : 1));

do {
/*
* generate image, and prepare data to pass into
Expand Down
87 changes: 25 additions & 62 deletions src/backend/opengl/colormap_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,85 +22,48 @@ namespace opengl
{

colormap_impl::colormap_impl()
:mDefaultMapBuffer(0), mSpecMapBuffer(0), mRainbowMapBuffer(0),
mRedMapBuffer(0), mMoodMapBuffer(0), mHeatMapBuffer(0),
mBlueMapBuffer(0)
{
using namespace forge::common;

size_t channel_bytes = sizeof(float)*4; /* 4 is for 4 channels */
mDefMapLen = (GLuint)(sizeof(cmap_default) / channel_bytes);
mSpecMapLen = (GLuint)(sizeof(cmap_spectrum) / channel_bytes);
mRainbowMapLen = (GLuint)(sizeof(cmap_rainbow) / channel_bytes);
mRedMapLen = (GLuint)(sizeof(cmap_red) / channel_bytes);
mMoodMapLen = (GLuint)(sizeof(cmap_mood) / channel_bytes);
mHeatMapLen = (GLuint)(sizeof(cmap_heat) / channel_bytes);
mBlueMapLen = (GLuint)(sizeof(cmap_blue) / channel_bytes);
mInfernoMapLen = (GLuint)(sizeof(cmap_inferno) / channel_bytes);
mMagmaMapLen = (GLuint)(sizeof(cmap_magma) / channel_bytes);
mPlasmaMapLen = (GLuint)(sizeof(cmap_plasma) / channel_bytes);
mViridisMapLen = (GLuint)(sizeof(cmap_viridis) / channel_bytes);
mMapLens[0] = (GLuint)(sizeof(cmap_default) / channel_bytes);
mMapLens[1] = (GLuint)(sizeof(cmap_spectrum) / channel_bytes);
mMapLens[2] = (GLuint)(sizeof(cmap_rainbow) / channel_bytes);
mMapLens[3] = (GLuint)(sizeof(cmap_red) / channel_bytes);
mMapLens[4] = (GLuint)(sizeof(cmap_mood) / channel_bytes);
mMapLens[5] = (GLuint)(sizeof(cmap_heat) / channel_bytes);
mMapLens[6] = (GLuint)(sizeof(cmap_blue) / channel_bytes);
mMapLens[7] = (GLuint)(sizeof(cmap_inferno) / channel_bytes);
mMapLens[8] = (GLuint)(sizeof(cmap_magma) / channel_bytes);
mMapLens[9] = (GLuint)(sizeof(cmap_plasma) / channel_bytes);
mMapLens[10] = (GLuint)(sizeof(cmap_viridis) / channel_bytes);

mDefaultMapBuffer = CREATE_UNIFORM_BUFFER(cmap_default, mDefMapLen);
mSpecMapBuffer = CREATE_UNIFORM_BUFFER(cmap_spectrum, mSpecMapLen);
mRainbowMapBuffer = CREATE_UNIFORM_BUFFER(cmap_rainbow, mRainbowMapLen);
mRedMapBuffer = CREATE_UNIFORM_BUFFER(cmap_red, mRedMapLen);
mMoodMapBuffer = CREATE_UNIFORM_BUFFER(cmap_mood, mMoodMapLen);
mHeatMapBuffer = CREATE_UNIFORM_BUFFER(cmap_heat, mHeatMapLen);
mBlueMapBuffer = CREATE_UNIFORM_BUFFER(cmap_blue, mBlueMapLen);
mInfernoMapBuffer = CREATE_UNIFORM_BUFFER(cmap_inferno, mInfernoMapLen);
mMagmaMapBuffer = CREATE_UNIFORM_BUFFER(cmap_magma, mMagmaMapLen);
mPlasmaMapBuffer = CREATE_UNIFORM_BUFFER(cmap_plasma, mPlasmaMapLen);
mViridisMapBuffer = CREATE_UNIFORM_BUFFER(cmap_viridis, mViridisMapLen);
mMapIds[0] = CREATE_UNIFORM_BUFFER(cmap_default , mMapLens[0]) ;
mMapIds[1] = CREATE_UNIFORM_BUFFER(cmap_spectrum, mMapLens[0]) ;
mMapIds[2] = CREATE_UNIFORM_BUFFER(cmap_rainbow , mMapLens[0]) ;
mMapIds[3] = CREATE_UNIFORM_BUFFER(cmap_red , mMapLens[0]) ;
mMapIds[4] = CREATE_UNIFORM_BUFFER(cmap_mood , mMapLens[0]) ;
mMapIds[5] = CREATE_UNIFORM_BUFFER(cmap_heat , mMapLens[0]) ;
mMapIds[6] = CREATE_UNIFORM_BUFFER(cmap_blue , mMapLens[0]) ;
mMapIds[7] = CREATE_UNIFORM_BUFFER(cmap_inferno , mMapLens[0]) ;
mMapIds[8] = CREATE_UNIFORM_BUFFER(cmap_magma , mMapLens[0]) ;
mMapIds[9] = CREATE_UNIFORM_BUFFER(cmap_plasma , mMapLens[0]) ;
mMapIds[10] = CREATE_UNIFORM_BUFFER(cmap_viridis , mMapLens[0]) ;
}

colormap_impl::~colormap_impl()
{
glDeleteBuffers(1, &mDefaultMapBuffer);
glDeleteBuffers(1, &mSpecMapBuffer);
glDeleteBuffers(1, &mRainbowMapBuffer);
glDeleteBuffers(1, &mRedMapBuffer);
glDeleteBuffers(1, &mMoodMapBuffer);
glDeleteBuffers(1, &mHeatMapBuffer);
glDeleteBuffers(1, &mBlueMapBuffer);
glDeleteBuffers(1, &mInfernoMapBuffer);
glDeleteBuffers(1, &mMagmaMapBuffer);
glDeleteBuffers(1, &mPlasmaMapBuffer);
glDeleteBuffers(1, &mViridisMapBuffer);
glDeleteBuffers(ForgeNumColorMaps, mMapIds.data());
}

GLuint colormap_impl::cmapUniformBufferId(forge::ColorMap cmap) const
{
switch(cmap) {
case FG_COLOR_MAP_SPECTRUM: return mSpecMapBuffer;
case FG_COLOR_MAP_RAINBOW : return mRainbowMapBuffer;
case FG_COLOR_MAP_RED : return mRedMapBuffer;
case FG_COLOR_MAP_MOOD : return mMoodMapBuffer;
case FG_COLOR_MAP_HEAT : return mHeatMapBuffer;
case FG_COLOR_MAP_BLUE : return mBlueMapBuffer;
case FG_COLOR_MAP_INFERNO : return mInfernoMapBuffer;
case FG_COLOR_MAP_MAGMA : return mMagmaMapBuffer;
case FG_COLOR_MAP_PLASMA : return mPlasmaMapBuffer;
case FG_COLOR_MAP_VIRIDIS : return mViridisMapBuffer;
default: return mDefaultMapBuffer;
}
return mMapIds[static_cast<unsigned int>(cmap)];
}

GLuint colormap_impl::cmapLength(forge::ColorMap cmap) const
{
switch(cmap) {
case FG_COLOR_MAP_SPECTRUM: return mSpecMapLen;
case FG_COLOR_MAP_RAINBOW : return mRainbowMapLen;
case FG_COLOR_MAP_RED : return mRedMapLen;
case FG_COLOR_MAP_MOOD : return mMoodMapLen;
case FG_COLOR_MAP_HEAT : return mHeatMapLen;
case FG_COLOR_MAP_BLUE : return mBlueMapLen;
case FG_COLOR_MAP_INFERNO : return mInfernoMapLen;
case FG_COLOR_MAP_MAGMA : return mMagmaMapLen;
case FG_COLOR_MAP_PLASMA : return mPlasmaMapLen;
case FG_COLOR_MAP_VIRIDIS : return mViridisMapLen;
default: return mDefMapLen;
}
return mMapLens[static_cast<unsigned int>(cmap)];
}

}
Expand Down
31 changes: 5 additions & 26 deletions src/backend/opengl/colormap_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#pragma once

#include <common.hpp>
#include <memory>
#include <array>

namespace forge
{
namespace opengl
{

constexpr unsigned int ForgeNumColorMaps = 11;

class colormap_impl {
private:
/*
Expand All @@ -32,33 +34,10 @@ class colormap_impl {
* the size of array declared in the shaders
* used by *_impl objects to reflect appropriate
* size */
gl::GLuint mDefaultMapBuffer;
gl::GLuint mSpecMapBuffer;
gl::GLuint mRainbowMapBuffer;
gl::GLuint mRedMapBuffer;
gl::GLuint mMoodMapBuffer;
gl::GLuint mHeatMapBuffer;
gl::GLuint mBlueMapBuffer;
gl::GLuint mInfernoMapBuffer;
gl::GLuint mMagmaMapBuffer;
gl::GLuint mPlasmaMapBuffer;
gl::GLuint mViridisMapBuffer;

/* Current color map lengths */
gl::GLuint mDefMapLen;
gl::GLuint mSpecMapLen;
gl::GLuint mRainbowMapLen;
gl::GLuint mRedMapLen;
gl::GLuint mMoodMapLen;
gl::GLuint mHeatMapLen;
gl::GLuint mBlueMapLen;
gl::GLuint mInfernoMapLen;
gl::GLuint mMagmaMapLen;
gl::GLuint mPlasmaMapLen;
gl::GLuint mViridisMapLen;
std::array<gl::GLuint, ForgeNumColorMaps> mMapIds;
std::array<gl::GLuint, ForgeNumColorMaps> mMapLens;

public:
/* constructors and destructors */
colormap_impl();
~colormap_impl();

Expand Down
3 changes: 3 additions & 0 deletions src/backend/opengl/window_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ window_impl::window_impl(int pWidth, int pHeight, const char* pTitle,

window_impl::~window_impl()
{
mCMap.reset();
mFont.reset();
mWidget.reset();
destroyWtkIfDone();
}

Expand Down

0 comments on commit d2f4bc7

Please sign in to comment.