accessing a specific mip level in OpenGL ES 2.0

Hi!

I want to do environment mapping with selecting a specific mip level of a cube map in the sahder according to the roughness parameter of the material (per pixel).
Since I target GL ES 2.0 (specifically webgl), I cannot use textureCubeLod (which is available on desktop GL and is working as expected), the only thing I can try is the bias parameter. In the cube map I store the mip level in the alpha channel. So first I do a lookup to find out for which mip level the hardware selecting, then do the bias in a second read. However I cannot select the specific mip level this way. To do some debugging, I put a specific color into each cube face mip level, like the entire 0. level for all faces is red, the 1. level is blue etc. With the following sample code, I wanted to access the 3rd mip level, but unfortunately it doesn’t work:


float targetLevel = 3.0;
vec4 color = textureCube(envmap, tc);
float level = color.a * 255.0;
float bias = targetLevel - level;
gl_FragColor = textureCube(envmap, tc, bias);

The result is a gradient transition across multiple mip levels, and also it’s changing for minification/magnification. If I put the read level into gl_FragColor, it’s ok. So I guess the bias is not working this way.

Any help is appreciated.
Thanks!

Maybe this thread should be in the OpenGL Shading Language section and the title should be changed to “Sampling from a specific mip level in GLSL ES 1.0” or “how texture sampling bias parameter works”.

That’s your answer. If your implementation doesn’t expose textureLod (i.e. via EXT_shader_texture_lod in ES2 on iOS) then you can’t do it.

For debugging only (i.e. visualizing unfolded cube map faces), you can force a specific LOD by carefully drawing with the right pixel-to-texel ratio. But you can’t do it for general purpose environment maps.

An approximate solution would be to use dFdx/dFdy to calculate the miplevel that the fragment is probably going to use.
Use the negative of that as LoD bias and add your own targeted level. This should redirect the chosen miplevel to the one you intend to use.

[QUOTE=skynet;1259853]An approximate solution would be to use dFdx/dFdy to calculate the miplevel that the fragment is probably going to use.
Use the negative of that as LoD bias and add your own targeted level. This should redirect the chosen miplevel to the one you intend to use.[/QUOTE]

My code snippet exactly do that without using the dFdx/dFdy. I sample the texture two times, the first sample is used to read the miplevel that the fragment is going to use (the mip level is encoded into the alpha channel of the texture). But unfortunately bias = targetLevel - level is not good for accessing targetLevel.