OutOfMemoryError updating textures to a PShader
in
Core Library Questions
•
6 months ago
Hi,
I'm trying to compute a weighted mean algorithm in the GPU. I want to pass several images to the shader, via sampler2d uniforms, in a FIFO queue style, having a new one each frame by grabbing it from a camera. This is a simplified version using a PImage to illustrate whats happening:
- PGraphics pg;
- PShader kSmooth;
- int texIndex = 0;
- boolean ready = true;
- PImage img;
- public void setup()
- {
- size(640 , 480, P2D);
- img = createImage(640,480,ARGB);
- pg = createGraphics(640,480,P2D);
- kSmooth = loadShader("weightedmean.glsl");
- kSmooth.set("width", 640);
- kSmooth.set("height", 480);
- }
- public void draw()
- {
- background(0);
- kSmooth.set("tex"+texIndex, img.get());
- kSmooth.set("index", texIndex);
- pg.beginDraw();
- pg.shader(kSmooth);
- pg.rect(0,0,pg.width,pg.height);
- pg.endDraw();
- texIndex = (texIndex+1) % 5;
- image(pg,0,0,pg.width,pg.height);
- frame.setTitle("Frame "+frameCount);
- }
And a simplified version of the fragment shader, where the weights are fixed instead of depeanding on the index uniform:
- #define PROCESSING_COLOR_SHADER
- uniform sampler2D tex0;
- uniform sampler2D tex1;
- uniform sampler2D tex2;
- uniform sampler2D tex3;
- uniform sampler2D tex4;
- uniform int width;
- uniform int height;
- uniform int index;
- void main()
- {
- vec2 surfacePos = vec2( (gl_FragCoord.x / width) , ( (height - gl_FragCoord.y) / height) );
- float val1 = (texture2D(tex0, surfacePos)).x;
- float val2 = (texture2D(tex1, surfacePos)).x * 2.0;
- float val3 = (texture2D(tex2, surfacePos)).x * 3.0;
- float val4 = (texture2D(tex3, surfacePos)).x * 4.0;
- float val5 = (texture2D(tex4, surfacePos)).x * 5.0;
- float result = (val1 + val2 + val3 + val4 + val5) / 15.0; // 15 is the sum of all weights
- gl_FragColor = vec4(result, 0.0, 0.0, 1.0);
- }
In my old windows xp machine
the memory used by the process keeps increasing until reaching the max memory available set in processing's preferences, then the program freezes for a few seconds, the memory is released and the process begins again, increasing the memory usage once again.
In my new windows 8 machine I get an OutOfMemoryError after 200 or so frames.
Is there a method to free the memory of previous textures no longer used?
Thanks.
Note: I asked first how should I do it in this post which was moved to the general discussion forum, I thought I should post a simplified code here to illustrate the problem I found while tryng a solution. Hope that's not rude.
1