0

Often newer version shaders can be simple to convert but this one uses the function texelFetch which is not available in version 120. Leastways my version is not working:

New version:

in vec4 v_colour;
in vec2 tex_coord;
out vec4 pixel;

uniform sampler2D t0;
uniform float bloom_spread = 1;
uniform float bloom_intensity = 2;

void main() {
    ivec2 size = textureSize(t0, 0);

    float uv_x = tex_coord.x * size.x;
    float uv_y = tex_coord.y * size.y;

    vec4 sum = vec4(0.0);
    for (int n = 0; n < 9; ++n) {
        uv_y = (tex_coord.y * size.y) + (bloom_spread * float(n - 4));
        vec4 h_sum = vec4(0.0);
        h_sum += texelFetch(t0, ivec2(uv_x - (4.0 * bloom_spread), uv_y), 0);
        h_sum += texelFetch(t0, ivec2(uv_x - (3.0 * bloom_spread), uv_y), 0);
        h_sum += texelFetch(t0, ivec2(uv_x - (2.0 * bloom_spread), uv_y), 0);
        h_sum += texelFetch(t0, ivec2(uv_x - bloom_spread, uv_y), 0);
        h_sum += texelFetch(t0, ivec2(uv_x, uv_y), 0);
        h_sum += texelFetch(t0, ivec2(uv_x + bloom_spread, uv_y), 0);
        h_sum += texelFetch(t0, ivec2(uv_x + (2.0 * bloom_spread), uv_y), 0);
        h_sum += texelFetch(t0, ivec2(uv_x + (3.0 * bloom_spread), uv_y), 0);
        h_sum += texelFetch(t0, ivec2(uv_x + (4.0 * bloom_spread), uv_y), 0);
        sum += h_sum / 9.0;
    }

    pixel = texture(t0, tex_coord) - ((sum / 9.0) * bloom_intensity);
}

This is my 120 version but it doesn't work, it just makes the texture black:

varying vec4 v_color;
varying vec2 v_texCoords;

uniform sampler2D u_texture;
const float bloom_spread = 1.0;
const float bloom_intensity = 2.0;
uniform vec2 size;

vec4 texelFetch(sampler2D tex, ivec2 size, ivec2 coord) {
    return texture2D(tex, vec2(float(coord.x) / float(size.x), float(coord.y) / float(size.y)));
}

void main() {
    // ivec2 size = textureSize(u_texture, 0);

    float uv_x = v_texCoords.x * size.x;
    float uv_y = v_texCoords.y * size.y;

    vec4 sum = vec4(0.0);
    for (int n = 0; n < 9; ++n) {
        uv_y = (v_texCoords.y * size.y) + (bloom_spread * float(n - 4));
        vec4 h_sum = vec4(0.0);

        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x - (4.0 * bloom_spread), uv_y));
        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x - (3.0 * bloom_spread), uv_y));
        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x - (2.0 * bloom_spread), uv_y));
        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x - bloom_spread, uv_y));
        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x, uv_y));
        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x + bloom_spread, uv_y));
        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x + (2.0 * bloom_spread), uv_y));
        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x + (3.0 * bloom_spread), uv_y));
        h_sum += texelFetch(u_texture, ivec2(size), ivec2(uv_x + (4.0 * bloom_spread), uv_y));
        sum += h_sum / 9.0;
    }

    gl_FragColor = texture2D(u_texture, v_texCoords) - ((sum / 9.0) * bloom_intensity);
}
Hasen
  • 11,710
  • 23
  • 77
  • 135
  • 1
    That shader is not version 1.50. It's more like 4.40 or something. `textureSize` is way more recent than 1.50. – Nicol Bolas Sep 18 '22 at 16:34
  • @Nicol Bolas Ok that doesn't really make any difference, it still doesn't work with 120. – Hasen Sep 18 '22 at 16:42
  • You're not providing the right arguments to "your" `texelFetch` implementation. – LJᛃ Sep 20 '22 at 10:11
  • @LJᛃ Ok fixed that but it still shows a black texture. The shader is supposed to be bloom but it just makes the texture dark or fully black, the higher the intensity, the darker it is. I don't think the custom `texelFetch` function does the same as the `texelFetch` function built into later version of opengl used in the original shader. – Hasen Sep 20 '22 at 12:10
  • `texelFetch` doesn't do any filtering like `texture2D` does, which is also why it requires the mip level to be specified(like `texture2DLod` does), but in your context it should more or less work assuming you've filtering setup correctly so your texture can be sampled from at all, which you have going by the behavior you describe. You should validate that `size` is properly passed by replacing it with a hardcoded constant. – LJᛃ Sep 21 '22 at 16:54
  • @LJᛃ `size` is already clearly passed with `uniform vec2 size;`, the problem is not there, changing its values doesn't fix anything. The shader literally just makes things varying degrees of darkness. – Hasen Sep 23 '22 at 08:55
  • @Hasen you've validated that by replacing it with a constant in the shader yes? – LJᛃ Sep 23 '22 at 09:46
  • @LJᛃ Yes with a constant in the main body `vec2 size = vec2(4.0, 4.0);` and the values are confirmed the same that are being passed and the result looks identical - a slightly black texture. – Hasen Sep 24 '22 at 12:05
  • so the texture you're blooming is 4 x 4 pixels ... ? – LJᛃ Sep 24 '22 at 15:56

0 Answers0