Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Apps/BusyBoxVi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ vi [-c CMD] [-R] [-H] [FILE]...
- `.` repeat, undo, `:set` options (autoindent, ignorecase, showmatch, tabstop)

Not supported: regular expressions, `:!` shell commands, signals (suspend/resize).
The terminal has no Home/End/PgUp/PgDn keys: use `0`, `$`, `Ctrl+B` and `Ctrl+F`.
The terminal has no PgUp/PgDn keys: use `Ctrl+B` and `Ctrl+F`.

## Source

Expand Down
4 changes: 2 additions & 2 deletions Apps/BusyBoxVi/manifest.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ manifest.version=0.3
target.sdk=0.8.0-dev
target.platforms=all
id=tactility.busyboxvi
version.name=0.9.1
version.code=10
version.name=0.9.2
version.code=11
name=BusyBox vi
description.short=BusyBox vi text editor
description=The BusyBox vi text editor, for use from the Terminal app.\n\nRun: vi [-c CMD] [-R] [-H] [FILE]...\n\nSupports colon commands (:w, :q, :wq, :e, :r, :s/old/new/), yank and put, marks, plain-text search, repeat, undo and :set options. Regular expressions, shell commands and suspend are not supported. The terminal has no Home, End, PgUp or PgDn keys: use 0, $, Ctrl+B and Ctrl+F.
Expand Down
112 changes: 112 additions & 0 deletions Apps/Voidskim/PERFORMANCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Performance

What was tried to make Voidskim faster on ESP32 devices, what helped and what didn't. Measured on an ESP32-S3 with a 320x240 SPI display (LilyGO T-Deck) and an ESP32-P4 with a 1280x720 MIPI-DSI display (M5Stack Tab5, frame 640x360 scaled up by the PPA). Medium 3D quality (75%) unless stated; the default is high.

## Measuring

`main/Source/platform/Profiler.h` logs the frame time per phase every 3 seconds (`VOIDSKIM_PROFILE`, on by default):

```
VoidskimProfile: 25.6 fps, ms/frame: update 1.49, 3D 20.06, scale 4.58, overlay 3.20, present 5.29, total 39.08
```

- `update`: game logic and simulation.
- `3D`: Jet's transform, sort and rasterisation.
- `scale`: scaling the 3D up into the frame.
- `overlay`: HUD, menus and touch controls.
- `present`: sending the frame to the display. With the display task (see below), it's only the wait for the display task to be ready for the next frame.

A second line shows what the 3D culled, averaged per frame:

```
VoidskimProfile: 3D per frame: objects <drawn> of <all> after frustum cull, triangles <queued> queued, <rasterised> rasterised, of <all>
```

- `objects`: meshes that survived Jet's frustum cull (bounding sphere, then bounding box), out of all meshes. The track is one mesh per 8 samples (32 m), plus the start gantry and each craft.
- `queued`: triangles of those meshes left after per-triangle culling (back faces, zero area, near/far plane).
- `rasterised`: triangles that drew pixels.

A third line shows the display task (see below), per frame:

```
VoidskimProfile: Display per frame: convert 12.50 ms, waiting for a band buffer 15.27 ms, send 27.28 ms
```

- `convert`: the frame converted to the panel's pixel format, a band of rows at a time. On chips with a PPA (P4, S31): the PPA scaling and rotating the frame into the panel's frame buffer.
- `waiting`: the conversion waiting for the sender to free a band buffer (software path only).
- `send`: `display_draw_bitmap()` sending the bands over the bus. With the panel's own frame buffers (Tab5) it only switches buffers, ~0.1 ms.

## Where it stands

About 36 fps at medium quality, 35 at high: update ~1.3 ms, 3D ~9.5 ms (high ~12), scale ~4.6 ms, overlay ~3.6 ms, so the game needs ~20 ms per frame. Sending a frame takes ~27 ms (150 KB over SPI), which caps the frame rate at ~36 fps, so the game waits for the display. A faster frame needs a faster SPI clock (firmware) or fewer bytes sent.

On the ESP32-P4 (Tab5): ~36 fps in a race, ~23 in the menu (from ~26 and ~18). Update ~0.7 ms, 3D ~8.5 ms, scale ~14 ms, overlay ~2 ms (menu ~17 ms), and the PPA needs ~21 ms per frame on the display task. The game is the limit: mostly the software scale-up into the 640x360 frame, and the menu's semi-transparent panels.

