melonDS-emu / melonDS

DS emulator, sorta
https://melonds.kuribo64.net
GNU General Public License v3.0
2.98k stars 495 forks source link

polygon flickering when using OpenGL renderer #2017

Open Matytoonist opened 2 months ago

Matytoonist commented 2 months ago

no matter the settings, when using OpenGL rendering, random polygons on all 3d models flicker. It's not a rom issue, but i dont know what else could it be. I attach video showing the issue: https://file.garden/ZHiIgOH23R-SBqSb/2024-04-21%2016-48-31.remuxed.mp4 i also attach my system's specs just in case:

OS: Arch Linux x86_64 Host: 20150 Lenovo G480 Kernel: 6.8.7-arch1-1 Uptime: 4 hours, 49 mins Packages: 1305 (pacman), 11 (flatpak) Shell: bash 5.2.26 Resolution: 1440x900 DE: Plasma 6.0.4 WM: KWin Theme: [Plasma], Breeze [GTK2/3] Icons: oxygen [Plasma], TokyoNight-SE2 [GTK2/3] Terminal: konsole CPU: Intel Celeron 1000M (2) @ 1.800GHz GPU: Intel 3rd Gen Core processor Graphics Controller Memory: 5411MiB / 9814MiB

Smu1zel commented 2 months ago

Any chance you could try New Super Mario Bros? I've had a similar issue with the crocus driver (covers some GMAs and Sandy Bridge to Haswell iGPUS) with that game, and I ended up reporting the issue to Mesa. Unfortunately, the issue is currently dormant.

https://gitlab.freedesktop.org/mesa/mesa/-/issues/10379

If you have similar artifacts to what I've shown, I would "like" the issue and (optionally) reply to it.

TL;DR: This might be a Mesa bug, not a melonDS one.

InDubitubly commented 6 days ago

Any chance you could try New Super Mario Bros? I've had a similar issue with the crocus driver (covers some GMAs and Sandy Bridge to Haswell iGPUS) with that game, and I ended up reporting the issue to Mesa. Unfortunately, the issue is currently dormant.

https://gitlab.freedesktop.org/mesa/mesa/-/issues/10379

If you have similar artifacts to what I've shown, I would "like" the issue and (optionally) reply to it.

TL;DR: This might be a Mesa bug, not a melonDS one.

This linked mesa thread has found a solution and figured some things out, but it's pointing back to melonds as the culprit: "The emulator uses vertex shader, where it sets gl_Position = fpos;, but fpos.z is never assigned to. Instead it sets fragment depth by setting gl_FragDepth in the fragment shader. This works completely fine for the depth testing, and usually doesn't cause problems. But, if I understand correctly, when it just so happens that garbage gl_Position.z is outside of the clip space, the triangle is clipped early, so it's partially (or fully, if all vertices are outside) not rendered."

Hopefully this reply is seen by the devs so we can address the problem, I've been facing it on the title cinematic of Pokemon Platinum and inside the pokecenter in White/Black.

Smu1zel commented 5 days ago

Any chance you could try New Super Mario Bros? I've had a similar issue with the crocus driver (covers some GMAs and Sandy Bridge to Haswell iGPUS) with that game, and I ended up reporting the issue to Mesa. Unfortunately, the issue is currently dormant. https://gitlab.freedesktop.org/mesa/mesa/-/issues/10379 If you have similar artifacts to what I've shown, I would "like" the issue and (optionally) reply to it. TL;DR: This might be a Mesa bug, not a melonDS one.

This linked mesa thread has found a solution and figured some things out, but it's pointing back to melonds as the culprit: "The emulator uses vertex shader, where it sets gl_Position = fpos;, but fpos.z is never assigned to. Instead it sets fragment depth by setting gl_FragDepth in the fragment shader. This works completely fine for the depth testing, and usually doesn't cause problems. But, if I understand correctly, when it just so happens that garbage gl_Position.z is outside of the clip space, the triangle is clipped early, so it's partially (or fully, if all vertices are outside) not rendered."

Hopefully this reply is seen by the devs so we can address the problem, I've been facing it on the title cinematic of Pokemon Platinum and inside the pokecenter in White/Black.

I believe their hypothesis is correct. The following diff makes the problem disappear. Not sure if it's a proper fix, as I haven't done any rigorous testing to check for regressions, but Luigi's casino no longer corrupts, and the main game seems to be fixed too.

index 03bd43f9..0a229846 100644
--- a/src/GPU3D_OpenGL_shaders.h
+++ b/src/GPU3D_OpenGL_shaders.h
@@ -684,6 +684,7 @@ void main()
     fZ = float(vPosition.z << zshift) / 16777216.0;
     fpos.w = float(vPosition.w) / 65536.0f;
     fpos.xy *= fpos.w;
+    fpos.z = 0;

     fColor = vec4(vColor) / vec4(255.0,255.0,255.0,31.0);
     fTexcoord = vec2(vTexcoord) / 16.0;

image