Compute PShader for both Android and Windows

Before I say anything, I code on my phone (Android) almost all the time, but sometimes I have to write something that runs on Windows as well.

So, I’ve been trying to learn about shaders. There is the PShader class that works both on Android and Windows, but only runs “graphics shaders”. There are plenty of examples of compute shaders for Windows/PC/whatever using JOGL.

So, my questions are…
Is there a way to use PShader to run a compute shader?
How do I write, compile and run a compute shader on Android?
Is there a way to write a Java-side or a “Processing-language-side” code for it that would work on both Android and Windows?

And if it helps, I’m currently trying to write a 2D (and possibly 3D/4D) Simplex noise generator as a compute shader.

There are some examples on the site of compute shades in the gallery section though it makes use of g4l and is therefore not necessarily accessible to everyone.

You could alternatively create a sketch with multiple pairs of canvas elements or pimage elements.

One for numbers before the decimal and another for numbers after the decimal.

Using base 256 multiplication combined with modulus you could update the canvas to store a very large numbee. This could then be sent to the shader to do calculations.

Int a = floor(num *256*256*256*256) %256 would for example locate the red pixel and the number could the be stored there using

col = color(a, g, b, alpha) ;

Note that you would need to repeat the process four times to completely fill the pixel.

Then shader would retrieve all the values in a format of 0-1.0 so you would need to multiply everything by 255 then revers the process to restore the value, in the shader, however you could just use normal shader operations.

The next part is maybe a little bit more difficult as you need to either use simple operations or make use of Ann evaluation parser which you write in the shader itself unless you wanted to hardcode the operations required directly in the shader.

This is something I’ve been attempting to do recently I’ve coded the base 256 conversion, currently writing a parser although there are lots floating around on the internet and you therefore don’t need to reinvent the wheel unless you want a coding exercise.

As for sending the parser operations and variables to the shader I haven’t got that far yet, the parser is still incomplete, operations include

/-+*^() sin cos tan simple, these should be the basics required and it also stores variables but is limited to the range of the alphabet.

After that you need to consider how to send the order of operations to the shader and how to retrieve them in order the variables etc, and whether or not you are making use of dynamic or static variables. Static are set outside the shader and so parts of the expression could be calculated outside the shader then imported, and dynamic variables would be set outside the shader but updated during the shades calculations.

So begin with setting some arrays in the shader, for operations and variables, note that shades do not have dynamic arrays and so you would need to set a maximum size, then set them in the sketch and send them to the shader.

Then finally when all operations are completed you retrieve the values back in the sketch by reversing the base 256 operations. If you are creating a particle shader then this might not be necessary as once you’ve initialised the positions and velocities vertices. The shader would then update continuously until you needed to read the positions.

There’s probably more to it I have quite finished yet and my code is likely to bery confusing.

sketch example convert int to pixel

mx = (int)map(mouseX,0,width,0,100000);
  c = (mx);
  background(c);
  
  fill(255);
  text("c: "+c,100,100);
  text("mx: "+mx,100,120);
  
  text("red: "+red(c),100,140);
  text("green: "+green(c),100,160);
  text("blue: "+blue(c),100,180);
  
  r = floor(mx/(base*base)%base);
  g = floor((mx/(base)%base));
  b = floor(mx)%base;
  
  text("r: "+r,100,220);
  text("g: "+g,100,240);
  text("b: "+b,100,260);
  
  num = r*base*base+g*base+b;
  
  text("num: "+num,100,300);

converting a float to a pixel and reading it back

color c;
void setup(){
  size(400,400);
};
float mx = 120000;
int base = 256;
float r,g,b,r_,b_,g_,num;
int mm = 0;
void draw(){
  //if(mousePressed)mx+=2;
  mx = map(mouseX,0,width,0,0.000001);
  mx*=1000000000;
  mm = int(mx);
  c = (mm);
  background(c);
  
  fill(255);
  text("c: "+c,100,100);
  text("mx: "+mm,100,120);
  
  text("red: "+red(c),100,140);
  text("green: "+green(c),100,160);
  text("blue: "+blue(c),100,180);
  
  r = floor(mm/(base*base)%base);
  g = floor((mm/(base)%base));
  b = floor(mm)%base;
  
  text("r: "+r,100,220);
  text("g: "+g,100,240);
  text("b: "+b,100,260);
  
  num = r*base*base+g*base+b;
  
  text("num: "+num,100,300);
  
};