- attempt to avoid crash if color picking index is larger than buffer, increase it

This commit is contained in:
SoftCoder 2016-01-16 21:49:36 -08:00
parent e7a9c01bf6
commit 74da39e83e
2 changed files with 28 additions and 1 deletions

View File

@ -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<uint32> pboIds;
int bufferSize;
void cleanup();
void addBuffersToPixelBuf(int pboCount);
};
class BaseColorPickEntity {

View File

@ -1760,6 +1760,7 @@ int PixelBufferWrapper::index = 0;
vector<unsigned int> 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;