## Starting point

About 18 fps: update ~1.5 ms, 3D ~17 ms, scale ~4.7 ms, overlay ~3–6 ms, present ~24 ms. Sending the frame to the display took almost half of every frame.

## What helped

### Sending frames on the other core: 18 → 26 fps

`present` mostly waits for the SPI display bus. The game runs on core 1. A display task on core 0 now converts and sends a frame while the game draws the next one into a second frame buffer (`DisplayOutput`, `Game::setFramebuffer()`). A frame now takes as long as the slower of the two, not both together. `present` dropped from ~24 ms to 0–6 ms of waiting.

Costs a second frame buffer in PSRAM (150 KB at 320x240). Used for colour displays on dual core chips without a PPA. Monochrome displays, chips with a PPA (P4, S31) and single core chips still send frames on the game's core.

### Converting a band while the previous one is sent: 26 → 36 fps

`display_draw_bitmap()` blocks until a band has been sent, so the display task converted (~10 ms per frame) and sent (~27 ms) one after the other: ~38 ms per frame, whatever the game did. A sender task now does the sending, while the display task converts the next band into a second band buffer (16 KB of internal DMA RAM). A frame now takes as long as sending it.

### Half width rendering with fast span fills: 3D 20 → 9.5 ms

Jet's fast span fills (`JET_FAST_SIMPLE_SPANS`) fill each row of a flat triangle with 32-bit stores. Without them, every pixel goes through the general rasteriser loop (edge tests and dither per pixel). Jet only enables them with `HALF_WIDTH_BUFFERS` (one stored pixel per two columns) and without `DEPTH_ALPHA_BLEND` (distance fog). With `FAST_Z` the fog is one alpha per triangle, which the span path dithers already, so `JetConfig.hpp` turns them on with the fog kept. The 3D has half the horizontal resolution; the HUD and menus keep the full resolution. On its own this didn't raise the frame rate, as the display was the limit then.

The 3D buffer rows must start 16-byte aligned: Jet's ESP32-S3 clear uses 128-bit stores that ignore the low address bits. An unaligned buffer had them overwrite the heap header in front of it, which crashed the app on exit (`WorldRenderer::applyResolution()`).

### The display task on chips with a PPA (P4, S31)

The PPA scale and `display_draw_bitmap()` (~17 ms) run on the display task too. Two things went wrong on the Tab5 first, both showing as flashes:

- **Cache write-back on the wrong core.** Each P4 core has its own L1 data cache. The frame was written back from the CPU caches (`esp_cache_msync()`) for the PPA on the display task's core, which misses lines still dirty in the drawing core's cache: the PPA read stale parts of the frame. Worst in the menu, whose semi-transparent panels write a lot. `present()` now does the write-back on the core that drew the frame.
- **Suspected: writing into the buffer on screen.** The Tab5 allows tearing, so `display_draw_bitmap()` with a panel frame buffer only queues the switch, and the other buffer is on screen until the next refresh. A test that waited 17 ms after each switch before the next PPA write made no visible difference once the cache write-back was fixed, so it was removed.

### Welding vertices

Faces that meet now share their corners (`MeshBuilder` in `WorldRenderer.cpp`), so Jet transforms each corner once per frame instead of once per face that uses it. On Harbor Loop: 9116 → 2813 vertices. Its effect on the frame rate wasn't measured on its own (it went in with the first profiling build), but it costs nothing and saves memory.

## What didn't help

### Voxels instead of polygons

Considered, not built. The world is a thin ribbon of track over an empty void. A voxel ray caster pays for every pixel, and void pixels are its worst case (a ray marches to its full distance). It also reads the voxel data at random places in PSRAM, which is slow. Cheap heightmap voxels (one height per column) can't draw tunnels, bridges, overpasses or the underside of the track. Polygons draw a few hundred visible triangles, so they're the cheaper choice for this scene.

### Rendering into internal RAM: at most 1 fps

(Measured before the fast span fills, with the general rasteriser loop.)


The theory: Jet's painter's algorithm writes most pixels several times, and PSRAM is much slower than internal RAM, so a render target in internal RAM should be faster. There isn't enough internal RAM for a whole frame (the largest free block is ~31 KB, a frame is 86–150 KB). So the 3D was rasterised in bands of rows into a 16 KB internal buffer (Jet's `prepareFrame()` / `clearBand()` / `rasterizeBand()`), and each band was copied into the frame.

