Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Lod caculation for this shader

  1. #1
    Newbie Newbie
    Join Date
    May 2018
    Posts
    1

    Lod caculation for this shader

    Hi, all

    i am trying debug a render error issue for this case.

    this original fragment shader:

    Code :
    precision mediump float;
    uniform float mixAmount;
    uniform sampler2D inTexture;
    uniform sampler2D colorCube0;
    uniform sampler2D colorCube1;
    varying vec2 v_texCoord;
     
    vec4 sampleAs3DTexture(sampler2D tex, vec3 texCoord, float size) {
       float sliceSize = 1.0 / size;                         // space of 1 slice
       float slicePixelSize = sliceSize / size;              // space of 1 pixel
       float sliceInnerSize = slicePixelSize * (size - 1.0); // space of size pixels
       float zSlice0 = min(floor(texCoord.z * size), size - 1.0);
       float zSlice1 = min(zSlice0 + 1.0, size - 1.0);
       float xOffset = slicePixelSize * 0.5 + texCoord.x * sliceInnerSize;
       float s0 = xOffset + (zSlice0 * sliceSize);
       float s1 = xOffset + (zSlice1 * sliceSize);
       vec4 slice0Color = texture2D(tex, vec2(s0, texCoord.y));
       vec4 slice1Color = texture2D(tex, vec2(s1, texCoord.y));
       float zOffset = mod(texCoord.z * size, 1.0);
       #if defined(USE_NEAREST)
         return mix(slice0Color, slice1Color, step(0.5, zOffset));
       #else
         return mix(slice0Color, slice1Color, zOffset);
       #endif
    }
     
    void main() {
      vec4 originalColor = texture2D(inTexture, v_texCoord);
      vec4 color0 = sampleAs3DTexture(colorCube0, originalColor.rgb, 8.0);
      vec4 color1 = sampleAs3DTexture(colorCube1, originalColor.rgb, 8.0);
      gl_FragColor = vec4(mix(color0, color1, mixAmount).rgb, originalColor.a);
    }

    for the sampler colorCube0, colorCube1 the texture size is 64 x 8, and in the function sampleAs3DTexture(),
    there will be some quad(2x2 pixel) which zSlice0 = a and the pixel next zSlice0 = a + 1.

    so when caculate the lod for the colorCube0 and colorCube1 like below:

    Code :
      float2 dx = dfdx( uv * 64);
      float2 dy = dfdy( uv * 8 );
      float d = max( dot( dx, dx ), dot( dy, dy ) );
      float lod= 0.5 * log2(d);
    there will be lod which almost equal to 3 in the shader.

    but on some gpu like Qualcomm adreno, the lod always zero. is there any something i missing?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,790
    Quote Originally Posted by cristiano790 View Post
    Code :
      float2 dx = dfdx( uv * 64);
      float2 dy = dfdy( uv * 8 );
    there will be lod which almost equal to 3 in the shader.

    but on some gpu like Qualcomm adreno, the lod always zero. is there any something i missing?
    I'm assuming dfdx() is a typo; the built-in function is dFdx() with an upper-case F. And if you're using OpenGL ES, bear in mind that ES 2 doesn't have derivatives as standard (they're available via the OES_standard_derivatives extension).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •