News for hackers. Stuff that matters.
Re:GTA III and Vice City fully reverse engineered: re3 by aap_ on Sunday February 14, 2021 @08:24AM (#26132195)
Maybe this site is the best place to ask. There are two major things we could use help with:

- the games do not run particularly well on the raspberry pi (any version really) and I have a feeling it's at least partially due to the OpenGL layer. We could really use someone with how to optimize OpenGL (especially on the pi).

- physics have always been wonky beyond 30fps. I think someone with a good knowledge of game physics could really help us here.

Re:GTA III and Vice City fully reverse engineered: re3 by sokoloff on Sunday February 14, 2021 @08:33AM (#26132247)
I did some of the physics work (but was not the lead) for Papyrus racing games in the ‘95-‘97 era. Email’s in my profile if I can help.

My recollection of Vice City is that the vehicle dynamics model is not particularly realistic but optimized for “fun gameplay with a hint of realism” whereas Papyrus optimized towards “realistic gameplay with a hint of fun”. :D

Our physics engine was deeply, deeply tied to a particular isochronous rate (30 Hz, then later 60Hz, IIRC) with all kinds of hackery to make that happen and divorce rendering rate from physics rate. (On the PSX, we ran physics engine tied to the VBlank.)

Re:GTA III and Vice City fully reverse engineered: re3 by aap_ on Sunday February 14, 2021 @08:41AM (#26132303)
Yeah, i think so too. In Vice City the most surprising thing to me was that they actually modelled tire temperature. it's probably a bit crude but i found it interesting. As for the physics code, there's a big ProcessControl function all sorts of physical entities that does everything that has to happen to that entity each frame. some of it is physics, some not. My theory as to why varying timestep produces different results is that the physics calculations form some sort of differential equations whose solution depends on the timestep. That may be wrong but it makes sense to me at least. My approach to fixing it would be to log all sorts of forces applied to the physical objects each frame and log them so they can be compared. this is quite a bit of stuff to implement though so nobody has done it yet. if you or somebody is interested in that, we'd appreciate that a lot.
Re:GTA III and Vice City fully reverse engineered: re3 by rzzzt on Sunday February 14, 2021 @09:03AM (#26132453)
The physics engine of Vice City is definitely tied to the frame rate. If you switch off vsync, you can only drive in reverse veeery slowly.
Re:GTA III and Vice City fully reverse engineered: re3 by aap_ on Sunday February 14, 2021 @09:05AM (#26132471)
That one i was actually able to fix :)
Re:GTA III and Vice City fully reverse engineered: re3 by mypalmike on Sunday February 14, 2021 @04:27PM (#26136425)
Ha, back in that same era I was working at a small game company that was in discussions with Papyrus about doing some work on the Indy Car Racing game - probably a Playstation port. It never happened - I think it was determined that it wouldn't sell enough to cover the development costs. I don't remember much else except we played the PC game ("research") and it was super difficult - you needed a lot of skill not to crash at every curve. I also seem to recall that the polygon models for the cars were based on manually edited text files. Good times.
Re:GTA III and Vice City fully reverse engineered: re3 by Const-me on Sunday February 14, 2021 @11:02AM (#26133421)
> We could really use someone with how to optimize OpenGL (especially on the pi).

Not sure that gonna help much.

I think what you should do instead, port your OpenGL code to OpenGL ES. Specifically, Raspberry Pi 1-3 support GLES 2.0, Pi 4 supports GLES 3.1. As a nice side effect, renderer will become portable to smartphones.

This may or may not be a lot of work, depending how exactly you use OpenGL.

Pi 4 introduced Vulkan as well, but unlike GLES I have not tested it and not sure how good is the support.

Re:GTA III and Vice City fully reverse engineered: re3 by aap_ on Sunday February 14, 2021 @11:05AM (#26133452)
I have in fact tested everything with GLES 2.0, 3.1, and desktop GL 2.1 and 3.3 and they should all work. If it's as efficient as it could be is another question. I find OpenGL rather messy as an API compared to d3d9.
Re:GTA III and Vice City fully reverse engineered: re3 by Const-me on Sunday February 14, 2021 @03:40PM (#26136065)
I don’t have GTA3 but I do have VC on Steam. Compiled 32-bit Windows build of VC with GL2. Here’s a renderdoc capture from my PC: http://const.me/tmp/vc-win10-gl.zip

1. On the main rendering pass, your code renders stuff mostly back to front. Consider sorting opaque objects by Z and render then front to back. If you do that, early Z rejection gonna save tons of pixel shaders and fill rate. The sorting doesn’t need to be perfect, an approximate will do as well, but with these ~5k draw calls I’m pretty sure even C qsort gonna be adequate on Pi4.

Translucent objects need to be rendered back to front like you’re currently doing.

2. In your pixel shader you often have this code:

    if( a < u_alphaRef.x || a >= u_alphaRef.y ) discard;
Where u_alphaRef is [ -1000; +1000 ] i.e. very large interval.

Don’t do that. Write another pixel shader for cases when you don’t need alpha tests. GPUs disable some optimizations (early Z rejection is one of them) when you calling discard in GLSL/HLSL.

3. Up to EID 5328, the game rendered the next frame. Starting from EID 5377, the game was finishing rendering of the previous one. That’s a good idea by itself, the problem with that, the temporary texture has size 4096*4096 pixels. Only top-center 3840*2160 portion was actually used. My desktop PC is fast enough to deliver 60 FPS, but on RPi you should not use textures much larger than necessary. All modern GPUs support non-power-of-2 textures, Pi4 included.

4. You are using 2x MSAA for the main pass. At least for my test cases on Pi4 (see e.g. this project https://github.com/Const-me/Vrmac ), even 2x MSAA ruined performance, if you use the same setting on Pi4, I think that contributed the most to the performance issue.

Try to disable MSAA and see what gonna happen. If you’ll find out it helps with the performance but the quality is too bad now, try to implement a cheaper substitute somehow, search the web for FXAA and SMAA keywords.

Re:GTA III and Vice City fully reverse engineered: re3 by aap_ on Sunday February 14, 2021 @04:55PM (#26136630)
Thank you for your input!

For shaders I'm never sure if I should do something dynamically or switch to a different shader, but it makes sense of course to kill the alpha test code if it's not used.

As for MSAA, our support for that is pretty rather young and it gives artifacts. I'm actually surprised it's on at all by default...

Re:GTA III and Vice City fully reverse engineered: re3 by Const-me on Sunday February 14, 2021 @07:39PM (#26137721)
> it makes sense of course to kill the alpha test code if it's not used.

It’s used, but only for some meshes like vegetation, u_alphaRef was like [ 0.50196, 1000.00 ] for them.

> I'm actually surprised it's on at all by default

I don’t think I have adjusted anything, just built and run your code on Win10 with GL. This doesn’t mean it gonna use the same defaults on Pi, but it might.

Re:GTA III and Vice City fully reverse engineered: re3 by phire on Sunday February 14, 2021 @09:01AM (#26132434)
It's a bit of an underpowered GPU, but I would have thought it would be powerful enough to run an early PS2 era game like this.

It might just be really unhappy with the number of draw calls that you are doing. Looks like you are currently doing a drawcall per mesh; You might need to pack multiple meshes into a single drawcall.

Do you have any idea how many drawcalls per frame you are currently doing?

Re:GTA III and Vice City fully reverse engineered: re3 by aap_ on Sunday February 14, 2021 @09:09AM (#26132505)
Using renderdoc with normal opengl i count 660 draw calls on a hopefully representative frame. Multiple meshes per drawcall are hard due to the architecture of renderware. I have considered sharing vertex and index buffers though. Do you think that will make a big difference?
Re:GTA III and Vice City fully reverse engineered: re3 by phire on Sunday February 14, 2021 @09:29AM (#26132653)
I suspect that sharing vertex/index buffers won't really help (alone).

Last time I profiled something graphically intensive on the pi 4, I found that touching any of the draw state or uniforms between draw calls would trigger an expensive re-validation on the whole pipeline. 50% of my cpu time was spent in the driver.

Of course, the app I was profiling was pushing quite a lot of uniforms per draw call. Your workload might be different, so profile before doing anything drastic.

I recommend building a version of mesa with symbols so you can see exactly where CPU time is spent.

BTW, try prepending `mesa_glthread=true` to your command to see if that gives you a free speed boost.

Re:GTA III and Vice City fully reverse engineered: re3 by aap_ on Sunday February 14, 2021 @09:34AM (#26132703)
hm, state changes between draw calls are hard to get rid of...after all that's why they're separate calls in the first place. profiling the driver is probably indeed something i should look into.
Re:GTA III and Vice City fully reverse engineered: re3 by garaetjjte on Sunday February 14, 2021 @09:49AM (#26132826)
You should be able to batch CPU-side drawcalls by packing multiple meshes into bigger vertex buffer, then generating draw parameters for every object into SSBO/UBO, and then utilizing ARB_multi_draw_indirect to draw everything at once. To change textures inside these batched drawcalls you might need ARB_bindless_texture. This talk is helpful: https://www.gdcvault.com/play/1020791/Approaching-Zero-Drive...

I don't know if these extensions are available on Pi driver, though.

Re:GTA III and Vice City fully reverse engineered: re3 by reasonabl_human on Sunday February 14, 2021 @12:10PM (#26134021)
I am a developer but you guys are speaking a foreign language from my perspective in this thread. Where can I learn about graphical programming for the Pi, or are you guys well versed in game programming in general and applying it here? Sounds like an interesting skill set and challenge
Re:GTA III and Vice City fully reverse engineered: re3 by wheybags on Sunday February 14, 2021 @02:56PM (#26135656)
https://antongerdelan.net/opengl/ is a decent resource for learning opengl (the author was a TA in my graphics course in college :D). And I do think it's worth Lear ing opengl over vulkan today. Vulkan dials the api complexity up to 11. If you're happy with windows only, then directx 11 would be a good choice too, but I don't have a good reference off hand.

The problem with graphics programming is that really you need to learn two things simultaneously: how 3d graphics are rendered, and how to use a GPU api. They can both be a significant thing to learn alone, and the complexity of cramming the two into your brain together makes things hard.

Re:GTA III and Vice City fully reverse engineered: re3 by FillardMillmore on Sunday February 14, 2021 @01:13PM (#26134612)
Seconded, I too would like resources to learn more about this topic. Spend most of my time with business logic.
Re:GTA III and Vice City fully reverse engineered: re3 by wxkn on Sunday February 14, 2021 @12:41PM (#26134315)
Try out an introductory OpenGL tutorial. There are many out ther. Just drawing a square will be a good lesson
Re:GTA III and Vice City fully reverse engineered: re3 by rwmj on Sunday February 14, 2021 @08:54AM (#26132384)
> physics have always been wonky beyond 30fps

I suppose this is because the original developers knew exactly what hardware this would run on and the achievable framerate on the PS2 and so took lots of short-cuts in the code?

Edit: Incredible project by the way.

Re:GTA III and Vice City fully reverse engineered: re3 by aap_ on Sunday February 14, 2021 @08:55AM (#26132397)
They actually did try to make it work at all frame rates but I don't think they were actually able to test it very well because the hardware, as you said, didn't do it.
Re:GTA III and Vice City fully reverse engineered: re3 by peruvian on Sunday February 14, 2021 @10:35AM (#26133198)
A lot of games were hardcoded for a specific FPS. Even modern-ish ones!
Re:GTA III and Vice City fully reverse engineered: re3 by ricardobayes on Sunday February 14, 2021 @11:19AM (#26133579)
Our local computer graphics prof hated this and set the loop rate random for the opengl idle function on our submission server. At one point it hang for 5 minutes between frames.
Re:GTA III and Vice City fully reverse engineered: re3 by lostgame on Sunday February 14, 2021 @01:07PM (#26134550)
Brilliant move, must have been one hell of a smart guy.

I hate to say it, but fixed frame rates these days seem very 'lazy'? Obviously depending on use case and hardware.

Re:GTA III and Vice City fully reverse engineered: re3 by dagmx on Sunday February 14, 2021 @02:59PM (#26135672)
I wouldn't call it lazy. All development is compromise. Especially game development.

If you know the game is only going to run on certain hardware, and you make the most money during the launch period of the game, it doesn't make sense to spend time, effort and money on something niche for the future.

Re:GTA III and Vice City fully reverse engineered: re3 by Macha on Tuesday February 16, 2021 @08:33PM (#26161501)
Delta times make difficulty for games that rely on lockstep for multiplayer. It's why factorio, for example, is capped at 60fps and will slow down gameplay at slower framerates.
Re:GTA III and Vice City fully reverse engineered: re3 by zamalek on Sunday February 14, 2021 @10:30AM (#26133154)
Time corrected vertlet integration[1] is really approachable and I'd strongly recommend trying it out.

[1]: https://www.gamedev.net/articles/programming/math-and-physic... Edit: http://archive.gamedev.net/archive/reference/articles/articl...

Re:GTA III and Vice City fully reverse engineered: re3 by zennit on Sunday February 14, 2021 @10:39AM (#26133232)
Could box86 help here? https://github.com/ptitSeb/box86
Re:GTA III and Vice City fully reverse engineered: re3 by terramex on Sunday February 14, 2021 @10:50AM (#26133324)
No, according to README.md re3 works natively on arm64.