Asset Source & Loading

assets/src/asset_source.rs is the asynchronous byte source shared by native and browser builds. It reads byte ranges; it does not decode formats or know scene objects, dependency graphs, or GPU residency.

Requests and Ownership

  • An AssetKey names a file relative to the source's directory.
  • AssetSource::new(directory, Limits { requests, bytes }) reads on one process-wide worker shared by every source; dropping a source never joins it.
  • request(key, generation, offset, size) asks for an exact, nonempty byte range. Identical live ranges share one read and one resident copy; each subscriber keeps its own generation and cancellation.
  • poll(request) returns Pending, Resident, Failed, or Cancelled. A resident result is a shared, immutable Lease. wait blocks on the same result for tools and tests.
  • The generation belongs to the caller: it publishes a result only while it still matches its own load generation. Request::cancel drops that subscriber immediately, including a completed result; it never cancels another subscriber or a read already running.
  • Limits cover queued, in-flight, and leased bytes and requests. A lease keeps its reservation until the last copy is dropped; queued work keeps its reservation until dequeued, so cancelling cannot bypass bounds. Exhausted limits, missing files, and invalid extents fail explicitly; a key escaping the source or a zero-length range is a programmer error.

Providers, Packs, and the Store

  • A provider (AssetProvider, registered for a directory with AssetSource::provide) serves keys before the local directory: browser hash-verified chunks, installation packs (assets/src/pack.rs, seekable Zstandard blocks), and world packs. A provider receives a cancelled callback and may stop early.
  • The asset store (assets/src/store.rs) is one root directory of converted assets addressed by lowercase /-separated paths (<kind>/<package>/<path>.<extension>). Its reads go through the byte source, so packs and providers registered under the root serve them on every platform. read_asset_range reads part of a file; overlay_asset replaces a file's bytes in memory for tests and editors.
  • Bundled assets (assets/src/bundled_assets.rs) are the files a host ships beside its executable (or inside the app bundle on iOS).

Background Work

BackgroundLoad (platform/src/background_load.rs) runs a closure on a worker and is polled from the frame thread. It never joins on cancellation or drop: a worker owns its inputs until it finishes, and a cancelled result is dropped off the frame thread. Work that needs the engine's random stream carries it with the job instead of restarting it on the worker.

Audio

No frame waits for audio bytes.

  • MusicPlayer requests the full Ogg when created and polls it in update. Until the bytes are resident, a source is active but silent (a looping source does not count as playing; a one-shot does).
  • SoundLibrary decodes effects one at a time on a BackgroundLoad, in request order. A cue whose sound is still decoding plays silence and is not replayed later. The ambient scheduler does not roll a random one-shot whose sound is loading; a looping sound takes its slot immediately and starts on the first update that finds it decoded.

Fonts

A font resolves its face file on a worker and requests the bytes through the source; the lease outlives the face. Text is not drawn while its face is pending, and measurement returns zero until it is ready. An unsupported family or style and a source error fail explicitly.

Tests

The source tests cover shared ranges, generation and cancellation, reservations held by leases, providers, and packs; music tests compare direct, packed, and provider reads.