Shader code frezzes my computer

edited June 2014 in GLSL / Shaders

The following code es freezing my entire machine after running just a few seconds, I'm using Debian 7 Weezy. It's Debian strength to be super stable as long as I know. The code will frezee only when it uses the shaders. The blur shader it's the same as the une used in the blur code on shaders examples. Any guess somebody ?

import peasy.test.*; 
import peasy.org.apache.commons.math.*; 
import peasy.*; 
import peasy.org.apache.commons.math.geometry.*; 
float r = 100; //Radius 
float f=0.01; 
float dist=0; 
PeasyCam cam;
postProcessing pp;

void setup() { 
  size(1280, 720, P3D); 
  cam = new PeasyCam(this, 500); 
  cam.setMaximumDistance(5000 ); 
  cam.setMinimumDistance(50); 
  background(0);
  pp = new postProcessing();
}


void draw() { 
  background(0);
  getDna();
  pp.setGlow();
}

void getDna() { 
  dist = (float)cam.getDistance(); 
  lights(); 
  noStroke(); 
  fill(color(100, 100, 255)); 
  pushMatrix(); 
  translate(-dist, 0); 
  rotateX(frameCount*0.01); 
  sphereDetail(round(map(dist, 10, 2000, 15, 1))); 
  float rr=20; 
  for (int i=0;i<8;i++) renderHelix(0, noise(i, frameCount*0.01)*rr-rr/2, noise(i+10, frameCount*0.01)*rr, noise(i+15, frameCount*0.01)*rr); 
  //Second helix which phase + PI 
  for (int i=0;i<8;i++) renderHelix(PI, noise(i, frameCount*0.01)*rr-rr/2, noise(i+10, frameCount*0.01)*rr, noise(i+15, frameCount*0.01)*rr); 
  fill(color(150, 150, 255)); 
  sphereDetail(round(map(dist, 10, 600, 15, 1))); 
  for (float x=0;x<dist*2;x+=TWO_PI*4) { 
    float y= sin(x*f)*r; 
    float z= cos(x*f)*r; 
    float y2= sin(x*f+PI)*r; 
    float z2= cos(x*f+PI)*r; 
    for (float i=0;i<1;i+=0.1) { 
      float ySphere = lerp(y, y2, i);
      float zSphere = lerp(z, z2, i);
      pushMatrix(); 
      translate(x, ySphere, zSphere); 
      sphere(2); 
      popMatrix();
    }
  } 
  popMatrix(); 
  frame.setTitle(int(frameRate) + " fps");
}

void renderHelix(float phase, float randomnessX, float randomnessY, float randomnessZ) { 
  for (float x=0;x<dist*2;x+=TWO_PI*4) { 
    float y= sin(x*f+phase)*(r+randomnessY); 
    float z= cos(x*f+phase)*(r+randomnessZ); 
    pushMatrix(); 
    translate(x+randomnessX, y, z); 
    sphere(6); 
    popMatrix();
  }
}



class postProcessing {
  PShader blur = loadShader("blur.glsl");


  void setBlur() {
    filter(blur);
  }

  void setGlow() {
    PImage screen = get(0, 0, width, height);
    PGraphics screenWithEffect = setGlow(screen);
    set(0, 0, screenWithEffect.get());
  }


  PGraphics setGlow(PImage img) {
    PGraphics p1 = createGraphics(img.width, img.height, P2D);
    PGraphics p2 = createGraphics(img.width, img.height, P2D);


    p1.beginDraw();
    p1.image(img, 0, 0);
    p1.filter(blur);
    p1.endDraw();

    p2.beginDraw();
    p2.image(img, 0, 0);
    p2.blendMode(SCREEN);
    p2.image(p1, 0, 0);  
    p2.blendMode(BLEND);
    p2.endDraw();

    return p2;
  }
}

Comments

  • Well, I auto solved my question. If anyone is interested it seems that Java Garbage Collector can't stand creating so many PGraphics and then de-allocationg them. If I don't call createGraphics in each draw the program runs normally.

  • edited June 2014

    Your program above is still instantiating lotsa PGraphics objects via createGraphics() and a buncha PImage via get()!
    Rather than making new 1s @ 60 FPS, why not re-using them?

    Also notice that variable g is the field which holds reference to the canvas PGraphics.
    So there's no need to use get() to get a PImage outta canvas. Just use its g reference!

    I've re-written your class above. Check it out:
    P.S.: Not tested since i don't have your "blur.glsl" file!

    class PostProcess {
    
      final PGraphics bg  = createGraphics(width, height, P2D);
      final PGraphics tmp = createGraphics(width, height, P2D);
      final PShader blur;
    
      PostProcess(PShader ps) {
        blur = ps;
    
        bg.beginDraw();
        bg.endDraw();
    
        tmp.beginDraw();
        tmp.endDraw();
      }
    
      void renderGlow() {
        tmp.background(g);
        tmp.filter(blur);
        tmp.endDraw();
    
        bg.background(g);
        bg.blendMode(SCREEN);
        bg.image(tmp, 0, 0);
        //bg.blendMode(BLEND);
        bg.endDraw();
    
        background(bg);
      }
    }
    
Sign In or Register to comment.