Search Unity

Dependent Texture Reads

Discussion in 'Shaders' started by spraycanmansam, Aug 15, 2014.

  1. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    Just wanted to ask a quick question regarding texture reads.
    I've read time and again that dependent texture reads are very bad for shader performance.
    As far as I can gather, a dependent texture read is when you use the result of a previous texture lookup as coordinates for a following texture lookup.
    I haven't really had need for that situation much, but it did make me wonder if there were any other gotcha's with texture coordinates..

    I assume performing a texture lookup using coordinates supplied straight from the vertex shader like so is pretty quick..
    Code (csharp):
    1. half3 foo = tex2D(_BarTex, IN.uv);
    But is there any significant performance cost when supplying modified texture coordinates like so?
    Code (csharp):
    1. half3 foo = tex2D(_BarTex, IN.uv + half2(0.5, 0.5) * half2(123, 456) / bar.xy);

    Sam.
     
    Last edited: Aug 15, 2014
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Significant? No, hardly, that's just a little math, any GPU will eat that up like nothing.
     
  3. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    the coordinate variable is independent from the texture, so adding a value to it is not changing the number of sampler2d maths, you could add and multiply about 1000 values on it prior to having a graphics slowdown on a pc.
     
  4. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    Ok cool, thanks for helping me clarify that :)
     
  5. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    292
    The iOS performance documents define dependent texture reads as doing any type of calculation on the texture coordinates. On OpenGL ES 2.0 you need to use the coordinates straight from the vertex shader, or the hardware won't be able to pre-fetch the texture data.

    On OpenGL ES 3.0 hardware these types of dependent texture reads should have no extra cost.

    So it does also depend on the range of hardware you want to support.
     
  6. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    From a technical standpoint, I'd agree that you need to use the coordinates directly coming from the vertex shader. The point of independent texture reads is that the gpu can pre-fetch the texture before the pixel shader even runs to avoid the latency that comes with texture samples. If you're doing any additional per-pixel calculations on the coordinates, the sampler unit would need to do them as well, which, to my knowledge, it is not capable of.
     
  7. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    That's what got me thinking and why I asked about modifying texture coordinates.. I remember reading about fast shader blurring algorithms that mentioned something about calculating and packing as many modified texture coordinates on the vertex shader side for speed..

    So is modifying texture coordinates something to really avoid or more something to be aware of when performance is critical?
     
  8. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    To improve shader performance on iOS you move all those calculations to the vertex shader. iOS and Android don't like it. They state in their docs to avoid dependent texture reads.

    These are mostly for like wavy water or screen distortion effects. Most desktop gpus can handle them.

    There is a article floating about the interwebs about the light shafts in infinity blade and how they optimised them by moving the blur coordinates out of the fragment shader. They could do this because they were just using a hard coded +1 xy pixel offset.

    You can't really move dependent texture reads into the vertex shader because you'll lose the per pixel distotion. You'll just get an interpolated vertex value.

    Also if you play shadowgun and your screen gets hit with water effect you'll notice the distortion is blocky. I'm assuming they distort the screen using a tessellated plane