nannou-org / nannou

A Creative Coding Framework for Rust.
https://nannou.cc/
6.04k stars 305 forks source link

Use Bevy meshes as Nannou's rendering backend #966

Closed tychedelia closed 7 months ago

tychedelia commented 8 months ago

So far, we've taken an incremental approach to moving our existing rendering algorithm into Bevy:

While the approach taken in #964 works, we're interested in ditching our rendering code entirely, and writing our vertex data directly into Bevy meshes. What this would look like is that each PrimitiveRender would result in a new logical mesh with a matching Bevy StandardMaterial that could be used for texturing and (in future api extensions) other material-y things like emissives, pbr, etc.

This should be understood as a "logical" mesh because our immediate mode api presents some difficulties here: Bevy's assets are asynchronous, with the assumption that you'll pay the cost of uploading a persistent mesh (a sophisticated 3d model, etc) up front and work with that.

As such, we'll need to experiment with caching mesh assets to dynamically provision for Nannou's render primitives. There's a few approaches we might take here:

  1. Allocate new meshes as needed, rendering new primitives into the first available mesh. This has the benefit of being very simple and performing well when a sketch is relatively stable, but likely has poor worst-case characteristics in terms of memory use and performance.
  2. Sort primitives by the size of their vertex data to try to match them with equivalently sized existing gpu resources.
  3. Try to figure out some kind of ad hoc persistence identifier, a hash of the draw data used to create it, etc., and try to optimistically associate primitives with meshes. This is likely error prone and could run into some pathological scenarios that are hard to handle.

While starting with a growable cache should be fine, we'll also need to figure out a long term strategy for cache eviction, i.e. if a mesh hasn't been used in N frames.

TODO: