Posssible memory leak in OpenGL Intel Win7 drivers

Started by
35 comments, last by mshafey 12 years, 2 months ago
Hi.

I have been experiencing a memory leak when using pixel buffer objects to update textures. The leak seems to only occur on certain platforms with certain operating systems, and so we began to suspect the leak is occurring inside the Intel HD Series Windows 7 driver. Below is a summary of our testing on different platforms.

Can anyone tell if I'm doing something wrong in the code, or if this is potentially a driver leak?

Series 4 Chipset (Lenovo SL300), Windows XP SP3: No Leak

Series 4 Chipset (Lenovo SL300), Windows 7: Leaks ~500 kB/min

Intel HD Series (Lenovo X1), Windows 7: Leaks ~500 kB/min

Intel HD 3000 (11" MacBook Air) Mac OS 10.7.3: No Leak

Nvidia Quadro NVS, Windows XP: No Leak

Here is a stripped down version of the code to reproduce this issue (VS2008 project at http://www.viionsyst...k_Test_Case.zip). Extensive testing of this code shows no memory leaks detectable by VS2008's memory leak detector, yet GPU memory seems to grow indefinitely (according to ProcessExplorer).

I would appreciate any thoughts from the community on this issue.


#include <stdio.h>
#include <windows.h>
#include "GL\GLee.h"
#include "GL\freeglut.h"

unsigned int w = 640;
unsigned int h = 480;
unsigned int s = 4;

char* img = NULL;
char* texData1 = NULL;
char* texData2 = NULL;
char* mappedBuf = NULL;

GLuint pixelbufferHandle;

void timerCallback(int value);
void initializeTextureBuffer();
void mapAndCopyToBuffer(char* img1);
void paintGL();
void changeSize(int w, int h);

GLuint errorCode;

#define checkForGLError() \
if ((errorCode = glGetError()) != GL_NO_ERROR) \
printf("OpenGL error at %s line %i: %s", __FILE__, __LINE__-1, gluErrorString(errorCode) );


int main(int argc, char **argv)
{
texData1 = new char[w * h * s];
texData2 = new char[w * h * s];
memset(texData1, 85, w * h * s);
memset(texData2, 170, w * h * s);

img = texData1;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

glutInitWindowPosition(300, 300);
glutInitWindowSize(w, h);
glutCreateWindow("Window");
glutDisplayFunc(paintGL);
glutReshapeFunc(changeSize);

initializeTextureBuffer();

timerCallback(0);

glutMainLoop();

glDeleteBuffers(1, &pixelbufferHandle);
delete[] texData1;
delete[] texData2;

return 0;
}

void initializeTextureBuffer()
{
glGenBuffers(1, &pixelbufferHandle);
checkForGLError();
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);
checkForGLError();

glBufferData(GL_PIXEL_UNPACK_BUFFER, w * h * s, 0, GL_DYNAMIC_DRAW);

checkForGLError();

// initialize and upload the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
checkForGLError();

// Specify filtering and edge actions

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

img = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);

if (img == NULL){
return;
}

memset(img, 0, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
}

void mapAndCopyToBuffer(char* img1)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(mappedBuf, img1, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
mappedBuf = NULL;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}

void paintGL()
{
if (img == texData1) img = texData2;
else img = texData1; // swap images

mapAndCopyToBuffer(img);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glDisable(GL_BLEND);
glDepthMask(GL_FALSE);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_MULTISAMPLE);

glBegin(GL_QUADS);
{
glTexCoord2f(0,0); glVertex3f(-1, 1, 0);
glTexCoord2f(1,0); glVertex3f(1, 1, 0);
glTexCoord2f(1,1); glVertex3f(1, -1, 0);
glTexCoord2f(0,1); glVertex3f(-1, -1, 0);
}
glEnd();

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glutSwapBuffers();
}

void changeSize(int w, int h)
{
glViewport(0, 0, w, h);
}

void timerCallback(int value)
{
glutPostRedisplay();
glutTimerFunc(5, timerCallback, 0);
}
Advertisement
void mapAndCopyToBuffer(char* img1)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(mappedBuf, img1, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
mappedBuf = NULL;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}


seems like you don't free your mappedBuf after using it. you need to do this if you want to really free some memory:


void mapAndCopyToBuffer(char* img1)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(mappedBuf, img1, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
delete [] mappedBuf; //this is what you need, see explanation below
mappedBuf = NULL;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}




