When does a shader patching(so they called) occur?

HI all



I’ve read the presentations of GDC provided by the imgtec website.



A concept called “shader patching” was mentioned. I’m not sure what exactly it is.

It seems like to happen when some gl states are changed, and may impact the performance.



I want to know why this could happen, and what kind of states changing(please be specifically) could make this happen.



Thanks!



Chris

Excuse me, I also want to know what the thing: “shader patching” exactly does.

Thanks!

Hi Chrishoo,



Shader patching is something we do at run time, after shader compilation but before we actually use the shader to draw something. When a shader is compiled, it is done so semi-generically, so that there is scope to optimise for various reasons. Rather than causing a recompilation of the entire shader every time we need to change something, we simply apply a patch to the existing binary. This operation is generally not that expensive though, and is always done out of either necessity or because it will make the shader run faster.



So some GL states don’t physically exist as a switch in the hardware, and are instead handled by passing code through the shader. In the case of blend states, we don’t have fixed function blending hardware, instead we patch the current fragment shader with a small binary blob to tell it how to blend.



There are other reasons we perform shader patching, but they aren’t quantifiable in API terms, and I’m afraid I’m not at liberty to discuss the specifics. However, the way to optimise around these is the same optimisation we ask for other thing - group your draw calls by their various state. In other words, group opaque, blended and alpha tested values together, and group renders using the same shader/textures/vertex data/etc. together. The fewer changes you make between draw calls, the better.



For the most part there are generally other things you’ll need to optimise well before this becomes an issue though, so I wouldn’t spend too much time worrying about it. As long as you follow the guidelines in the performance recommendations document you should get the best out of your renderer.



Regards,

Tobias