How would you convert this shader to work with version 120?

Viewed 50

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);
}
0 Answers
Related