Passing shader texture with negative values/accesing 32bit floats shader texture

Hi there,

In my processing sketch I am passing textures between two shaders by first drawing into a PGraphics and than passing the PGraphics with the .set function of the shader.

This works fine,but the PGraphics clamps all the texture values between 0 and 255. Is there a way to pass textures between shader without the clamping? So I can also pass negative values?

1 Like

Hi :slight_smile: Welcome to the forum! To be able to use negative values the buffers should be of type floating point, but by default Processing does not provide them. Maybe @kosowski or @neilcsmith know if it’s doable?

1 Like

Thank you Hamoid, would be great to have some suggestions on how to handle that.

With difficulty I think. It’s probably possible dropping to lowlevel OpenGL (as described at https://github.com/processing/processing/wiki/Advanced-OpenGL ) but you’d have to create the framebuffers from scratch as far as I know.

The obvious question is why you need this? Have worked around in the past by treating 0.5 / 127 as mid-point. Not great on resolution of steps though! :confounded:

1 Like

I was using some example from shadertoy on fluid simulation. Thanks for the tips, my next idea was indeed to remap things for 0.5/127, lets see if that works :).

For more resolution, you could pack your float into more than one byte (eg. for depth map information like this

vec4 packDepth(float depth) {
   float depthFrac = fract(depth * 255.0);
    return vec4(  
     depth - depthFrac / 255.0, 
    depthFrac, 
     O.0, // or whatever
     1.0);
}

and unpack like

float rawget(int x, int y) {
    color c=pixel(x, y);
    float f=(blue(c)+green(c)/255.0);
    if (inverted) f=255-f;
    return f;
  }

Pretty naive, but works for me, somewhere I also have functions for a 3byte float encoding (good for RGB only pictures), I will dig them up if interested.

Or you could encode them as 32bit native IEEE floats…if you find a worKing way to do that (on Android!) I’d like to know.

2 Likes

There is this modification to Processing’s source for enabling floating point texture, but it breaks some other functionalities.

Besides, I think the PixelFlow library uses floating point textures as well. Although the simplest method would be packing your values into the RGBA channels as @uheinema suggested (note you can use all 4 channels, thus 32 bits available)

4 Likes

Great, that conversation is exactly my problem. I will follow their instructions, thank you!

For the people that have the same problem with accesing the 32bit buffer texture of a loaded shader.
For me the easiest was to use the Shadertoy class of PixelFlow.
When you render your Shader not to the PGraphics but only apply the shader with toyA.apply(width, height); Than with the toyA.tex.getFloatTextureData() you can get the 32bit floats.
If you render your shader to PGraphics, it doesnt store that 32bit in the texture of the ShaderToy, so nothing comes out of it when you get Texture data.

Thank everybody for the help.