• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
mshafey

OpenGL
Posssible memory leak in OpenGL Intel Win7 drivers

36 posts in this topic

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: [b]No Leak[/b]

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

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

Intel HD 3000 (11" MacBook Air) Mac OS 10.7.3: [b]No Leak[/b]

Nvidia Quadro NVS, Windows XP: [b]No Leak[/b]

Here is a stripped down version of the code to reproduce this issue (VS2008 project at [url="http://www.viionsystems.com/Intel_HD_Series_Win7_Leak_Test_Case.zip"]http://www.viionsyst...k_Test_Case.zip[/url]). 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.

[code]
#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);
}
[/code]
1

Share this post


Link to post
Share on other sites
[CODE]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);
}[/CODE]

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

[CODE]
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);
}
[/CODE]



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

to clear things up:

[CODE]
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";
}
[/CODE]

oh and please write back if this solved your problem.

best regards,
Yours3!f
-5

Share this post


Link to post
Share on other sites
[quote name='Yours3!f' timestamp='1329240914' post='4913038']
seems like you don't free your mappedBuf after using it. you need to do this if you want to really free some memory:
[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='mshafey' timestamp='1329242080' post='4913052']
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).
[/quote]

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]
-5

Share this post


Link to post
Share on other sites
@Yours3!f - glMapBuffer doesn't work that way. I [b]strongly[/b] 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:
[code]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);
}[/code]

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

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1329245602' post='4913072']@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:
[code]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);
}[/code]

Oh - and make sure that the value of s is 4. ;)
[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='mshafey' timestamp='1329247414' post='4913082']
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.
[/quote]
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)?
1

Share this post


Link to post
Share on other sites
[quote]
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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:
[quote]
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] thanks for the portable code!) so that it makes more sense:
[CODE]
#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;
[/CODE]

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?
-2

Share this post


Link to post
Share on other sites
[quote name='Yours3!f' timestamp='1329257604' post='4913132']
[quote]
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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:
[quote]
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] thanks for the portable code!) so that it makes more sense:
[CODE]
#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;
[/CODE]



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.
0

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1329245602' post='4913072']
@Yours3!f - glMapBuffer doesn't work that way. I [b]strongly[/b] 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:
[code]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);
}[/code]

Oh - and make sure that the value of s is 4. ;)
[/quote]

hehe now you got me. I didn't read the specs for glMapBuffer, but now I see clearly how it works... :)
I just searched the code quickly to find some error prone stuff, but didn't catch the glMapBuffer

I hope the collision course I gave on pointers at least cleared some stuff in his head :)
-2

Share this post


Link to post
Share on other sites
[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.
[/quote]
well it does work, glUnmapBuffer doesn't throw any errors if you add checking after it. It reproduces the same behaviour on Linux Mint 12.1 64 bit HD 5670 Catalyst 12.1
0

Share this post


Link to post
Share on other sites
[quote name='Yours3!f' timestamp='1329258346' post='4913139']
[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.
[/quote]
well it does work, glUnmapBuffer doesn't throw any errors if you add checking after it. It reproduces the same behaviour on Linux Mint 12.1 64 bit HD 5670 Catalyst 12.1
[/quote]
Try check its return value. In any case, here's what the specification has to say about double unmapping:
[quote]
If the buffer data store is already in the unmapped state, UnmapBuffer returns FALSE, and an INVALID_OPERATION error is generated.
[/quote]
I have noticed before that error flags are not always set properly for error conditions when they should be according to the specification. I generally trust set flags to indicate an error, but I rarely trust a reset flag to indicate no error.

It is possible that the pointer returned by the mapping remains valid in your particular case as far as the memory mapper and the content of the buffer is concerned. It is not guaranteed in anyway by the specification though.

edit: And the relevant quote from the specification regarding unmapping the buffer:
[quote]
Unmapping a mapped buffer object invalidates the pointer to its data store [...]
[/quote]
0

Share this post


Link to post
Share on other sites
[quote name='Yours3!f' timestamp='1329258189' post='4913138']
I hope the collision course I gave on pointers at least cleared some stuff in his head [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]
i'm sorry, but he was doing nothing wrong in his initial code when it comes to pointers. and to be fair, he seems to have a better understanding then your "course"

for example:
[code]
int* pointer; // creates an 8-byte pointer to an integer, pointer's value is 0 (Null-pointer)
///////////This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)
//////////Secondly, not defining a value, does [B]NOT[/B] mean it defaults as a null pointer, it is simply undefined(do not rely on uninitialized pointers to equal 0, this is extremely bad practice.)

int value = *pointer; // ERROR: you tried to access the contents of a null-pointer, this is illegal
//again, not invalid, but is undefined behavior(generally seg-fault level, but it is not illegal by compilation to access a null pointer.)

pointer = new int; // dynamically (at runtime) assigns 4 bytes of memory at the place which is pointed by pointer, pointer is now valid
//pointer is not "now" valid, the assumption is that you were successful in creating the memory, but does not gurantee the returned data is valid(i.e: out of memory errors)

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
//Yes, undefined behavior.

delete pointer; // tells the operating system to free the place pointed by pointer
//Assuming that the pointer was successfully created.

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)
//Yes, undefined behavior.

pointer = 0; // now pointer is a null-pointer again, but this doesn't free up any space, as opposed to delete
//Yipee, finally, the pointer actually points to 0.

int value_4 = *pointer; //ERROR: you tried to access a null pointer again.
//No, not an Error, just undefiend attempt at reading w/e is written at the first address in ram.(this could be theoretically valid in some embedded systems.)

// 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 is pointing to undefined data after deletion.
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
//again, some embeded systems can use 0 as a logical address, as well, null does not always = 0, in 16/8 bit days, the processor did some near/far stuff with pointers, so do not assume null = 0
{
std::cout << "Pointer is valid.\n";
}
else
{
std::cout << "Pointer is INvalid.\n";
}

[/code]
generally, my nitpick is with your first line, claiming that an uninitialized pointer is always 0 is wrong, it is UNDEFINED, never trust data that is uninitiated, and for suggesting that it well become 0 tells me your understanding of pointers/data structures is flawed.

[quote]
well it does work, glUnmapBuffer doesn't throw any errors if you add checking after it. It reproduces the same behaviour on Linux Mint 12.1 64 bit HD 5670 Catalyst 12.1
[/quote]

as brother bob said, check the return value.

as per specifications, it should not work in the same way as OP describes. I can not tell you why it does, but it shouldn't.
1

Share this post


Link to post
Share on other sites
..snip..

[img]http://public.gamedev.net//public/style_emoticons/default/ohmy.png[/img]

Oops... I misunderstood the nature of glMapBuffer. I looked at his code and thought "Wait a minute. He just had data sent to him in mappedBuf from glMapBuffer and now he is overwriting it? [img]http://public.gamedev.net//public/style_emoticons/default/huh.png[/img] "...

[img]http://public.gamedev.net//public/style_emoticons/default/sleep.png[/img]
0

Share this post


Link to post
Share on other sites
I have noticed before that error flags are not always set properly for error conditions when they should be according to the specification. I generally trust set flags to indicate an error, but I rarely trust a reset flag to indicate no error.

you're right it returns false.
0

Share this post


Link to post
Share on other sites
[quote]
This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)

[/quote]

Just a nitpick, I don't like the word "generally" here. You could say, however, that for 32 bit executables a pointer is generally 4 bytes and for a 64 bit executable it is generally 8 bytes. ;)
0

Share this post


Link to post
Share on other sites
[quote name='Madhed' timestamp='1329259950' post='4913160']
[quote]
This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)

[/quote]

Just a nitpick, I don't like the word "generally" here. You could say, however, that for 32 bit executables a pointer is generally 4 bytes and for a 64 bit executable it is generally 8 bytes. ;)
[/quote]

true enough sir, assuming the executable is built for 64-bit processor's, however in my experience, 32 bit executable's have far outweighed 64 executable's, even in 64 bit environments..
0

Share this post


Link to post
Share on other sites
[quote name='slicer4ever' timestamp='1329259378' post='4913151']
i'm sorry, but he was doing nothing wrong in his initial code when it comes to pointers. and to be fair, he seems to have a better understanding then your "course"

for example:
[code]
int* pointer; // creates an 8-byte pointer to an integer, pointer's value is 0 (Null-pointer)
///////////This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)
//////////Secondly, not defining a value, does [B]NOT[/B] mean it defaults as a null pointer, it is simply undefined(do not rely on uninitialized pointers to equal 0, this is extremely bad practice.)

int value = *pointer; // ERROR: you tried to access the contents of a null-pointer, this is illegal
//again, not invalid, but is undefined behavior(generally seg-fault level, but it is not illegal by compilation to access a null pointer.)

pointer = new int; // dynamically (at runtime) assigns 4 bytes of memory at the place which is pointed by pointer, pointer is now valid
//pointer is not "now" valid, the assumption is that you were successful in creating the memory, but does not gurantee the returned data is valid(i.e: out of memory errors)

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
//Yes, undefined behavior.