Result: `3D` didn't get faster (it got a little slower: triangles crossing band edges are set up once per band). Only the scale-up got faster, 4.7 → 3.1 ms, as it read from internal RAM. In total at most 1 fps. The rasteriser is limited by the CPU, not by memory: most likely the ESP32-S3's data cache absorbs the overdraw, since a triangle writes neighbouring pixels. Removed again: all frame buffers are in PSRAM, which keeps internal RAM free for the system.

### Better frustum culling: not needed

Jet already culls each mesh against the view (bounding sphere, then bounding box) and then each triangle (back faces, zero area, near/far plane). The profiler's second line shows it works: during a race 11–12 of 43 meshes survive, ~300 of ~4900 triangles are queued and ~250 rasterised. The bounding boxes are large (each track mesh includes its pillars and towers), but tighter ones can't save much when only ~300 triangles are left. The 3D time (~20 ms for ~250 triangles at 240x180) went into filling pixels, not into vertices or culling: see the fast span fills above.

### Scaling the 3D up with the PPA (P4, S31): no gain, and flashes

The CPU scales the 3D up into the 640x360 frame in ~14 ms. The game's task got a PPA client of its own to do that, with 3D sizes that the PPA's 1/16 scale steps scale exactly (medium became 512x288). Measured on the Tab5: the same ~36 fps in a race and ~23 in the menu. `scale` only dropped to ~10–11 ms, as the game waited while the display task used the PPA (~20 ms per frame), and the 3D got slower (~8.5 → ~12 ms), most likely from the PPA's PSRAM traffic. The flashes came back too, and the PPA's smoothed scaling lost the sharp pixel look of the nearest neighbour scale-up. Removed, together with dropping the display task's cache write-back of the panel buffer after the PPA (~1 ms, tried at the same time).

The PPA is the limit on the P4 now: it scales and rotates every frame to the panel, ~20 ms.

### Enabling Jet's ESP32 mode

Not possible. Jet's ESP32 mode places its hot functions in IRAM, and an ELF-loaded app can't do that (see `JetPlatform.h`). Jet's ESP32-S3 SIMD code paths don't depend on it and are already on.

## Ideas not tried yet

- `overlay` sometimes takes 9–13 ms (menus, pause, touch controls drawn), against ~3.6 ms during a race. Then the game needs more than the display's ~27 ms and the frame rate drops to ~28–33 fps. Worth profiling which 2D drawing it is.
- A faster SPI clock for the panel, in the firmware's device configuration: the display is the limit now.
- Less overdraw: pillars, towers and cliff faces extend far below the track (`DEBUG_OVERDRAW` in `JetConfig.hpp` shows it). Matters again once the display is faster.
- The menu's semi-transparent panels take ~17 ms on the P4: draw them opaque, or once into a cached layer.
5 changes: 3 additions & 2 deletions Apps/Voidskim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ With no opponents it's a time trial: the HUD shows the current lap time and the
| Pause | Escape, P | | pause button (top) |
| Menus | arrows, Enter, Escape/Backspace, or the race keys | roll, click | tap a row (left half goes back, right half forward) |

**Settings** (in the main menu, and in the pause menu during a race) has the audio (music and effects, effects only, or off), the **3D quality** (low renders the world at 50% of the width and height and scales it up, medium, the default, at 75%, high at the full resolution: lower is faster on big screens, while the HUD and menus stay sharp), a frame rate counter, the **touch layout** (steering buttons on the left and the others on the right, or the other way round) and **Controls**, where you change the keys: pick a control and a slot, then press the new key (each control has two). A key belongs to one control at a time, so taking a key moves it. **Defaults** restores the keys above. Settings are saved in `settings.txt` in the app's user data directory.
**Settings** (in the main menu, and in the pause menu during a race) has the audio (music and effects, effects only, or off), the **3D quality** (low renders the world at 50% of the height and 25% of the width and scales it up, medium at 75% and 37.5%, high, the default, at the full height and half the width: lower is faster on big screens, while the HUD and menus stay sharp), a frame rate counter, the **touch layout** (steering buttons on the left and the others on the right, or the other way round) and **Controls**, where you change the keys: pick a control and a slot, then press the new key (each control has two). A key belongs to one control at a time, so taking a key moves it. **Defaults** restores the keys above. Settings are saved in `settings.txt` in the app's user data directory.

