# How is mipmap level used mathemetically in image sampling?

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.

• 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 Commented Aug 22, 2022 at 16:18
• 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. Commented Aug 22, 2022 at 16:23
• 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...) Commented Aug 22, 2022 at 16:38
• @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 Commented Aug 22, 2022 at 16:55
• @NathanReed Considering the example I gave should I first do Sampler * lod and then  result = sampler * P  ? Commented Aug 22, 2022 at 16:56

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.