Using texture2D for sampling in Processing 3 results in a black screen

edited March 2017 in GLSL / Shaders

I'm trying to build a chained set of fragment shaders, where the first generates an image (similar to Shadertoy) and the remainder are applied as postprocessing effects.

Back in Processing 2 I was able to do this with a pair of fragment shaders without trouble:

PShader shader1;
PShader shader2;

void setup() {
  size(256, 256, P2D);
  fill(128);
  noStroke();
  shader1 = loadShader("shader1.glsl");
  shader2 = loadShader("shader2.glsl");
}

void draw() {
  shader(shader1);
  shader(shader2);
  rect(0, 0, width, height);
}

Where shader1 is as follows:

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform sampler2D textureSampler;

varying vec4 vertColor;
varying vec4 vertTexCoord;

void main()
{
    vec3 pos = vertTexCoord.xyz;

    gl_FragColor = vec4(pos.x, pos.y, pos.z, 1.0);
}

And shader2 is as follows:

#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D texture;

varying vec4 vertColor;
varying vec4 vertTexCoord;

void main () {
    gl_FragColor = texture2D(texture, vertTexCoord.xy);
}

No matter what I do in Processing 3, this always results in a black image when shader2 is applied, but works fine with just shader1. I have tried all sorts of things, including different OpenGL version directives, passing the resolution to shader2 and dividing the frag coord by it before sampling, and re-arranging the shader calls. I have tried both P2D and P3D mode.

In fact, many of the examples I have tried from the documentation fail to work at all in Processing 3.3.

Any idea what's going on? If it matters, I'm on a GTX 980 Ti with the latest drivers.

Answers

  • @gsuberland,

    I also had the same problem, but partially solved it thanks to lord of the galaxy.

    PShader shader1;
    PShader shader2;
    PImage buffer;
    
    void setup() {
      size(256, 256, P2D);
      fill(128);
      noStroke();
      shader1 = loadShader("shader1.glsl");
      shader2 = loadShader("shader2.glsl");
    }
    
    void draw() {
      shader(shader1);
      rect(0, 0, width, height);
      buffer = get();
      shader2.set("u_texture",buffer);
      shader(shader2);
      rect(0, 0, width, height);
    }
    

    in shader2.glsl just changed texture to u_texture, I did the same misstake, you can name tex, or whatever you want, just not texture.
    Regarding the texture2D just learned that you can omit the 2D, and use just texture() , according to standard that is, in practice texture2D works better.
    I'm now struggling with texelFetch(), I can use it without errors but get no result.
    Until I find a solution I'll have to sampe the centerpoints of pixels with texture() when I want un-interpolated values, it's a mess.

  • Until Processing decides to properly document what it does when we use shaders, we just keep guessing. So for at best a few months, worst when Processing 4 comes out, we have no real knowledge.

  • now suddenly texelFetch() works, and I didn't do anything differently than when it didn't haha

  • edited March 2017

    Are there certain limitations with Processing that make it extra difficult to work with frame buffers in the way you might with Cinder or Unity?

    This thread advises that it requires a subclass to be written

    https://forum.processing.org/one/topic/2-0xx-how-to-set-multiple-render-targets-for-a-fragment-shader.html

    I'm wondering what effects are not currently possible in Processing compared to other implementations..

    There seems to be some really interesting stuff that is possible with low level operations using frame buffers so I'm trying to see if there are roadblocks way down the road

  • @smokeredsky take a two-way trip over the north bridge

  • Right now all we do is guess. We keep trying new methods and hope that they work.

  • I have implemented such a system today. It uses a ping-pong buffer to create multi-pass renderings.

    Check out the code here: https://github.com/cansik/processing-postfx

    The g object is really not working great with shaders, so I recommend to use a P2D PGraphics as texture. Draw your stuff onto it and the draw it back onto the g object.

  • Yes, do not touch that g object.

Sign In or Register to comment.