Performance wise, calling a texture and multiplying it by 0 is the same as not calling it?

Hello! I was wondering it because I added an emissive map to the parent material of my environments, and I’m not sure if the engine is going to call an extra texture map every time, even if it’s usually multiplied by 0.

If the 0 is a constant that’s hard-coded into the shader, then the shader compiler will probably (but not guaranteed) not sample the texture.

If the 0 is a static parameter that you can change in the material, then the shader compiler may still be smart enough to not sample the texture.

If the 0 is a dynamic parameter that you can change at runtime, then the shader compiler must include the texture sample operation, because it doesn’t know what value you will give to the “0” parameter.

However, there’s a simple way you can tell for yourself!
Put the derived material on an object. Check how many shader instructions it’s using. Now, change the “0” to a “1,” recompile, and check how many shader instructions it’s using. If the number goes up, then it’s likely the “times zero” was optimized by the shader compiler.

image

1 Like

Thanks a lot, really useful. Yes, the texture lookups raised 1 unit, as well as the texture samplers, when adding an emissive map and multiplying it by 0. I understand that this is a bad thing for performance, right? Or doesn’t matter while it’s 0?

Yes, when it uses one more texture lookup, it will cost more shader performance, even if it’s multiplied by zero.

Whether this actually matters for your scene on your target hardware, depends on where the bottleneck is. If you’re bottlenecked on something else, then it doesn’t matter. If you’re bottlenecked on pixel shader fill rate, then it would matter a little bit.

Then again, the resources are there to be used. If it’s easier to ship a game that is fun and looks good, and you still hit very playable frame rates on target hardware, then just go with the more complex shader. Performance only matters when it actually matters :slight_smile: