Depends on the shader compiler (which depends on what platform you’re working on and building for). Desktop, especially Windows PCs with an Nvidia GPU, are quite lenient about this kind of thing and will make a guess as to what the author wanted. That line will likely result in a compiler error if building for Android or iOS, and maybe even OSX. On PCs with AMD GPUs I’ve seen stuff like that compile, but then just not do what you might expect.
Let’s dissect that line a little.
result = lerp(1.0, result, result.a);
The variable result is a float4, so that’s a lerp between a float and a float4 which you correctly noticed doesn’t make any sense. So there’s three ways to handle that. One is to throw an error and say “don’t do that”, which is what you expected.
The other two are to treat that as being either:
result = lerp(float4(1.0, 1.0, 1.0, 1.0), result, result.a);
or:
result = lerp(1.0, result.r, result.a);
The first of those two is what is what it is likely choosing. In the case of the lerp function that’s probably a safe bet since the value you’re setting is also a float4, and otherwise if that value was a float the outcome would still kind of “work” even if it chose to expand the 1.0 to a float4 as it would then just take the first component of that result and the shader compiler’s optimizer would just skip the other 3 components ever being calculated. I’ve seen some compilers actually choose the second option though, probably because the first variable in the lerp is a float, and float4 = float is completely valid, even in more strict compilers.
So, that’s a long way of saying yes, it shouldn’t work, but it does because desktop shader compilers try to be smart.
As for why desktop is “smart” and mobile is “dumb”, really it comes down to the early days of PC gaming. A lot of devs were writing software that didn’t work, and they’d go to the GPU makers and API writers and ask “why doesn’t this work.” Early on you wouldn’t get any kind of error, usually something just didn’t render, or rendered wrong, or outright crashed the computer! Sometimes it’d be a legitimate bug with something other than the game dev’s code, but likely most of the time it was a silly typo or mistake like this. Someone probably decided it’d be easier to just “make it work” than spend time debugging and helping devs understand why terrible code didn’t work. Over a short time it became a marketing point; “our GPU can run game X perfectly and vendor B can’t!” Ever wonder why whenever a new big PC game comes out there are new drivers for your GPU with special “optimizations” for games? Part of that is because the GPU makers are building in new fixes to work around bad code from devs, and sometimes outright replacing bits of the game’s code with faster / working code! It’s become a bit of an arms race between Intel, AMD and Nvidia, and one that Nvidia has been winning. This is especially true since having devs continue to write badly authored graphics code that can run with out a noticeable issue on an Nvidia GPU (which a lot of devs are probably using themselves) but might have graphical glitches or outright fail on AMD and Intel only helps Nvidia’s image. Personally I use Nvidia for dev because I have found Photoshop has annoying issues like the selection marquee being off by one pixel when using AMD.
For mobile it seems early on it was decided to just make the APIs more strict, and make sure there were tools that would give an error instead of just failing to render or crashing the hardware (the later of which is what happened for early PC devs most of the time). Plus you can’t expect a phone to get updated as often as a gaming PC. Mobile devs probably have some fun stories about having to work around issues on specific devices because of this.
Thus concludes the bgolus unrequested history hour… tune back in next week…