One Godot frame on a keychain: from two hours to thirty seconds
· 15 min read
By Phil · building Legitsauce
The Saucelet already runs small 3D scenes at 60 FPS (Saucelet progress). This week I went to the other end of the scale. I wanted to know how close these two chips can get to a real Godot frame, with fog, glow and bounced light, and then how fast I can make that frame without losing the look.
The test frame is frame 1440 of Godot's third-person shooter demo: the robot standing in front of a red and white divider in a corridor full of light. On the P4 it now takes 30 seconds, down from almost two hours. On the XIAO it takes 255 seconds. Neither is close to real time yet, but the pictures are close to Godot's.

How I measured
The two chips are the ones from the chip comparison:
| ESP32-P4 (M5Stack Stamp-P4) | XIAO RP2350 | |
|---|---|---|
| Cores | 2 RISC-V at 360 MHz | 2 Arm Cortex-M33 at 150 MHz |
| RAM | 768 KB on chip, 32 MB PSRAM | 520 KB |
| Extras used here | PIE vector unit, image engines, JPEG decoder | DSP instructions, interpolators, PIO |
The rules for every number in this post:
- One fixed frame. The scene, the camera and the robot's pose never change, so every run renders the same picture.
- Rendered on the chip. Times are measured on the board itself, from the first stage to the finished 240 by 240 image. The P4 has no screen attached for this test; its times include sending the finished image back to my PC over USB, about a quarter of a second.
- Cold frames. Every time is one full frame from a fresh start, with no reuse of a previous frame's work.
- Short runs. A P4 measurement may take at most 2 minutes. The XIAO's longest frames were measured in pieces of up to 5 minutes each, then added up.
- Scored against Godot. Each frame is compared with Godot's own render using the average brightness error over 8 by 8 pixel blocks. It runs from 0 to 255, and lower is better.
- A PC model of the same code. The chip's frames are checked against a PC build of the same renderer, so a speed-up can't hide a bug.
- Checked on more than one frame. Most changes were also scored on 14 other frames of the demo, so nothing is tuned for frame 1440 alone.
- My eyes have the last word. The first round allowed a speed-up to add at most 1.0 to the error. Later I raised the limit to 3.0 over the reference, because at 240 by 240 I couldn't see differences the number was flagging. Every change since then has come with a side-by-side picture, and I decide.
The P4: from 7,008 to 30 seconds
| Step | Seconds a frame | FPS | Error vs Godot |
|---|---|---|---|
| Plain C reference | 7,008 | 0.00014 | 1.08 |
| 720 internal view, work done once per view, scratch data in PSRAM | 156.0 | 0.0064 | 1.35 |
| Shading once per surface, a cheaper background view, sparser bounced light | 56.4 | 0.018 | 1.49 |
| 480 view with 2 x 2 MSAA, the image engines, float and fixed point mixed | 46.45 | 0.022 | 2.55 |
| A SIMD layer on the PIE vector unit | 44.03 | 0.023 | 2.55 |
| The second bounce of light by volume sweeps | 37.97 | 0.026 | 2.51 |
| Baked lighting, a cleaner bounced-light grid, sharper edges near the camera | 30.26 | 0.033 | 2.22 |
That's about 230 times faster than where it started. The error went up from step 4 because I raised the limit there and started judging by eye. The last step brought it back down to the best it has been since.
The XIAO: from 2,487 to 255 seconds
The plain C reference never finished on the XIAO. I stopped it after 30 minutes, when it had done 90 of 544 tiles of one early pass, which projects to about nine hours. So the XIAO's table starts from the P4's 156-second version, run as it was:
| Step | Seconds a frame | FPS | Error vs Godot |
|---|---|---|---|
| The P4's 156-second version, unchanged | 2,487 | 0.0004 | 1.35 |
| Data streams and the flash cache tuned for the XIAO | 1,955 | 0.0005 | 1.34 |
| The P4's next round of changes | 478.8 | 0.0021 | 1.32 |
| Shading once per surface, a cheaper background view | 368.0 | 0.0027 | 1.43 |
| Sparser bounced light, plus the XIAO's own hardware | 288.5 | 0.0035 | 1.49 |
| 480 view with 2 x 2 MSAA, cheaper fog, float and fixed point mixed | 255.4 | 0.0039 | 2.17 |
The XIAO hasn't run the P4's last three steps on the board yet: the SIMD layer, the volume sweeps, and baked lighting with the new edges.
Doing less of the same work
The first big drop on both chips changed no maths, only how much of it gets done. The reference renders a 1080 by 1080 view and supersamples every pixel 4.5 by 4.5 times before shrinking it to 240. A 720 view scored almost as well. Work that was repeated for every tile, like the fog volume and the lists of lights, is now done once for the whole view. Each pixel is lit once per surface it covers, the way MSAA does it, instead of once per sample.
On the XIAO the bigger wins came from using the chip properly. Code that runs on both cores moved out of flash into on-chip RAM, which took one pass from 152 to 78 seconds. The PIO blocks and DMA now stream data from the external flash chip at 4.2 MB/s while the cores keep working. The interpolators step texture coordinates in hardware.
Fixed point everywhere was slower
On small chips, fixed-point maths (integers standing in for fractions) is usually the first thing to try. I moved the whole renderer to fixed point, with the floating-point unit switched off so any stray float instruction would crash.
It ran twice as slow on both chips: 113.4 seconds on the P4 against 56.4 in float, and 555.7 on the XIAO against 288.5. Both chips have a floating-point unit that does a multiply and an add in one instruction. Fixed point at the precision this lighting needs takes 64-bit multiplies, 64-bit shifts and some 64-bit divides. On the XIAO, a 64-bit square root alone was 21% of the bounced-light pass.
The images came out fine, so the work wasn't wasted. It turned into the float and fixed-point mix below. The next test is fixed point that never needs 64-bit maths, at 32 and 16 bits.
Fewer pixels, and every engine at once
The output is 240 by 240, so the question was how big the internal view needs to be. After trying 720, 640 and 480, I settled on a 480 by 480 view with 2 x 2 MSAA. Edges are found at 480, each output pixel is lit once, and the result is halved to 240. On the P4 the PPA, its pixel processing engine, does the halving.
The P4 also has image hardware next to its cores, so I moved every part of the frame that could go to an engine onto one:
- The PPA blends the glow and the post-processing layers, composites the ambient occlusion and scales images.
- The 2D-DMA engine copies and prefetches data. Its colour converter doubles as a small matrix engine for the tone mapping.
- The BitScrambler applies lookup tables for the tone curve and sRGB.
- The hardware JPEG decoder unpacks the colour textures at load time.
The engines take load off the cores. The bigger gain came from mixing number formats: float where the floating-point unit's multiply-add wins (lighting, cone tracing, the screen-space passes) and fixed point where integers win (transforms, packing, the glow and fog filters). Together with the 480 view and the engines, that took the P4 from 56.4 to 46.45 seconds. The XIAO's version, with cheaper shadow filtering and a coarser grid for rough reflections, reached 255.4 seconds. That's still the XIAO's best.
A SIMD layer, and why it helped less than I hoped
Both chips have integer SIMD. The P4's PIE unit works on 128-bit registers, eight 16-bit numbers at a time. The XIAO's M33 cores have DSP instructions that handle two 16-bit numbers in one 32-bit register. I built one layer with a PIE version, a DSP version and a plain C version of every kernel, all three giving identical results, and ran every fixed-point inner loop through it.
The P4 went from 46.45 to 44.03 seconds, nearly all of it from the glow filter (2.49 to 0.49 seconds). On the XIAO the frames came out identical and I haven't timed it on the board. The gain was small because after the mix, the expensive stages are the float ones, and both SIMD units only do integers.
Two algorithm changes
The second bounce of light, by volume sweeps. Light that bounces twice was found by scattered lookups through the scene's light volume. I rewrote it as six sweeps through the volume along fixed directions, on data laid out in rows of 16-bit numbers that PIE can process. One level of it ran ten times faster than plain float code. The second bounce went from 10.4 to 3.5 seconds, and the frame to 37.97.
Baked lighting. Most of the lights in this scene never move. Godot can bake their light into lightmaps with its own LightmapGI at export time, on the PC's graphics card, so the chip only has to look the result up. The 13 projector lights and anything that moves stay live. This went against one of my original rules for the test, "live lighting only", but it's the right trade for a handheld. The Godot exporter will warn when a game leans on lots of live lights.
Pushing the image quality
With baked lighting the frame was much faster, but two things still looked wrong. The rings on the robot's head and the top edge of the divider were jagged, and the divider had a faint dark band down one side.
The band was a bug. To save time, the bounced light was computed for one pixel in 16 and stretched back up by the PPA. The PPA's stretch is a plain blend that knows nothing about depth, so the dark wall behind the divider bled up to four pixels into the red panel. The XIAO already used a grid that blends by depth and surface direction on the cores, so it can't mix the divider with the wall behind it. On the P4 that version is also faster than the PPA one.
The jagged edges come from lighting each output pixel once: a thin ring is either all in a pixel or not in it at all. I tried a thin-line smoothing pass after rendering, which helped and stayed on. I also tried six versions of re-rendering only the pixels that matter. The one that stuck works like this: near the camera and on objects marked important in Godot (the player), inside the centre of the screen, each 8 by 8 block is rasterized again at 720 to find exactly where the edges fall. Each pixel then gets one extra lighting calculation per surface it actually contains, weighted by how much of the pixel that surface covers. My first version rasterized each block on its own and cost almost as much as rendering everything at 720. Rasterizing once per tile cut that pass from 5.6 seconds to 0.44 on the PC.

