When you choose a model to race against, that model is your rival. The game downloads the rival’s latest program and runs it in your browser. Your car and the rival’s car then race on your machine.
We want two things from that race. First, the screen can redraw at any rate, and that rate must not change how the race goes. Second, when the rival’s program does not answer, the game must still be able to stop the race. So the game splits the work into three parts. One runs the race, one reads your controls and prepares what you see, and one draws.
Each part is a thread in the browser. The physics worker runs the rival’s program and moves both cars. The main thread sends it your controls and reads back snapshots, copies of the race state that are sent out for drawing. It turns the latest snapshot into a scene, the data for one frame, and hands it to the render worker, which draws it.
The race runs apart from the drawing
How often the screen redraws must not decide how fast the race moves. So the physics worker keeps the real race state and a clock with fixed steps. The main thread keeps the controls, the camera and the interface. On every frame it reads the worker’s state and never steps the physics. Drawing one more frame never needs one more driving decision.
Each physics step is exactly 1/60 s. In a step, the worker reads the controls that have arrived, calls the rival’s program and waits for it to return, then moves both cars. That call returns the action that was saved earlier. The action the rival works out now is saved for the next tick.
That call blocks the physics worker until it returns. So a timer on the main thread, which the code calls a watchdog, checks that the worker still answers. It waits 1,000 ms, asks, and allows 5,000 ms for a reply. With no reply, it terminates the worker from outside. When the simulation fails, the game stops drawing, pauses the engine audio and reports the error. It does not move the physics onto the main thread.
The game checks the rival’s program before it runs
A choice in the menu names a rival. Before the race, the physics worker downloads that model’s program and checks it in two separate steps. First it checks that the file is the expected one. Then it checks that the file’s instructions are allowed to run.
The program that runs is a guarded copy of the one the agent submitted. The guard checks a program’s instructions and meters the work the program does. The game first gets a small record for the selected model, which the code calls a guard pin. It gives the size and SHA-256 of the guarded copy. The worker rejects a download that is too long, one that is incomplete, and one with the wrong hash. A matching hash proves the file is the expected one. It does not prove the file is allowed to run.
So the worker then checks the instructions on its own, before it creates a WebAssembly instance. If setup fails, preparation stops. The worker does not load the original program without the guard.
One snapshot is enough to draw a frame
A frame can fall between two physics steps. To draw a car then, the main thread blends between where the car was one step ago and where it is now. Each snapshot holds both. One snapshot is therefore enough to draw, and the main thread does not need to receive every one.
The code calls each of the two a pose: where a car is and which way it points, its position and heading. The main thread works out a blend fraction from the time since the snapshot’s step. It keeps the fraction between zero and one, so it never guesses beyond the poses it received. Position moves in a straight line between the two poses. Heading turns the shorter way around. Blending this way puts the picture one step behind the race.
We prefer recent state to complete delivery. When shared memory is available, snapshots go through a mailbox, a piece of shared memory with three slots that holds the latest value. Otherwise, three buffers go back and forth between the physics worker and the main thread. Both paths keep recent state. Neither promises to deliver every step.
One worker draws every frame with WebGPU
The render worker draws each scene with WebGPU. The renderer is built on vgpu. A scene is what the worker needs to draw one frame: the blended car poses, a camera and the size in pixels of the picture. Every frame draws the scene once and ends by writing the result to the canvas.
A pass is one round of drawing into a texture or the canvas. Quality comes in four presets, from Low to Ultra. A Low frame has two passes: the scene, and the composite that writes to the canvas. With a moving chase camera, a High frame has 13. It adds a shadow map for the sun, a quarter-size velocity field for motion blur, and nine passes of bloom, the glow around bright areas.
The scene pass draws all the geometry once into HDR (high dynamic range) color. It uses depth, each pixel’s distance from the camera. That depth does not survive the pass. Every effect after it therefore reads color or camera data, never depth.
A slow GPU must not leave the worker drawing out-of-date scenes. So the main thread keeps one scene in progress at the render worker and one waiting. A newer scene replaces the waiting one. One render worker serves the menu showroom, the race and the garage. It creates the GPU device the first time it is needed and keeps it when you change pages.