Skip to content
Open
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
436 changes: 436 additions & 0 deletions creations/demos/shape_debug/lod_zoom_profile_scene.hpp

Large diffs are not rendered by default.

43 changes: 41 additions & 2 deletions creations/demos/shape_debug/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "post_effects_fixture.hpp"
#include "recorder_frame_fixture.hpp"
#include "lod_scene.hpp"
#include "lod_zoom_profile_scene.hpp"
#include "sdf_voxel_scene.hpp"

#include <algorithm>
Expand Down Expand Up @@ -65,7 +66,10 @@
#include <irreden/update/systems/system_prefab_lod_parts.hpp>
#include <irreden/update/systems/system_propagate_transform.hpp>
#include <irreden/render/systems/system_gate_voxel_sets_by_lod.hpp>
#include <irreden/render/systems/system_entity_canvas_to_framebuffer.hpp>
#include <irreden/render/systems/system_lod_update.hpp>
#include <irreden/render/systems/system_propagate_canvas_rotation.hpp>
#include <irreden/voxel/systems/system_rebuild_detached_voxels.hpp>
#include <irreden/voxel/systems/system_rebuild_grid_voxels.hpp>
#include <irreden/voxel/systems/system_update_voxel_set_children.hpp>
#include <irreden/voxel/systems/system_rebuild_detached_voxels.hpp>
Expand Down Expand Up @@ -611,6 +615,9 @@ bool g_cullEvictTest = false;
bool g_lodDenseSwap = false;
bool g_lodScene = false;
IRShapeDebug::LodScene g_lodSceneState;
// --lod-zoom-profile: see lod_zoom_profile_scene.hpp.
bool g_lodZoomProfile = false;
IRShapeDebug::LodZoomProfileScene g_lodZoomProfileState;