this indicates you're not completely aware of how to use pointers.

to clear things up:


int* pointer = 0; // creates an 8-byte pointer to an integer, pointer's value is 0 (Null-pointer) EDIT: yep it was unintialized...
int value = *pointer; // ERROR: you tried to access the contents of a null-pointer, this is illegal
pointer = new int; // dynamically (at runtime) assigns 4 bytes of memory at the place which is pointed by pointer, pointer is now valid
int value_2 = *pointer; // VALID: pointer actually points to somewhere, so value_2 should be either 0 or a random number, this depends on the compiler
delete pointer; // tells the operating system to free the place pointed by pointer
int value_3 = *pointer; // VALID: pointer still points to somewhere in the memory, and most probably you'll get back the old value pointed by pointer, since nothing has overwritten it. BUT this cannot be guaranteed (so DO NOT do this)
pointer = 0; // now pointer is a null-pointer again, but this doesn't free up any space, as opposed to delete
int value_4 = *pointer; //ERROR: you tried to access a null pointer again.
// for arrays you need to use this:
pointer = new int[32]; // allocate 32 integers (4 bytes per int) to the place pointed by pointer
//pointer will actually point to the first element of the array meaning this will be valid, and give a value:
int value_5 = *pointer; // VALID: gives back pointer[0]
delete [] pointer; //this frees up the space pointed by pointer, but pointer will still be valid, so you need to set it to 0
pointer = 0; // now pointer is a null-pointer again
// to check wether pointer is valid you can use a simple if
if(pointer) // if pointer is 0 (logical false) it will be invalid (null-pointer) else it will be valid
{
std::cout << "Pointer is valid.\n";
}
else
{
std::cout << "Pointer is INvalid.\n";
}


oh and please write back if this solved your problem.

best regards,
Yours3!f

seems like you don't free your mappedBuf after using it. you need to do this if you want to really free some memory:


But we didn't allocate any memory here. What I understand is that after the call to glMapBuffer(), mappedBuf should be treated as GPU memory, so it's not up to us to free it. We copy the texture into this mapped memory, then call glUnmapBuffer() to indicate to OpenGL that we're done copying/modifying that buffer (the buffer that belongs to the driver, not us).

Just to be sure, I tried to delete[] mappedBuf now (before the statement where I set it to NULL). It crashes.

But we didn't allocate any memory here. What I understand is that after the call to glMapBuffer(), mappedBuf should be treated as GPU memory, so it's not up to us to free it. We copy the texture into this mapped memory, then call glUnmapBuffer() to indicate to OpenGL that we're done copying/modifying that buffer (the buffer that belongs to the driver, not us).


Any pointer in your code resides in CPU memory. The data contained within may come from GPU memory, but the pointer still needs to be declared in your code.

This function doesn't make any sense:

[source]
void mapAndCopyToBuffer(char* img1)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(mappedBuf, img1, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
mappedBuf = NULL;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}[/source]

The memcpy is copying img1 into mappedBuf and then you are setting mappedBuf to NULL. I suspect that you meant: memcpy(img1,mappedBuf,w * h * s);?

Also, since you know that img1 and mappedBuf will be w * h * s bytes, and that mappedBuf is only used in this function, why not do this:

[source]
void mapAndCopyToBuffer(char* img1)
{
char *mappedBuf;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = new char[w * h * s];
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(img1, mappedBuf, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
delete [] mappedBuf;
mappedBuf = NULL;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}
[/source]
@Yours3!f - glMapBuffer doesn't work that way. I strongly suggest that you read the documentation for it. The "fix" you gave will actually crash the program (if you're lucky).
@MarkS - the code does make sense. That's the way glMapBuffer works, and it's correct and in accordance with the documentation (aside from an extra unnecessary glBindBuffer which shouldn't be a cause of the observed leak).

@OP: have you tried this using glBufferSubData instead of glMapBuffer? It would be interesting to know if the same symptoms are observed with that. The following should be equivalent:
void mapAndCopyToBuffer(char* img1)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
glBufferSubData (GL_PIXEL_UNPACK_BUFFER_ARB, 0, w * h * s, img1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}


Oh - and make sure that the value of s is 4. ;)

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


seems like you don't free your mappedBuf after using it. you need to do this if you want to really free some memory:

