Strange shader-related issue on macOS with Safari and Chrome (not Firefox)

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.

Here’s a playground showing that issue: macOS_Safari_and_Chrome_Shader_issue

When launched in the conditions described above, there are some flickering glitches on the left side of the texture:

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 ?

1 Like

I’m curious how did you figure out what causes the issue :smiley: Kudos!

A quick fix here is to discard the unwanted pixels instead of setting the color to vec4(0.0)

Tested on MacOS Sequioa 15.0.1, Chrome Version 129.0.6668.101 (Official Build) (arm64)

Thanks roland :slight_smile: 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 ?).

1 Like

I believe @Evgeni_Popov is the person who can give us a correct answer here.

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 !!

Would that explain everything ?

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 think you should open a bug for each browser:

If the problem isn’t browser-related, they’ll pass on the information appropriately (probably!).

1 Like

They both use angle in the same way so there might be the issue

1 Like

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:

2 Likes