// --viewport-portrait: a secondary-viewport fixture. One blue voxel cube and
// one orange decoy sit in the world at zoom 1; a viewport pinned to a GUI
Expand Down Expand Up @@ -1885,6 +1892,12 @@ void registerCliArgs() {
"Replace the scene + capture table with two co-located DENSE voxel sets on disjoint LOD "
"bands, captured on either side of the swap; needs --auto-screenshot"
);
args.flag(
"--lod-zoom-profile",
"Replace the scene + capture table with the authored LOD threshold / hysteresis fixture: "
"variant pairs in every output mode under fractional zoom, plus two portraits; needs "
"--auto-screenshot"
);
args.flag(
"--viewport-portrait",
"Replace the scene + capture table with the secondary-viewport fixture: a world cube at "
Expand Down Expand Up @@ -1998,6 +2011,7 @@ void readCliArgs() {
g_cullEvictTest = args.getFlag("--cull-evict-test");
g_lodDenseSwap = args.getFlag("--lod-dense-swap");
g_lodScene = args.getFlag("--lod-scene");
g_lodZoomProfile = args.getFlag("--lod-zoom-profile");
g_viewportPortrait = args.getFlag("--viewport-portrait");
g_textSizeSample = args.getFlag("--text-size-sample");
g_voxelTextSample = args.getFlag("--voxel-text-sample");
Expand Down Expand Up @@ -2229,6 +2243,16 @@ int main(int argc, char **argv) {
IR_LOG_ERROR("--lod-scene cannot be combined with another scene or capture override");
return 2;
}
if (g_lodZoomProfile &&
(g_lodScene || g_lodDenseSwap || g_viewportPortrait || g_guiTest || g_zoomSweep ||
g_panSweep || g_yawSweep || g_zoomCalibration || g_canvasImageSample ||
g_framebufferImageSample || !g_loadPrefabPath.empty() || g_autoWarmupFrames <= 0)) {
IR_LOG_ERROR(
"--lod-zoom-profile needs --auto-screenshot and cannot be combined with another "
"scene or capture override"
);
return 2;
}
if (g_shapeSampleProbe && (g_shapeSampleProbePixel.x < 0 || g_autoWarmupFrames <= 0)) {
IR_LOG_ERROR(
"--shape-sample-probe requires integer screenshot coordinates and --auto-screenshot"
Expand Down Expand Up @@ -4561,6 +4585,12 @@ void initSystems() {
IRSystem::createSystem<IRSystem::REBUILD_GRID_VOXELS>(),
IRSystem::createSystem<IRSystem::REBUILD_GRID_VOXELS_IMPLICIT>()
};
if (g_lodZoomProfile) {
// The fixture's DETACHED and DETACHED_REVOXELIZE pairs draw through
// their own canvases.
updatePipeline.push_back(IRSystem::createSystem<IRSystem::PROPAGATE_CANVAS_ROTATION>());
updatePipeline.push_back(IRSystem::createSystem<IRSystem::REBUILD_DETACHED_VOXELS>());
}
if (!g_loadPrefabPath.empty() || g_lodScene || IREngine::args().getFlag("--sdf-voxel-motion")) {
// After LOD_UPDATE, so the parts follow this tick's tier, and before
// PROPAGATE_TRANSFORM, so a part spawned this tick is placed this tick
Expand Down Expand Up @@ -4711,7 +4741,7 @@ void initSystems() {
IRSystem::createSystem<IRSystem::GUI_CANVAS_TO_SCREEN>(),
}
);
if (g_gatherFilterFixture) {
if (g_gatherFilterFixture || g_lodZoomProfile) {
renderPipeline.insert(
std::next(
std::find(renderPipeline.begin(), renderPipeline.end(), trixelToFramebufferId)
Expand Down Expand Up @@ -4756,7 +4786,7 @@ void initSystems() {
IRSystem::createSystem<IRSystem::IMAGE_TO_FRAMEBUFFER>()
);
}
if (g_viewportPortrait) {
if (g_viewportPortrait || g_lodZoomProfile) {
// The sync feeds the viewport's pool before the voxel raster reads it;
// the composite lands on top of the world + GUI composite.
renderPipeline.insert(
Expand Down Expand Up @@ -5446,6 +5476,9 @@ void initSystems() {
IRVideo::setAutoScreenshotShots(cfg, IRShapeDebug::LodScene::shots_);
cfg.settleFrames_ = IRConstants::kPrefabPartsTierSettleTicks + 12;
cfg.onCaptureFrame_ = [](int shotIndex) { g_lodSceneState.capture(shotIndex); };
} else if (g_lodZoomProfile) {
IRVideo::setAutoScreenshotShots(cfg, IRShapeDebug::LodZoomProfileScene::shots_);
cfg.onCaptureFrame_ = [](int shotIndex) { g_lodZoomProfileState.capture(shotIndex); };
} else if (g_lodDenseSwap) {
IRVideo::setAutoScreenshotShots(cfg, kLodDenseSwapShots);
} else if (g_viewportLodSwap) {
Expand Down Expand Up @@ -6889,6 +6922,12 @@ void initEntities() {
setupCanvasLighting();
return;
}
if (g_lodZoomProfile) {
IR_LOG_INFO("--- LOD zoom-profile fixture scene ---");
g_lodZoomProfileState.initialize();
setupCanvasLighting();
return;
}
if (g_lodDenseSwap) {
IR_LOG_INFO("--- DENSE LOD-swap fixture scene ---");
initLodDenseSwapScene();
Expand Down
99 changes: 93 additions & 6 deletions docs/design/lod-strategy.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# LOD strategy — artist-driven detail tiers, prefab-manifest composition

**Status:** Phase 1 (#708, #1467, #3966) and Phase 2 (#3975) shipped.
**Status:** Phase 1 (#708, #1467, #3966, #4891) and Phase 2 (#3975) shipped.
Phase 3 (cross-tier interpolation) is deferred to its own design pass.

**Decision:** LOD lives at two layers — runtime selection (Phase 1) and
Expand Down Expand Up @@ -274,10 +274,10 @@ writer of it: fog's whole-body reveal owns `visible_`, and two owners of one
flag would overwrite each other's verdict. A set draws only while
`renders()` — both gates open.

Zoom snaps to powers of two, so tier changes are discrete and the engine adds
no hysteresis; a creation's policy owns any debounce. `computeLodLevel`'s
thresholds are unchanged. `lodVoxelScale` is removed: it had no callers, and
sub-world voxel pitch is ruled out.
The zoom -> tier mapping itself is a camera's zoom profile (Phase 1d below):
the default keeps the 2 / 4 / 8 / 16 thresholds with no deadband.
`lodVoxelScale` is removed: it had no callers, and sub-world voxel pitch is
ruled out.

`C_VoxelSetNew` persists its authored `lodMin_` / `lodMax_` band in snapshot
format version 3, so a reloaded set retains its inclusive LOD range.
Expand All @@ -289,6 +289,92 @@ The `shape_debug --lod-dense-swap` fixture (two co-located DENSE sets on
disjoint bands) is the reference; render-verify covers it at zoom 2× / 8×, and
`LodTierLua` covers the read, the pin and the gate headlessly.

### Phase 1d — zoom profiles and hysteresis (#4891)

A camera can render at fractional zoom (`IRPrefab::Camera::setZoomContinuous`),
so a zoom that wanders across a fixed threshold — 3.99× / 4.01× — would swap
two disjoint variants on every crossing. A **zoom profile** moves the
thresholds and adds a deadband around each.

- **Shape.** `C_LodZoomProfile { IRRender::LodZoomProfile profile_ }`: four
ascending thresholds and a hysteresis half-width. Threshold `i` is the
boundary between `LOD_(4-i)` and the finer `LOD_(3-i)`, in display zoom —
the `C_ZoomLevel` scale, `max(x, y)`, the same number
`IRRender::getCameraZoom` returns. The half-width `h` is in **octaves** of
zoom, so a band is symmetric in log-zoom: `[t / 2^h, t * 2^h]`. The default
profile is 2, 4, 8, 16 with `h = 0`, identical to `computeLodLevel`.
- **Validation.** Every threshold finite and positive, strictly ascending;
`h` finite and nonnegative; adjacent bands must not overlap
(`t_i * 2^h <= t_(i+1) / 2^h`; touching is allowed).
`LodZoomProfile::make` is the only constructor besides the default, so no
invalid profile exists. `C_LodZoomProfile::set`, the
`IRPrefab::Lod::set*ZoomProfile` setters, the Lua surface and the save
reader all apply `whyInvalid`; an invalid write keeps the previous profile.
- **Crossing rule.** A consumer moving toward finer detail crosses threshold
`t` once zoom reaches `t * 2^h`; moving toward coarser detail it crosses
once zoom falls strictly below `t / 2^h`; inside a band the tier holds. At
`h = 0` the finer tier owns the exact threshold, as before, and the answer
is independent of direction. One update resolves every threshold a jump
crosses, so 1× -> 64× lands on `LOD_0` in one frame.
- **History.** Each consumer owns its previous tier in an
`IRRender::LodTierHistory`: the main camera's in the `C_ActiveLodLevel`
singleton (`history_`), each secondary viewport's in its
`C_ViewportCamera::lodHistory_`. A history seeds from `tierAt(zoom)` — the
zero-width answer — at creation (first update), after
`IRPrefab::Lod::resetMainZoomHistory` / `resetViewportZoomHistory`, and
whenever the profile it resolves under differs from the one it was seeded
with (a replacement, an in-place `set`, a removal back to the default, a
viewport gaining or dropping its own). A seed inside a band therefore takes
the finer side of that band's threshold. A nonfinite zoom keeps the
previous tier.
- **Ownership and inheritance.** The main camera (the entity named
`"camera"`) owns its optional profile; `LOD_UPDATE` reads it once per
tick. A secondary viewport **inherits** the main camera's profile unless it
carries its own `C_LodZoomProfile`; `SYNC_VIEWPORT_SUBJECTS` resolves each
viewport's tier from its own zoom and its own history, so a portrait and
the world never share a deadband state. Viewport zoom still snaps to a
power of two.
- **Configuration surface.** C++: `IRPrefab::Lod::` in
`render/lod_zoom_policy.hpp` (`setMainZoomProfile`,
`setViewportZoomProfile`, `clear*`, `reset*ZoomHistory`, `mainZoomProfile`,
`viewportZoomProfile`, `viewportTier`). Lua: `IREntity.addLuaComponent(e,
IRComponent.C_LodZoomProfile, { thresholds = { ... }, hysteresis = h })`,
a prefab's `components = { C_LodZoomProfile = { ... } }`, and on a held
component `threshold(i)`, `hysteresis` and `set{ ... }`; each raises with
the `whyInvalid` reason.
- **Persistence.** `C_LodZoomProfile` saves its four thresholds and `h`
(version 1); a record that is not a valid profile fails the load. The
histories are derived and never saved: a loaded world seeds from the
loaded zoom. An entity saved before the component existed has none and
selects with the default.
- **Unchanged.** Per-part inclusive bands, `C_LodTierOverride` pins (world
consumers only — a viewport still ignores them), and fractional raster
sampling (continuous zoom places content independently of the tier) keep
their meaning. `computeLodLevel` is the default profile's `tierAt`.

**Three debounce mechanisms, three purposes.** The zoom profile's deadband
decides *which tier* a camera selects. Above it, a creation's structural
policy decides *which parts exist* at that tier: `PREFAB_LOD_PARTS` waits for
a tier to hold `kPrefabPartsTierSettleTicks` before spawning or destroying
parts, and `resident` parts stay alive out of band (Phase 2). Separately,
`CANVAS_RESIDENCY` keeps a promote / demote margin around the camera's
interest region so an entity does not churn between GRID and its own canvas
([composite canvases](composite-entity-canvases.md#residency)) — a spatial
budget policy that never reads the LOD tier. None substitutes for another:
the settle window debounces part churn, not the tier the shape filter and
DENSE gate see, and the residency margin is about distance, not zoom.

The `shape_debug --lod-zoom-profile` fixture is the reference: GRID,
DETACHED and DETACHED_REVOXELIZE DENSE pairs, an SDF pair, a pinned control
and two portraits (one inheriting, one with its own 0.45-octave profile)
under a fractional zoom sequence that oscillates across 5× at width zero and
then inside a quarter-octave band. Each capture asserts the resolved tiers,
the DENSE gate state and the authored records;
`scripts/render-lod-zoom-profile-metric.py` reads the variant colour inside
each pair's logged screen box. `LodZoomProfile`, `LodZoomHysteresis`,
`LodZoomMainCamera`, `LodZoomProfileLua` and the `ViewportTest` profile cases
cover the rules headlessly.

What Phase 1 explicitly does **not** do:

- Multi-tier `.vxs` composition. A Phase 1 entity carries all its shapes
Expand Down Expand Up @@ -441,7 +527,8 @@ without a format break. Same escape hatch as `.vxs`.
rasters its subject at its own camera's zoom-derived density
(`getVoxelRenderEffectiveSubdivisionsForZoom`), so a portrait at zoom 16 is
the finest tier while the world draws the same entity coarse. Its subjects'
DENSE bands are filtered at the viewport's own zoom-derived tier;
DENSE bands are filtered at the viewport's own tier — its zoom under its own
or the inherited main zoom profile, from its own history (Phase 1d);
`C_LodTierOverride` pins the world's tier only and is not read there.
- **Subdivision-count scaling** (`render_manager.cpp:240-253`). Existing
per-zoom behavior: subdivision passes per voxel scale with
Expand Down
4 changes: 3 additions & 1 deletion docs/design/secondary-viewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ the world's sun shadow and the fog pass never sees it.

The viewport rasters at its own zoom's subdivision density
(`IRRender::getVoxelRenderEffectiveSubdivisionsForZoom`) and resolves its own
tier from that zoom (`IRRender::computeLodLevel`). `SYNC_VIEWPORT_SUBJECTS`
tier from that zoom: its own `C_LodZoomProfile`, else the main camera's, moved
from the viewport's own previous tier (`C_ViewportCamera::lodHistory_`;
[LOD strategy §Phase 1d](lod-strategy.md#phase-1d--zoom-profiles-and-hysteresis-4891)). `SYNC_VIEWPORT_SUBJECTS`
drops every tagged part whose `[lodMax_ .. lodMin_]` band excludes that tier
(`IRRender::shouldSkipAtLod`) before laying out the box, so a subject authored
as co-located variants on disjoint bands, every variant tagged, shows the world
Expand Down
2 changes: 1 addition & 1 deletion engine/prefabs/irreden/render/CLAUDE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# engine/prefabs/irreden/render/ — canvases, framebuffers, cameras, text

Engine-side state and device drivers: [`engine/render/CLAUDE.md`](../../../render/CLAUDE.md#the-pipeline-one-frame).
Expand Down Expand Up @@ -78,7 +78,7 @@
arbitrates only across canvases — use separate detached units.
- A `C_CanvasCamera` canvas is viewed through that camera by every `*_TO_TRIXEL` stage;
`IRPrefab::Viewport::` owns it and bands at its own zoom, not `C_LodTierOverride` ([design](../../../../docs/design/secondary-viewport.md)).
- `C_ActiveLodLevel` is the singleton `LOD_UPDATE` writes. A `C_ShapeDescriptor`
- `C_ActiveLodLevel` is the singleton `LOD_UPDATE` writes through the camera's `C_LodZoomProfile` (thresholds + hysteresis; viewports inherit it, `lod_zoom_policy.hpp`). A `C_ShapeDescriptor`
(`SHAPES_TO_TRIXEL`) or `C_VoxelSetNew` (`GATE_VOXEL_SETS_BY_LOD`) draws only
inside its `[lodMax_ .. lodMin_]` band; disjoint co-located bands swap. A new
consumer resolves via `IRPrefab::Lod::TierSnapshot`, which honours `C_LodTierOverride`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define COMPONENT_ACTIVE_LOD_LEVEL_H

#include <irreden/ir_render.hpp>
#include <irreden/render/lod_zoom_profile.hpp>

namespace IRComponents {

Expand All @@ -12,8 +13,13 @@ namespace IRComponents {
// Default is LOD_4 (coarsest tier) so a creation that doesn't register
// LOD_UPDATE — and therefore never writes the singleton — still draws every
// default-band entity. A missing row resolves the same way.
//
// `history_` is the main camera's previous tier under its zoom profile, the
// state its hysteresis deadband reads; `current_` mirrors its tier once
// LOD_UPDATE has run. Derived, never saved.
struct C_ActiveLodLevel {
IRRender::LodLevel current_ = IRRender::LodLevel::LOD_4;
IRRender::LodTierHistory history_{};
};

} // namespace IRComponents
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef COMPONENT_LOD_ZOOM_PROFILE_H
#define COMPONENT_LOD_ZOOM_PROFILE_H

#include <irreden/render/lod_zoom_profile.hpp>

#include <optional>

namespace IRComponents {

// The zoom -> tier mapping of the camera entity carrying it. On the main
// camera (the entity named "camera") it drives LOD_UPDATE; on a secondary
// viewport it overrides the main camera's profile for that viewport only. A
// camera without one selects with the default profile (2, 4, 8, 16, width 0);
// a viewport without one inherits the main camera's. Authored data only: each
// consumer's previous tier lives beside its other derived camera state.
struct C_LodZoomProfile {
IRRender::LodZoomProfile profile_{};

C_LodZoomProfile() = default;

explicit C_LodZoomProfile(const IRRender::LodZoomProfile &profile)
: profile_{profile} {}

// Replaces the profile when the values are valid; otherwise keeps the
// current one and returns false.
bool set(const IRRender::LodZoomProfile::Thresholds &thresholds, float hysteresisOctaves) {
const std::optional<IRRender::LodZoomProfile> next =
IRRender::LodZoomProfile::make(thresholds, hysteresisOctaves);
if (!next) {
return false;
}
profile_ = *next;
return true;
}
};

} // namespace IRComponents

#endif /* COMPONENT_LOD_ZOOM_PROFILE_H */
Loading
Loading