delete pointer; // tells the operating system to free the place pointed by pointer
//Assuming that the pointer was successfully created.

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)
//Yes, undefined behavior.

pointer = 0; // now pointer is a null-pointer again, but this doesn't free up any space, as opposed to delete
//Yipee, finally, the pointer actually points to 0.

int value_4 = *pointer; //ERROR: you tried to access a null pointer again.
//No, not an Error, just undefiend attempt at reading w/e is written at the first address in ram.(this could be theoretically valid in some embedded systems.)

// 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 is pointing to undefined data after deletion.
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
//again, some embeded systems can use 0 as a logical address, as well, null does not always = 0, in 16/8 bit days, the processor did some near/far stuff with pointers, so do not assume null = 0
{
std::cout << "Pointer is valid.\n";
}
else
{
std::cout << "Pointer is INvalid.\n";
}

[/code]
generally, my nitpick is with your first line, claiming that an uninitialized pointer is always 0 is wrong, it is UNDEFINED, never trust data that is uninitiated, and for suggesting that it well become 0 tells me your understanding of pointers/data structures is flawed.
[/quote]

you're right about the first line, 8 bytes is in fact platform specific :) but for 64 bit systems it should be 8 bytes
and pointer declaration depends on the compiler (but you are right it should be undefined), for me this code works (with g++ on linux):
[CODE]
int* a;
if(a == 0)
{
std::cout << "A is 0.\n";
}
[/CODE]

What I described should work using Visual C++ and g++ as well. I don't know about the embedded systems, so thanks for clarifying.
0

Share this post


Link to post
Share on other sites
[quote name='Yours3!f' timestamp='1329260792' post='4913164']
What I described should work using Visual C++ and g++ as well. I don't know about the embedded systems, so thanks for clarifying.
[/quote]
It works on neither Visual C++ nor g++. Or, rather, it works for the very same reason that your unmapped buffer appeared valid; undefined behavior. Try, for example, to enable optimization and see that happens.
0

Share this post


Link to post
Share on other sites
[quote name='Yours3!f' timestamp='1329260792' post='4913164']
What I described should work using Visual C++ and g++ as well. I don't know about the embedded systems, so thanks for clarifying.
[/quote]

Not for me...
[code]c:\test>cat a.cpp
#include <stdio.h>
int main()
{
int* ptr;
printf("%p\n", ptr);
}
c:\test>g++ a.cpp

c:\test>a.exe
7EFDE000

c:\test>cl a.cpp /nologo
a.cpp
c:\test\a.cpp(5) : warning C4700: uninitialized local variable 'ptr' used

c:\test>a.exe
00000001

c:\test>[/code]
0

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1329261373' post='4913169']
[quote name='Yours3!f' timestamp='1329260792' post='4913164']
What I described should work using Visual C++ and g++ as well. I don't know about the embedded systems, so thanks for clarifying.
[/quote]
It works on neither Visual C++ nor g++. Or, rather, it works for the very same reason that your unmapped buffer appeared valid; undefined behavior. Try, for example, to enable optimization and see that happens.
[/quote]

I use kdevelop which uses cmake and by default sets optimization to -O3, I tried in both debug and release mode, both works. Again it shouldn't.
0

Share this post


Link to post
Share on other sites
[quote name='Yours3!f' timestamp='1329262329' post='4913178']
try this:
[/quote]
Primitives at global scope are always default initialized to zero. At function scope, they are not.
0

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1329262789' post='4913180']
[quote name='Yours3!f' timestamp='1329262329' post='4913178']
try this:
[/quote]
Primitives at global scope are always default initialized to zero. At function scope, they are not.
[/quote]

