PDA

View Full Version : accessing a specific mip level in OpenGL ES 2.0



thekend
06-03-2014, 01:58 AM
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!

thekend
06-06-2014, 12:17 AM
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".

arekkusu
06-06-2014, 10:00 AM
I target GL ES 2.0 (specifically webgl), I cannot use textureCubeLod

That's your answer. If your implementation doesn't expose textureLod (i.e. via EXT_shader_texture_lod (http://www.khronos.org/registry/gles/extensions/EXT/EXT_shader_texture_lod.txt) 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.

skynet
06-06-2014, 12:58 PM
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.

thekend
06-06-2014, 02:30 PM
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.

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.