Humus said:You could render intermediate values to a render target and use that in all shaders thereafter. It'll cost some, but if there's enough shared parts it would be a gain.
That's what I was afraid of. First, the technique needs a render target that's the size of the front/back buffer to store the variable. Will the GPU allow reading from the render target and writing to it if more "if-then-else" statements occur after the first one? Would you need ping-pong buffers?
And an extra texture lookup would be required to read the variables back in the next passes. That's going to cost bandwidth unless the variables are zero a lot of the time. And if you need to compute more than four variables, it's MRT time.
But I guess on hardware that do not support dynamic branching, beggars cannot be choosers. Simple emulated "if-then-else" is better than nothing as you've already shown. I think techniques may already exist before one has even thought about the problem, but it's the people who really popularise the techniques that eventually get remembered.