I know, that's why replied it. but lets return to the topic now. solving the memory leak...
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By Solid_Spy
      Hello, I have been working on SH Irradiance map rendering, and I have been using a GLSL pixel shader to render SH irradiance to 2D irradiance maps for my static objects. I already have it working with 9 3D textures so far for the first 9 SH functions.
      In my GLSL shader, I have to send in 9 SH Coefficient 3D Texures that use RGBA8 as a pixel format. RGB being used for the coefficients for red, green, and blue, and the A for checking if the voxel is in use (for the 3D texture solidification shader to prevent bleeding).
      My problem is, I want to knock this number of textures down to something like 4 or 5. Getting even lower would be a godsend. This is because I eventually plan on adding more SH Coefficient 3D Textures for other parts of the game map (such as inside rooms, as opposed to the outside), to circumvent irradiance probe bleeding between rooms separated by walls. I don't want to reach the 32 texture limit too soon. Also, I figure that it would be a LOT faster.
      Is there a way I could, say, store 2 sets of SH Coefficients for 2 SH functions inside a texture with RGBA16 pixels? If so, how would I extract them from inside GLSL? Let me know if you have any suggestions ^^.
    • By KarimIO
      EDIT: I thought this was restricted to Attribute-Created GL contexts, but it isn't, so I rewrote the post.
      Hey guys, whenever I call SwapBuffers(hDC), I get a crash, and I get a "Too many posts were made to a semaphore." from Windows as I call SwapBuffers. What could be the cause of this?
      Update: No crash occurs if I don't draw, just clear and swap.
      static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 32, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 24, // 24Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; if (!(hDC = GetDC(windowHandle))) return false; unsigned int PixelFormat; if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) return false; if (!SetPixelFormat(hDC, PixelFormat, &pfd)) return false; hRC = wglCreateContext(hDC); if (!hRC) { std::cout << "wglCreateContext Failed!\n"; return false; } if (wglMakeCurrent(hDC, hRC) == NULL) { std::cout << "Make Context Current Second Failed!\n"; return false; } ... // OGL Buffer Initialization glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glBindVertexArray(vao); glUseProgram(myprogram); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, (void *)indexStart); SwapBuffers(GetDC(window_handle));  
    • By Tchom
      Hey devs!
       
      I've been working on a OpenGL ES 2.0 android engine and I have begun implementing some simple (point) lighting. I had something fairly simple working, so I tried to get fancy and added color-tinting light. And it works great... with only one or two lights. Any more than that, the application drops about 15 frames per light added (my ideal is at least 4 or 5). I know implementing lighting is expensive, I just didn't think it was that expensive. I'm fairly new to the world of OpenGL and GLSL, so there is a good chance I've written some crappy shader code. If anyone had any feedback or tips on how I can optimize this code, please let me know.
       
      Vertex Shader
      uniform mat4 u_MVPMatrix; uniform mat4 u_MVMatrix; attribute vec4 a_Position; attribute vec3 a_Normal; attribute vec2 a_TexCoordinate; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { v_Position = vec3(u_MVMatrix * a_Position); v_TexCoordinate = a_TexCoordinate; v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0)); gl_Position = u_MVPMatrix * a_Position; } Fragment Shader
      precision mediump float; uniform vec4 u_LightPos["+numLights+"]; uniform vec4 u_LightColours["+numLights+"]; uniform float u_LightPower["+numLights+"]; uniform sampler2D u_Texture; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { gl_FragColor = (texture2D(u_Texture, v_TexCoordinate)); float diffuse = 0.0; vec4 colourSum = vec4(1.0); for (int i = 0; i < "+numLights+"; i++) { vec3 toPointLight = vec3(u_LightPos[i]); float distance = length(toPointLight - v_Position); vec3 lightVector = normalize(toPointLight - v_Position); float diffuseDiff = 0.0; // The diffuse difference contributed from current light diffuseDiff = max(dot(v_Normal, lightVector), 0.0); diffuseDiff = diffuseDiff * (1.0 / (1.0 + ((1.0-u_LightPower[i])* distance * distance))); //Determine attenuatio diffuse += diffuseDiff; gl_FragColor.rgb *= vec3(1.0) / ((vec3(1.0) + ((vec3(1.0) - vec3(u_LightColours[i]))*diffuseDiff))); //The expensive part } diffuse += 0.1; //Add ambient light gl_FragColor.rgb *= diffuse; } Am I making any rookie mistakes? Or am I just being unrealistic about what I can do? Thanks in advance
    • By yahiko00
      Hi,
      Not sure to post at the right place, if not, please forgive me...
      For a game project I am working on, I would like to implement a 2D starfield as a background.
      I do not want to deal with static tiles, since I plan to slowly animate the starfield. So, I am trying to figure out how to generate a random starfield for the entire map.
      I feel that using a uniform distribution for the stars will not do the trick. Instead I would like something similar to the screenshot below, taken from the game Star Wars: Empire At War (all credits to Lucasfilm, Disney, and so on...).

      Is there someone who could have an idea of a distribution which could result in such a starfield?
      Any insight would be appreciated
    • By afraidofdark
      I have just noticed that, in quake 3 and half - life, dynamic models are effected from light map. For example in dark areas, gun that player holds seems darker. How did they achieve this effect ? I can use image based lighting techniques however (Like placing an environment probe and using it for reflections and ambient lighting), this tech wasn't used in games back then, so there must be a simpler method to do this.
      Here is a link that shows how modern engines does it. Indirect Lighting Cache It would be nice if you know a paper that explains this technique. Can I apply this to quake 3' s light map generator and bsp format ?
  • Popular Now