shader: fix range calculation for generic and patch shader variables
The size is always last - first + 1
so make sure that we are not using the sid within this calculation.
Put loop-independent assignment out of the loop and make sure that the blocks are consistent to help read it.
Edited by Corentin Noël