The pointer returned by glMapBuffer shall not be deleted. It is a pointer to memory managed by OpenGL. The memory can be thought of as deleted by glUnmapBuffer, and he is correctly calling it once he's done.


this indicates you're not completely aware of how to use pointers.

Likewise, it shows that you are not thinking about every aspect of pointers; you shall only delete a pointer that you allocated.


This function doesn't make any sense:

[source]
void mapAndCopyToBuffer(char* img1)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(mappedBuf, img1, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
mappedBuf = NULL;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}[/source]

The memcpy is copying img1 into mappedBuf and then you are setting mappedBuf to NULL. I suspect that you meant: memcpy(img1,mappedBuf,w * h * s);?

First of all, the target is an unpack buffer, which is a buffer you write to. Second, the buffer is mapped as write only. Third, he's copying from the image to the mapped buffer. Fourth, he's unmapping the buffer when he's done. Finally, he's creating a texture from the mapped buffer he just wrote to.

So the chain of commands lines up perfectly for writing to the mapped buffer and updating the texture.


Also, since you know that img1 and mappedBuf will be w * h * s bytes, and that mappedBuf is only used in this function, why not do this:

[source]
void mapAndCopyToBuffer(char* img1)
{
char *mappedBuf;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = new char[w * h * s];
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(img1, mappedBuf, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
delete [] mappedBuf;
mappedBuf = NULL;

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}
[/source]

This, on the other hand, makes little sense. Reading from a write-to buffer target? Reading from write-only mapped memory? Allocating memory with new, only to overwrite the pointer the line after? Attempting to delete the pointer returned by glMapBuffer? The logical flow of the function makes little sense also by reading from a buffer and then creating a texture from the same buffer.
@OP: have you tried this using glBufferSubData instead of glMapBuffer? It would be interesting to know if the same symptoms are observed with that. The following should be equivalent:
void mapAndCopyToBuffer(char* img1)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
glBufferSubData (GL_PIXEL_UNPACK_BUFFER_ARB, 0, w * h * s, img1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}


Oh - and make sure that the value of s is 4. ;)


Thanks for the suggestion. I've tried your code, it works, but it doesn't fix the leak. I'm still seeing memory leaking at the same rate on the affected platforms.

Thanks for the suggestion. I've tried your code, it works, but it doesn't fix the leak. I'm still seeing memory leaking at the same rate on the affected platforms.

That's interesting. Let's try to pin down which call is causing it. What happens if you comment out the glTexSubImage2D call? And if you comment out the glMapBuffer block (but leave glTexSubImage2D in and coming from the PBO)?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Just to be sure, I tried to delete[] mappedBuf now (before the statement where I set it to NULL). It crashes.
[/quote]

ok, so I was wrong smile.png but still you're doing pretty intresting things with those pointers...

I tried to decypher what you're trying to do with the pointers so here it is:

img = 0
img points to where texdata1 does
img points to the write-only memory from where OGL will take back the texture data
fill img with 0-s
copy img-s content back to GPU (unmapping to pixelbufferhandle)
since img points to the write-only memory of OGL, you make it point to texdata1
fill mappedbuf with img (essentially texdata1)
upload mappedbuf to GPU (unmapping to pixelbufferhandle)

loop:
img points to texdata1, so set it to texdata2
fill mappedbuf with img (essentially texdata2)
upload mappedbuf to GPU (unmapping to pixelbufferhandle)

img points to texdata2, so set it to texdata1
fill mappedbuf with img (essentially texdata1)
upload mappedbuf to GPU (unmapping to pixelbufferhandle)
[/quote]

the code essentially sets the texture to grey and white very fast, but with setting glutTimerFunc(5, timerCallback, 0); to glutTimerFunc(120, timerCallback, 0);
I can clearly see this behaviour.

So next I tried to modify the source you've given (on Linux smile.png thanks for the portable code!) so that it makes more sense:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include "GLee.h"
#include "GL/freeglut.h"
unsigned int w = 640;
unsigned int h = 480;
unsigned int s = 4;
char* texData1 = NULL;
char* texData2 = NULL;
char* mappedBuf = NULL;
GLuint pixelbufferHandle;
bool pingpong = true; //so that the first time we use texdata1
void timerCallback ( int value );
void initializeTextureBuffer();
void mapAndCopyToBuffer ( char* img1 );
void paintGL();
void changeSize ( int w, int h );
GLuint errorCode;
#define checkForGLError() \
if ((errorCode = glGetError()) != GL_NO_ERROR) \
printf("OpenGL error at %s line %i: %s", __FILE__, __LINE__-1, gluErrorString(errorCode) );

