I managed to get Reaction Diffusion Algorithm to run on the GPU, but now I'm having problems. The code runs and all, but the effect is much different from the effect I get with Reaction Diffusion Algorithm on the CPU. Why?
Code for shader based Reaction-Diffusion -
PDE file -
PShader algorithm, pass;
PGraphics pg;
public static final float DA = 1.0;
public static final float DB = 0.5;
public static final float feed = 0.055;
public static final float kill = 0.062;
public static final float dt = 1.0;
public static final float min = 0.48;
public static final float max = 0.52;
public static final int N_PASS = 10;
boolean first = true;
void setup() {
size(500, 500, P2D);
algorithm = loadShader("algorithm.frag");
pass = loadShader("pass.frag");
pg = createGraphics(width, height, P2D);
pg.noSmooth();
algorithm.set("resolution", float(pg.width), float(pg.height));
algorithm.set("DA", DA);
algorithm.set("DB", DB);
algorithm.set("feed", feed);
algorithm.set("kill", kill);
algorithm.set("dt", dt);
algorithm.set("min", min);
algorithm.set("max", max);
}
void draw() {
algorithm.set("first", first);
first = false;
for(int i = 0; i < N_PASS; i++){
pg.beginDraw();
pg.background(0);
pg.shader(algorithm);
pg.rect(0, 0, pg.width, pg.height);
pg.endDraw();
}
shader(pass);
image(pg, 0, 0);
if(frameCount%30 == 0)println(frameRate);//forget this
}
algortihm.frag -
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 resolution;
uniform sampler2D ppixels;
uniform float DA;
uniform float DB;
uniform float feed;
uniform float kill;
uniform float dt;
uniform float min;
uniform float max;
uniform bool first;
void main (void) {
vec2 position = ( gl_FragCoord.xy / resolution.xy );
vec2 pixel = 1.0/resolution;
if(first){
if(position.x > min && position.x < max && position.y > min && position.y < max){
gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}else{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
}else{
float A = texture(ppixels, position).r;
float sumA = -A;
sumA += 0.05*texture(ppixels, position + pixel * vec2(-1.0, -1.0)).r;
sumA += 0.05*texture(ppixels, position + pixel * vec2(-1.0, 1.0)).r;
sumA += 0.05*texture(ppixels, position + pixel * vec2(1.0, -1.0)).r;
sumA += 0.05*texture(ppixels, position + pixel * vec2(1.0, 1.0)).r;
sumA += 0.2*texture(ppixels, position + pixel * vec2(1.0, 0.0)).r;
sumA += 0.2*texture(ppixels, position + pixel * vec2(-1.0, 0.0)).r;
sumA += 0.2*texture(ppixels, position + pixel * vec2(0.0, -1.0)).r;
sumA += 0.2*texture(ppixels, position + pixel * vec2(0.0, 1.0)).r;
float B = texture(ppixels, position).g;
float sumB = -B;
sumB += 0.05*texture(ppixels, position + pixel * vec2(-1.0, -1.0)).g;
sumB += 0.05*texture(ppixels, position + pixel * vec2(-1.0, 1.0)).g;
sumB += 0.05*texture(ppixels, position + pixel * vec2(1.0, -1.0)).g;
sumB += 0.05*texture(ppixels, position + pixel * vec2(1.0, 1.0)).g;
sumB += 0.2*texture(ppixels, position + pixel * vec2(1.0, 0.0)).g;
sumB += 0.2*texture(ppixels, position + pixel * vec2(-1.0, 0.0)).g;
sumB += 0.2*texture(ppixels, position + pixel * vec2(0.0, -1.0)).g;
sumB += 0.2*texture(ppixels, position + pixel * vec2(0.0, 1.0)).g;
float nA = A + ((DA*sumA) - (A*B)*B + (feed*(1.0 - A)))*dt;
float nB = B + ((DB*sumB) + (A*B)*B - ((kill + feed)*B))*dt;
gl_FragColor = vec4(clamp(nA, 0.0, 1.0), clamp(nB, 0.0, 1.0), 0.0, 1.0);
}
}
pass.frag -
uniform sampler2D texture;
varying vec4 vertTexCoord;
void main(){
vec4 inColor = texture2D(texture, vertTexCoord.st);
gl_FragColor = vec4(1.0 - inColor.g, 1.0 - inColor.g, 1.0 - inColor.g, 1.0);
}