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): half3 foo = tex2D(_BarTex, IN.uv); But is there any significant performance cost when supplying modified texture coordinates like so? Code (csharp): half3 foo = tex2D(_BarTex, IN.uv + half2(0.5, 0.5) * half2(123, 456) / bar.xy); Sam.
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.
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.
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.
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?
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