We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › PGraphicsOpenGL image leak
Pages: 1 2 
PGraphicsOpenGL image leak (Read 8029 times)
PGraphicsOpenGL image leak
Jun 9th, 2007, 12:58am
 
I'm not sure what I'm doing wrong but I get a strange memory leak when trying to copy the stage and drawing it again over itself.

Code:
import processing.opengl.*;

PImage screenshot;

void setup()
{
size(400,300,OPENGL);
}
void draw()
{
PGraphicsOpenGL t_g = (PGraphicsOpenGL) g;
screenshot = t_g.get();
image(screenshot, 0, 0);
screenshot = null;
}


What am I missing?


Thanks, chr
Re: PGraphicsOpenGL image leak
Reply #1 - Jun 9th, 2007, 1:07am
 
It should be something really stupid, also with P3D there is the leak, my brain doesn't seem to be really connected tonight. Can someone spot it? Smiley

chr
Re: PGraphicsOpenGL image leak
Reply #2 - Jun 9th, 2007, 4:15am
 
what is the get() function ?
Re: PGraphicsOpenGL image leak
Reply #3 - Jun 9th, 2007, 9:56am
 
Here the doc.
Re: PGraphicsOpenGL image leak
Reply #4 - Jun 9th, 2007, 11:42am
 
i wouldnt use the functions that access pixelbuffer directly.. opengl does not allow that. bad idea.

access the framebuffer using glCopyTexSubImage2D and then
render that texture on a quad that is over the screen.

you should look into 'opengl render to texture'.
Re: PGraphicsOpenGL image leak
Reply #5 - Jun 10th, 2007, 2:32pm
 
that code works fine for me, what's the memory leak? and you don't need to do that cast for the PGraphicsOpenGL object.
Re: PGraphicsOpenGL image leak
Reply #6 - Jun 10th, 2007, 3:04pm
 
fry,
what is the internal work that get() function is doing to access opengl framebuffer ?
Re: PGraphicsOpenGL image leak
Reply #7 - Jun 10th, 2007, 10:39pm
 
hi Ben, the code I posted (now cleaned a bit):

Code:
import processing.opengl.*;

PImage screenshot;

void setup()
{
size(400,300,OPENGL);
}
void draw()
{
screenshot = g.get();
image(screenshot, 0, 0);
screenshot = null;
}


Makes the Mem Usage of Java.exe growth infinitively, should I send you the exported .exe? I'm using P5 0124.

Let me know, chr
Re: PGraphicsOpenGL image leak
Reply #8 - Jun 11th, 2007, 7:52pm
 
I was experiencing a similar problem while trying to do "feedback" type effects.

I believe it can be traced to something in the texture caching, bind/unbind area -- there doesn't appear to be a clean way to fully dispose of a texture once bound.  The code below shows two different ways to duplicate the leak in "simpler" ways. (comparing the two is what leads me to think its leaking in the cache/bind)

Quote:

import processing.opengl.*;
PImage newbie;
void setup() {
 size(400,300,OPENGL);
}
void draw() {
 // get() does something like this internally:
 newbie = new PImage(width,height);
 // use it as a texture...
 image(newbie,0,0);
 // try to release it...
 newbie = null; // doesn't matter, will leak anyway
 System.gc(); // doesn't matter, will leak anyway
}


Quote:

import processing.opengl.*;
PImage newbie;
void setup() {
 size(400,300,OPENGL);
 // create the image only once
 newbie = new PImage(width,height);
}
void draw() {
 // use it as a texture...
 image(newbie,0,0);
 // hackery:
 // wipe out the cache reference to fool the cache system into
 // thinking it needs to rebind the image on next frame, will
 // cause same leak as prior example w/o creating new PImage
 newbie.cache = null;
 System.gc();
}


System:  WinXP, 0124 Beta with included JRE 1.4.2_10 with included JOGL 1.0.0 - I've tried it on several video cards/drivers, incl recent ATI & NVidia, but it doesn't appear to be tied to the card/driver.
Re: PGraphicsOpenGL image leak
Reply #9 - Jun 11th, 2007, 8:20pm
 
Interesting...
Re: PGraphicsOpenGL image leak
Reply #10 - Jun 11th, 2007, 9:24pm
 
V wrote on Jun 9th, 2007, 11:42am:
=access the framebuffer using glCopyTexSubImage2D


Do you have crmx an example of it with P5

thanks, chr
Re: PGraphicsOpenGL image leak
Reply #11 - Jun 11th, 2007, 9:55pm
 
found and fixed for 0125. this was bug #150, a long-standing feller that's been giving a lot of people trouble:
http://dev.processing.org/bugs/show_bug.cgi?id=150

i don't know when i'll have time to finalize release 125, but in the meantime anyone interested can build it from the source (see dev.processing.org).
Re: PGraphicsOpenGL image leak
Reply #12 - Jun 11th, 2007, 11:30pm
 
create texture and load to video memory:

   int _id;


 void CreateTexture()
 {
   int[] pixelss = new int[width*height];
   
   for (int i=0;i<width*height;i++)
     pixelss[i] = 0x000000FF;

   IntBuffer pixBuffer = IntBuffer.wrap( pixelss );
   pixBuffer.rewind();

   opengl.glGenTextures( 1, _id, 0 );
   opengl.glEnable(GL.GL_TEXTURE_2D);
   opengl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
   opengl.glBindTexture(GL.GL_TEXTURE_2D, _id );
   opengl.glTexImage2D( GL.GL_TEXTURE_2D, 0, 4, width, height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, pixBuffer );    
   opengl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
   opengl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
   opengl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
   opengl.glDisable(GL.GL_TEXTURE_2D);
 }



in your mainloop:

   //opengl.glViewport( 0, 0, w, h );
   opengl.glEnable( GL.GL_TEXTURE_2D );
   opengl.glBindTexture( GL.GL_TEXTURE_2D, _id );
   opengl.glCopyTexSubImage2D( GL.GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_w, m_h );
   opengl.glDisable( GL.GL_TEXTURE_2D );


i use that for render-to-texture in processing. its useful for some effects, and if you use shaders it gets even better

im not sure if processing already has a function that can give you the same thing, but since i like to use the opengl object this is how i do it..
Re: PGraphicsOpenGL image leak
Reply #13 - Jun 11th, 2007, 11:46pm
 
On Eclipse glGenTextures doesn't seem to accept those parameters.

If I look the API, in facts, it accepts different paramenters. The second should be or an Array or an IntBuffer but without the third parameter.

The object you called opengl is (from PApplet) ((PGraphicsOpenGL) g).gl

Thanks, chr
Re: PGraphicsOpenGL image leak
Reply #14 - Jun 12th, 2007, 12:42am
 
hmm well just create a int array of size 1 then..

int[] _id = new int[1];

that should do it
Pages: 1 2