Hello! There is a shader that draws geometry. And I need to add switches between (for example) normal mapping, texture mapping, and so on... How best to implement this? Add if conditions or create separate shaders for each case? Or is it better to combine?
One shader and conditions in it or several shaders for each case
Just a few random thoughts:
- one draw call needs one shader pipeline bound
- glsl supports shader subroutines, spirv not (yet ?)
- performance can suffer when pipelines with complex interfaces are switched
- simple pipelines don't impact performance that much
- draw calls can be batched to all use the same pipeline
- state influences shader compiling and execution
Less with APIs like Vulkan as the state is part of the pipeline afaik, but opengl starts to recompile shaders when states change and make that necessary. Debug messages tell me that ...
If it is just the method of normal calculation or surface colour, i'd go with a single pipeline and pass in a uniform to select the routine (Edit because the rest of the interface doesn't change). Thus i keep the ability to switch between versions more easily, if that is necessary. But hey, just random thoughts .... my experience is limited.