I am trying to simulate the behavior of "texelFetch()" and so far I've understood the following:

If "gvec4 texelFetch(gsampler3D sampler, ivec3 P, int lod)" is given then :

result = sampler * P

But what should I do with "lod"? Is it supposed to be used to scale the sampler?

I've read sufficient information about the purpose of lod/level-of-details/mipmapping level theoretically but I would like to understand the computation usage of it in the sampling process.

  • $\begingroup$ when having a texture with its level of details, you can use the dFdx() and dFdy() function of the textureCoordinate variable to see, how the textureCoordinate vary in fragment shader stage... The important information in general is: What textureCoordinate has the neighboring pixel, to decide which LOD to use $\endgroup$
    – Thomas
    Commented Aug 22, 2022 at 16:18
  • $\begingroup$ please look at bluevoid.com/opengl/sig00/advanced00/notes/node57.html there you can see a texture and its mipmaps... when using texelFetch the int lod parameter is the selection of the mipmap, where 0 is the original image, 1 is the next smaller image and so on. $\endgroup$
    – Thomas
    Commented Aug 22, 2022 at 16:23
  • $\begingroup$ Mipmaps are normally precomputed and would be presented to the sampler as an array of images, each one half the size of the previous. So as far as texelFetch is concerned, the lod value is simply an array index, and sampling proceeds as normal within the selected mip level. (BTW, I don't know what you mean by "result = sampler * P". A sampler is a data structure, not a number that you can multiply by...) $\endgroup$ Commented Aug 22, 2022 at 16:38
  • $\begingroup$ @NathanReed Your information is really helpful.I understood that the lod is an array index so it shows the selection of which level is being used. but I have a follow-up question. I am trying to implement texelFetch function for verification purpose so when I said "result = sampler * P" I meant that : result.x = sampler.x * P.x; result.y = sampler.y * P.y; result.z = sampler.z * P.z; result.w = sampler.w * P.w; But what I don't understand is that how does lod fit into this computation for my implementation? I got the theory part but I'm trying to understand the mathematical aspect now $\endgroup$
    – BusyCoder
    Commented Aug 22, 2022 at 16:55
  • $\begingroup$ @NathanReed Considering the example I gave should I first do Sampler * lod and then ` result = sampler * P ` ? $\endgroup$
    – BusyCoder
    Commented Aug 22, 2022 at 16:56

1 Answer 1


texelFetch doesn't do any computations. The texture coordinates are in integer texels in texel space. That represents the exact texel you're asking for. The same goes for the lod: it specifies the exact mipmap level to fetch from, with 0 being the base level (largest) for the texture.

sampler * P is not valid GLSL. Samplers are opaque types; the only way to access the resources the sampler represents is to use the standard library functions like texelFetch. You cannot "simulate the behavior of texelFetch" with a sampler.

  • $\begingroup$ Thanks for this information. I understand that sampler has opaque types and texelFetch isn't something that I can simulate but I am doing the implementation for verification purpose. What I am asking is how the code below the texelFetch may look like. $\endgroup$
    – BusyCoder
    Commented Aug 23, 2022 at 15:18
  • 1
    $\begingroup$ @BusyCoder: What are you trying to "verify" here? Because you can't verify anything. If you have a texture in some memory, you have to use the texture interface to access its data. You can't backdoor them and access the memory directly; the most you could do is allocate some buffer and access that buffer arbitrarily. But you wouldn't be verifying anything because the texture would have separate memory from that buffer. "Verification" is a process for catching mistakes. What mistake do you think your verification is going to catch? $\endgroup$ Commented Aug 23, 2022 at 15:29
  • $\begingroup$ @BusyCoder: "how the code below the texelFetch may look like" It would look like a series of pointer offsets and array indexes. But that's dependent on the byte layout of the texel data. $\endgroup$ Commented Aug 23, 2022 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.