From 2fd89f11d5eeadad04294e6c76d5459bc1f10fcc Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 13 Dec 2011 22:50:14 +0000 Subject: [PATCH] - performance improve for color picking using pbo --- source/shared_lib/include/graphics/model.h | 3 +- source/shared_lib/sources/graphics/model.cpp | 29 ++++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/source/shared_lib/include/graphics/model.h b/source/shared_lib/include/graphics/model.h index 31ca3398..498df48a 100644 --- a/source/shared_lib/include/graphics/model.h +++ b/source/shared_lib/include/graphics/model.h @@ -225,6 +225,7 @@ public: static Pixmap2D *getPixelBufferFor(int x,int y,int w,int h, int colorComponents); static void begin(); static void end(); + static bool getIsPBOEnable() { return isPBOEnabled; } private: static bool isPBOEnabled; @@ -239,7 +240,7 @@ class BaseColorPickEntity { public: BaseColorPickEntity(); - static const int COLOR_COMPONENTS = 3; + static const int COLOR_COMPONENTS = 4; static void init(int bufferSize); static void beginPicking(); static void endPicking(); diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index 7e0e56ca..1749b6cd 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -1002,9 +1002,9 @@ Pixmap2D *PixelBufferWrapper::getPixelBufferFor(int x,int y,int w,int h, int col // OpenGL should perform asynch DMA transfer, so glReadPixels() will return immediately. glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[index]); - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, 0); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + //glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, 0); + //glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // measure the time reading framebuffer //t1.stop(); @@ -1195,15 +1195,16 @@ vector BaseColorPickEntity::getPickedList(int x,int y,int w,int h, const ve //printf("In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); static Chrono lastSnapshot(true); + const int selectionMillisecondUpdate = 350; - Pixmap2D *pixmapScreenShot = BaseColorPickEntity::pbo->getPixelBufferFor(x,y,w,h, COLOR_COMPONENTS); - if(pixmapScreenShot != NULL) { - // Only update the pixel buffer every 350 milliseconds or as required + if(PixelBufferWrapper::getIsPBOEnable() == true) { + // Only update the pixel buffer every x milliseconds or as required if(cachedPixels.get() == NULL || cachedPixelsW != w+1 || cachedPixelsH != h+1 || - lastSnapshot.getMillis() > 350) { + lastSnapshot.getMillis() > selectionMillisecondUpdate) { //printf("Updating selection millis = %ld\n",lastSnapshot.getMillis()); lastSnapshot.reset(); + Pixmap2D *pixmapScreenShot = BaseColorPickEntity::pbo->getPixelBufferFor(x,y,w,h, COLOR_COMPONENTS); cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]); memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount()); @@ -1214,18 +1215,18 @@ vector BaseColorPickEntity::getPickedList(int x,int y,int w,int h, const ve } } else { - // Only update the pixel buffer every 350 milliseconds or as required + // Only update the pixel buffer every x milliseconds or as required if(cachedPixels.get() == NULL || cachedPixelsW != w+1 || cachedPixelsH != h+1 || - lastSnapshot.getMillis() > 350) { + lastSnapshot.getMillis() > selectionMillisecondUpdate) { //printf("Updating selection millis = %ld\n",lastSnapshot.getMillis()); lastSnapshot.reset(); - pixmapScreenShot = new Pixmap2D(w+1, h+1, COLOR_COMPONENTS); - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); - //glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + Pixmap2D *pixmapScreenShot = new Pixmap2D(w+1, h+1, COLOR_COMPONENTS); + //glPixelStorei(GL_PACK_ALIGNMENT, 1); + //glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); + glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); + //glPixelStorei(GL_UNPACK_ALIGNMENT, 1); cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]); memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());