godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.76k stars 21.12k forks source link

OpenGL: Shader render mode `blend_mul` not working correctly in (and alpha effecting blend modes that shouldn't use alpha) #72873

Open TheRektafire opened 1 year ago

TheRektafire commented 1 year ago

Godot version

4.0 beta 17

System information

Windows 10, gl_compatibility, Intel UHD 600

Issue description

I was working on a basic water caustic shader that I wanted to apply as sort of an overlay on top of other geometry under the water, and for it I wanted to use blend_mul to brighten up the parts of the surface the caustics are effecting, and since I'm using the same device from issue https://github.com/godotengine/godot/issues/43891 I also wanted to use the gl_compatibility renderer since it would provide the best performance for me. But my shader doesn't seem to work correctly in the compatibility renderer, only in mobile and forward+. In those two it looks as expected, but in gl_compatibility it looks completely white.

Example of the shader in forward+/mobile: Untitled_073828

Example of the shader in gl_compatibility: Untitledb_073830

Not only that, but setting ALPHA in the shader effects the outcome as well which I wouldn't really expect for multiply blending. If I set the ALPHA to basically any value the effect just doesn't work, as you can see here Untitledc_073832

Here is the shader code itself

shader_type spatial;
render_mode blend_mul;

uniform sampler2D layer1;
uniform sampler2D layer2;

uniform float s1; uniform float s2; //layer shift speeds
uniform vec2 d1; uniform vec2 d2; //layer shift directions

void fragment() {
    // Place fragment code here.
    vec2 l1offset = UV + (normalize(d1) * (TIME * s1));
    vec2 l2offset = UV + (normalize(d2) * (TIME * s2));
    vec3 l1p = texture(layer1, l1offset).rgb;
    vec3 l2p = texture(layer2, l2offset).rgb;

    ALBEDO = min(l1p, l2p);
    ALBEDO = ALBEDO + vec3(1.0);
    //ALPHA = 0.5;
}

FWIW I think this might have something to do with the difference between 3.x and 4.0 fragment representation as discussed in #66978 but I'm not sure

Steps to reproduce

  1. Open example project
  2. Open testing.tscn
  3. Set renderer to compatibility

Minimal reproduction project

blendmul_073824.zip

clayjohn commented 1 year ago

~Related to https://github.com/godotengine/godot/issues/72488~