Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

OutOfMemoryError updating textures to a PShader

$
0
0
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:

  1.   PGraphics pg;
  2.   PShader kSmooth;
  3.   int texIndex = 0;
  4.   boolean ready = true;
  5.   PImage img;

  6. public void setup()
  7. {
  8.             size(640 , 480, P2D);
  9.             img = createImage(640,480,ARGB);
  10.             pg = createGraphics(640,480,P2D);
  11.             kSmooth = loadShader("weightedmean.glsl");
  12.             kSmooth.set("width", 640);
  13.             kSmooth.set("height", 480);
  14. }

  15. public void draw()
  16. {
  17.              background(0);
  18.              kSmooth.set("tex"+texIndex, img.get());
  19.              kSmooth.set("index", texIndex);
  20.              pg.beginDraw();
  21.              pg.shader(kSmooth);
  22.              pg.rect(0,0,pg.width,pg.height);
  23.              pg.endDraw();
  24.              texIndex = (texIndex+1) % 5;
  25.              image(pg,0,0,pg.width,pg.height);
  26.              frame.setTitle("Frame "+frameCount);
  27.   }

And a simplified version of the fragment shader, where the weights are fixed instead of depeanding on the index uniform:
  1. #define PROCESSING_COLOR_SHADER

  2. uniform sampler2D tex0;
  3. uniform sampler2D tex1;
  4. uniform sampler2D tex2;
  5. uniform sampler2D tex3;
  6. uniform sampler2D tex4;
  7. uniform int width;
  8. uniform int height; 
  9. uniform int index;

  10. void main()
  11. {
  12. vec2 surfacePos = vec2( (gl_FragCoord.x / width) , ( (height - gl_FragCoord.y) / height)  );
  13. float val1 = (texture2D(tex0, surfacePos)).x;
  14. float val2 = (texture2D(tex1, surfacePos)).x * 2.0;
  15. float val3 = (texture2D(tex2, surfacePos)).x * 3.0;
  16. float val4 = (texture2D(tex3, surfacePos)).x * 4.0;
  17. float val5 = (texture2D(tex4, surfacePos)).x * 5.0;
  18. float result = (val1 + val2 + val3 + val4 + val5) / 15.0; // 15 is the sum of all weights
  19. gl_FragColor = vec4(result, 0.0, 0.0, 1.0);
  20. }

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.




Viewing all articles
Browse latest Browse all 1768

Trending Articles