12

I'm getting into GLSL and need some help with texture lookups. I'm trying to use a texture for storage but I cannot get "proper" texture lookups. I would prefer using the usual texture2D method (using GLSL 1.2) but the results are not good enough.

Using texture2D: one

Using texelFetch: two

Now obviously something is wrong with the first one. Here is what I am trying to do (yes sizes are correct unless there is something I don't know about):

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

So, how would this be done properly?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Rohan Singh
  • 326
  • 1
  • 3
  • 8
  • 1
    What are the texture filtering parameters you are using? Does your texture have mipmaps? – Nicol Bolas Dec 27 '11 at 04:35
  • It was on nearest and had no mipmaps. I figured out the problem though. I was using the texture size instead of range, so it was downscaling a bit. Poor naming. – Rohan Singh Dec 27 '11 at 07:01

2 Answers2

6

This has become some sort of FAQ:

I answered it here

https://stackoverflow.com/a/5879551/524368

and here

https://stackoverflow.com/a/7272871/524368

and in a few other places as well.

texture2D(tex, (2 * uv + vec2(1.))/2 * u_texsize);

Hasen
  • 11,710
  • 23
  • 77
  • 135
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • 1
    Thanks. I did spend the last few days trying to figure out what was wrong but I never came across your answers or another like them. I guess my keywords weren't too great. – Rohan Singh Dec 27 '11 at 16:57
  • 17
    Chastising people for not googling isn't helpful; people who **do** take the time to search tend to run in to all the links of people getting mad that someone didn't search, which ends up making the pool useless for the ones actually trying to look up solutions before asking new questions. – Skrylar Aug 09 '12 at 08:17
  • 31
    Ironically, this answer was the first stackoverflow link in my search results. – May Oakes Nov 09 '12 at 23:06
  • ...and no one upvoted this for that reason :0 – mlvljr Nov 02 '13 at 20:46
  • 1
    You still don't have an actual answer here. What would be a fully working `texelFetch` function then? – Hasen Sep 18 '22 at 15:48
  • @Hasen: Uh, you just plug in the equations into the regular call to texture. You'll have to pass the texture dimensions as an extra `vec2i` uniform, since GLSL version profiles that don't support `texelFetch` also don't support `textureSize`. Anyway, an implementation would look like `texture2D(tex, (2*uv + vec2(1.))/2*u_texsize);` – seriously, all you have to do is plug in the equation. – datenwolf Sep 18 '22 at 17:36
  • @datenwolf I added that into your question but you should really put the whole formula in for a complete answer, at the moment it's just a couple of links. Probably explains the lack of upvotes. Even the comments on your answer have more upvotes than your answer. He is already passing the texture size in his function. So `uv` is supposed to refer to the `ivec2 coord` in his original function? – Hasen Sep 20 '22 at 12:33
3

I would say, use textureRect. Then you can use texture2D, and supply the actual coodinates in pixels that you would like to access.