Together that's today's frame: 30.26 seconds, with an error of 2.22. It's the best-looking frame so far.
Triangles per second
Frame 1440 isn't limited by triangles. The level has about 614,000 unique triangles, but after culling the frame reads about 25,000 of them. Working out which triangle covers each pixel takes about 1.5 seconds of the P4's 30. Almost all the time goes into lighting.
The real-time demos give a better idea of what the chips can draw. These ran the same code on both chips. The triangles per second are worked out from the measured triangles per frame and the measured time per frame:
| Demo | Triangles a frame | XIAO RP2350 | ESP32-P4 |
|---|---|---|---|
| Ka'zhurn | 1,000 | 10.2 ms: about 98,000 a second | 5.0 ms: about 200,000 a second |
| 3D scene | about 1,500 | 17-19 ms: about 85,000 a second | 8.4 ms: about 180,000 a second |
| Triangle storm, at 60 FPS | XIAO about 1,500, P4 about 3,400 | about 90,000 a second | about 204,000 a second |
- Ka'zhurn is the main character of Ka'zhurn: The Unbinding, drawn on his own as a 1,000-triangle model.
- The 3D scene is Ka'zhurn among brick pillars and crates on a tiled floor. The XIAO runs it in a 45 Hz screen mode.
- The triangle storm is a stress test: it keeps adding shapes until a frame takes 14 ms of computing, then counts the triangles drawn.
On the same code, the P4 draws a little over twice as many triangles as the XIAO.
The P4 also ran two bigger scenes:
- Ka'zhurn at 37,500 triangles: 57 milliseconds a frame, about 656,000 triangles a second sent to the rasterizer.
- Warlord-max, a stripped-down tech demo version of my game Yours Truly, the Warlord: two armies on a battlefield, scaled up until the frame budget runs out. Its busiest frame drew 9,411 triangles in 15.8 milliseconds, about 600,000 a second. The XIAO's Warlord-max holds 60 FPS with smaller armies, drawing about 640 triangles a frame on average.
The hardware in use
ESP32-P4
| Part | Its job in this renderer |
|---|---|
| Floating-point unit with fused multiply-add | the lighting and cone tracing; fixed point alone was twice as slow |
| PIE vector unit | the second-bounce sweeps, the glow filter, packing and texture expansion |
| PPA | halving 480 to 240, scaling the glow, blending the post-processing layers, compositing ambient occlusion, filtering shadows |
| 2D-DMA | copies and prefetching; its colour converter as a matrix engine for tone mapping |
| BitScrambler | lookup tables for the tone curve and sRGB |
| JPEG decoder | colour textures at load time |
| PSRAM | bulk scratch data, read in large batches |
I tried and dropped running the whole post-processing chain on the engines (too much error), accumulating the fog on the PPA (slower), the PPA's bounced-light grid (the dark band), heavier JPEG compression (too much error) and PIE for the light-volume filter (its cost is scattered memory reads, which PIE can't speed up).
XIAO RP2350
| Part | Its job in this renderer |
|---|---|
| Floating-point unit | the float half of the mix: 283 seconds against 509 for fixed point at the same settings |
| DSP instructions | glow (6.8 to 2.3 seconds), post-processing (12.2 to 8.6), fog (45.5 to 38.4), texture and light filters |
| Interpolators | stepping texture coordinates |
| PIO and DMA | streaming the external flash at 4.2 MB/s with the cores free |
| On-chip RAM | hot code and data kept out of flash |
On the XIAO I dropped LZ4-compressed data (more flash traffic, so slower), data formats the PIO could unpack (too big for the flash), DMA gathers (no gain) and the -O3 compiler setting everywhere (the program no longer fit).
What's left to test
- Fixed point without 64-bit maths, at 32 and 16 bits, stage by stage, with the error of each stage checked.
- Keeping every part busy. Both cores and every engine should be working all the time. Today they often wait on each other: in the P4's main pass one core spends over 2 seconds waiting.
- More of the frame on PIE, starting with the bounced-light grid and the tone mapping in 16-bit numbers.
- A cache-friendlier order. Walking the screen along a Hilbert curve keeps neighbouring work in cache. The XIAO should gain the most.
- Spot-light shadows in parallel. Today they're rendered one after another.
- A bigger cache on the P4. Its L2 cache can be doubled to 256 KB, but that costs on-chip RAM the frame needs.
- A live surface cache, where lighting is stored on surfaces and updated a little each frame instead of computed from scratch.
- The P4's H.264 encoder for motion search, and its low-power core.
- The XIAO catching up: the SIMD layer timed on the board, the volume sweeps with DSP, and baked lighting with the new edge pass.
How far 1 FPS is
The goal for this scene is one frame a second. The P4 needs about 30 times more speed, the XIAO about 255 times. Lighting each pixel is the biggest single cost on both chips; on the P4 it's about 10 seconds of the 30. No stage is under one second yet.
Tuning alone won't get there. The changes that worked best this week moved work out of the frame entirely: baked lighting moved it to export time, and the volume sweeps changed the algorithm. Reusing work between frames and keeping every engine busy come next. Each change still gets checked against Godot's picture, and by eye on the 240 by 240 screen, so speed never quietly costs quality.