- fixed pixel buffer logic on windows (wacky vc++ 2008 doesn't properly allocate memory on vector.reserve();

This commit is contained in:
Mark Vejvoda 2011-12-13 22:21:21 +00:00
parent dc4ba27048
commit 724d274367
3 changed files with 24 additions and 11 deletions

View File

@ -229,7 +229,9 @@ public:
private:
static bool isPBOEnabled;
static int index;
static vector<unsigned int> pboIds;
static vector<uint32> pboIds;
void cleanup();
};
class BaseColorPickEntity {

View File

@ -964,12 +964,18 @@ int PixelBufferWrapper::index = 0;
vector<unsigned int> PixelBufferWrapper::pboIds;
PixelBufferWrapper::PixelBufferWrapper(int pboCount,int bufferSize) {
if(isGlExtensionSupported("GL_ARB_pixel_buffer_object")) {
//if(isGlExtensionSupported("GL_ARB_pixel_buffer_object") == true &&
if(GLEW_ARB_pixel_buffer_object) {
PixelBufferWrapper::isPBOEnabled = true;
pboIds.reserve(pboCount);
glGenBuffersARB(pboCount, &pboIds[0]);
cleanup();
// For some wacky reason this fails in VC++ 2008
//pboIds.reserve(pboCount);
//glGenBuffersARB(pboCount, (GLuint*)&pboIds[0]);
//
for(unsigned int i = 0; i < pboCount; ++i) {
for(int i = 0; i < pboCount; ++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]);
@ -1013,7 +1019,7 @@ Pixmap2D *PixelBufferWrapper::getPixelBufferFor(int x,int y,int w,int h, int col
GLubyte* src = (GLubyte*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB);
if(src) {
pixmapScreenShot = new Pixmap2D(w+1, h+1, colorComponents);
memcpy(pixmapScreenShot->getPixels(),src,pixmapScreenShot->getPixelByteCount());
memcpy(pixmapScreenShot->getPixels(),src,(size_t)pixmapScreenShot->getPixelByteCount());
glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer
@ -1044,7 +1050,7 @@ void PixelBufferWrapper::end() {
}
}
PixelBufferWrapper::~PixelBufferWrapper() {
void PixelBufferWrapper::cleanup() {
if(PixelBufferWrapper::isPBOEnabled == true) {
if(pboIds.empty() == false) {
glDeleteBuffersARB(pboIds.size(), &pboIds[0]);
@ -1053,6 +1059,10 @@ PixelBufferWrapper::~PixelBufferWrapper() {
}
}
PixelBufferWrapper::~PixelBufferWrapper() {
cleanup();
}
//unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = {1, 1, 1, 1};
unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = { 1, 1, 1 };
Mutex BaseColorPickEntity::mutexNextColorID;
@ -1195,8 +1205,8 @@ vector<int> BaseColorPickEntity::getPickedList(int x,int y,int w,int h, const ve
lastSnapshot.reset();
cachedPixels.reset(new unsigned char[pixmapScreenShot->getPixelByteCount()]);
memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),pixmapScreenShot->getPixelByteCount());
cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]);
memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());
cachedPixelsW = w+1;
cachedPixelsH = h+1;
@ -1217,8 +1227,8 @@ vector<int> BaseColorPickEntity::getPickedList(int x,int y,int w,int h, const ve
//glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels());
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
cachedPixels.reset(new unsigned char[pixmapScreenShot->getPixelByteCount()]);
memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),pixmapScreenShot->getPixelByteCount());
cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]);
memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());
cachedPixelsW = w+1;
cachedPixelsH = h+1;

View File

@ -173,6 +173,7 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits,bool
//return 1;
throw std::runtime_error((char *)glewGetErrorString(err));
}
//fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
int bufferSize = (resW * resH * BaseColorPickEntity::COLOR_COMPONENTS);
BaseColorPickEntity::init(bufferSize);