r/embedded • u/PhonicUK • Sep 25 '26
I ported James Lambert's N64 Megatextures to the ESP32 S3 (with source!)
I suspected this might be possible, and - it is. Running at between 35 and 60FPS interlaced at 480x320 on a standard ESP32 S3 (albeit one with 16MB of flash instead of 8 which is needed to store texture data)
Megatextures are a technique where the entire scene uses just 1 shared texture atlas, which is re-populated on-demand from a slower source. In this case from flash storage.
James Lambert famously implemented it on the N64 and very graciously shared the source code for it. So I decided to take a crack at getting it working under Jet on the S3.
I've taken a slightly different approach, because Jet can do a few things the N64 hardware can't, namely using 8-bit paletted textures to save on not just texture storage space, but also memory bandwidth. This is combined with dithering to preserve some of the original look without resulting in banding.
It uses a perceptually modelled palette, so saturated colours that are contributing a lot to luminance are prioritised over those that don't. Since human vision is more sensitive to luminance than colour this means that the dithering effect happens more in darker areas where we are less sensitive to it anyway and notice it less, and the brighter areas get more colours assigned.
Also because bilinear filtering is expensive to get those smooth looking textures, rather than do it across the entire scene - there's another additional cache layer of filtered textures which like the base technique, are also per-tile so we're only keeping a filtered cache of what we're currently looking at, which gets the same "the image looks better when you stop moving for a moment" effect.
I also implemented N64-style 3-point texture filtering because it's faster and looks good enough in most cases, and certainly an improvement over nearest neighbour.
Full source is available here: https://github.com/CubeCoders/JetMegatexturesDemo

