Here’s a very strange issue I’ve encountered with shaders on Babylon. This only happens on macOS, with Safari and Chrome (not FireFox). Windows and Linux are OK.
To give some context, this shader adds a blue outline around a texture, based on alpha values.
The most crazy thing is that the problem can be solved by commenting a single line that deals with the blue sampled value (line 46 in the script). This does not make any sense to me, but maybe I’m missing something or there’s something wrong in my shader. Let me know if that’s the case.
I’m suspecting something like a problem in the shader compilation, and I do not think this is really related to Babylon, but I’m not sure.
Let me know if you have any hints or ideas. I’m also not sure where to report that issue if it’s not Babylon related ?
Thanks roland It took me something like 3 days to figure out the issue, it was really making no sense. I’m pretty sure the problem was not present 5 or 6 months ago, and appeared because of an update of some kind in either macOS, Chrome or Safari. I simplified my script step by step, until trying to assign only the alpha in the “else” part. Not sure why I did that, but I was really despaired and was trying everything !
The “discard” approach also works well (and is indeed faster I guess). As long as you don’t change the blue value in the “else” part, no problem. It’s a bit like if the assigning blue overlaps or corrupts the alpha value that is used a few lines later.
My current guess is an issue on the shader compilation process, specific to macOS / (Chrome or Safari), maybe related to an optimization of some kind… I’d be happy to register a bug about this somewhere, but I’m not sure where this “faulty” compiler might be located ? Is it in the browser, or in the OS (I tend to think it’s in the browser as Firefox works well ?).
I did some further investigations, and it looks like the problem is related to the use of Metal on Chrome and Safari instead of OpenGL. I changed the Chrome preferences to use OpenGL instead of Metal in Chrome (there’s an option named “Choose ANGLE graphics backend” in “chrome://flags”). Just change it to “OpenGL” and no problem anymore !!
Sadly, there are no options in Safari to force the use of OpenGL instead of Metal. So I think I should … open a bug on… Metal (really ??). I haven’t the faintest idea on how to contact the Metal developpers or if there’s a forum for that, but I’d really like that issue to be fixed in a generic way.
I’d say the shader is faulty as it samples a mipmapped texture in non-uniform control flow, which I think is UB. Sampling a specific mip or evaluating the texture/mip level in uniform control flow fixes the issue: