UNITY_SAMPLE_TEX2D vs UNITY_SAMPLE_TEX2D_SAMPLER

Can someone describe the differences between these preprocessor macros?

The descriptions in the manual aren’t all that discerning between the differences, advantages, usages, etc.

Is this so that you can save on samplers? I just don’t understand why you’d use a sampler for another texture.

Because even with modern GPUs there’s a limit to the number of samplers available to a shader, but there isn’t really much of a limit to the number of textures. You could sample a hundred different textures with the same sampler if you wanted to.

As for why, it’s because on the GPU each sampler is tied to a physical texture unit in the hardware. If you’ve got 16 textures being used in your shader and they’re all using separate samplers, they could all plausibly be sampled in parallel meaning if you had 16 textures of the same size, format, and sampling quality, sampling from all 16 could theoretically be no slower than just sampling one. If they’re all using a single sampler then they would have to be sampled in serial, so theoretically sampling 16 textures would be 16 times slower than just sampling one.

The inverse case of sampling multiple textures with one sampler is sampling one texture with multiple samplers. This could be wanted because you want to sample a texture with both anisotropic filtering and a point sampling, you could define two in the shader or use the one that “comes with” the texture and have a additional point sampler defined in the shader to use. You could also want to do multiple samples of one texture, like for doing blurs or edge detection, and it might be cheaper to define multiple samplers to do this with rather than reuse the one multiple times.

The reality is far more complicated, and changes between hardware and GPU vendors, but that’s kind of the “high level” view of the “low level” hardware. AMD for example has publicly documented the “cost” of sampling a texture in terms of the number of clock rate ticks between the request and the data being available to the shader, and recommending not using the texture data immediately after sampling to help hide this cost (also referred to as latency). Nvidia and Intel on the other hand are far more handwavy about it and basically say “don’t worry about it, our shader compiler will make it fast”, and to be fair most of the time they’re right, at least when it comes to Nvidia. Some people have been finding following the AMD guidelines for shader optimization is in fact producing more optimized shaders for Nvidia hardware as well too.

5 Likes