Issue
This Content is from Stack Overflow. Question asked by Hasen
Often version 150 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:
150 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;
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(uv_x - (4.0 * bloom_spread), uv_y), ivec2(0));
h_sum += texelFetch(u_texture, ivec2(uv_x - (3.0 * bloom_spread), uv_y), ivec2(0));
h_sum += texelFetch(u_texture, ivec2(uv_x - (2.0 * bloom_spread), uv_y), ivec2(0));
h_sum += texelFetch(u_texture, ivec2(uv_x - bloom_spread, uv_y), ivec2(0));
h_sum += texelFetch(u_texture, ivec2(uv_x, uv_y), ivec2(0));
h_sum += texelFetch(u_texture, ivec2(uv_x + bloom_spread, uv_y), ivec2(0));
h_sum += texelFetch(u_texture, ivec2(uv_x + (2.0 * bloom_spread), uv_y), ivec2(0));
h_sum += texelFetch(u_texture, ivec2(uv_x + (3.0 * bloom_spread), uv_y), ivec2(0));
h_sum += texelFetch(u_texture, ivec2(uv_x + (4.0 * bloom_spread), uv_y), ivec2(0));
sum += h_sum / 9.0;
}
gl_FragColor = texture2D(u_texture, v_texCoords) - ((sum / 9.0) * bloom_intensity);
}
vec4 texelFetch(sampler2D tex, ivec2 size, ivec2 coord) {
return texture2D(tex, vec2(float(coord.x) / float(size.x), float(coord.y) / float(size.y)));
}
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.