Files
raea/engine/render/plugins/src/lib.rs
T
shaneandshinyobjectz 14cdac0687 Render memory: halcyon r109 from killed at 24-25 GB to a 7.8 GB peak (RM0-RM10) (#273)
* RM0: each App's meshes and images, CPU and GPU bytes, after every RAEA_MEM_MARKS mark (raea-sys-memtally); docs/render-memory.feature RM0-RM4

* RM1-RM10: halcyon r109's sequence from killed at 24-25 GB to a 7.8 GB peak (footprint 5.5 GB), the look within run-to-run noise

- RM1: materials' maps and Solari's traced meshes on the GPU only (RAEA_KEEP_CPU_ASSETS=1: the control)
- RM2: a sequence's first 3D engine let go before a second opens
- RM5: JS solids deduplicated by expression, their Manifolds in a two-generation cache (192 MB each); solid.free
- RM6: a map whose picture changes by frame is one part and one material, its picture swapped as frames show it
  (raea-sys-material swapped.rs): the pool's 96 water maps were 96 meshes and 186 textures
- RM7: Manifold shapes welded into indexed meshes (4.65 corners a vertex in halcyon; RAEA_NO_WELD=1: the control);
  a held frame settles to the same picture, boiling 0.30% against 0.38%
- the shape text kept out of the store and the Edit (the engine spawns from the program's rows)
- RM8: a BufferMesh of 64 KB or more kept on disk by its content (buffer:@NAME, raea's caches/buffers):
  shape text 274 MB -> 11.8 MB
- RM9: material maps as BC7 with their mips (intel_tex_2), cached by content in raea's caches/maps, any size
  resampled to whole blocks (RAEA_NO_BC=1: the control): GPU images 733 MB -> 282 MB
- RM10: raea-sys-contact's solids as bytes, a count and u16 surface points: 1.27 GB -> 0.37 GB mid-render
- memtally names the biggest GPU images; RAEA_MEM_MARKS prints the program's text by column and shape call
- tools/memprobe: memseq, heapwho/heapseq (malloc stack logging, holders.py), abenv/abseq/steadyab (A/B on one binary)

docs/render-memory.feature has each step's numbers and gates.

* raea-sys-memtally in tools/layers.toml: a system, as the layers gate asks

* Size: render-memory's eight over-limit crates and files allowed once at today's sizes (Shane 11-29, for 0.1.13): the folders hold other lanes' uncommitted GI/RT work, so they're split once that lands

* Fold render-memory's copies into what they copied (the dupes gate, 297/60 back to 293/57): one two-generation cache, raea_core_shape::Generations, for core/shape's meshes and runtime/calls' built Manifolds; swapped maps load their pictures with maps.rs's panel_picture; raea_io_home::write_whole for a file written whole or not at all (the BC7 maps first); spawn_sig(it, swapped) one function, its callers saying which; the store's spawn given its columns. Size allowances taken down to the folded sizes (solid.rs is back under 500)

* Split the five crates render-memory took past 2000 lines, each where it parts cleanly, so the next lane has room (the size and rask gates; the one-time allowance is gone):
- raea-core-tables (core/tables): a row's per-frame tables, from raea-sys-frame (re-exported there as tables)
- raea-runtime-ecs (runtime/ecs): raea/ecs on Bevy's ECS, from raea-runtime-calls (ecs.rs re-exports it)
- raea-sys-toon (systems/toon): toon surfaces and outlines, from raea-sys-material; its shaders keep the embedded path compositions name (raea_sys_material/toon.wesl)
- raea-render-plugins (engine/render/plugins): RaeaPlugins, from raea-render (engine.rs re-exports it as raea_plugins; the imports only it used are gone)
- raea-runtime-kinds (runtime/kinds): the rows of models, panels, decals, plates, fog, fire, splats, mesh sequences and grass, with placement and name_column, from raea-runtime-scene
maps.rs's inline and panel pictures move to maps/pictures.rs (re-exported, 422 lines left)

* Engine::open_in: a live engine (re)opened in its slot, the one there let go first (RM2 in one place, docs/render-memory.feature). raea-runtime-export's five opens go through it, so a fed render stepped again in the page's box keeps its AOVs as its first engine did; aov_on takes the engine in place. raea-runtime-export is back under its planned 2000 (LT1 and LX6 hold the rest)

* Drop the dependencies render-memory's splits left behind (cargo shear, CI's unused-dependencies gate): raea-render's eleven systems crates went with RaeaPlugins to raea-render-plugins, raea-runtime-calls's bevy with raea/ecs to raea-runtime-ecs, raea-sys-material's raea-core-shape with toon to raea-sys-toon. raea-render's cargo-shear ignore goes too: spawn.rs reads a mesh buffer through raea_core_shape::buffer_json now

---------

Co-authored-by: shinyobjectz <[email protected]>
2026-11-29 16:40:57 -08:00

187 lines
12 KiB
Rust

//! raea's Engine as Bevy plugins (docs/sdk.feature S6a). Each module that brings materials, passes or render-world
//! systems is a Plugin, and RaeaPlugins adds them in Engine::new's order, so any Bevy App can draw raea's looks beside
//! its own: `app.add_plugins((DefaultPlugins, RaeaPlugins))`, or `RaeaPlugins.build().disable::<LookPlugin>()` as with
//! Bevy's own groups. A plugin is the module's `install`, unchanged, so Engine::new and this group add the same things.
//!
//! RegisteredPlugins adds a project's Bevy plugins (package.json raea.bevy.plugins) and the clock they step by.
//!
//! Scene3dPlugin moves the 3D by the frame's rows (meshes, models, camera, lights, looks) each Update, as Engine::new
//! does: Engine::new runs the same systems, scene3d_systems(), inside its frame's chain between its own marks.
use bevy::app::{PluginGroup, PluginGroupBuilder};
use bevy::ecs::schedule::ScheduleConfigs;
use bevy::ecs::system::ScheduleSystem;
use bevy::prelude::*;
// the frame's system sets, raea-sys-frame's: a plugin orders its systems by them
pub use raea_sys_frame::{FrameSystems, Scene3dSystems};
// raea's modules, by the names Engine::new installs them under (raea-render's engine.rs)
use raea_runtime_plugins as plugins;
use raea_runtime_scene3d as scene3d;
use raea_sys_compose::{compose, passes};
use raea_sys_environment as environment;
use raea_sys_fog::{fire, froxel, froxel_glass};
use raea_sys_fog_half as fog_half;
use raea_sys_frame::bevy_api;
use raea_sys_grid as grid;
use raea_sys_look::{look, paint};
use raea_sys_material::{decal, shadow_catch, shadow_only};
use raea_sys_meshseq as meshseq;
use raea_sys_post::{cut, dof, effect_stack, lens, motion_blur, outline};
use raea_sys_screen as screen;
use raea_sys_shader as shader_ext;
use raea_sys_splat as splat;
use raea_sys_sprite as sprite2d;
use raea_sys_text3d as text3d;
use raea_sys_toon as toon;
use raea_sys_volume as volume;
macro_rules! raea_plugin {
($($name:ident: $install:path, $doc:literal;)*) => {
$(
#[doc = $doc]
#[derive(Default)]
pub struct $name;
impl Plugin for $name {
fn build(&self, app: &mut App) {
$install(app);
}
}
)*
/// Every raea plugin, in the order Engine::new installs them.
pub struct RaeaPlugins;
impl PluginGroup for RaeaPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>().add(FramePlugin)$(.add($name))*
}
}
};
}
raea_plugin! {
VolumetricFogPlugin: crate::scene3d::install_fog_shader, "Volumetric fog's shader, its dither seeded from the film frame (scene3d.rs).";
DepthOfFieldPlugin: crate::dof::install, "Bevy's depth of field, keeping the frame's alpha (dof.rs).";
EffectStackPlugin: crate::effect_stack::install, "The effect stack (a <Grade>'s aberration and vignette), keeping alpha (effect_stack.rs).";
Bevy019DefaultsPlugin: crate::bevy_api::install_0_19_defaults, "Bevy 0.19's tone curve \"none\" and screen-space transmission, which 0.20 changed (bevy_api.rs).";
HalfFogPlugin: crate::fog_half::install, "Volumetric fog at a fraction of the frame's resolution, upsampled by depth (fog_half.rs).";
EnvironmentPlugin: crate::environment::install, "Each panorama's light filtered once (environment.rs).";
FroxelFogPlugin: crate::froxel::install, "Fog as one medium in a grid along the view (froxel.rs).";
FroxelGlassPlugin: crate::froxel_glass::install, "Glass seen through froxel fog (froxel_glass.rs).";
LookPlugin: crate::look::install, "Models drawn as pixel art or the site's grain (look.rs).";
OutlinePlugin: crate::outline::install, "Ink outlines (outline.rs).";
SteadyPlugin: raea_sys_post::steady::install, "raea's temporal filter: each frame jittered and blended with those before it (raea-sys-post steady.rs, docs/realism.feature LT12).";
ComposePlugin: crate::compose::install, "Composition passes: each object's pass mask, read by Look, Outline, Bloom and Grade (compose.rs).";
Text3dPlugin: crate::text3d::install, "Text in the 3D world: bevy_rich_text3d and its faces (raea-sys-text3d).";
FirePlugin: crate::fire::install, "Flames drawn as volumes (fire.rs).";
SplatPlugin: crate::splat::install, "Gaussian splats, sorted per frame for the scene camera (raea-sys-splat).";
MeshSequencePlugin: crate::meshseq::install, "Mesh sequences by the frame, glitches on parts (meshseq.rs).";
Draws2dPlugin: raea_sys_sprite::install, "Plugins' 2D drawing brings up the scene's 2D camera (raea-sys-sprite's Draws2d).";
ScreenPlugin: crate::screen::install, "Live pictures on surfaces, drawn as CRTs (screen.rs).";
GridPlugin: crate::grid::install, "Greybox surfaces: graph paper and outlines (grid.rs).";
ProcMatPlugin: raea_sys_procmat::install, "Procedural surfaces: wood, stone, brick, tiles and the rest, with dust and dirt (systems/procmat).";
PaintPlugin: crate::paint::install, "The paint and cartoon looks' surfaces (paint.rs).";
PassesPlugin: crate::passes::install, "Render passes per view (passes.rs).";
TracedAoPlugin: raea_sys_rt::passes, "Traced passes: ambient occlusion, raealism's world probes and the sun's far shadow map (raea-sys-rt).";
GiScreenPlugin: raea_sys_gi::install, "raealism's light on screen: each pixel's indirect diffuse from the world cache, read by the main pass (raea-sys-gi, RAEA_GI=1).";
VolumePlugin: crate::volume::install, "Simulated smoke, clouds and contrails, raymarched (volume.rs).";
GrassPlugin: raea_sys_grass::install, "Fields of grass, made in tiles as detailed as the camera's distance asks (systems/grass).";
DetailPlugin: raea_sys_lod::install, "Where each part is drawn: levels by distance or size on screen, cross-faded, draw distance and shadows left to a coarser level (raea-sys-lod, docs/realism.feature LT21).";
ShadowOnlyPlugin: crate::shadow_only::install, "Meshes in the shadow maps only (shadow_only.rs).";
ShadowCatchPlugin: crate::shadow_catch::install, "Surfaces that show only the shadows falling on them (shadow_catch.rs).";
TintPlugin: raea_sys_material::tint::install, "A colour a copy over an <Instances>' surface, three's instanceColor (tint.rs).";
ToonPlugin: crate::toon::install, "The toon surface and its ink outline's materials (toon.rs).";
ShaderExtPlugin: crate::shader_ext::install, "Shaders written in TypeScript over the lit standard surface (raea-sys-shader).";
ThreeShaderPlugin: raea_sys_three_shader::install, "three.js's shaders: a ShaderMaterial's GLSL (naga) and a node material's WGSL, with three's built-ins (raea-sys-three-shader).";
FramePassPlugin: raea_sys_framepass::install, "Full-screen passes written in TypeScript (raea-sys-framepass).";
ComputePlugin: raea_sys_compute::install, "Compute shaders written in TypeScript, run before the cameras draw (raea-sys-compute).";
PhaseItemPlugin: raea_sys_phase::install, "Your own draws in a camera's opaque phase, as Bevy's custom_phase_item queues one (raea-sys-phase).";
LensPlugin: crate::lens::install, "The camera's lens (Fisheye, LensDistortion), after tone mapping (lens.rs).";
ParityPlugin: raea_sys_parity::install, "The parity probe: a frame's cost and World, written when RAEA_PARITY is set (raea-sys-parity, docs/control.feature FC0a).";
CameraCutPlugin: crate::cut::install, "A camera that jumps is a cut: its history starts again and its motion blur waits (cut.rs, RT31).";
MotionBlurPlugin: crate::motion_blur::install, "Motion blur's frame counter pinned to the frame (motion_blur.rs).";
}
/// The frame's state that raea's plugins' systems read (the clock, the frame's rows, the loaded edit, the error), empty
/// until something loads an edit: first in RaeaPlugins, so a plain Bevy App that adds the group runs it with nothing
/// of raea's drawn. Engine::new inserts its own over these when it loads.
pub struct FramePlugin;
impl Plugin for FramePlugin {
fn build(&self, app: &mut App) {
app.init_resource::<raea_sys_frame::Clock>().init_resource::<raea_sys_frame::Error>().init_resource::<raea_sys_frame::FrameRows>();
if !app.world().contains_resource::<raea_sys_frame::Edit>() {
let prog = raea_core_program::ir::Program::default();
let timeline = raea_core_program::timeline::Timeline::new(&prog, &[]).expect("an empty timeline");
app.insert_resource(raea_sys_frame::Edit { prog, timeline: std::sync::Arc::new(timeline), media: Vec::new() });
}
}
}
/// The registered Bevy plugins (plugins.rs), stepping by a clock at `fps` frames a second. Nothing when none are.
pub struct RegisteredPlugins {
pub fps: f64,
}
impl Plugin for RegisteredPlugins {
fn build(&self, app: &mut App) {
crate::plugins::install(app, self.fps);
}
}
/// The systems that move the 3D by the frame's rows, in order: models set up, toon materials over a model's own, then
/// meshes, camera, lights, fog, models, bones, looks, paint, decals and 3D text.
pub fn scene3d_systems() -> ScheduleConfigs<ScheduleSystem> {
(
// 2D over the 3D: its camera once there's a part, then each part's place, cell and colour (raea-sys-sprite)
(crate::sprite2d::camera_2d, crate::sprite2d::sync_2d).chain(),
crate::scene3d::model_setup,
crate::scene3d::instance_models,
crate::scene3d::model_shadow_markers,
crate::scene3d::toon_models,
((crate::scene3d::sync_meshes, crate::scene3d::sync_instances, crate::scene3d::sync_pictures, raea_sys_rendermap::sync_render_maps, raea_sys_material::swapped::sync_swapped_maps, crate::shader_ext::sync_shaded, raea_sys_sampler::arrays::sync_arrays, raea_sys_sampler::sync_samplers).chain(), crate::toon::sync_toon, (crate::scene3d::sync_camera, crate::scene3d::sync_post).chain(), crate::motion_blur::sync_motion_blur, (crate::environment::sync_environment, crate::environment::sync_prefiltered, crate::environment::sync_suns).chain(), (crate::scene3d::sync_light_sets, crate::scene3d::sync_lights).chain(), crate::scene3d::sync_fog_volumes, crate::scene3d::sync_fog_dither, crate::scene3d::sync_models, crate::scene3d::sync_morphs, crate::scene3d::sync_attach, crate::grid::grid_models, crate::scene3d::sync_bones, crate::look::sync_frame_look, crate::look::sync_looks, crate::compose::sync_passes, crate::look::sync_flat, crate::paint::sync_paint, crate::decal::sync_decals, crate::text3d::sync_texts).chain(),
// --clay and --review's clay half: every standard surface grey, after the rows set their colours
crate::scene3d::clay::clay_materials,
)
.chain()
}
/// The 3D moved by the frame's rows each Update (scene3d_systems), in the Scene3dSystems set.
pub struct Scene3dPlugin;
impl Plugin for Scene3dPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, scene3d_systems().in_set(Scene3dSystems));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_group_holds_every_plugin_and_disables_one_as_bevys_do() {
let all = RaeaPlugins.build();
assert!(all.contains::<VolumetricFogPlugin>() && all.contains::<LookPlugin>() && all.contains::<MotionBlurPlugin>());
let off = RaeaPlugins.build().disable::<LookPlugin>();
assert!(off.contains::<LookPlugin>() && !off.enabled::<LookPlugin>() && off.enabled::<ToonPlugin>());
}
#[test]
fn the_frame_plugin_gives_a_plain_app_the_state_raeas_systems_read() {
let mut app = App::new();
app.add_plugins(FramePlugin);
let w = app.world();
assert!(w.contains_resource::<raea_sys_frame::Clock>() && w.contains_resource::<raea_sys_frame::Error>());
assert!(w.contains_resource::<raea_sys_frame::FrameRows>() && w.contains_resource::<raea_sys_frame::Edit>());
assert!(RaeaPlugins.build().enabled::<FramePlugin>());
}
#[test]
fn scene3d_plugin_adds_its_systems_to_update_in_its_set() {
let mut app = App::new();
app.add_plugins(Scene3dPlugin);
let update = app.get_schedule(Update).expect("Update");
assert!(update.systems_len() >= 21);
assert!(update.graph().system_sets.contains(Scene3dSystems));
}
}