Skip to content

Fix stb_image optimization bug with gcc 15.2.0#6927

Merged
omichel merged 5 commits intomasterfrom
fix-stb_image-optimization-bug-with-gcc-15.2.0
Feb 13, 2026
Merged

Fix stb_image optimization bug with gcc 15.2.0#6927
omichel merged 5 commits intomasterfrom
fix-stb_image-optimization-bug-with-gcc-15.2.0

Conversation

@omichel
Copy link
Member

@omichel omichel commented Feb 5, 2026

This PR fixes a random 3D rendering bug showing up on some Windows 11 (GeForce RTX 3060) in release mode (not in debug). It appears to be caused by gcc 15.2.0 failing to optimize some weird code (with some goto inside a loop) in stb_image.h. See nothings/stb#1884 for details.

This patch is temporary and should be removed whenever nothings/stb#1884 gets merged.

Fixes #6909.

@omichel omichel requested a review from a team as a code owner February 5, 2026 09:13
Copilot AI review requested due to automatic review settings February 5, 2026 09:13

This comment was marked as resolved.

@CoolSpy3 CoolSpy3 added test webots build Start the build tests test suite Start the test suite labels Feb 7, 2026
Copy link
Contributor

@CoolSpy3 CoolSpy3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't reviewed the underlying change in-depth, but it makes sense at a high level, so I'm not opposed to merging this in until stb approves the change to be merged upstream. Out of curiosity, does this fix #6909, or is this a different rendering bug?

I do want to verify that Webots builds against this new version of stb (as it looks like there have been some other changes since then), but if so, this is good to merge.

@CoolSpy3
Copy link
Contributor

CoolSpy3 commented Feb 7, 2026

Hmmm. Looks like the Windows test suite is failing (I ran it twice) because Python can't find Controller.dll. Tests pass on the other branches, and this seems unrelated to stb, so I'm fine with merging this. Should maybe look into that tho :P

@omichel
Copy link
Member Author

omichel commented Feb 8, 2026

Out of curiosity, does this fix #6909, or is this a different rendering bug?

Yes, I believe it does as I saw exactly the same kind of weird images appearing randomly (in a non reproducible way).

@CoolSpy3
Copy link
Contributor

CoolSpy3 commented Feb 10, 2026

Out of curiosity, does this fix #6909, or is this a different rendering bug?

Yes, I believe it does as I saw exactly the same kind of weird images appearing randomly (in a non reproducible way).

Unfortunately, I tested this and that bug still appears on my machine when building from this branch, so the root cause is probably something else.

I also did some basic playing around with the test suite, and it works for me locally. In CI, it looks like the dll load is using an absolute path to the Controller dll, which should definitely be installed and have no weird dependencies, so idk what's going on there. It's possible that the cause lies with the other error (of failing to allocate a tcp port), which could indicate that Webots is failing to free TCP ports, but imo, it would be more likely for the python error to cause that than for that to cause the python error. In summary, for now, I'm stumped.

Again tho, I doubt that it's a problem related to the stb change.

Update: I did a clean build and now it looks like the above issue might be fixed 👀 I'll try to do some better testing later to see for sure. Exciting (for me) if true tho :D

Update 2: After doing some better testing, this does appear to be fixed! Amazing :D Idk how you even thought to test for a single-platform, optimization-based bug in an external library, but kudos!

@omichel
Copy link
Member Author

omichel commented Feb 10, 2026

On my local Windows 11 machine, this PR breaks some tests in the test suite. I am investigating why.

@omichel
Copy link
Member Author

omichel commented Feb 12, 2026

Hmmm. Looks like the Windows test suite is failing (I ran it twice) because Python can't find Controller.dll.

It turns out I had this problem in my development environment as well. I propose this fix.

@omichel
Copy link
Member Author

omichel commented Feb 13, 2026

Idk how you even thought to test for a single-platform, optimization-based bug in an external library, but kudos!

I found out that the bug was only showing up in release mode, not in debug. The bug was actually showing up with gcc -O2 or -O3 and not with -O1 optimization level. So I tried to compile some files with -O1 and some with -O3, until I isolated the problem to a single file: WbBackground.cpp. Inside this file, I used the #pragma statement to compile some functions in -O1 and some others in O3. I found that the culprit was the stb_image.h include file. In this file, I isolated the function causing problem. I even further split this function in sub-functions to isolate the lines of codes causing the problem. Then, I found out this very strange goto statement which was hard to understand (for me and also for gcc) and that gcc was failing to optimize properly. I fixed the code by getting rid of this odd goto and that fixed the problem. It took long, but I believe the fix is clean, as it also makes the code of stb_image.h more readable and maintainable. However, I agree the bug is fundamentally in gcc, not in stb_image.h. But fixing gcc would have taken me even more time.

@CoolSpy3
Copy link
Contributor

Wow! Awesome! Thanks for the explanation! I'll keep some of those tricks in mind for the future 👀

@omichel omichel merged commit e9061d7 into master Feb 13, 2026
22 checks passed
@omichel omichel deleted the fix-stb_image-optimization-bug-with-gcc-15.2.0 branch February 13, 2026 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test suite Start the test suite test webots build Start the build tests

Development

Successfully merging this pull request may close these issues.

TexturedBackground Occasionally Creates Lighting Artifacts

3 participants