melonDS-emu / melonDS

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

Deformed floor textures in the Celestial Tower (Pokemon Black/White) (OpenGL only) #709

Open RafaelTrepaUnCarballo opened 3 years ago

RafaelTrepaUnCarballo commented 3 years ago

Sometimes, not always, while you're moving inside the tower, the floor textures warp.

This happens in OpenGL (even at native resolution), but apparently not with the Software render.

I attach the save of my Pokemon Black (Eur Spanish edition) inside the tower, you only have to walk a tile north to see it. Pokemon Edicion Negra.zip

RafaelTrepaUnCarballo commented 3 years ago

I attach videos that shows the texture wrapping in different renders. The difference between native resolution OpenGL and Software render is clear.

Captures.zip

Look at the flood in the bottom-left corner of the screen (although the ones in the botton-right area deforms a little too).

poudink commented 3 years ago

I've had a few instances where textures got deformed on the edges of the screen in OpenGL as well. I remember it happening once in the Super Mario 256 demo.

Arisotura commented 3 years ago

this might be caused by polygons becoming quads or other fun shit. while the DS can fill these polygons natively and have them look correct, modern GPUs are only really good at drawing triangles, so the quads/etc have to be broken down into triangles, which can result in distorted textures.

not quite sure how to go about fixing that, besides dividing these into smaller polygons so the error is less visible.

RafaelTrepaUnCarballo commented 3 years ago

Desmume doesn't have this issue (even in OpenGL render), so there must be a way to completely remove the warping. Although I don't know the performance cost of its solution.

DeSmuME 0.9.12 git#d787b737 x64-JIT SSE2 2020-08-18 11-07-55.zip

RSDuck commented 3 years ago

I also doubt this stems from the lack of native quad support. It almost looks like there's a problem with perspective correction.

Arisotura commented 3 years ago

don't get misled, DeSmuME totally has the quad-texture-distortion issue (it's visible on a specific quad in SM64DS Bob-Omb Battlefield, right under the bridge at the beginning).

it might be less prominent on screen edges because DeSmuME doesn't emulate clipping in OpenGL.

which is confirmed with the aforementioned SM64DS issue -- in melonDS's GL renderer, you can see that polygon's texture warp when the polygon gets partly offscreen, which doesn't happen on DeSmuME.

Arisotura commented 3 years ago

I came up with this: http://kuribo64.net/get.php?id=7Uasnw9njGd3t4Cc

nothing to do with melonDS itself, this is a simple test app that mimicks a few possible ways to split a quad into triangles. (I made it a DS app because it's much quicker to get 3D rendering going there)

first one is the original quad. second and third are basic diagonal split, as is currently implemented in melonDS's OpenGL renderer (and DeSmuME's, too). they show the two possible directions for a quad, but the basic idea is the same.

fourth is another mode I thought of: figuring out the center of the polygon and calculating the attributes (color, texcoord) at that point, then splitting into triangles around that point. we can see that the result is closer to what it should be. it's not perfect, but the test case shown there is an extreme case -- the difference should be much less noticeable in real-world cases, on generally smaller polygons.

of course, there is no perfect way to go about this. subdividing polygons isn't gonna be perfect, unless you get to the point where each polygon is one pixel or less, but then you're killing performance.

there was that post about rendering quads 'properly' under OpenGL. basically involved running custom rasterization on the GPU, in a fragment shader. thing is, said fragment shader needs to access the attributes for each vertex, and barycentric coordinates. I don't think this can work with more than 4 vertices, due to limitations in GPUs regarding how much crap can be passed to the fragment shader. and on the DS, we can get up to 10 vertices, so... yeah.

Arisotura commented 3 years ago

well, can you try on the latest revision?

ghost commented 3 years ago

I also remember warping when playing Tak: The Great Juju Challenge - the walls warped around when you moved. EDIT: well it's still there with the latest build but maybe it got less noticeable? didnt compare.

ghost commented 3 years ago

Well look like its worse now : https://streamable.com/96pllg it didn't distort like this before. Not all walls distort like that but its kinda distracting. another example : https://streamable.com/kp5xrq