From 74da39e83ea8b9fb3ab4dd3c1eae596b6a012870 Mon Sep 17 00:00:00 2001 From: SoftCoder Date: Sat, 16 Jan 2016 21:49:36 -0800 Subject: [PATCH] - attempt to avoid crash if color picking index is larger than buffer, increase it --- source/shared_lib/include/graphics/model.h | 4 +++- source/shared_lib/sources/graphics/model.cpp | 25 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/source/shared_lib/include/graphics/model.h b/source/shared_lib/include/graphics/model.h index 08b93d1b7..d01e3dff0 100644 --- a/source/shared_lib/include/graphics/model.h +++ b/source/shared_lib/include/graphics/model.h @@ -250,7 +250,7 @@ public: PixelBufferWrapper(int pboCount,int bufferSize); ~PixelBufferWrapper(); - static Pixmap2D *getPixelBufferFor(int x,int y,int w,int h, int colorComponents); + Pixmap2D *getPixelBufferFor(int x,int y,int w,int h, int colorComponents); static void begin(); static void end(); static bool getIsPBOEnable() { return isPBOEnabled; } @@ -259,8 +259,10 @@ private: static bool isPBOEnabled; static int index; static vector pboIds; + int bufferSize; void cleanup(); + void addBuffersToPixelBuf(int pboCount); }; class BaseColorPickEntity { diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index d261e14d8..347414604 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -1760,6 +1760,7 @@ int PixelBufferWrapper::index = 0; vector PixelBufferWrapper::pboIds; PixelBufferWrapper::PixelBufferWrapper(int pboCount,int bufferSize) { + this->bufferSize = bufferSize; //if(isGlExtensionSupported("GL_ARB_pixel_buffer_object") == true && if(GLEW_ARB_pixel_buffer_object) { PixelBufferWrapper::isPBOEnabled = true; @@ -1769,6 +1770,7 @@ PixelBufferWrapper::PixelBufferWrapper(int pboCount,int bufferSize) { //glGenBuffersARB(pboCount, (GLuint*)&pboIds[0]); // + /* for(int i = 0; i < pboCount; ++i) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("PBO Gen i = %d\n",i); @@ -1780,9 +1782,25 @@ PixelBufferWrapper::PixelBufferWrapper(int pboCount,int bufferSize) { glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, bufferSize, 0, GL_STREAM_READ_ARB); } glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); + */ + addBuffersToPixelBuf(pboCount); } } +void PixelBufferWrapper::addBuffersToPixelBuf(int pboCount) { + for(int i = 0; i < pboCount; ++i) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("PBO Gen i = %d\n",i); + + pboIds.push_back(0); + glGenBuffersARB(1, (GLuint*)&pboIds[i]); + // create pixel buffer objects, you need to delete them when program exits. + // glBufferDataARB with NULL pointer reserves only memory space. + glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[i]); + glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, bufferSize, 0, GL_STREAM_READ_ARB); + } + glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); +} + Pixmap2D *PixelBufferWrapper::getPixelBufferFor(int x,int y,int w,int h, int colorComponents) { Pixmap2D *pixmapScreenShot = NULL; if(PixelBufferWrapper::isPBOEnabled == true) { @@ -1791,6 +1809,13 @@ Pixmap2D *PixelBufferWrapper::getPixelBufferFor(int x,int y,int w,int h, int col // "nextIndex" is used to process pixels in the other PBO index = (index + 1) % 2; + // Check for out of range + if(index >= (int)pboIds.size()) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"Error / Warning in [%s::%s] on line: %d pixel buffer out of range, index: %d size: %d, attempting to expand buffer...\n",__FILE__,__FUNCTION__,__LINE__,index, (int)pboIds.size()); + //throw megaglest_runtime_error(szBuf); + addBuffersToPixelBuf((index - pboIds.size()) + 1); + } // pbo index used for next frame //int nextIndex = (index + 1) % 2;