int main ( int argc, char **argv )
{
texData1 = new char[w * h * s];
texData2 = new char[w * h * s];
memset ( texData1, 0, w * h * s );
memset ( texData2, 255, w * h * s );
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowPosition ( 300, 300 );
glutInitWindowSize ( w, h );
glutCreateWindow ( "Window" );
glutDisplayFunc ( paintGL );
glutReshapeFunc ( changeSize );
glDisable ( GL_BLEND );
glDepthMask ( GL_FALSE );
glDisable ( GL_CULL_FACE );
glDisable ( GL_DEPTH_TEST );
glEnable ( GL_TEXTURE_2D );
glEnable ( GL_MULTISAMPLE );
initializeTextureBuffer();
timerCallback ( 0 );
glutMainLoop();
glDeleteBuffers ( 1, &pixelbufferHandle );
delete[] texData1;
delete[] texData2;
return 0;
}
void initializeTextureBuffer()
{
glGenBuffers ( 1, &pixelbufferHandle );
checkForGLError();
glBindBuffer ( GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle );
checkForGLError();
glBufferData ( GL_PIXEL_UNPACK_BUFFER, w * h * s, 0, GL_DYNAMIC_DRAW );
checkForGLError();
// Specify filtering and edge actions
glTexParameteri ( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP );
glTexParameteri ( GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP );
// initialize and upload the texture
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0 );
checkForGLError();
mappedBuf = ( char* ) glMapBuffer ( GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY );
checkForGLError();
if ( !mappedBuf )
{
std::cerr << "Couldn't create write-only mapping buffer.\n Exiting.\n";
exit ( 1 );
}
memset(mappedBuf, 0, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
checkForGLError();
}
void mapAndCopyToBuffer ( char* img_ptr )
{
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy ( mappedBuf, img_ptr, w * h * s );
if (!glUnmapBuffer ( GL_PIXEL_UNPACK_BUFFER ))
{
std::cerr << "Buffer has already been unmapped.\n Exiting.\n";
exit(1);
}
checkForGLError();
glTexSubImage2D ( GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0 );
checkForGLError();
}
void paintGL()
{
if ( pingpong )
{
mapAndCopyToBuffer ( texData1 );
}
else
{
mapAndCopyToBuffer ( texData2 );
}
pingpong = !pingpong;
glMatrixMode ( GL_MODELVIEW );
glPushMatrix();
glBegin ( GL_QUADS );
{
glTexCoord2f ( 0,0 );
glVertex3f ( -1, 1, 0 );
glTexCoord2f ( 1,0 );
glVertex3f ( 1, 1, 0 );
glTexCoord2f ( 1,1 );
glVertex3f ( 1, -1, 0 );
glTexCoord2f ( 0,1 );
glVertex3f ( -1, -1, 0 );
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void changeSize ( int w, int h )
{
glViewport ( 0, 0, w, h );
}
void timerCallback ( int value )
{
glutPostRedisplay();
glutTimerFunc ( 1, timerCallback, 0 );
}
// kate: indent-mode cstyle; space-indent on; indent-width 0;


EDIT: corrected the not-mapping back issue :)

I don't know if the memory leak is still present with this version since I don't own an intel graphics card... could you please test this?


Just to be sure, I tried to delete[] mappedBuf now (before the statement where I set it to NULL). It crashes.


ok, so I was wrong smile.png but still you're doing pretty intresting things with those pointers...

I tried to decypher what you're trying to do with the pointers so here it is:

img = 0
img points to where texdata1 does
img points to the write-only memory from where OGL will take back the texture data
fill img with 0-s
copy img-s content back to GPU (unmapping to pixelbufferhandle)
since img points to the write-only memory of OGL, you make it point to texdata1
fill mappedbuf with img (essentially texdata1)
upload mappedbuf to GPU (unmapping to pixelbufferhandle)

loop:
img points to texdata1, so set it to texdata2
fill mappedbuf with img (essentially texdata2)
upload mappedbuf to GPU (unmapping to pixelbufferhandle)

img points to texdata2, so set it to texdata1
fill mappedbuf with img (essentially texdata1)
upload mappedbuf to GPU (unmapping to pixelbufferhandle)
[/quote]

the code essentially sets the texture to grey and white very fast, but with setting glutTimerFunc(5, timerCallback, 0); to glutTimerFunc(120, timerCallback, 0);
I can clearly see this behaviour.

So next I tried to modify the source you've given (on Linux smile.png thanks for the portable code!) so that it makes more sense:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include "GLee.h"
#include "GL/freeglut.h"
unsigned int w = 640;
unsigned int h = 480;
unsigned int s = 4;
char* texData1 = NULL;
char* texData2 = NULL;
char* mappedBuf = NULL;
GLuint pixelbufferHandle;
bool pingpong = true; //so that the first time we use texdata1
void timerCallback ( int value );
void initializeTextureBuffer();
void mapAndCopyToBuffer ( char* img1 );
void paintGL();
void changeSize ( int w, int h );
GLuint errorCode;
#define checkForGLError() \
if ((errorCode = glGetError()) != GL_NO_ERROR) \
printf("OpenGL error at %s line %i: %s", __FILE__, __LINE__-1, gluErrorString(errorCode) );

int main ( int argc, char **argv )
{
texData1 = new char[w * h * s];
texData2 = new char[w * h * s];
memset ( texData1, 0, w * h * s );
memset ( texData2, 255, w * h * s );
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowPosition ( 300, 300 );
glutInitWindowSize ( w, h );
glutCreateWindow ( "Window" );
glutDisplayFunc ( paintGL );
glutReshapeFunc ( changeSize );
glDisable ( GL_BLEND );
glDepthMask ( GL_FALSE );
glDisable ( GL_CULL_FACE );
glDisable ( GL_DEPTH_TEST );
glEnable ( GL_TEXTURE_2D );
glEnable ( GL_MULTISAMPLE );
initializeTextureBuffer();
timerCallback ( 0 );
glutMainLoop();
glDeleteBuffers ( 1, &pixelbufferHandle );
delete[] texData1;
delete[] texData2;
return 0;
}
void initializeTextureBuffer()
{
glGenBuffers ( 1, &pixelbufferHandle );
checkForGLError();
glBindBuffer ( GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle );
checkForGLError();
glBufferData ( GL_PIXEL_UNPACK_BUFFER, w * h * s, 0, GL_DYNAMIC_DRAW );
checkForGLError();
// Specify filtering and edge actions
glTexParameteri ( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP );
glTexParameteri ( GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP );
// initialize and upload the texture
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0 );
checkForGLError();
mappedBuf = ( char* ) glMapBuffer ( GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY );
checkForGLError();
if ( !mappedBuf )
{
std::cerr << "Couldn't create write-only mapping buffer.\n Exiting.\n";
exit ( 1 );
}
}
void mapAndCopyToBuffer ( char* img_ptr )
{
memcpy ( mappedBuf, img_ptr, w * h * s );
glUnmapBuffer ( GL_PIXEL_UNPACK_BUFFER );
glTexSubImage2D ( GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0 );
}
void paintGL()
{
if ( pingpong )
{
mapAndCopyToBuffer ( texData1 );
}
else
{
mapAndCopyToBuffer ( texData2 );
}
pingpong = !pingpong;
glMatrixMode ( GL_MODELVIEW );
glPushMatrix();
glBegin ( GL_QUADS );
{
glTexCoord2f ( 0,0 );
glVertex3f ( -1, 1, 0 );
glTexCoord2f ( 1,0 );
glVertex3f ( 1, 1, 0 );
glTexCoord2f ( 1,1 );
glVertex3f ( 1, -1, 0 );
glTexCoord2f ( 0,1 );
glVertex3f ( -1, -1, 0 );
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void changeSize ( int w, int h )
{
glViewport ( 0, 0, w, h );
}
void timerCallback ( int value )
{
glutPostRedisplay();
glutTimerFunc ( 240, timerCallback, 0 );
}
// kate: indent-mode cstyle; space-indent on; indent-width 4;




I don't know if the memory leak is still present with this version since I don't own an intel graphics card... could you please test this?
[/quote]

that should not work, you've kindof botched his original sample, you map the buffer in the initialization routine, but when modifying the pixel buffer, you unmap that pointer(and never map it again(or bind the correct pbo).)

glUnmapBuffer should be throwing errors pretty much after the first draw.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

This topic is closed to new replies.

Advertisement