Touch controls are shown on devices with a touch screen. They hide when you use a keyboard and come back when you touch the screen.

Expand Down Expand Up @@ -99,7 +99,7 @@ main/Source/

`core/` and `render/` only use standard C++, so they also build into a desktop harness (used to render the test screenshots and to tune the AI and craft balance).

The game renders at a reduced resolution with the display's aspect ratio, at most 400x240 (640x360 on chips with a PPA). It's then scaled up and rotated to the panel: in hardware on ESP32-P4/S31, in software elsewhere. Monochrome and greyscale panels get an ordered dither.
The game renders at a reduced resolution with the display's aspect ratio, at most 400x240 (640x360 on chips with a PPA). It's then scaled up and rotated to the panel: in hardware on ESP32-P4/S31, in software elsewhere. Monochrome panels get an ordered dither, with black lines along the edges between faces. Greyscale panels show the frame's brightness. On dual core chips, a display task on the other core scales (with the PPA) or converts and sends each frame while the game draws the next one into a second frame buffer. Without a PPA, it converts a band of rows while a sender task sends the previous one. See [PERFORMANCE.md](PERFORMANCE.md).

### Networking (not implemented yet)

Expand Down Expand Up @@ -136,5 +136,6 @@ The game thread only posts to the synth, and an audio task renders it into Tacti

## Notes

- [PERFORMANCE.md](PERFORMANCE.md) has frame time measurements, and what was tried to make the game faster.
- Jet is built without its ESP32 mode: an ELF-loaded app can't place code in IRAM, so `JetPlatform.h` turns those placement attributes into no-ops.
- `platform/CxxRuntime.cpp` provides `__cxa_atexit` for Jet's static scratch buffers. They're freed when the game exits.
2 changes: 1 addition & 1 deletion Apps/Voidskim/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Apps/Voidskim/icon_mono.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion Apps/Voidskim/main/Source/JetConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@
// resolution; the display driver doubles each pixel on scanout.
// Halves framebuffer RAM and fill bandwidth — critical on ESP32 SRAM.
// Set to 0 on platforms with sufficient memory (e.g. high-res / large-RAM targets).
#define HALF_WIDTH_BUFFERS 0
// Voidskim: on. The rasteriser's fast span fills (JET_FAST_SIMPLE_SPANS in Renderer.cpp) need it,
// and it halves the pixels to fill. WorldRenderer doubles the columns when scaling into the frame.
#define HALF_WIDTH_BUFFERS 1


// FIELD_BUFFERS: Split the framebuffer into two half-height packed buffers —
// one for even output rows, one for odd. The rasterizer alternates which
Expand Down Expand Up @@ -324,3 +327,14 @@
// 10,792 bytes of static RAM. Use lookupSin/lookupCos rather than indexing the
// full float_sin_table symbol, which is absent in this mode. Default 0.
// #define JET_COMPACT_TRIG_TABLE 1

// ---------------------------------------------------------------------------
// Voidskim: fast span fills with fog
// ---------------------------------------------------------------------------
// Voidskim: Jet only enables its fast span fills when DEPTH_ALPHA_BLEND is off, but with FAST_Z the
// fog is one alpha per triangle (Renderer.cpp, "Triangle-level depth fog"), which the span path
// already dithers with SCREEN_DOOR_ALPHA. So they're turned on here, keeping the fog. Without this,
// every pixel goes through the general rasteriser loop (edge tests and dither per pixel).
#if FAST_Z && HALF_WIDTH_BUFFERS && !Z_BUFFERING && !Z_BRIGHTNESS && !NOISE_ALPHA && !DEBUG_OVERDRAW && !RENDER_TILE_BUFFER && MAX_PICK_QUERIES == 0
#define JET_FAST_SIMPLE_SPANS 1
#endif
5 changes: 5 additions & 0 deletions Apps/Voidskim/main/Source/core/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ int Game::textScale() const {
return std::max(1, canvas.height / 170);
}

void Game::setFramebuffer(uint16_t* framebuffer) {
canvas.pixels = framebuffer;
world.setOutput(framebuffer);
}

bool Game::init(uint16_t* framebuffer, int width, int height, const GameConfig& gameConfig) {
canvas = { framebuffer, width, height, nullptr };
config = gameConfig;
Expand Down
Loading
Loading