patriciogonzalezvivo / glslViewer

Console-based GLSL Sandbox for 2D/3D shaders
BSD 3-Clause "New" or "Revised" License
4.62k stars 350 forks source link

Sampling texture for post-processing logs error in terminal #307

Closed marioecg closed 1 year ago

marioecg commented 1 year ago

Hi and first of all thanks Patricio and the community for your efforts on glslViewer. I'm trying to setup a simple post-processing pass using defines, but instead of rendering a color an error shows: UNSUPPORTED (log once): POSSIBLE ISSUE: unit 0 GLD_TEXTURE_INDEX_2D is unloadable and bound to sampler type (Float) - using zero texture because texture unloadable.

The command I'm running is glslViewer main.frag -l, and this is the shader:

#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D u_scene;

varying vec2 v_texcoord;

void main(void) {
    vec2 uv = v_texcoord;

    vec4 color = vec4(1., 0., 0., 1.);

    #if defined(POSTPROCESSING)

    color.rgb = texture2D(u_scene, uv).rgb;

    #endif

    gl_FragColor = color;
}

I would expect a red color output on the screen, perhaps I'm missing something? I'm running glslViewer on a 14-in MBP 2021 with Monterey 12.6.

Screen Shot 2022-10-26 at 20 53 12
patriciogonzalezvivo commented 1 year ago

Hi @marioecg! So good that you brought this up! u_scene/POSTPROCESSING passes were originally designed for 3D workflows. Where you have a 3D scene (with some geometry) in the main pass and you want to apply some 2D post processing effect after that.

For what you want to make using simple buffers is the canonical way. The main pass becomes the "postprocessing" . For example:

uniform sampler2D   u_buffer0;
varying vec2             v_texcoord;

void main(void) {
    vec4 color = vec4(0., 0., 0., 1.);
    vec2 uv = v_texcoord;

#if defined(BUFFER_0)
    color.r = 1.0; 
#else
    color.rgb = texture2D(u_buffer0, uv).rgb;
#endif    

    gl_FragColor = color;
}

But now that you brought this up, it makes a lot of conceptual sense to still be able to have another 2D post processing pass. I will take a look to the code and see if I can make that work. In the mean time, for 2D pipelines I recommend using buffers, they are powerful and a hell lot of fun. If you are into doing ping-pong or feedback loops remember you have u_doubleBuffer<N>/DOUBLE_BUFFER_<N>

marioecg commented 1 year ago

Ah right! I knew I was missing something, it looked more straightforward to use defined(POSTPROCESSING). Your example works like a charm, thank you!! I will be exploring more with buffers soon 💯