Merge branch 'claude/hopeful-darwin-ae8b87' — Phase A.5 SHIP + Quality Preset system
Phase A.5 — Two-tier Streaming + Horizon LOD shipped. Headline: 2.3 km terrain horizon (radius=4 near + 12 far) with off-thread mesh build, fog blend at N₁, mipmaps + 16x AF, MSAA 4x + A2C foliage, depth-write audit, BUDGET_OVER diag, Quality Preset system (Low/Medium/ High/Ultra) with env-var overrides + F11 mid-session re-apply. ~999 tests pass, 8 pre-existing physics/input failures unchanged. Two structural-to-A.5 bug fixes shipped post-T26: - Bug A (9217fd9): far-tier worker strips entities (T13/T16 had only wired the controller side; far-tier was loading full entity layers, ~71K entities instead of ~10K, 5x perf regression). - Bug B (0ad8c99): WalkEntities scratch list reused across frames (was 480 KB / frame allocation). Tier 1 entity-classification cache attempted as polish (3639a6f), reverted (9b49009) — broke animation by caching mutable per-frame state. Retry deferred to post-A.5 polish phase (ISSUE #53). Deferred to post-A.5 polish: - Tier 1 retry with animation-mutation audit (ISSUE #53) - Lifestone missing visual (ISSUE #52) - JobKind plumbing through BuildLandblockForStreaming (ISSUE #54) - Tier 2 (static/dynamic split) + Tier 3 (GPU compute cull) — separate multi-week phases. Roadmap at docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md. SHIP commit:9245db5.
This commit is contained in:
commit
d3d78fa14f
37 changed files with 6001 additions and 281 deletions
41
CLAUDE.md
41
CLAUDE.md
|
|
@ -110,6 +110,21 @@ ourselves".
|
|||
uvec2 + `sampler2DArray(handle)` constructor pattern (NOT the direct
|
||||
`uniform sampler2DArray` + `glProgramUniformHandleARB` form, which
|
||||
GL_INVALID_OPERATIONs on at least one driver).
|
||||
- **Two-tier streaming architecture (Phase A.5, shipped 2026-05-10).**
|
||||
`src/AcDream.App/Streaming/` owns the full streaming pipeline. Key types:
|
||||
`StreamingRegion` (two-radius Chebyshev window: N₁=near, N₂=far; produces
|
||||
`TwoTierDiff` with 5 transition lists per tick), `StreamingController`
|
||||
(render-thread coordinator: routes `TwoTierDiff` to the worker queue and
|
||||
drains completions up to `MaxCompletionsPerFrame` per frame),
|
||||
`LandblockStreamer` (single background worker thread: `LoadFar` = heightmap
|
||||
+ mesh only, `LoadNear` = heightmap + `LandBlockInfo` + scenery + mesh,
|
||||
`PromoteToNear` = `LandBlockInfo` + scenery only),
|
||||
`GpuWorldState` (render-thread entity state: `AddEntitiesToExistingLandblock`
|
||||
for promotions, `RemoveEntitiesFromLandblock` for demotions).
|
||||
Default: N₁=4 (81 near LBs, full detail), N₂=12 (544 far LBs, terrain
|
||||
only). Quality Preset system (`QualitySettings.From(preset)`) controls
|
||||
both radii and MSAA/anisotropic/A2C/completions-per-frame as a unit.
|
||||
Spec: `docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md`.
|
||||
|
||||
**Execution phases:** R1→R8 in the architecture doc. Each phase has clear
|
||||
goals, test criteria, and builds on the previous. Don't skip phases.
|
||||
|
|
@ -510,19 +525,19 @@ acdream's plan lives in two files committed to the repo:
|
|||
acceptance criteria. Do not drift from the spec without explicit user
|
||||
approval.
|
||||
|
||||
**Currently in flight: Phase N.6 — Perf polish.**
|
||||
Roadmap entry at [`docs/plans/2026-04-11-roadmap.md`](docs/plans/2026-04-11-roadmap.md).
|
||||
Builds on N.5 + N.5b. Legacy renderers (`InstancedMeshRenderer`,
|
||||
`StaticMeshRenderer`, `WbFoundationFlag`) were retired in the N.5 ship
|
||||
amendment, and the terrain legacy renderer (`TerrainChunkRenderer` +
|
||||
`TerrainRenderer` + legacy `terrain.vert/.frag`) was retired in N.5b.
|
||||
N.6 scope is perf-only: WB atlas adoption, persistent-mapped buffers
|
||||
(strong candidate after N.5b's per-frame DEIC `BufferSubData`),
|
||||
GPU-side culling via compute pre-pass, GL_TIME_ELAPSED query
|
||||
double-buffering, direct higher-radius perf comparison once A.5 lands,
|
||||
legacy `Texture2D`/`sampler2D` TextureCache path retirement (Sky / Debug
|
||||
remain on the legacy path now that Terrain has migrated).
|
||||
Plan + spec written when work begins.
|
||||
**Currently in flight: Post-A.5 polish — Tier 1 retry + lifestone fix + JobKind plumbing.**
|
||||
Open issues: #52 (lifestone missing), #53 (Tier 1 entity cache redo with animation-mutation
|
||||
audit), #54 (JobKind plumbing through BuildLandblockForStreaming for proper far-tier skip).
|
||||
After those three close, the next planned phase is N.6 (perf polish) — see roadmap for scope.
|
||||
|
||||
**Phase A.5 (Two-tier Streaming + Horizon LOD) shipped 2026-05-10.**
|
||||
N₁=4 near-tier (81 LBs, full detail) + N₂=12 far-tier (544 LBs, terrain only); fog
|
||||
horizon; QualityPreset system (Low/Medium/High/Ultra) with env-var overrides; F11
|
||||
mid-session reapply. Two post-ship-prep bugs fixed: Bug A (far-tier worker was loading
|
||||
full entity layer — ~71K entities, ~5x perf regression vs spec), Bug B (WalkEntities
|
||||
per-frame list alloc — ~480 KB/frame GC pressure). Tier 1 entity cache reverted (animation
|
||||
regression; see #53). Plan archived at
|
||||
[`docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md`](docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md).
|
||||
|
||||
**Phase N.5b (Terrain on Modern Rendering Path) shipped 2026-05-09.**
|
||||
`TerrainModernRenderer` mirrors WB's `TerrainRenderManager` pattern
|
||||
|
|
|
|||
|
|
@ -46,6 +46,74 @@ Copy this block when adding a new issue:
|
|||
|
||||
# Active issues
|
||||
|
||||
## #54 — A.5/jobkind-plumbing: far-tier worker loads full entity layer then strips
|
||||
|
||||
**Status:** OPEN
|
||||
**Severity:** LOW (correctness/perf; worker wastes CPU on far-tier LandBlockInfo + scenery generation that is immediately discarded)
|
||||
**Filed:** 2026-05-10
|
||||
**Component:** streaming / LandblockStreamer
|
||||
|
||||
**Description:** Bug A's fix (commit `9217fd9`) patches at the worker output — after a far-tier job completes the full `LoadNear` path, the result's entity list is stripped before posting to the completion queue. This means far-tier LBs still load `LandBlockInfo` + run `SceneryGenerator` + call `LandblockLoader.BuildEntitiesFromInfo` even though those results are thrown away. At N₂=12, that is ~544 far-tier LBs × unnecessary dat reads + scenery math on promotion sequences.
|
||||
|
||||
**Proper fix:** plumb `LandblockStreamJobKind` through `BuildLandblockForStreaming` so far-tier jobs call only `LandBlock` heightmap read + `LandblockMesh.Build`, skipping `LandBlockInfo` + `SceneryGenerator` entirely. The function signature change is ~5 lines; wiring is ~10 lines. Estimated 30 min–1 hour total.
|
||||
|
||||
**Files:**
|
||||
- `src/AcDream.App/Streaming/LandblockStreamer.cs` — `HandleJob` + `BuildLandblockForStreaming`
|
||||
|
||||
**Acceptance:** Far-tier LB worker path reads only the `LandBlock` dat file (no `LandBlockInfo`, no `SceneryGenerator` call). Verified by adding a counter diagnostic or via dotnet-trace showing the dat-read call count per job kind.
|
||||
|
||||
---
|
||||
|
||||
## #53 — A.5/tier1-redo: entity-classification cache broke animation (reverted)
|
||||
|
||||
**Status:** OPEN
|
||||
**Severity:** MEDIUM (perf gap; the classification cache would save ~1-2ms/frame but cannot land until animation-mutation audit is done)
|
||||
**Filed:** 2026-05-10
|
||||
**Component:** rendering / WbDrawDispatcher / AnimationSequencer
|
||||
|
||||
**Description:** Tier 1 entity-classification cache (commit `3639a6f`) was reverted at `9b49009` due to an animation regression. The cache stored `meshRef.PartTransform` at first-classify time. For static entities this is stable. For animated entities, `AnimationSequencer` mutates `meshRef.PartTransform` every frame to apply the current skeletal pose. The cache froze the pose, causing NPCs and some animated entities to stop animating (some buildings also showed at wrong positions, likely entities incorrectly flagged as animated).
|
||||
|
||||
**Root cause:** the "trust MeshRefs as the source of truth" comment in the dispatcher gave false confidence — MeshRefs IS the source of truth, but it is mutated EVERY frame for animated entities.
|
||||
|
||||
**Next attempt needs:**
|
||||
|
||||
1. Audit `AnimationSequencer` + `AnimationHookRouter` to identify ALL per-frame mutations of `MeshRef` state (not just `PartTransform` — are any other fields mutated?).
|
||||
2. Redesign cache to: (a) bypass animated entities entirely (classify them each frame, cache only static entities), OR (b) cache only the animation-invariant subset of the classification key (group key, texture handle, blend mode) while reading the per-frame pose from the live `MeshRef`.
|
||||
3. Test specifically with a moving animated NPC visible on screen before shipping.
|
||||
|
||||
**Estimated:** 1 week including audit + redesign + retest.
|
||||
|
||||
**Files:**
|
||||
- `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` — dispatcher classification logic
|
||||
- `src/AcDream.Core/Animation/AnimationSequencer.cs` — mutation source
|
||||
- `src/AcDream.Core/Animation/AnimationHookRouter.cs` — secondary mutation source
|
||||
|
||||
---
|
||||
|
||||
## #52 — A.5/lifestone-missing: Holtburg lifestone not rendering
|
||||
|
||||
**Status:** OPEN
|
||||
**Severity:** MEDIUM (visible missing landmark; lifestone is the player's respawn anchor and should always be visible)
|
||||
**Filed:** 2026-05-10
|
||||
**Component:** streaming / rendering
|
||||
|
||||
**Description:** The Holtburg lifestone (spinning blue crystal) has not rendered since earlier in A.5 development. Reproduce: launch live client, walk to Holtburg town center, look toward the lifestone position. Should see the spinning blue crystal; instead see nothing.
|
||||
|
||||
**Root cause (suspected, two candidates):**
|
||||
|
||||
1. Bug A's far-tier strip (commit `9217fd9`) may be incorrectly stripping a near-tier entity. The lifestone's server GUID is `0x5000000A`; its dat object may be registering via the `LandBlockInfo` path but getting stripped as if it were a far-tier entity due to a tier-classification race or incorrect LB-tier tracking.
|
||||
2. Separate regression from earlier in the A.5 development chain — possibly introduced when entity registration was restructured during T13/T16 streaming controller wiring.
|
||||
|
||||
**Investigation approach:**
|
||||
|
||||
1. Add a `[STREAMING-DIAG]` log line when far-tier stripping drops an entity — log the entity's GfxObj ID and LB address so the lifestone's GfxObj ID appears in the log if it is being stripped.
|
||||
2. If not in the strip log, check whether the lifestone's LB is registering as near-tier at all during first-tick bootstrap.
|
||||
3. Bisect to find the commit that broke it if the above two checks don't isolate the cause.
|
||||
|
||||
**Acceptance:** Launch live, walk to Holtburg center, spinning blue crystal visible at the lifestone position. No regression on other static entities in the area.
|
||||
|
||||
---
|
||||
|
||||
## #50 — Road-edge tree at 0xA9B1 visible in acdream but not retail
|
||||
|
||||
**Status:** OPEN
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# acdream — strategic roadmap
|
||||
|
||||
**Status:** Living document. Updated 2026-05-09 for Phase N.5b shipping (terrain on the modern rendering path via Path C — mirror WB's `TerrainRenderManager` pattern, consume `LandblockMesh.Build` for retail formula compliance; closes ISSUE #51). N.6 (perf polish) remains the in-flight phase.
|
||||
**Status:** Living document. Updated 2026-05-10 for Phase A.5 shipping (two-tier streaming N₁=4/N₂=12 + QualityPreset system + Bug A/B fixes; closes the two-tier streaming spec). Post-A.5 polish (Tier 1 retry + lifestone fix + JobKind plumbing) is now the in-flight work.
|
||||
**Purpose:** One source of truth for where the project is and where it's going. Every observed defect or missing feature has a named phase that owns it; when something looks wrong in-game, look here to find the phase that'll address it. Implementation details live in per-phase specs under `docs/superpowers/specs/`, not in this file.
|
||||
|
||||
---
|
||||
|
|
@ -31,6 +31,7 @@
|
|||
| A.1 | Streaming landblock loader — runtime-configurable visible window (default 5×5, `ACDREAM_STREAM_RADIUS`), camera-centered offline / player-centered live, hysteresis-based unloads, pending-spawn list for late CreateObject events | Live ✓ |
|
||||
| A.2 | Frustum culling — per-landblock AABB test (Gribb-Hartmann), terrain + static-mesh renderers skip culled landblocks, perf overlay in window title | Visual ✓ |
|
||||
| A.3 | Background net receive thread — dedicated daemon thread buffers UDP into Channel, render thread drains | Visual ✓ |
|
||||
| A.5 | Two-tier streaming + horizon LOD — N₁=4 (full detail, 81 LBs) + N₂=12 (terrain only, 544 LBs); fog blend at N₁; per-LB entity dispatcher walk tightened (Change #1 animated-walk fix + Change #2 cached AABB); single-worker off-thread mesh build; mipmaps + 16x anisotropic on TerrainAtlas; A2C with MSAA 4x on foliage; depth-write audit + lock-in test; **NEW T22.5: QualityPreset system** (Low/Medium/High/Ultra) with per-preset radii + MSAA + anisotropic + A2C + completions; env-var overrides per field; F11 mid-session re-apply. **Bug fixes post-T26 ship-prep**: (Bug A) far-tier worker now strips entities from far-tier loads — without this fix, far-tier LBs were loading their full entity layer (~71K entities) defeating the two-tier optimization; (Bug B) WalkEntities switched from per-frame fresh-list allocation to caller-provided scratch list (eliminated ~480 KB/frame GC pressure). **Deferred to post-A.5**: Tier 1 entity-classification cache (first attempt broke animation; revert + redo with animation-mutation audit), lifestone visual (missing in render), JobKind plumbing through BuildLandblockForStreaming (proper Bug A fix), Tier 2/3 perf optimizations (roadmap at docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md). Plan archived at docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md. | Live ✓ |
|
||||
| B.3 | Physics MVP resolver foundation — terrain contact, CellSurface prototype, streaming-populated collision inputs, and first `PhysicsEngine` resolver path. Not the complete retail collision system. | Tests ✓ |
|
||||
| B.2 | Player movement mode — Tab-toggled WASD ground walking, walk/run/idle animations, third-person chase camera, MoveToState + AutonomousPosition outbound, portal entry. Outdoor-only MVP. | Live ✓ |
|
||||
| D.1 | 2D ortho overlay + font rendering (StbTrueTypeSharp atlas + TextRenderer + DebugOverlay) | Visual ✓ |
|
||||
|
|
@ -82,7 +83,7 @@ Plus polish that doesn't get its own phase number:
|
|||
- **✓ SHIPPED — A.2 — Frustum culling.** Per-landblock AABB test (Gribb-Hartmann plane extraction + positive-vertex AABB test) in both `TerrainRenderer.Draw` and `StaticMeshRenderer.Draw`. Per-entity culling deferred. LOD deferred to Phase C. Performance overlay in window title shows FPS, frame time, visible/total landblock ratio, entity count, animated count. ~160fps uncapped at 5×5 radius.
|
||||
- **✓ SHIPPED — A.3 — Background net receive thread.** Dedicated daemon thread continuously pulls raw UDP datagrams from the kernel buffer into a `Channel<byte[]>`. Render thread's `Tick()` drains the channel. All decode, fragment assembly, ISAAC crypto, event dispatch, and ack-sending remain on the render thread — minimal change that prevents packet drops during frame stalls. Thread starts after `EnterWorld()` completes; `PumpOnce()` during handshake still reads the socket directly.
|
||||
- **A.4 — Async dat decoding.** Folded into the streaming worker — it's the worker's read path, not a separate subsystem. Called out here because regressions in dat caching could land on this surface.
|
||||
- **A.5 — Two-tier streaming + terrain horizon LOD.** Split `ACDREAM_STREAM_RADIUS` into two: `ACDREAM_TERRAIN_RADIUS` (large, 8-12 cells = 1.5-2.3km) for terrain mesh + `ACDREAM_ENTITY_RADIUS` (small, 2-3 cells, current default) for entities + scenery. Distant landblocks render terrain only — no NPCs, no procedural scenery, no static objects. Tune `SceneLightingUbo`'s `uFogParams` so the far edge fades into sky color (eliminates the hard streaming boundary visible at higher radii). Optional: terrain LOD via mesh decimation for very distant chunks (combine 2×2 landblocks into one decimated mesh; cribs from `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TerrainRenderManager.cs`). Motivation: at radius=5 today, perf scales from ~810 fps → ~200-300 fps because everything stays full-detail; both retail and WorldBuilder render terrain way out and strip entities/scenery at distance. Enables WB-style horizon visibility. **Estimate: 3-5 days for the radius split + fog tuning; +1 week if terrain LOD is included.** Not yet brainstormed.
|
||||
- **✓ SHIPPED — A.5 — Two-tier streaming + horizon LOD.** Shipped 2026-05-10. See shipped table above for full description. Plan archived at `docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md`.
|
||||
|
||||
**Acceptance:**
|
||||
- Walk across 10+ landblocks in any direction, no crashes, no empty voids.
|
||||
|
|
@ -665,7 +666,7 @@ for our deletions/additions; merge upstream `master` periodically.
|
|||
manifest at higher radius. Spec acceptance criterion #5 was wrong;
|
||||
amended via `docs/plans/2026-05-09-phase-n5b-perf-baseline.md`. Plan
|
||||
archived at `docs/superpowers/plans/2026-05-09-phase-n5b-terrain-modern.md`.
|
||||
- **N.6 — Perf polish.** **Currently in flight.**
|
||||
- **N.6 — Perf polish.** **Planned (post-A.5 polish takes priority).**
|
||||
Builds on N.5 + N.5b. Legacy renderer retirement was pulled forward
|
||||
into N.5 ship amendment — `InstancedMeshRenderer`, `StaticMeshRenderer`,
|
||||
`WbFoundationFlag` are gone — and the terrain legacy renderer
|
||||
|
|
@ -676,8 +677,8 @@ for our deletions/additions; merge upstream `master` periodically.
|
|||
is a candidate), GPU-side culling via compute pre-pass (eliminates
|
||||
the per-frame slot walk + DEIC build entirely), GL_TIME_ELAPSED query
|
||||
double-buffering (deferred from N.5 — diagnostic shows `gpu_us=0/0`
|
||||
under `ACDREAM_WB_DIAG=1`), direct higher-radius perf comparison once
|
||||
A.5 lands (where modern's architectural wins manifest), retire the
|
||||
under `ACDREAM_WB_DIAG=1`), direct higher-radius perf comparison (A.5
|
||||
has now landed — modern's architectural wins are measurable), retire the
|
||||
legacy `Texture2D`/`sampler2D` path in `TextureCache` (currently kept
|
||||
for Sky + Debug + particle paths now that Terrain has migrated).
|
||||
Plan + spec written when work begins. **Estimate: 1-2 weeks.**
|
||||
|
|
|
|||
195
docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md
Normal file
195
docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
# Performance Tiers 2 + 3 — Future Roadmap
|
||||
|
||||
**Created:** 2026-05-10 during Phase A.5 polish.
|
||||
**Status:** Future planning — not for current execution.
|
||||
**Context:** A.5 shipped two-tier streaming with the entity dispatcher landing at ~3.5ms median (post-Bug-A and Bug-B fixes). Tier 1 (entity-classification cache) lands as A.5 polish and brings the dispatcher inside the 2.0ms spec budget. Tiers 2 + 3 are the "next big perf wins" beyond Tier 1.
|
||||
|
||||
---
|
||||
|
||||
## Background — why this exists
|
||||
|
||||
Discussion captured 2026-05-10: user observed 200-240 FPS at radius=12 on a Radeon 9070 XT @ 1440p and asked why an "old game like AC" doesn't deliver Unreal-level (1000+ FPS) on this hardware.
|
||||
|
||||
The honest answer: the bottleneck is *architectural*, not hardware. The CPU is single-threaded and rebuilds the entire draw plan from scratch every frame. Modern engines pre-bake static-world batches at content-cook time and rebuild only what changes.
|
||||
|
||||
AC's design — server-spawned per-entity world streamed at runtime — doesn't naturally batch the way Unreal's pre-cooked content does. Closing the gap requires backporting modern techniques while preserving AC's data model. Tiers 2 and 3 are that backporting work.
|
||||
|
||||
---
|
||||
|
||||
## Tier 2 — Static/dynamic split with persistent groups
|
||||
|
||||
**Estimated effort:** ~10-15 days (2-week phase).
|
||||
**Estimated win:** entity dispatcher ~3.5ms → **~0.5-1ms median** at radius=12.
|
||||
**Total frame time:** ~4-5ms → **~2-3ms = 400-600 FPS at standstill.**
|
||||
|
||||
### The core idea
|
||||
|
||||
Today, `WbDrawDispatcher._groups` (the dictionary of "(mesh + texture + blend) → list of instances to draw") is cleared and rebuilt from scratch every frame.
|
||||
|
||||
For trees, rocks, buildings, and other static entities (~95% of the world), the answer is identical every frame forever. Tier 2 makes the static-group instance buffers **persistent GPU-resident data**, just like Unreal's pre-baked world. The CPU only orchestrates "which groups are visible" per frame.
|
||||
|
||||
### Architectural shift
|
||||
|
||||
```csharp
|
||||
class StaticInstancedGroup
|
||||
{
|
||||
public GroupKey Key;
|
||||
public Matrix4x4[] Matrices; // grown as entities spawn
|
||||
public BitArray ActiveSlots; // for free-list reuse
|
||||
public bool NeedsGpuUpload; // dirty flag for delta upload
|
||||
public Dictionary<uint, int> EntityToSlot; // for despawn lookup
|
||||
public uint InstanceBufferOffset; // start of group's slice in global SSBO
|
||||
}
|
||||
```
|
||||
|
||||
**On entity spawn (atlas-tier static):** allocate a slot in each relevant group, write the matrix, mark dirty.
|
||||
|
||||
**On entity despawn:** free the slot, mark dirty.
|
||||
|
||||
**Per frame:**
|
||||
- Static groups: LB-cull each group (cheap). For visible groups, flag for draw. **No matrix copy. No list rebuild.**
|
||||
- Dynamic entities (~50 NPCs/players): today's per-frame walk-and-classify. Keeps the existing slow path for things that legitimately change every frame.
|
||||
- Upload only the dirty groups' matrix slices (delta upload, not full reupload).
|
||||
- Issue 2 multi-draw-indirect calls.
|
||||
|
||||
### Sub-decisions
|
||||
|
||||
**Frustum cull granularity at the group level:** at group level you can't reject individual instances; you draw the whole group or none of it. Two strategies:
|
||||
|
||||
- **Per-LB subgroups:** split each group into per-landblock subgroups. LB-frustum-culls reject subgroups whose LB is invisible. ~2K groups × ~5 LBs per group on average = ~10K subgroups. Each subgroup AABB cull is ~0.3 µs → ~3 ms per frame. Roughly a wash with today's per-entity cull.
|
||||
- **Per-instance GPU cull (Tier 3):** compute pre-pass on the GPU writes which instances are visible to a draw-indirect buffer. ~0.05ms CPU. The right long-term answer.
|
||||
|
||||
For Tier 2 alone, per-LB subgroups are the recommended approach — keep CPU culling, just at coarser granularity than per-entity.
|
||||
|
||||
**Dynamic entities crossing LB boundaries:** when an NPC walks across a landblock boundary, it stays in the same group key but its "spatial bucket" changes. Solution: dynamic entities are tracked in a single global "dynamic group" outside the per-LB structure; they don't need spatial bucketing because there are only ~50 of them.
|
||||
|
||||
**Palette override invalidation:** server event swaps an NPC's clothing color → group key changes. Treat as despawn-from-old + spawn-into-new. NPCs are dynamic so this just rebuckets them.
|
||||
|
||||
**Animation overrides on static entities:** static entities don't animate. Trees don't bend (foliage wave is a vertex shader effect, not a group-key change). Buildings don't move. So the static path never invalidates.
|
||||
|
||||
**EnvCell visibility:** dungeon entities are gated by per-cell visibility state. Need to track which group instances are tied to which cell, and during visibility cull, gate per-cell. Keep using existing `ParentCellId` field on WorldEntity.
|
||||
|
||||
**Streaming load/unload integration:** when an LB unloads, all its static entity matrices need to be removed from their groups. Free-list management. Matches existing `LandblockSpawnAdapter` lifecycle.
|
||||
|
||||
### Effort breakdown
|
||||
|
||||
| Task | Days |
|
||||
|---|---|
|
||||
| Design + invariants document | 2 |
|
||||
| Spawn-time slot allocator + free-list | 3 |
|
||||
| Per-frame visibility + dirty-flag delta upload | 2 |
|
||||
| Dynamic entity path (NPCs, projectiles) | 2 |
|
||||
| Invalidation (palette/ObjDesc events) | 2 |
|
||||
| EnvCell visibility integration | 1 |
|
||||
| Streaming load/unload integration | 1 |
|
||||
| Conformance testing | 2-3 |
|
||||
| **Total** | **~10-15 days** |
|
||||
|
||||
### Risks
|
||||
|
||||
- **Slot management bugs** = double-frees or leaks (entities draw at random positions — visible).
|
||||
- **Invalidation bugs** = stale matrices (entity teleports back to spawn point when palette changes).
|
||||
- **Dynamic entity tracking** adds complexity around the static/dynamic boundary.
|
||||
|
||||
### Mitigations
|
||||
|
||||
- **Conformance test:** render a fixed scene through both pipelines, compare draw output. Adds CI infrastructure.
|
||||
- **Per-frame validation in debug:** walk all groups, assert no orphan slots.
|
||||
- **Hash invariant test:** static entities should produce stable group keys frame-over-frame. Add a debug assertion that fires once per frame in Debug builds.
|
||||
|
||||
---
|
||||
|
||||
## Tier 3 — GPU-side culling (compute pre-pass)
|
||||
|
||||
**Estimated effort:** ~1 month (longer phase).
|
||||
**Estimated win:** entity dispatcher ~0.5-1ms (post-Tier-2) → **~0.05ms median.**
|
||||
**Total frame time:** ~2-3ms → **~1.5-2ms = 600-1000+ FPS at standstill.**
|
||||
|
||||
### The core idea
|
||||
|
||||
Today (and after Tier 2), the CPU does per-LB or per-subgroup frustum culling and tells the GPU which groups to draw.
|
||||
|
||||
Tier 3 moves per-instance frustum cull to the GPU via a compute shader pre-pass. The CPU just uploads "here are all 1M instance matrices" once; the GPU compute shader writes which ones are visible to a draw-indirect buffer; the rasterizer draws only those.
|
||||
|
||||
This is the level Unreal is at. With this, per-frame CPU work for the entity dispatcher becomes essentially "tell the GPU what to do" + a tiny scratch upload.
|
||||
|
||||
### Why Tier 3 needs Tier 2 first
|
||||
|
||||
Without Tier 2's persistent group structure, GPU culling has nothing stable to operate on. The compute shader needs an addressable "here are the static instances" buffer to read from; that buffer only exists after Tier 2.
|
||||
|
||||
### Sub-decisions to be made
|
||||
|
||||
**Compute shader API:** OpenGL 4.3+ compute shaders are sufficient. We're already at GL 4.3+ for bindless. No additional capability requirement.
|
||||
|
||||
**Indirect draw command generation:** the compute shader writes a `DrawElementsIndirectCommand[]` buffer per pass. Render thread issues `glMultiDrawElementsIndirect` reading from that buffer. No CPU readback.
|
||||
|
||||
**LOD selection:** opportunity to add per-instance LOD selection in the compute shader (distance-based mesh detail). Not needed for A.5's scope; could be a Tier 4 follow-up.
|
||||
|
||||
**Per-light shadow map culling:** if shadows ship, GPU culling extends naturally to per-light frustum cull. Significant win for shadow rendering.
|
||||
|
||||
### Effort breakdown
|
||||
|
||||
| Task | Days |
|
||||
|---|---|
|
||||
| Compute shader design + GLSL implementation | 4 |
|
||||
| Buffer layout coordination with Tier 2 | 2 |
|
||||
| Silk.NET compute dispatch integration | 3 |
|
||||
| Indirect command compaction logic | 4 |
|
||||
| LOD selection (optional, ~stretch) | 4 |
|
||||
| Validation: per-instance cull matches CPU cull within epsilon | 3 |
|
||||
| Conformance + regression testing | 5 |
|
||||
| **Total** | **~21-25 days, ~1 month** |
|
||||
|
||||
### Risks
|
||||
|
||||
- **GPU stalls** if the compute shader takes longer than expected (esp. on lower-end GPUs).
|
||||
- **Sync overhead** between compute pre-pass and rasterizer pass.
|
||||
- **Debugging difficulty** — GPU compute bugs are harder to diagnose than CPU bugs.
|
||||
|
||||
### Mitigations
|
||||
|
||||
- **Profile-driven design:** measure compute shader runtime on target hardware before committing.
|
||||
- **Fallback path:** keep CPU cull as a runtime-toggleable option (env var) so we can A/B compare.
|
||||
- **GPU debugging tools:** RenderDoc captures + frame-by-frame compute shader inspection.
|
||||
|
||||
---
|
||||
|
||||
## When to schedule these
|
||||
|
||||
**Tier 2:**
|
||||
- Best fit: dedicated 2-week phase after a SHIP cycle. Treat it like a Phase B/C/N (i.e., name it Phase A.6 or N.7).
|
||||
- Trigger: user wants to push radius beyond 12 (e.g., to 15 or 20 for true continent-scale horizon).
|
||||
- Trigger: user wants to add 100+ active NPCs in a city without dropping below 240Hz.
|
||||
|
||||
**Tier 3:**
|
||||
- Best fit: after Tier 2 has been live and stable for at least one cycle.
|
||||
- Trigger: shadow map work begins (GPU cull + shadow cull share the same compute pre-pass infrastructure).
|
||||
- Trigger: user wants 500+ FPS sustained for very-high-refresh scenarios (360Hz monitors, future hardware).
|
||||
|
||||
**Both:**
|
||||
- Don't bundle with other phases. These are dedicated perf phases with their own brainstorm + spec + plan + SHIP cycles.
|
||||
|
||||
---
|
||||
|
||||
## What's "free" or smaller (out of Tier 1/2/3 scope but worth noting)
|
||||
|
||||
- **Plumb `JobKind` properly through `BuildLandblockForStreaming`** (~30 min). Today's Bug A patch wastes worker-thread CPU on hydration that gets thrown away for far-tier. Cleaner code, slight CPU savings on worker.
|
||||
- **Eliminate `ToEntries` adapter allocation in `Draw`** (~15 min). Tiny win (~25 KB / frame). Could fold into Tier 1.
|
||||
- **Persistent-mapped indirect buffer** (~2 days). Today's `glBufferData` per frame becomes a pre-mapped persistent buffer. Marginal win on RDNA 4; meaningful on lower-end GPUs.
|
||||
- **Multi-thread mesh-build worker pool** (~1 day). 2.7s first-traversal horizon-fill drops to 0.7s with 4 workers. UX win on first walk-into-region.
|
||||
|
||||
These are good candidates for a "perf polish" mini-phase or to backfill into Tier 2.
|
||||
|
||||
---
|
||||
|
||||
## The architectural ceiling
|
||||
|
||||
Even with all three tiers, **a faithful AC client written in C# with bindless OpenGL tops out around 800-1500 FPS at radius=12 on RDNA 4 hardware**. Beyond that requires:
|
||||
|
||||
- Native C++ rendering core (eliminate .NET GC + JIT overhead)
|
||||
- DX12/Vulkan API (eliminate driver state validation)
|
||||
- Offline content cooking (eliminate runtime mesh/texture decode)
|
||||
|
||||
Each of those is a several-month undertaking and represents "becoming a different engine." The realistic target for acdream is 240-500 FPS at the user's monitor refresh, comfortably ahead of the visible-stutter threshold. Tier 1 + Tier 2 alone should deliver that for radius=12-15.
|
||||
|
||||
For "Unreal-level FPS at full quality," that's a different project.
|
||||
2525
docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md
Normal file
2525
docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,829 @@
|
|||
# Phase A.5 — Two-tier Streaming + Horizon LOD — Design
|
||||
|
||||
**Created:** 2026-05-09 (immediately after N.5b ship + brainstorm).
|
||||
**Status:** Spec — awaiting user review before plan-writing.
|
||||
**Branch:** `claude/hopeful-darwin-ae8b87` (worktree under `.claude/worktrees/hopeful-darwin-ae8b87`).
|
||||
**Predecessor:** Phase N.5b SHIP at `08b7362`. A.5 handoff at `f7f8867`.
|
||||
|
||||
---
|
||||
|
||||
## 1. Goal
|
||||
|
||||
Scale acdream's visible reach from radius=5 (~1 km) to radius=12 (~2.3 km horizon)
|
||||
while sustaining 240 FPS at standstill on a 240 Hz / 1440p monitor.
|
||||
|
||||
Delivered through:
|
||||
1. Two-tier streaming (near = full detail, far = terrain only).
|
||||
2. Tightening the existing per-LB entity dispatcher walk.
|
||||
3. Off-thread mesh build (single worker).
|
||||
4. Fog blend at the near-tier boundary to mask the scenery cutoff.
|
||||
5. Three nearly-free visual quality wins (terrain mipmaps + anisotropic, A2C
|
||||
with MSAA on foliage, depth-write audit).
|
||||
|
||||
The headline win: walking around Holtburg, the user sees a real horizon
|
||||
(2.3 km of visible terrain) without the client falling off a perf cliff.
|
||||
|
||||
**User goal verbatim (2026-05-09):**
|
||||
> "I just want great smooth HIGH fps visuals. Should look great. As long as
|
||||
> it scales and we get very high FPS"
|
||||
|
||||
---
|
||||
|
||||
## 2. Hardware target + acceptance metrics
|
||||
|
||||
### Target hardware
|
||||
|
||||
- AMD Radeon RX 9070 XT (RDNA 4, ~December 2025).
|
||||
- 240 Hz @ 2560×1440 (verified via `Get-CimInstance Win32_VideoController`).
|
||||
- Frame budget: **4.166 ms** at vsync.
|
||||
|
||||
### Acceptance metrics (as shipped — revised with Quality Preset system)
|
||||
|
||||
1. **Build green; existing tests still green.** N.5b conformance sentinel
|
||||
passes (visual mesh Z = TerrainSurface.SampleZ within 1 mm).
|
||||
2. **Standstill at user's selected preset on user's hardware:**
|
||||
- 95% of frames hit ≤ (1000ms / monitor refresh rate).
|
||||
- No absolute FPS number is required — the Quality Preset system (§4.10)
|
||||
is the user's knob for trading quality vs frame budget.
|
||||
3. **Walking at user's selected preset:**
|
||||
- 95% of frames hit ≤ 1.5× (1000ms / monitor refresh rate).
|
||||
4. **First traversal into virgin region (cold mesh cache):**
|
||||
- Render thread frame time stays within 2× the standstill budget while
|
||||
the worker fills the far-tier horizon (~2.7 s of "horizon filling in" is OK).
|
||||
5. **Visual gate (user-driven, same on all presets):** user launches the
|
||||
client, walks Holtburg → North Yanshi, and confirms:
|
||||
- Horizon visible at ~2.3 km.
|
||||
- Fog blend at N₁ smooths the scenery boundary (no harsh cliff).
|
||||
- Distant terrain does not shimmer (mipmaps work).
|
||||
- Tree edges are smooth (A2C works).
|
||||
- No new z-fighting / depth artifacts (depth-write audit).
|
||||
6. **Per-subsystem regression budgets** (added to `[WB-DIAG]` /
|
||||
`[TERRAIN-DIAG]` output):
|
||||
- Entity dispatcher cpu_us median ≤ **2.0 ms** at standstill.
|
||||
- Terrain dispatcher cpu_us median ≤ **1.0 ms** at standstill (all 625 LBs).
|
||||
7. **N.5b sentinel intact:** TerrainSlot, TerrainModernConformance, Wb*,
|
||||
MatrixComposition, TextureCacheBindless, SplitFormulaDivergence — all
|
||||
pass clean.
|
||||
8. **SHIP record + perf baseline doc + memory entry** mirroring N.5b's pattern.
|
||||
|
||||
A failure on (5) is a SHIP-blocker. A failure on (3) walking-FPS criterion
|
||||
escalates to "fix or document the tradeoff and ship N.6 next" — not a
|
||||
direct blocker but pushes the gate to user discretion.
|
||||
|
||||
---
|
||||
|
||||
## 3. Two-tier streaming model
|
||||
|
||||
### Tier definitions
|
||||
|
||||
| Tier | Radius | LB count | Loads | GPU mem |
|
||||
|---|---|---|---|---|
|
||||
| **Near** (N₁ = 4) | 9×9 = 81 LBs | terrain mesh + LandBlockInfo (stabs/buildings) + scenery generation + EnvCells + collision data + entity registration with WB dispatcher | scenery instance buffers + per-entity textures (depends on PaletteOverrides) |
|
||||
| **Far** (N₂ = 12) | 25×25 - 9×9 = 544 LBs | terrain mesh ONLY (LandBlock heightmap + atlas blend) | ~14 MB shared atlas slots |
|
||||
| **Total** | 25×25 = 625 LBs | combined | ~30 MB total estimated |
|
||||
|
||||
### Hysteresis (Q7 Option A — match existing radius+2 convention)
|
||||
|
||||
- **Near-tier:** entity load at distance 4, demote (entity unload) at distance 6.
|
||||
- **Far-tier:** terrain load at distance 12, terrain unload at distance 14.
|
||||
|
||||
Both boundaries get the same 2-LB buffer. Phase A.1's existing hysteresis
|
||||
mechanism in `StreamingRegion.RecenterTo` is the reference pattern; A.5
|
||||
extends it from one radius to two.
|
||||
|
||||
### Tier transitions
|
||||
|
||||
| Transition | Trigger | Action |
|
||||
|---|---|---|
|
||||
| `null → far` | LB enters far window from outside | Worker reads LandBlock heightmap, builds mesh, posts `LandblockStreamResult.Loaded { Tier = Far }`. Render thread adds slot in `TerrainModernRenderer`. No entity work. |
|
||||
| `null → near` | LB jumps null → near in one tick (first-tick bootstrap; teleport into virgin region) | Worker reads LandBlock heightmap + `LandBlockInfo`, generates scenery, builds entity list, builds mesh. Posts `LandblockStreamResult.Loaded { Tier = Near }`. Render thread adds terrain slot AND merges entities. |
|
||||
| `far → near` | LB enters near window from far-resident | Worker reads `LandBlockInfo`, generates scenery, builds entity list. Posts `LandblockStreamResult.Promoted`. Render thread merges entities into `GpuWorldState` for the existing LB (terrain already loaded). |
|
||||
| `near → far` | LB leaves near window past hysteresis (distance > 6) | Render thread drops the LB's entities from `GpuWorldState` (which fires `_wbSpawnAdapter.OnLandblockUnloaded`). Terrain stays. |
|
||||
| `far → null` | LB leaves far window past hysteresis (distance > 14) | Render thread removes the terrain slot from `TerrainModernRenderer`. |
|
||||
|
||||
The order matters: when a player walks outward, the same LB goes
|
||||
`near → far → null` over time. Each transition is one event per LB per
|
||||
crossing.
|
||||
|
||||
### Why the player crossing the N₁ boundary works
|
||||
|
||||
The player is always at radius=0 from the streaming center (the streaming
|
||||
center IS the player). The boundary effects are about LBs at the edge of N₁
|
||||
crossing inward/outward as the player moves. Server-spawned NPCs are
|
||||
delivered by ACE's broadcast (radius typically 5-7 LBs ≥ N₁), so when an
|
||||
LB promotes back to near, ACE will already have its NPCs broadcast or
|
||||
re-broadcast as the player moves through. Dat-static entities (stabs,
|
||||
buildings) are reloaded from `LandBlockInfo` on promotion. Scenery is
|
||||
re-generated from the deterministic seed at the same time.
|
||||
|
||||
---
|
||||
|
||||
## 4. Component-by-component design
|
||||
|
||||
### 4.1 `LandblockStreamTier` — new enum
|
||||
|
||||
```csharp
|
||||
namespace AcDream.App.Streaming;
|
||||
|
||||
public enum LandblockStreamTier
|
||||
{
|
||||
Far, // terrain only
|
||||
Near, // full detail (terrain + entities + scenery + EnvCells)
|
||||
}
|
||||
```
|
||||
|
||||
### 4.2 `StreamingRegion` — extended to two radii
|
||||
|
||||
```csharp
|
||||
public sealed class StreamingRegion
|
||||
{
|
||||
public int CenterX { get; }
|
||||
public int CenterY { get; }
|
||||
public int NearRadius { get; } // N₁ (default 4)
|
||||
public int FarRadius { get; } // N₂ (default 12)
|
||||
|
||||
public IReadOnlyCollection<uint> NearVisible { get; } // 9×9 window
|
||||
public IReadOnlyCollection<uint> FarVisible { get; } // 25×25 window minus near
|
||||
public IReadOnlyCollection<uint> Resident { get; } // hysteresis-retained
|
||||
|
||||
public TwoTierDiff RecenterTo(int newCx, int newCy);
|
||||
}
|
||||
|
||||
public readonly record struct TwoTierDiff(
|
||||
IReadOnlyList<uint> ToLoadFar, // entered far window from null (need terrain only)
|
||||
IReadOnlyList<uint> ToLoadNear, // entered near window from null (need terrain + entities — first-tick bootstrap, teleport)
|
||||
IReadOnlyList<uint> ToPromote, // entered near window from far-resident (need entities only — terrain already loaded)
|
||||
IReadOnlyList<uint> ToDemote, // exited near window past hysteresis (drop entities)
|
||||
IReadOnlyList<uint> ToUnload); // exited far window past hysteresis (drop terrain)
|
||||
```
|
||||
|
||||
The hysteresis math:
|
||||
- Near-unload threshold: `NearRadius + 2` = 6.
|
||||
- Far-unload threshold: `FarRadius + 2` = 14.
|
||||
|
||||
A landblock is "near-resident" if its distance ≤ 6; "far-resident" if its
|
||||
distance is in (6, 14]. Beyond 14, it unloads entirely.
|
||||
|
||||
### 4.3 `StreamingController` — routes by tier
|
||||
|
||||
```csharp
|
||||
public sealed class StreamingController
|
||||
{
|
||||
public int NearRadius { get; set; } = 4;
|
||||
public int FarRadius { get; set; } = 12;
|
||||
public int MaxCompletionsPerFrame { get; set; } = 4;
|
||||
|
||||
// Action signatures change to carry the tier.
|
||||
private readonly Action<uint, LandblockStreamTier> _enqueueLoad;
|
||||
private readonly Action<uint> _enqueueUnload;
|
||||
// ...
|
||||
|
||||
public void Tick(int observerCx, int observerCy)
|
||||
{
|
||||
// First-tick bootstrap: every near-window LB → ToLoadNear; every
|
||||
// far-window-only LB → ToLoadFar.
|
||||
// Steady-state RecenterTo: produces 5 transition lists.
|
||||
// - ToLoadFar → _enqueueLoad(id, JobKind.LoadFar)
|
||||
// - ToLoadNear → _enqueueLoad(id, JobKind.LoadNear)
|
||||
// - ToPromote → _enqueueLoad(id, JobKind.PromoteToNear)
|
||||
// - ToDemote → _state.RemoveEntities(id) on render thread (no worker job)
|
||||
// - ToUnload → _enqueueUnload(id)
|
||||
// Drain completions and route by result variant.
|
||||
}
|
||||
}
|
||||
|
||||
public enum LandblockStreamJobKind { LoadFar, LoadNear, PromoteToNear }
|
||||
```
|
||||
|
||||
The render thread decides the job kind up-front based on its own knowledge
|
||||
of which LBs are currently terrain-resident; the worker never peeks at
|
||||
render-thread state. Three distinct worker paths:
|
||||
|
||||
- **`LoadFar`:** read `LandBlock` heightmap only. Skip `LandBlockInfo`,
|
||||
skip `LandblockLoader.BuildEntitiesFromInfo`, skip
|
||||
`SceneryGenerator`/`WbSceneryAdapter`. Build `LandblockMesh`. Post
|
||||
`LandblockStreamResult.Loaded(Tier=Far, Entities=[], MeshData=mesh)`.
|
||||
- **`LoadNear`:** read `LandBlock` + `LandBlockInfo` + scenery generation
|
||||
+ build mesh. Post `LandblockStreamResult.Loaded(Tier=Near, Entities=...,
|
||||
MeshData=mesh)`. Used for first-tick bootstrap of the inner ring and
|
||||
for the rare null→Near jump (teleport into virgin region).
|
||||
- **`PromoteToNear`:** read `LandBlockInfo` + scenery generation only.
|
||||
Skip `LandBlock` heightmap (mesh already on GPU). Skip
|
||||
`LandblockMesh.Build`. Post `LandblockStreamResult.Promoted(id, entities)`.
|
||||
|
||||
### 4.4 `LandblockStreamResult` — new variants
|
||||
|
||||
```csharp
|
||||
public abstract record LandblockStreamResult
|
||||
{
|
||||
public sealed record Loaded(
|
||||
uint LandblockId,
|
||||
LandblockStreamTier Tier,
|
||||
LandBlock Heightmap,
|
||||
IReadOnlyList<WorldEntity> Entities, // empty for Far
|
||||
LandblockMeshData MeshData // built off-thread
|
||||
) : LandblockStreamResult;
|
||||
|
||||
public sealed record Promoted(
|
||||
uint LandblockId,
|
||||
IReadOnlyList<WorldEntity> Entities // entity layer for an already-loaded far-tier LB
|
||||
) : LandblockStreamResult;
|
||||
|
||||
// Existing:
|
||||
public sealed record Unloaded(uint LandblockId) : LandblockStreamResult;
|
||||
public sealed record Failed(uint LandblockId, string Error) : LandblockStreamResult;
|
||||
public sealed record WorkerCrashed(string Error) : LandblockStreamResult;
|
||||
}
|
||||
```
|
||||
|
||||
`Loaded` carries `MeshData` — the mesh is built on the worker thread, NOT
|
||||
in `_applyTerrain` on the render thread. `Promoted` only carries entities;
|
||||
the mesh is already in `TerrainModernRenderer`.
|
||||
|
||||
### 4.5 `LandblockStreamer` — single worker, mesh-build on-worker
|
||||
|
||||
Existing `LandblockStreamer` (today on a single background thread) gets
|
||||
extended to:
|
||||
|
||||
1. Read dat as today (`DatCollection.Get<LandBlock>` etc.).
|
||||
2. Build `LandblockMesh` on the same thread:
|
||||
```csharp
|
||||
var meshData = LandblockMesh.Build(
|
||||
block, lbX, lbY, heightTable, _ctx, _surfaceCache);
|
||||
```
|
||||
3. Post `LandblockStreamResult.Loaded(... MeshData = meshData)` to the
|
||||
completion queue.
|
||||
|
||||
Thread-safety implications:
|
||||
- `_ctx` (TerrainBlendingContext) is read-only after init — no change.
|
||||
- `_surfaceCache`: today a plain `Dictionary<uint, SurfaceInfo>`,
|
||||
populated lazily by `LandblockMesh.Build`. Currently safe because
|
||||
Build runs on the render thread; A.5 moves Build to the worker, so
|
||||
the cache must be thread-safe. **Swap to
|
||||
`ConcurrentDictionary<uint, SurfaceInfo>`** with `GetOrAdd` for the
|
||||
populate path. The factory inside `GetOrAdd` may run twice for the
|
||||
same key under contention (acceptable — the result is deterministic).
|
||||
|
||||
### 4.6 `WbDrawDispatcher` — entity bucketing tightening (Q5 Option A)
|
||||
|
||||
Three targeted changes inside the existing `Draw` flow:
|
||||
|
||||
#### Change 1: Animated-entity walk fix
|
||||
|
||||
Today (at lines 197-204 of `WbDrawDispatcher.cs`):
|
||||
|
||||
```csharp
|
||||
foreach (var entry in landblockEntries) {
|
||||
bool landblockVisible = ...;
|
||||
if (!landblockVisible && (animatedEntityIds is null || animatedEntityIds.Count == 0))
|
||||
continue;
|
||||
|
||||
foreach (var entity in entry.Entities) {
|
||||
...
|
||||
if (!landblockVisible && !isAnimated) continue;
|
||||
```
|
||||
|
||||
The `if (!landblockVisible && ...) continue;` only skips if there are NO
|
||||
animated entities. When `animatedEntityIds` is non-empty, the inner loop
|
||||
walks every entity in the invisible LB just to find the few animated
|
||||
ones. With ~10.7K entities at N₁=4, this is wasted iteration.
|
||||
|
||||
**Fix:** when an LB is invisible, iterate `animatedEntityIds` directly
|
||||
and look each up in a per-LB `Dictionary<uint, WorldEntity>` map (added
|
||||
to `LoadedLandblock` or kept in a parallel structure).
|
||||
|
||||
```csharp
|
||||
foreach (var entry in landblockEntries) {
|
||||
bool landblockVisible = ...;
|
||||
if (!landblockVisible) {
|
||||
if (animatedEntityIds is null || animatedEntityIds.Count == 0) continue;
|
||||
// Walk only animated entities in this invisible LB.
|
||||
foreach (var animatedId in animatedEntityIds) {
|
||||
if (!entry.AnimatedById.TryGetValue(animatedId, out var entity)) continue;
|
||||
// ... draw the entity
|
||||
}
|
||||
continue;
|
||||
}
|
||||
foreach (var entity in entry.Entities) { ... }
|
||||
}
|
||||
```
|
||||
|
||||
#### Change 2: Per-entity AABB cache at register time
|
||||
|
||||
Today: `Draw` recomputes `aMin = position - 5`, `aMax = position + 5` per
|
||||
entity per frame. Cheap individually, but ~16K × per frame = measurable.
|
||||
|
||||
**Fix:** add `Vector3 AabbMin, AabbMax` fields to `WorldEntity` (or a
|
||||
parallel struct keyed by entity id). Populate at `EntitySpawnAdapter.OnCreate`
|
||||
(server-spawned) and `LandblockLoader.BuildEntitiesFromInfo` (dat-static)
|
||||
time. Static entities never invalidate. Dynamic entities (NPCs, players)
|
||||
update on position change — add `WorldEntity.PositionDirty` flag set by
|
||||
the live position update path; AABB recompute happens lazily on first
|
||||
read after dirty.
|
||||
|
||||
The AABB radius today is hard-coded `PerEntityCullRadius = 5.0f` — keep
|
||||
that as a per-mesh-bucket fallback; future improvement is to compute the
|
||||
real AABB from the mesh, but defer that to a later phase (it's a
|
||||
cross-cutting change).
|
||||
|
||||
#### Change 3: 4×4 sub-LB cell cull for partially-visible LBs
|
||||
|
||||
When an LB is fully visible (its AABB entirely inside the frustum), all
|
||||
its entities are drawn — no per-entity cull needed. Today's per-entity
|
||||
cull is wasted work in this case.
|
||||
|
||||
When an LB is partially visible, today's per-entity cull is the right
|
||||
work — but it walks all ~132 entities. Cheap with the AABB-cache fix
|
||||
(memory read), so the win here is small. Worth doing only if the cache
|
||||
fix alone isn't enough to hit the 2.0ms budget.
|
||||
|
||||
**Add only if needed:** bucket each LB's entities into 4×4 sub-cells
|
||||
(each 48 m). Compute a sub-cell AABB at register time. Per frame: for
|
||||
partially-visible LBs, cull at sub-cell granularity first; walk
|
||||
entities only inside surviving sub-cells.
|
||||
|
||||
Ship change #1 and #2 unconditionally; ship #3 only if the budget
|
||||
isn't hit by #1 + #2.
|
||||
|
||||
### 4.7 `TerrainModernRenderer` — no structural change
|
||||
|
||||
The slot allocator (`TerrainSlotAllocator`) already grows by power-of-two
|
||||
doubling. At N₂=12 worst case, ~961 slots × ~15 KB per slot = ~14 MB.
|
||||
Allocator handles it without modification.
|
||||
|
||||
Per-LB frustum cull stays per-slot — at ~961 slots × ~0.3 µs/AABB-test
|
||||
the worst-case cull pass is ~0.3 ms. Acceptable inside the 1.0 ms terrain
|
||||
dispatcher budget.
|
||||
|
||||
The DEIC (`DrawElementsIndirectCommand`) array grows accordingly. The
|
||||
existing per-frame `BufferSubData` upload absorbs a 961-entry array
|
||||
without issue (~19 KB).
|
||||
|
||||
### 4.8 Fog tuning (`SceneLightingUbo`)
|
||||
|
||||
Existing fields (Phase G.1+):
|
||||
- `FogStart` — distance at which fog begins (today: somewhere outside the
|
||||
visible terrain range).
|
||||
- `FogEnd` — distance at which fog reaches full opacity.
|
||||
- `FogColor` — sourced from current sky state.
|
||||
|
||||
A.5 change: dynamically tune `FogStart` and `FogEnd` based on the
|
||||
current N₁/N₂:
|
||||
|
||||
- `FogStart = N₁ × LandblockSize × 0.7` ≈ `4 × 192 × 0.7` = **~538 m**.
|
||||
- `FogEnd = N₂ × LandblockSize × 0.95` ≈ `12 × 192 × 0.95` = **~2188 m**.
|
||||
|
||||
The fog color matches the current sky color (already provided by
|
||||
`SkyStateProvider`) — at the far horizon, fog blends terrain into
|
||||
sky, hiding the N₂ edge.
|
||||
|
||||
The 0.7 / 0.95 multipliers are tuning knobs. Iterate during user gate.
|
||||
**Expose as env vars during development** (`ACDREAM_FOG_START_MULT`,
|
||||
`ACDREAM_FOG_END_MULT`) to allow fast iteration without a recompile.
|
||||
|
||||
### 4.9 Visual quality wins (Q8 Option C — all three)
|
||||
|
||||
#### 4.9.1 Mipmaps + 16x anisotropic on `TerrainAtlas`
|
||||
|
||||
Today: `TerrainAtlas.Upload` uses `GL_LINEAR` minification, no mipmaps.
|
||||
|
||||
A.5 change: after upload, call `glGenerateMipmap(GL_TEXTURE_2D_ARRAY)`.
|
||||
Sampler state: `GL_LINEAR_MIPMAP_LINEAR` (trilinear) +
|
||||
`GL_TEXTURE_MAX_ANISOTROPY = 16`.
|
||||
|
||||
Affects only `TerrainAtlas`. Mesh atlas (entity textures) and other
|
||||
texture caches stay as-is.
|
||||
|
||||
Verification: at N₂=12, walk to a vantage point looking at terrain at
|
||||
range 2 km. With the fix, no shimmer. Without, "moving sparkles" visible
|
||||
at distance.
|
||||
|
||||
#### 4.9.2 Alpha-to-coverage with MSAA on foliage
|
||||
|
||||
Today: `mesh_modern.frag` uses `if (alpha < cutoff) discard;` for ClipMap
|
||||
translucency. Produces hard, pixel-edged tree silhouettes.
|
||||
|
||||
A.5 change:
|
||||
- Enable MSAA 4x on the GL render target (window framebuffer).
|
||||
- In `mesh_modern.frag`, for ClipMap pass: write
|
||||
`gl_SampleMask[0]` based on alpha threshold instead of binary discard.
|
||||
|
||||
Risk: MSAA framebuffer interaction with sky / particles / UI overlay.
|
||||
Audit:
|
||||
- `SkyRenderer` — clears its own framebuffer? If so, must clear the MSAA
|
||||
attachment instead. Investigate.
|
||||
- `ParticleRenderer` — billboards already use alpha-blend; MSAA-friendly.
|
||||
- ImGui overlay — drawn after the 3D pass; must not interact with MSAA
|
||||
resolve.
|
||||
|
||||
If the audit finds blocking issues, ship 4.9.1 + 4.9.3 only and defer
|
||||
4.9.2 to a later phase. Document the result either way.
|
||||
|
||||
#### 4.9.3 Depth-write audit on translucent batches
|
||||
|
||||
Walk all translucent batch paths in `WbDrawDispatcher.Draw` and verify:
|
||||
- Alpha-blend (`AlphaBlend`, `Additive`, `InvAlpha`): `glDepthMask(false)`.
|
||||
- Clip-map (binary alpha): `glDepthMask(true)` (foliage casts depth).
|
||||
- Opaque: `glDepthMask(true)`.
|
||||
|
||||
Today's code at lines 401-433 sets `DepthMask(true)` for opaque,
|
||||
`DepthMask(false)` for transparent. Confirm ClipMap is in the opaque
|
||||
pass (it is, per `IsOpaque` returning true for ClipMap at line 738).
|
||||
|
||||
If audit finds nothing wrong, ship a comment + a unit test that locks in
|
||||
the partition. Cheap insurance against future regression.
|
||||
|
||||
### 4.10 Quality Preset System (T22.5 — added mid-execution)
|
||||
|
||||
**Background:** Added between T22 (fog wiring) and T23 (DIAG budgets) at
|
||||
user's direction. The original spec had no preset concept; §2 was written
|
||||
against absolute 240 FPS on fixed N₁/N₂. T22.5 makes both radii and every
|
||||
quality knob user-controllable via a single enum. §2 was amended above to
|
||||
reflect the per-preset, refresh-rate-relative acceptance criteria.
|
||||
|
||||
#### Schema
|
||||
|
||||
```csharp
|
||||
public enum QualityPreset { Low, Medium, High, Ultra }
|
||||
|
||||
public readonly record struct QualitySettings(
|
||||
int NearRadius,
|
||||
int FarRadius,
|
||||
int MsaaSamples,
|
||||
int AnisotropicLevel,
|
||||
bool AlphaToCoverage,
|
||||
int MaxCompletionsPerFrame);
|
||||
```
|
||||
|
||||
`QualitySettings.From(preset)` returns the canonical values:
|
||||
|
||||
| Preset | NearRadius | FarRadius | MsaaSamples | AnisotropicLevel | AlphaToCoverage | MaxCompletionsPerFrame |
|
||||
|---|---|---|---|---|---|---|
|
||||
| Low | 2 | 5 | 0 | 4 | false | 2 |
|
||||
| Medium | 3 | 8 | 2 | 8 | false | 3 |
|
||||
| High | 4 | 12 | 4 | 16 | true | 4 |
|
||||
| Ultra | 5 | 15 | 4 | 16 | true | 6 |
|
||||
|
||||
`QualitySettings.WithEnvOverrides(baseSettings)` applies per-field env-var
|
||||
overrides (see §4.10.3).
|
||||
|
||||
#### Persistence and UI
|
||||
|
||||
`DisplaySettings.Quality` (type `QualityPreset`) persists via the existing
|
||||
`settings.json` infrastructure (Phase L.0). The Settings panel (F11) exposes
|
||||
a Quality dropdown in its Display tab (`SettingsPanel.RenderDisplayTab`).
|
||||
|
||||
#### Wiring (GameWindow.OnLoad + ReapplyQualityPreset)
|
||||
|
||||
1. `GameWindow.OnLoad` resolves the active `QualitySettings`:
|
||||
`QualitySettings.From(displaySettings.Quality).WithEnvOverrides(...)`.
|
||||
2. `StreamingController` and `LandblockStreamer` are built with the preset's
|
||||
`NearRadius` / `FarRadius`.
|
||||
3. `TerrainAtlas.SetAnisotropic(settings.AnisotropicLevel)` called once at
|
||||
load and again on reapply.
|
||||
4. `WindowOptions.Samples = settings.MsaaSamples` applied at window creation
|
||||
time only (MSAA mid-session change is structurally unsupported by OpenGL).
|
||||
5. `WbDrawDispatcher.AlphaToCoverage = settings.AlphaToCoverage`.
|
||||
6. `StreamingController.MaxCompletionsPerFrame = settings.MaxCompletionsPerFrame`.
|
||||
|
||||
Mid-session quality change (F11 dropdown change → Save):
|
||||
|
||||
- `GameWindow.ReapplyQualityPreset` rebuilds `StreamingController` +
|
||||
`LandblockStreamer` with the new radii, re-applies anisotropic and
|
||||
AlphaToCoverage.
|
||||
- If `MsaaSamples` changed, logs a warning that MSAA sample count cannot be
|
||||
changed mid-session; requires restart.
|
||||
|
||||
#### Env-var overrides (§4.10.3)
|
||||
|
||||
Applied by `QualitySettings.WithEnvOverrides` after the base preset is resolved.
|
||||
Each field has one env var; all are optional. Logged at startup.
|
||||
|
||||
| Env var | Field overridden |
|
||||
|---|---|
|
||||
| `ACDREAM_NEAR_RADIUS` | `NearRadius` |
|
||||
| `ACDREAM_FAR_RADIUS` | `FarRadius` |
|
||||
| `ACDREAM_MSAA_SAMPLES` | `MsaaSamples` |
|
||||
| `ACDREAM_ANISOTROPIC` | `AnisotropicLevel` |
|
||||
| `ACDREAM_A2C` | `AlphaToCoverage` (1/0/true/false) |
|
||||
| `ACDREAM_MAX_COMPLETIONS_PER_FRAME` | `MaxCompletionsPerFrame` |
|
||||
|
||||
#### Tests
|
||||
|
||||
12 tests in `tests/AcDream.UI.Abstractions.Tests/Settings/QualityPresetTests.cs`
|
||||
cover: canonical preset values per enum member; `WithEnvOverrides` no-op when
|
||||
no env vars set; `WithEnvOverrides` each override individually; invalid env-var
|
||||
value falls back to base setting.
|
||||
|
||||
#### Files
|
||||
|
||||
- `src/AcDream.UI.Abstractions/Settings/QualityPreset.cs` — new
|
||||
- `src/AcDream.UI.Abstractions/Settings/DisplaySettings.cs` — `Quality` field added
|
||||
- `src/AcDream.UI.Abstractions/Panels/Settings/SettingsPanel.cs` — Display tab
|
||||
Quality dropdown (`RenderDisplayTab` method)
|
||||
- `src/AcDream.App/Rendering/GameWindow.cs` — `ReapplyQualityPreset`,
|
||||
`OnLoad` preset wiring
|
||||
- `tests/AcDream.UI.Abstractions.Tests/Settings/QualityPresetTests.cs` — new (12 tests)
|
||||
|
||||
#### Out of scope (deferred)
|
||||
|
||||
- Auto-detect preset on first launch (Phase A.6 / N.6.5).
|
||||
- Adaptive runtime preset drop on budget miss.
|
||||
- Per-feature toggles below preset level.
|
||||
|
||||
Commits: `afa4200` (schema + tests), `28d2c60` (wiring).
|
||||
|
||||
---
|
||||
|
||||
## 5. Data flow
|
||||
|
||||
### Per-frame (steady state)
|
||||
|
||||
```
|
||||
GameWindow.OnUpdate(dt)
|
||||
└─ StreamingController.Tick(playerCx, playerCy)
|
||||
├─ region.RecenterTo(...) // produces TwoTierDiff if center changed
|
||||
├─ for each ToLoadFar: _enqueueLoad(id, LoadFar)
|
||||
├─ for each ToLoadNear: _enqueueLoad(id, LoadNear)
|
||||
├─ for each ToPromote: _enqueueLoad(id, PromoteToNear)
|
||||
├─ for each ToDemote: _state.RemoveEntities(id) // on render thread
|
||||
├─ for each ToUnload: _enqueueUnload(id)
|
||||
└─ drainCompletions(MaxCompletionsPerFrame=4)
|
||||
├─ Loaded.Far: _terrain.AddLandblock(meshData); _state.AddLandblock(...)
|
||||
├─ Loaded.Near: _terrain.AddLandblock(meshData); _state.AddLandblock(... entities)
|
||||
├─ Promoted: _state.AddEntitiesToExisting(id, entities)
|
||||
├─ Unloaded: _terrain.RemoveLandblock(id); _state.RemoveLandblock(id)
|
||||
└─ Failed/Crash: log
|
||||
|
||||
GameWindow.OnRender
|
||||
├─ TerrainModernRenderer.Draw(camera, frustum)
|
||||
│ └─ glMultiDrawElementsIndirect across all near + far slots that pass cull
|
||||
└─ WbDrawDispatcher.Draw(camera, gpuWorldState.LandblockEntries, frustum, visibleCellIds, animatedEntityIds)
|
||||
├─ for each LB entry:
|
||||
│ ├─ if invisible: walk only animatedEntityIds (Change #1)
|
||||
│ └─ if visible: walk entities, AABB cache lookup (Change #2)
|
||||
├─ classify into groups, build SSBO, multi-draw indirect
|
||||
└─ flush DIAG every ~5 s
|
||||
```
|
||||
|
||||
### Worker thread
|
||||
|
||||
```
|
||||
LandblockStreamer.WorkerLoop
|
||||
while running:
|
||||
job = jobQueue.dequeue()
|
||||
switch job.Kind:
|
||||
LoadFar:
|
||||
block = dats.Get<LandBlock>(id)
|
||||
meshData = LandblockMesh.Build(block, ..., _surfaceCache)
|
||||
completionQueue.enqueue(Loaded(id, Far, block, [], meshData))
|
||||
LoadNear:
|
||||
block = dats.Get<LandBlock>(id)
|
||||
info = dats.Get<LandBlockInfo>(...)
|
||||
entities = LandblockLoader.BuildEntitiesFromInfo(info)
|
||||
scenery = WbSceneryAdapter.GenerateScenery(block, ...)
|
||||
meshData = LandblockMesh.Build(block, ..., _surfaceCache)
|
||||
completionQueue.enqueue(Loaded(id, Near, block, entities ∪ scenery, meshData))
|
||||
PromoteToNear:
|
||||
info = dats.Get<LandBlockInfo>(...)
|
||||
// Heightmap not re-read; scenery generation needs LandBlock for height
|
||||
// sampling — read it again from disk cache (DatCollection caches the
|
||||
// last-read block; cheap second access) OR pass through from render
|
||||
// thread's terrain-slot snapshot (deferred plan-level decision).
|
||||
block = dats.Get<LandBlock>(id)
|
||||
entities = LandblockLoader.BuildEntitiesFromInfo(info)
|
||||
scenery = WbSceneryAdapter.GenerateScenery(block, ...)
|
||||
completionQueue.enqueue(Promoted(id, entities ∪ scenery))
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. Threading model
|
||||
|
||||
- **Render thread:** drives `StreamingController.Tick`, drains the
|
||||
completion queue, calls `TerrainModernRenderer.AddLandblock` /
|
||||
`RemoveLandblock`, mutates `GpuWorldState`. All GL calls on this thread.
|
||||
- **One streaming worker thread:** dat reads, mesh build, scenery generation.
|
||||
Owns `_surfaceCache` (now `ConcurrentDictionary`) — render thread does
|
||||
not access it directly.
|
||||
- **Network thread:** unchanged from Phase A.3 — drains UDP into the
|
||||
channel; render thread decodes.
|
||||
|
||||
Synchronization:
|
||||
- Job queue: `Channel<LbStreamJob>` (writer = render thread via
|
||||
`_enqueueLoad`; reader = worker).
|
||||
- Completion queue: `ConcurrentQueue<LandblockStreamResult>` (writer =
|
||||
worker; reader = render thread).
|
||||
- `_surfaceCache`: `ConcurrentDictionary<uint, SurfaceInfo>` populated by
|
||||
`LandblockMesh.Build` on the worker; read by future paths if any
|
||||
(none today).
|
||||
- `TerrainBlendingContext`: read-only post-init. No lock.
|
||||
|
||||
---
|
||||
|
||||
## 7. Error handling
|
||||
|
||||
- **Worker crash:** caught in worker loop, posts
|
||||
`LandblockStreamResult.WorkerCrashed`. Render thread logs to console.
|
||||
(Existing pattern.)
|
||||
- **Dat read failure:** posts `LandblockStreamResult.Failed`. Render
|
||||
thread logs. Streaming continues with the LB skipped — region still
|
||||
tracks it as resident so we don't retry forever, but the slot stays empty.
|
||||
- **AABB cache invalidation race:** dynamic entity moves while the
|
||||
dispatcher is walking. Acceptable — at worst, the entity culls or
|
||||
draws based on the previous frame's position. Position is updated in
|
||||
the network handler (also render-thread today) so no actual race.
|
||||
- **Promotion timing:** if the player crosses N₁ inward, we enqueue a
|
||||
`Near` load on the worker. Until it completes, the LB has terrain but
|
||||
no scenery / entities. Frame budget is unaffected (only `LoadedLandblock`
|
||||
changes, and the dispatcher already handles missing entities by walking
|
||||
zero-length lists).
|
||||
- **Unload during in-flight load:** enqueue an unload while a load is
|
||||
in flight. When the load completes, render thread sees the LB is no
|
||||
longer resident — drop the result silently. Same pattern as today.
|
||||
|
||||
---
|
||||
|
||||
## 8. Testing strategy
|
||||
|
||||
### Unit tests (offline, no GL)
|
||||
|
||||
Add to `tests/AcDream.Core.Tests/Streaming/`:
|
||||
- `StreamingRegion_TwoTier_FirstTick_LoadsNearAndFarSeparately` — first
|
||||
call produces `ToLoadNear` populated for inner ring, `ToLoadFar`
|
||||
populated for outer ring, `ToPromote` empty (nothing was previously
|
||||
resident).
|
||||
- `StreamingRegion_TwoTier_NullToFar_OnFarRingEntry` — LB rolls into
|
||||
far window from null. Asserts entry in `ToLoadFar`, not
|
||||
`ToLoadNear`.
|
||||
- `StreamingRegion_TwoTier_FarToNear_OnNearRingEntry` — LB was
|
||||
far-resident, player walks toward it, LB enters near window. Asserts
|
||||
entry in `ToPromote`, not `ToLoadNear`.
|
||||
- `StreamingRegion_TwoTier_NullToNear_OnTeleport` — observer center
|
||||
jumps far enough that an LB goes from null → Near in one frame
|
||||
(e.g., teleport). Asserts entry in `ToLoadNear`, not `ToPromote`.
|
||||
- `StreamingRegion_TwoTier_NearToFar_OnNearBoundaryExitPlusHysteresis` —
|
||||
asserts entry in `ToDemote` only after distance exceeds
|
||||
`NearRadius + 2`.
|
||||
- `StreamingRegion_TwoTier_FarToNull_OnFarBoundaryExitPlusHysteresis` —
|
||||
asserts entry in `ToUnload` only after distance exceeds
|
||||
`FarRadius + 2`.
|
||||
- `StreamingRegion_TwoTier_HysteresisHoldsAcrossOscillation` — walk
|
||||
back-and-forth across N₁ five times within the hysteresis radius;
|
||||
assert no demote events fire.
|
||||
- `StreamingController_TwoTier_DrainsRoutedByVariant` — `Loaded.Far`,
|
||||
`Loaded.Near`, and `Promoted` each route to the right state mutation
|
||||
on the render thread.
|
||||
|
||||
Add to `tests/AcDream.Core.Tests/Rendering/Wb/`:
|
||||
- `WbDrawDispatcher_AnimatedEntities_InInvisibleLb_NoFullEntityWalk` —
|
||||
verify Change #1 (only iterates `animatedEntityIds`, not `Entities`).
|
||||
- `WbDrawDispatcher_PerEntityAabbCached_NotRecomputed` — assert AABB
|
||||
fields are read, not recomputed, for static entities.
|
||||
|
||||
### Conformance tests
|
||||
|
||||
- `TerrainModernConformanceTests` (existing) — must still pass. The
|
||||
visual mesh Z must agree with `TerrainSurface.SampleZFromHeightmap`
|
||||
to within 1 mm across both tiers.
|
||||
- `LandblockMeshTests` (existing) — must still pass. Worker-thread
|
||||
mesh build produces byte-identical results to render-thread build
|
||||
for the same inputs.
|
||||
|
||||
### Perf gate (manual, with `[WB-DIAG]` + `[TERRAIN-DIAG]`)
|
||||
|
||||
- **Standstill bench:** launch with `ACDREAM_WB_DIAG=1`, stand at
|
||||
Holtburg dueling field for 60 s. Read median + p95 + p99 from log.
|
||||
- **Walking bench:** launch with diag, run from Holtburg to North
|
||||
Yanshi, ~60 s. Same metrics.
|
||||
- **First traversal bench:** clear OS file cache (or reboot), launch
|
||||
with diag, walk into a region not previously visited, capture the
|
||||
worker-thread fill duration + render-thread frame time during fill.
|
||||
|
||||
### Visual gate (manual, user-driven)
|
||||
|
||||
User launches the client, walks the standard route, confirms:
|
||||
1. Horizon visible at 2.3 km.
|
||||
2. Fog blend is smooth (no scenery cliff at N₁).
|
||||
3. No shimmer on distant terrain.
|
||||
4. Smooth tree edges (foliage A2C).
|
||||
5. No new z-fighting / depth artifacts.
|
||||
|
||||
---
|
||||
|
||||
## 9. Out of scope (explicitly deferred)
|
||||
|
||||
Per the brainstorm Q10 confirmation:
|
||||
|
||||
- **GPU-side culling** (compute pre-pass) — N.6.
|
||||
- **Persistent-mapped indirect buffer** — N.6.
|
||||
- **Multi-thread mesh-build worker pool** — N.6 if first-traversal fill
|
||||
feels too slow at gate.
|
||||
- **Static/dynamic persistent groups** (Q5 Option B — the "compute the
|
||||
group key once at spawn" architecture change) — separate later phase
|
||||
(likely A.6 or N.6.5).
|
||||
- **Billboard / impostor scenery** at far tier — escalation only if the
|
||||
fog'd terrain horizon looks too bare at gate.
|
||||
- **Wider N₁ hysteresis** (Option C, radius+3) — single-line tweak only
|
||||
if gate finds entity pop-in along the boundary.
|
||||
- **Far-tier terrain mesh LOD** (decimating 2×2 LBs) — not needed at
|
||||
N₂=12; revisit only if N₂ grows beyond 15.
|
||||
- **Sky / particles modern path migration** — N.7+ phases.
|
||||
- **EnvCell modern path migration** — separate phase.
|
||||
- **Shadow mapping** — separate visual phase, later.
|
||||
- **Strict 240 Hz during walking** (Q9 Option A) — graduate to in a
|
||||
perf-polish phase if we want to commit to it.
|
||||
|
||||
---
|
||||
|
||||
## 10. Risks
|
||||
|
||||
1. **Fog tuning visual gate** *(highest risk).* Hardest non-engineering
|
||||
risk. The 0.7 / 0.95 multipliers in §4.8 are first-cut numbers. If
|
||||
the fog band is too thin (visible scenery cliff at N₁) or too thick
|
||||
(terrain looks washed out), iterate on the multipliers. Mitigation:
|
||||
expose `FogStart` / `FogEnd` as tunable env vars during A.5
|
||||
development for fast iteration.
|
||||
2. **A2C / MSAA framebuffer interaction** *(moderate risk).* MSAA on
|
||||
the GL render target may break sky / particles / UI rendering.
|
||||
Audit during implementation. **Fallback: ship Q8 Option B (mipmaps
|
||||
+ depth-audit only) if A2C goes sideways.** Document the result.
|
||||
3. **Worker starvation on first-traversal** *(low-moderate risk).*
|
||||
~2.7 s of sequential mesh build on first walk into virgin region.
|
||||
Render thread frame time stays in budget; the visible effect is the
|
||||
horizon visibly filling. Acceptable per Q9 Option B; graduate to
|
||||
multi-worker pool in N.6 if user complains.
|
||||
4. **Tier-boundary churn** *(low risk).* When player crosses N₁ both
|
||||
directions, demote→promote→demote fires. Hysteresis (radius+2) is
|
||||
the buffer. If thrash visible, widen to radius+3.
|
||||
5. **Entity AABB cache invalidation** *(low risk).* Dynamic entities
|
||||
must recompute AABB on position change. Single-threaded render
|
||||
thread means no concurrent mutation; the dirty-flag pattern is
|
||||
straightforward.
|
||||
6. **Server broadcast radius mismatch** *(low risk).* If ACE's broadcast
|
||||
radius is < N₁=4, NPCs in outer near-tier LBs won't be
|
||||
server-broadcast (they don't exist in our state). Mitigation:
|
||||
N₁=4 is conservative — typical ACE configs broadcast at 5-7 LBs.
|
||||
If observed, drop N₁ to 3.
|
||||
|
||||
---
|
||||
|
||||
## 11. What was deferred (post-A.5)
|
||||
|
||||
The following items were identified during A.5 development but deferred to
|
||||
post-A.5 phases. They are tracked as OPEN issues in `docs/ISSUES.md`.
|
||||
|
||||
1. **Tier 1 entity-classification cache** (commit `3639a6f` reverted at
|
||||
`9b49009`): First attempt cached `meshRef.PartTransform` which is mutated
|
||||
per frame for animated entities (skeletal pose). Next attempt needs:
|
||||
(a) audit AnimationSequencer + AnimationHookRouter to identify ALL
|
||||
per-frame mutations of MeshRef state; (b) redesign cache to bypass
|
||||
animated entities OR cache only the animation-invariant subset; (c) test
|
||||
specifically with a moving animated NPC on screen. (`docs/ISSUES.md` #53)
|
||||
|
||||
2. **Lifestone missing visual**: The Holtburg lifestone has not rendered since
|
||||
earlier in A.5 development. Possibly Bug A's far-tier strip incorrectly
|
||||
catching a near-tier entity, or a separate earlier regression.
|
||||
(`docs/ISSUES.md` #52)
|
||||
|
||||
3. **Plumb JobKind through BuildLandblockForStreaming**: Bug A's fix (commit
|
||||
`9217fd9`) strips entities post-load in the worker. Proper fix: skip the
|
||||
`LandBlockInfo` + scenery load entirely for far-tier jobs. ~30 min.
|
||||
(`docs/ISSUES.md` #54)
|
||||
|
||||
4. **Tier 2 — Static/dynamic split with persistent groups**: ~2-week phase.
|
||||
Avoids per-frame entity re-classification by maintaining stable groups
|
||||
keyed at spawn time. Roadmap doc at
|
||||
`docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md`.
|
||||
|
||||
5. **Tier 3 — GPU-side culling via compute pre-pass**: ~1-month phase.
|
||||
Same roadmap doc.
|
||||
|
||||
6. **Eliminate ToEntries adapter allocation**: tiny win (~25 KB/frame).
|
||||
|
||||
7. **InvalidateEntity wiring on palette/ObjDesc events**: needed by the
|
||||
Tier 1 retry.
|
||||
|
||||
8. **Visual gate at full High preset**: never validated due to the
|
||||
GPU+CPU stack-up OS crash earlier in A.5. With Bug A fixed the crash
|
||||
likely won't recur; defer retest to post-A.5 perf polish.
|
||||
|
||||
---
|
||||
|
||||
## 12. References (formerly §11)
|
||||
|
||||
- **Handoff (cold-start):** [`docs/research/2026-05-10-phase-a5-handoff.md`](../../research/2026-05-10-phase-a5-handoff.md)
|
||||
- **N.5b handoff (predecessor):** [`docs/research/2026-05-09-phase-n5b-handoff.md`](../../research/2026-05-09-phase-n5b-handoff.md)
|
||||
- **N.5b perf baseline:** [`docs/plans/2026-05-09-phase-n5b-perf-baseline.md`](../../plans/2026-05-09-phase-n5b-perf-baseline.md)
|
||||
- **Roadmap A.5 entry:** [`docs/plans/2026-04-11-roadmap.md`](../../plans/2026-04-11-roadmap.md)
|
||||
- **N.5b memory state:** `memory/project_phase_n5b_state.md` (three high-value
|
||||
gotchas — bindless uniform-sampler driver quirk, MaybeFlushTerrainDiag
|
||||
underflow, visual gate confirmation requirement).
|
||||
- **Existing streaming files:**
|
||||
- [`src/AcDream.App/Streaming/StreamingController.cs`](../../../src/AcDream.App/Streaming/StreamingController.cs)
|
||||
- [`src/AcDream.App/Streaming/StreamingRegion.cs`](../../../src/AcDream.App/Streaming/StreamingRegion.cs)
|
||||
- [`src/AcDream.App/Streaming/GpuWorldState.cs`](../../../src/AcDream.App/Streaming/GpuWorldState.cs)
|
||||
- [`src/AcDream.App/Streaming/LandblockStreamer.cs`](../../../src/AcDream.App/Streaming/LandblockStreamer.cs)
|
||||
- **Existing dispatcher:** [`src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`](../../../src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs)
|
||||
- **Existing terrain renderer:** [`src/AcDream.App/Rendering/TerrainModernRenderer.cs`](../../../src/AcDream.App/Rendering/TerrainModernRenderer.cs)
|
||||
- **Mesh builder (will move off render thread):** [`src/AcDream.Core/Terrain/LandblockMesh.cs`](../../../src/AcDream.Core/Terrain/LandblockMesh.cs)
|
||||
|
|
@ -83,7 +83,16 @@ public sealed class GameWindow : IDisposable
|
|||
private AcDream.App.Streaming.LandblockStreamer? _streamer;
|
||||
private AcDream.App.Streaming.GpuWorldState _worldState = new();
|
||||
private AcDream.App.Streaming.StreamingController? _streamingController;
|
||||
private int _streamingRadius = 2; // default 5×5
|
||||
private int _streamingRadius = 2; // default 5×5 (kept for debug overlay getStreamingRadius callback)
|
||||
private int _nearRadius = 4; // Phase A.5 T16: two-tier near ring (default 4 → 9×9)
|
||||
private int _farRadius = 12; // Phase A.5 T16: two-tier far ring (default 12 → 25×25)
|
||||
// A.5 T22.5: resolved quality settings (preset + env-var overrides).
|
||||
// Set once in OnLoad after LoadAndApplyPersistedSettings(); re-set on
|
||||
// ReapplyQualityPreset(). Default matches QualityPreset.High so the field
|
||||
// is valid before OnLoad fires (no GL calls are made before OnLoad anyway).
|
||||
private AcDream.UI.Abstractions.Settings.QualitySettings _resolvedQuality =
|
||||
AcDream.UI.Abstractions.Settings.QualitySettings.From(
|
||||
AcDream.UI.Abstractions.Settings.QualityPreset.High);
|
||||
private uint? _lastLivePlayerLandblockId;
|
||||
|
||||
// Phase B.3: physics engine — populated from the streaming pipeline.
|
||||
|
|
@ -97,13 +106,24 @@ public sealed class GameWindow : IDisposable
|
|||
// Step 4: portal-based interior cell visibility.
|
||||
private readonly CellVisibility _cellVisibility = new();
|
||||
|
||||
// Phase A.1 hotfix: DatCollection is NOT thread-safe. The streaming worker
|
||||
// thread and the render thread both read dats (BuildLandblockForStreaming
|
||||
// on the worker; ApplyLoadedTerrain + live-spawn handlers on the render
|
||||
// thread). Concurrent reads corrupt internal caches and produce
|
||||
// half-populated LandBlock.Height[] arrays, which caused terrain to render
|
||||
// as "a giant ball with spikes" before this lock was added. All _dats.Get
|
||||
// calls that can race with the worker thread MUST acquire this lock.
|
||||
// Phase A.1 hotfix / Phase A.5 T10: DatCollection is NOT thread-safe.
|
||||
// DatReaderWriter's DatBinReader uses a shared buffer position internally —
|
||||
// concurrent _dats.Get<T> calls from the streaming worker thread (T11+) and
|
||||
// the render thread (BuildLandblockForStreaming on the worker;
|
||||
// ApplyLoadedTerrain + live-spawn handlers + animation ticks on the render
|
||||
// thread) corrupt that state and produce half-populated LandBlock.Height[]
|
||||
// arrays, rendering as "a giant ball with spikes". All _dats.Get<T> call
|
||||
// sites that can race with the streaming worker MUST hold this lock.
|
||||
//
|
||||
// Worker-thread dat reads enter via the factory closures passed to
|
||||
// LandblockStreamer at construction (loadLandblock + buildMeshOrNull).
|
||||
// Those closures already acquire _datLock, so no additional wrapping is
|
||||
// needed for reads inside BuildLandblockForStreamingLocked /
|
||||
// BuildSceneryEntitiesForStreaming / BuildInteriorEntitiesForStreaming.
|
||||
// Render-thread paths (ApplyLoadedTerrain, OnLiveEntitySpawned) already
|
||||
// hold this lock via their outer wrappers; all remaining render-thread
|
||||
// _dats.Get calls run only when no worker dat read can be in flight (during
|
||||
// initialization or within the same lock scope).
|
||||
private readonly object _datLock = new();
|
||||
|
||||
// Terrain build context shared across all streamed landblocks. Stored as
|
||||
|
|
@ -111,7 +131,9 @@ public sealed class GameWindow : IDisposable
|
|||
// LandblockMesh.Build without re-deriving these each time.
|
||||
private float[]? _heightTable;
|
||||
private AcDream.Core.Terrain.TerrainBlendingContext? _blendCtx;
|
||||
private Dictionary<uint, AcDream.Core.Terrain.SurfaceInfo>? _surfaceCache;
|
||||
// Was: Dictionary<uint, SurfaceInfo>. ConcurrentDictionary so the off-thread
|
||||
// mesh builder (A.5 T11+) can call LandblockMesh.Build without a lock.
|
||||
private System.Collections.Concurrent.ConcurrentDictionary<uint, AcDream.Core.Terrain.SurfaceInfo>? _surfaceCache;
|
||||
|
||||
// Phase A.1 Task 8: worker thread pre-builds EnvCell room-mesh sub-meshes
|
||||
// (CPU only) and stores them here. ApplyLoadedTerrain (render thread) drains
|
||||
|
|
@ -805,6 +827,16 @@ public sealed class GameWindow : IDisposable
|
|||
|
||||
public void Run()
|
||||
{
|
||||
// A.5 T22.5: resolve quality preset BEFORE creating the window so
|
||||
// Samples (MSAA) is baked into WindowOptions correctly. GL context
|
||||
// sample count cannot change at runtime; all other quality fields are
|
||||
// applied again in OnLoad after the full settings load.
|
||||
var startupStore = new AcDream.UI.Abstractions.Panels.Settings.SettingsStore(
|
||||
AcDream.UI.Abstractions.Panels.Settings.SettingsStore.DefaultPath());
|
||||
var startupDisplay = startupStore.LoadDisplay();
|
||||
var startupBase = AcDream.UI.Abstractions.Settings.QualitySettings.From(startupDisplay.Quality);
|
||||
var startupQuality = AcDream.UI.Abstractions.Settings.QualitySettings.WithEnvOverrides(startupBase);
|
||||
|
||||
var options = WindowOptions.Default with
|
||||
{
|
||||
Size = new Vector2D<int>(1280, 720),
|
||||
|
|
@ -815,6 +847,11 @@ public sealed class GameWindow : IDisposable
|
|||
ContextFlags.ForwardCompatible,
|
||||
new APIVersion(4, 3)),
|
||||
VSync = false, // off during development so the perf overlay shows true framerate
|
||||
// A.5 T22.5: MSAA from quality preset (0 = disabled, 2/4/8 = multisample).
|
||||
// Silk.NET passes this to SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES).
|
||||
// Cannot be changed at runtime; Quality changes mid-session that would
|
||||
// alter MsaaSamples are logged as a restart-required warning.
|
||||
Samples = startupQuality.MsaaSamples,
|
||||
};
|
||||
|
||||
_window = Window.Create(options);
|
||||
|
|
@ -1078,6 +1115,18 @@ public sealed class GameWindow : IDisposable
|
|||
// without re-loading.
|
||||
LoadAndApplyPersistedSettings();
|
||||
|
||||
// A.5 T22.5: resolve quality preset immediately after settings load so
|
||||
// _resolvedQuality is available for TerrainAtlas.SetAnisotropic,
|
||||
// WbDrawDispatcher.AlphaToCoverage, and StreamingController wiring below.
|
||||
{
|
||||
var qBase = AcDream.UI.Abstractions.Settings.QualitySettings.From(_persistedDisplay.Quality);
|
||||
_resolvedQuality = AcDream.UI.Abstractions.Settings.QualitySettings.WithEnvOverrides(qBase);
|
||||
if (!_resolvedQuality.Equals(qBase))
|
||||
Console.WriteLine($"[QUALITY] Preset {_persistedDisplay.Quality} overridden by env vars: {_resolvedQuality}");
|
||||
else
|
||||
Console.WriteLine($"[QUALITY] Preset {_persistedDisplay.Quality} → {_resolvedQuality}");
|
||||
}
|
||||
|
||||
// Phase D.2a — ImGui devtools overlay. Zero cost when the env var
|
||||
// isn't set: no context creation, no per-frame branches hit.
|
||||
// See docs/plans/2026-04-24-ui-framework.md + memory/project_ui_architecture.md.
|
||||
|
|
@ -1136,7 +1185,8 @@ public sealed class GameWindow : IDisposable
|
|||
getNearestObjLabel: () => _lastNearestObjLabel,
|
||||
getColliding: () => _lastColliding,
|
||||
getDebugWireframes: () => _debugCollisionVisible,
|
||||
getStreamingRadius: () => _streamingRadius,
|
||||
getStreamingRadius: () => _nearRadius, // A.5 T16 follow-up: was _streamingRadius (legacy single-tier); show near tier
|
||||
|
||||
getMouseSensitivity: () => GetActiveSensitivity(),
|
||||
getChaseDistance: () => _chaseCamera?.Distance ?? 0f,
|
||||
getRmbOrbit: () => _rmbHeld,
|
||||
|
|
@ -1210,6 +1260,12 @@ public sealed class GameWindow : IDisposable
|
|||
// already track DisplayDraft via the
|
||||
// per-frame push.
|
||||
ApplyDisplayWindowState(display);
|
||||
// A.5 T22.5: apply quality preset if it changed.
|
||||
// MSAA changes log a restart-required warning
|
||||
// inside ReapplyQualityPreset; all other fields
|
||||
// apply immediately.
|
||||
_persistedDisplay = display;
|
||||
ReapplyQualityPreset(display.Quality);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -1436,6 +1492,10 @@ public sealed class GameWindow : IDisposable
|
|||
// atlas exposes bindless handles for the modern terrain path, so
|
||||
// BindlessSupport is threaded through.
|
||||
var terrainAtlas = AcDream.App.Rendering.TerrainAtlas.Build(_gl, _dats, _bindlessSupport);
|
||||
// A.5 T22.5: apply anisotropic level from quality preset. Build()
|
||||
// hard-codes 16x; override here to match the resolved quality so Low
|
||||
// (4x) and Medium (8x) actually take effect.
|
||||
terrainAtlas.SetAnisotropic(_resolvedQuality.AnisotropicLevel);
|
||||
|
||||
_terrain = new TerrainModernRenderer(_gl, _bindlessSupport, _terrainModernShader!, terrainAtlas);
|
||||
|
||||
|
|
@ -1465,7 +1525,7 @@ public sealed class GameWindow : IDisposable
|
|||
RoadAlphaRCodes: terrainAtlas.RoadAlphaRCodes);
|
||||
|
||||
_heightTable = heightTable;
|
||||
_surfaceCache = new Dictionary<uint, AcDream.Core.Terrain.SurfaceInfo>();
|
||||
_surfaceCache = new System.Collections.Concurrent.ConcurrentDictionary<uint, AcDream.Core.Terrain.SurfaceInfo>();
|
||||
|
||||
// (Bindless detection moved above — must precede TerrainAtlas.Build /
|
||||
// TerrainModernRenderer ctor so they can consume BindlessSupport.)
|
||||
|
|
@ -1545,6 +1605,8 @@ public sealed class GameWindow : IDisposable
|
|||
|
||||
_wbDrawDispatcher = new AcDream.App.Rendering.Wb.WbDrawDispatcher(
|
||||
_gl, _meshShader!, _textureCache!, _wbMeshAdapter!, _wbEntitySpawnAdapter, _bindlessSupport!);
|
||||
// A.5 T22.5: apply A2C gate from quality preset.
|
||||
_wbDrawDispatcher.AlphaToCoverage = _resolvedQuality.AlphaToCoverage;
|
||||
}
|
||||
|
||||
// Phase G.1 sky renderer — its own shader (sky.vert / sky.frag)
|
||||
|
|
@ -1562,27 +1624,57 @@ public sealed class GameWindow : IDisposable
|
|||
// the player.
|
||||
_particleRenderer = new ParticleRenderer(_gl, shadersDir, _textureCache, _dats);
|
||||
|
||||
// Phase A.1: replace the one-shot 3×3 preload with a streaming controller.
|
||||
// Parse runtime radius from environment (default 2 → 5×5 window).
|
||||
// Values outside [0, 8] fall back to the field default of 2.
|
||||
var radiusEnv = Environment.GetEnvironmentVariable("ACDREAM_STREAM_RADIUS");
|
||||
if (int.TryParse(radiusEnv, out var r) && r >= 0 && r <= 8)
|
||||
_streamingRadius = r;
|
||||
Console.WriteLine($"streaming: radius={_streamingRadius} (window={2*_streamingRadius+1}×{2*_streamingRadius+1})");
|
||||
// A.5 T22.5: apply radii from the already-resolved _resolvedQuality.
|
||||
// _resolvedQuality was set by the quality block immediately after
|
||||
// LoadAndApplyPersistedSettings() above, absorbing all env-var overrides.
|
||||
// Legacy ACDREAM_STREAM_RADIUS is still honoured for backward-compat.
|
||||
_nearRadius = _resolvedQuality.NearRadius;
|
||||
_farRadius = _resolvedQuality.FarRadius;
|
||||
|
||||
// The streamer's load delegate wraps LandblockLoader.Load + stab
|
||||
// hydration. Scenery + interior will land in Task 8.
|
||||
// Legacy override: ACDREAM_STREAM_RADIUS acts as nearRadius and
|
||||
// ensures farRadius >= streamRadius.
|
||||
{
|
||||
var legacyEnv = Environment.GetEnvironmentVariable("ACDREAM_STREAM_RADIUS");
|
||||
if (int.TryParse(legacyEnv, out var sr) && sr >= 0)
|
||||
{
|
||||
_nearRadius = sr;
|
||||
_streamingRadius = sr; // keep debug overlay in sync
|
||||
_farRadius = System.Math.Max(sr, _farRadius);
|
||||
}
|
||||
}
|
||||
Console.WriteLine(
|
||||
$"streaming: nearRadius={_nearRadius} (window={2*_nearRadius+1}x{2*_nearRadius+1})" +
|
||||
$" farRadius={_farRadius} (window={2*_farRadius+1}x{2*_farRadius+1})");
|
||||
|
||||
// Phase A.5 T11+: the streamer now runs on a dedicated worker thread.
|
||||
// loadLandblock acquires _datLock (T10) before touching DatCollection.
|
||||
// buildMeshOrNull (T12) receives the already-loaded LoadedLandblock so
|
||||
// it can call LandblockMesh.Build without a dat read — _heightTable and
|
||||
// _blendCtx are read-only after init, _surfaceCache is ConcurrentDictionary (T9).
|
||||
_streamer = new AcDream.App.Streaming.LandblockStreamer(
|
||||
loadLandblock: id => BuildLandblockForStreaming(id));
|
||||
loadLandblock: id => BuildLandblockForStreaming(id),
|
||||
buildMeshOrNull: (id, lb) =>
|
||||
{
|
||||
if (lb is null || _heightTable is null || _blendCtx is null || _surfaceCache is null)
|
||||
return null;
|
||||
uint lbX = (id >> 24) & 0xFFu;
|
||||
uint lbY = (id >> 16) & 0xFFu;
|
||||
// _surfaceCache is ConcurrentDictionary (T9) — safe from worker thread.
|
||||
// _heightTable and _blendCtx are read-only after initialization.
|
||||
// lb.Heightmap is the pre-loaded LandBlock; no dat read needed here.
|
||||
return AcDream.Core.Terrain.LandblockMesh.Build(
|
||||
lb.Heightmap, lbX, lbY, _heightTable, _blendCtx, _surfaceCache);
|
||||
});
|
||||
_streamer.Start();
|
||||
|
||||
_streamingController = new AcDream.App.Streaming.StreamingController(
|
||||
enqueueLoad: _streamer.EnqueueLoad,
|
||||
enqueueLoad: (id, kind) => _streamer.EnqueueLoad(id, kind),
|
||||
enqueueUnload: _streamer.EnqueueUnload,
|
||||
drainCompletions: _streamer.DrainCompletions,
|
||||
applyTerrain: ApplyLoadedTerrain,
|
||||
state: _worldState,
|
||||
radius: _streamingRadius,
|
||||
nearRadius: _nearRadius,
|
||||
farRadius: _farRadius,
|
||||
removeTerrain: id =>
|
||||
{
|
||||
// Phase G.2: release any LightSources attached to entities
|
||||
|
|
@ -1599,6 +1691,8 @@ public sealed class GameWindow : IDisposable
|
|||
_physicsEngine.RemoveLandblock(id);
|
||||
_cellVisibility.RemoveLandblock((id >> 16) & 0xFFFFu);
|
||||
});
|
||||
// A.5 T22.5: apply max-completions from resolved quality.
|
||||
_streamingController.MaxCompletionsPerFrame = _resolvedQuality.MaxCompletionsPerFrame;
|
||||
|
||||
// Phase 4.7: optional live-mode startup. Connect to the ACE server,
|
||||
// enter the world as the first character on the account, and stream
|
||||
|
|
@ -3895,7 +3989,7 @@ public sealed class GameWindow : IDisposable
|
|||
// position by adding the residual back (so the visual doesn't jerk
|
||||
// for one frame before the residual decay kicks in on the next tick).
|
||||
System.Numerics.Vector3 preSnapPos = entity.Position;
|
||||
entity.Position = worldPos;
|
||||
entity.SetPosition(worldPos);
|
||||
entity.Rotation = rot;
|
||||
|
||||
// Commit B 2026-04-29 — keep the shadow registry in sync with
|
||||
|
|
@ -4015,11 +4109,11 @@ public sealed class GameWindow : IDisposable
|
|||
if (!update.IsGrounded)
|
||||
{
|
||||
// Undo the unconditional entity hard-snap at the top of the
|
||||
// function (entity.Position = worldPos): the body is mid-arc
|
||||
// function (entity.SetPosition(worldPos)): the body is mid-arc
|
||||
// and TickAnimations will write entity = body next frame
|
||||
// anyway. Setting entity = body now prevents a 1-frame
|
||||
// teleport-to-server-then-yank-back rubber-band.
|
||||
entity.Position = rmState.Body.Position;
|
||||
entity.SetPosition(rmState.Body.Position);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -4128,12 +4222,12 @@ public sealed class GameWindow : IDisposable
|
|||
}
|
||||
|
||||
// Sync the visible entity to the body — overrides the unconditional
|
||||
// entity.Position = worldPos snap at the top of this function.
|
||||
// entity.SetPosition(worldPos) snap at the top of this function.
|
||||
// For the far-snap branch this is a no-op (body == worldPos); for
|
||||
// the near-enqueue branch this prevents a 1-frame teleport-then-
|
||||
// yank-back rubber-band as TickAnimations chases worldPos via the
|
||||
// queue.
|
||||
entity.Position = rmState.Body.Position;
|
||||
entity.SetPosition(rmState.Body.Position);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -4265,7 +4359,7 @@ public sealed class GameWindow : IDisposable
|
|||
rmState.ServerVelocity);
|
||||
}
|
||||
|
||||
entity.Position = rmState.Body.Position;
|
||||
entity.SetPosition(rmState.Body.Position);
|
||||
entity.Rotation = rmState.Body.Orientation;
|
||||
}
|
||||
|
||||
|
|
@ -4310,7 +4404,7 @@ public sealed class GameWindow : IDisposable
|
|||
resolved.Position.X, resolved.Position.Y, resolved.Position.Z);
|
||||
|
||||
// 3. Snap player entity + controller.
|
||||
entity.Position = snappedPos;
|
||||
entity.SetPosition(snappedPos);
|
||||
entity.Rotation = rot;
|
||||
_playerController.SetPosition(snappedPos, resolved.CellId);
|
||||
|
||||
|
|
@ -4970,24 +5064,26 @@ public sealed class GameWindow : IDisposable
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Phase A.1: render-thread callback from StreamingController.Tick
|
||||
/// Phase A.1 / A.5 T12: render-thread callback from StreamingController.Tick
|
||||
/// whenever a new landblock's terrain + entities are ready for GPU upload.
|
||||
/// Mirrors the terrain-build + entity-upload part of the old preload.
|
||||
/// Phase A.5 T12: the worker pre-builds <paramref name="meshData"/> off the
|
||||
/// render thread via <see cref="AcDream.Core.Terrain.LandblockMesh.Build"/>;
|
||||
/// this callback no longer pays that CPU cost.
|
||||
/// Must only be called from the render thread.
|
||||
/// </summary>
|
||||
private void ApplyLoadedTerrain(AcDream.Core.World.LoadedLandblock lb)
|
||||
private void ApplyLoadedTerrain(AcDream.Core.World.LoadedLandblock lb,
|
||||
AcDream.Core.Terrain.LandblockMeshData meshData)
|
||||
{
|
||||
if (_terrain is null || _dats is null || _blendCtx is null
|
||||
|| _heightTable is null || _surfaceCache is null) return;
|
||||
if (_terrain is null || _dats is null) return;
|
||||
|
||||
// Phase A.1 hotfix: render-thread path also takes the dat lock so it
|
||||
// doesn't race with BuildLandblockForStreaming on the worker thread.
|
||||
// Hold the lock across the entire apply because we read dats below
|
||||
// (GfxObj sub-mesh builds) and mutate the shared _surfaceCache from
|
||||
// LandblockMesh.Build.
|
||||
// Hold the lock across the entity hydration below (GfxObj sub-mesh
|
||||
// builds). The terrain mesh is pre-built by the worker (T12) and passed
|
||||
// in via meshData, so LandblockMesh.Build no longer runs under this lock.
|
||||
lock (_datLock)
|
||||
{
|
||||
ApplyLoadedTerrainLocked(lb);
|
||||
ApplyLoadedTerrainLocked(lb, meshData);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -5097,10 +5193,12 @@ public sealed class GameWindow : IDisposable
|
|||
_pendingCells.Add(loaded);
|
||||
}
|
||||
|
||||
private void ApplyLoadedTerrainLocked(AcDream.Core.World.LoadedLandblock lb)
|
||||
private void ApplyLoadedTerrainLocked(AcDream.Core.World.LoadedLandblock lb,
|
||||
AcDream.Core.Terrain.LandblockMeshData meshData)
|
||||
{
|
||||
if (_terrain is null || _dats is null || _blendCtx is null
|
||||
|| _heightTable is null || _surfaceCache is null) return;
|
||||
// _blendCtx / _surfaceCache no longer needed here (mesh pre-built by worker).
|
||||
// _heightTable still needed for physics TerrainSurface below.
|
||||
if (_terrain is null || _dats is null || _heightTable is null) return;
|
||||
|
||||
uint lbXu = (lb.LandblockId >> 24) & 0xFFu;
|
||||
uint lbYu = (lb.LandblockId >> 16) & 0xFFu;
|
||||
|
|
@ -5111,11 +5209,10 @@ public sealed class GameWindow : IDisposable
|
|||
(lbY - _liveCenterY) * 192f,
|
||||
0f);
|
||||
|
||||
// Build terrain mesh data on the render thread (pure CPU; acceptable
|
||||
// for the MVP; a future pass can move it to the worker thread).
|
||||
var meshData = AcDream.Core.Terrain.LandblockMesh.Build(
|
||||
lb.Heightmap, lbXu, lbYu, _heightTable, _blendCtx, _surfaceCache);
|
||||
_terrain.AddLandblock(lb.LandblockId, meshData, origin);
|
||||
// Phase A.5 T15/T16: route through AddLandblockWithMesh — the named
|
||||
// two-tier entry point. Delegates to AddLandblock internally; both
|
||||
// paths share one GPU upload path.
|
||||
_terrain.AddLandblockWithMesh(lb.LandblockId, meshData, origin);
|
||||
|
||||
// Step 4: drain pending LoadedCells from the worker thread.
|
||||
while (_pendingCells.TryTake(out var cell))
|
||||
|
|
@ -5887,7 +5984,7 @@ public sealed class GameWindow : IDisposable
|
|||
// the physics-resolved location each frame.
|
||||
if (_entitiesByServerGuid.TryGetValue(_playerServerGuid, out var pe))
|
||||
{
|
||||
pe.Position = result.RenderPosition;
|
||||
pe.SetPosition(result.RenderPosition); // A.5 T18: SetPosition propagates AabbDirty
|
||||
pe.Rotation = System.Numerics.Quaternion.CreateFromAxisAngle(
|
||||
System.Numerics.Vector3.UnitZ, _playerController.Yaw - MathF.PI / 2f);
|
||||
|
||||
|
|
@ -6291,6 +6388,28 @@ public sealed class GameWindow : IDisposable
|
|||
Lighting.Tick(camPos);
|
||||
var ubo = AcDream.Core.Lighting.SceneLightingUbo.Build(
|
||||
Lighting, in atmo, camPos, (float)WorldTime.DayFraction);
|
||||
|
||||
// A.5 T22: override fog ramp with N₁/N₂-derived distances so the
|
||||
// horizon fog masks the N₁ scenery boundary. Sky keyframe fog is
|
||||
// retail-accurate at normal view distances but far too short for
|
||||
// the extended N₂=12 (25×25 LB) streaming window.
|
||||
// FogStart = N₁ × 192m × 0.7 ≈ 538m at defaults (4/12).
|
||||
// FogEnd = N₂ × 192m × 0.95 ≈ 2188m at defaults.
|
||||
// Multipliers exposed as env vars for fast iteration at visual gate.
|
||||
{
|
||||
const float LandblockSize = 192.0f;
|
||||
float startMult = ParseEnvFloat("ACDREAM_FOG_START_MULT", 0.7f);
|
||||
float endMult = ParseEnvFloat("ACDREAM_FOG_END_MULT", 0.95f);
|
||||
float fogStart = _nearRadius * LandblockSize * startMult;
|
||||
float fogEnd = _farRadius * LandblockSize * endMult;
|
||||
// Preserve fog color (xyz), lightning flash (z), and mode (w).
|
||||
ubo.FogParams = new System.Numerics.Vector4(
|
||||
fogStart,
|
||||
fogEnd,
|
||||
ubo.FogParams.Z, // lightning flash — unchanged
|
||||
ubo.FogParams.W); // fog mode — unchanged
|
||||
}
|
||||
|
||||
_sceneLightingUbo?.Upload(ubo);
|
||||
|
||||
// Never cull the landblock the player is currently on.
|
||||
|
|
@ -6896,7 +7015,7 @@ public sealed class GameWindow : IDisposable
|
|||
rm.MaxSeqSpeedSinceLastUP = seqSpeedNow;
|
||||
}
|
||||
|
||||
ae.Entity.Position = rm.Body.Position;
|
||||
ae.Entity.SetPosition(rm.Body.Position); // A.5 T18: SetPosition propagates AabbDirty
|
||||
ae.Entity.Rotation = rm.Body.Orientation;
|
||||
}
|
||||
else
|
||||
|
|
@ -7224,7 +7343,7 @@ public sealed class GameWindow : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
ae.Entity.Position = rm.Body.Position;
|
||||
ae.Entity.SetPosition(rm.Body.Position); // A.5 T18: SetPosition propagates AabbDirty
|
||||
ae.Entity.Rotation = rm.Body.Orientation;
|
||||
}
|
||||
}
|
||||
|
|
@ -7511,7 +7630,11 @@ public sealed class GameWindow : IDisposable
|
|||
// we always want it animated in player mode.
|
||||
if (!_animatedEntities.TryGetValue(pe.Id, out var ae))
|
||||
{
|
||||
var setup = _dats.Get<DatReaderWriter.DBObjs.Setup>(pe.SourceGfxObjOrSetupId);
|
||||
// A.5 T10: lock around _dats.Get — worker thread may be
|
||||
// building a landblock mesh concurrently. DatBinReader's
|
||||
// shared buffer position would corrupt without serialization.
|
||||
DatReaderWriter.DBObjs.Setup? setup;
|
||||
lock (_datLock) { setup = _dats.Get<DatReaderWriter.DBObjs.Setup>(pe.SourceGfxObjOrSetupId); }
|
||||
if (setup is null) return;
|
||||
_physicsDataCache.CacheSetup(pe.SourceGfxObjOrSetupId, setup);
|
||||
|
||||
|
|
@ -7969,6 +8092,111 @@ public sealed class GameWindow : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A.5 T22.5: apply a new quality preset mid-session (called from the
|
||||
/// Settings panel Save path when <see cref="DisplaySettings.Quality"/>
|
||||
/// changes).
|
||||
///
|
||||
/// <para>
|
||||
/// What changes immediately:
|
||||
/// <list type="bullet">
|
||||
/// <item>Streaming radii: disposes the old
|
||||
/// <see cref="_streamingController"/> + <see cref="_streamer"/>
|
||||
/// and constructs new ones with the new radii.</item>
|
||||
/// <item>Anisotropic filtering: calls
|
||||
/// <c>TerrainAtlas.SetAnisotropic</c>.</item>
|
||||
/// <item>Alpha-to-coverage gate: sets
|
||||
/// <c>WbDrawDispatcher.AlphaToCoverage</c>.</item>
|
||||
/// <item>Max completions per frame: updates
|
||||
/// <c>StreamingController.MaxCompletionsPerFrame</c>.</item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// What requires a restart:
|
||||
/// MSAA samples are baked into the GL context via <c>WindowOptions.Samples</c>
|
||||
/// at window creation time and cannot change at runtime. If the new preset
|
||||
/// would change <c>MsaaSamples</c>, a warning is logged and MSAA is left
|
||||
/// at its current level until the next launch.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public void ReapplyQualityPreset(AcDream.UI.Abstractions.Settings.QualityPreset newPreset)
|
||||
{
|
||||
var newBase = AcDream.UI.Abstractions.Settings.QualitySettings.From(newPreset);
|
||||
var newResolved = AcDream.UI.Abstractions.Settings.QualitySettings.WithEnvOverrides(newBase);
|
||||
|
||||
Console.WriteLine($"[QUALITY] ReapplyQualityPreset: {newPreset} → {newResolved}");
|
||||
|
||||
// MSAA samples cannot change at runtime — warn if preset would differ.
|
||||
if (newResolved.MsaaSamples != _resolvedQuality.MsaaSamples)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"[QUALITY] MSAA samples change ({_resolvedQuality.MsaaSamples} → " +
|
||||
$"{newResolved.MsaaSamples}) requires a restart — skipped for this session.");
|
||||
}
|
||||
|
||||
_resolvedQuality = newResolved;
|
||||
|
||||
// A2C gate — immediate toggle, no GL context restart needed.
|
||||
if (_wbDrawDispatcher is not null)
|
||||
_wbDrawDispatcher.AlphaToCoverage = newResolved.AlphaToCoverage;
|
||||
|
||||
// Anisotropic — immediate GL TexParameter call on the terrain atlas.
|
||||
_terrain?.Atlas?.SetAnisotropic(newResolved.AnisotropicLevel);
|
||||
|
||||
// Streaming radii — requires tearing down + rebuilding the controller
|
||||
// (radii are constructor-time on StreamingController, not live-mutable).
|
||||
// The ~1-2s hitch while the worker drains is acceptable for a settings change.
|
||||
if (_streamer is not null && _streamingController is not null)
|
||||
{
|
||||
_nearRadius = newResolved.NearRadius;
|
||||
_farRadius = newResolved.FarRadius;
|
||||
|
||||
// StreamingController is stateless (no Dispose needed); dispose
|
||||
// only the LandblockStreamer worker thread.
|
||||
_streamer.Dispose();
|
||||
|
||||
_streamer = new AcDream.App.Streaming.LandblockStreamer(
|
||||
loadLandblock: id => BuildLandblockForStreaming(id),
|
||||
buildMeshOrNull: (id, lb) =>
|
||||
{
|
||||
if (lb is null || _heightTable is null || _blendCtx is null || _surfaceCache is null)
|
||||
return null;
|
||||
uint lbX = (id >> 24) & 0xFFu;
|
||||
uint lbY = (id >> 16) & 0xFFu;
|
||||
return AcDream.Core.Terrain.LandblockMesh.Build(
|
||||
lb.Heightmap, lbX, lbY, _heightTable, _blendCtx, _surfaceCache);
|
||||
});
|
||||
_streamer.Start();
|
||||
|
||||
_streamingController = new AcDream.App.Streaming.StreamingController(
|
||||
enqueueLoad: (id, kind) => _streamer.EnqueueLoad(id, kind),
|
||||
enqueueUnload: _streamer.EnqueueUnload,
|
||||
drainCompletions: _streamer.DrainCompletions,
|
||||
applyTerrain: ApplyLoadedTerrain,
|
||||
state: _worldState,
|
||||
nearRadius: _nearRadius,
|
||||
farRadius: _farRadius,
|
||||
removeTerrain: id =>
|
||||
{
|
||||
if (_lightingSink is not null &&
|
||||
_worldState.TryGetLandblock(id, out var lb))
|
||||
{
|
||||
foreach (var ent in lb!.Entities)
|
||||
_lightingSink.UnregisterOwner(ent.Id);
|
||||
}
|
||||
_terrain?.RemoveLandblock(id);
|
||||
_physicsEngine.RemoveLandblock(id);
|
||||
_cellVisibility.RemoveLandblock((id >> 16) & 0xFFFFu);
|
||||
});
|
||||
_streamingController.MaxCompletionsPerFrame = newResolved.MaxCompletionsPerFrame;
|
||||
|
||||
Console.WriteLine(
|
||||
$"[QUALITY] Streaming restarted: nearRadius={_nearRadius}, " +
|
||||
$"farRadius={_farRadius}, maxCompletions={newResolved.MaxCompletionsPerFrame}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// L.0 Display tab: framebuffer-resize handler — update GL viewport
|
||||
/// + camera aspect when the window is resized (by the user dragging
|
||||
|
|
@ -8532,7 +8760,10 @@ public sealed class GameWindow : IDisposable
|
|||
// 0.4 m fallbacks.
|
||||
if (_dats is not null && (playerEntity.SourceGfxObjOrSetupId & 0xFF000000u) == 0x02000000u)
|
||||
{
|
||||
var playerSetup = _dats.Get<DatReaderWriter.DBObjs.Setup>(playerEntity.SourceGfxObjOrSetupId);
|
||||
// A.5 T10: lock around _dats.Get — worker thread may be
|
||||
// building a landblock mesh concurrently.
|
||||
DatReaderWriter.DBObjs.Setup? playerSetup;
|
||||
lock (_datLock) { playerSetup = _dats.Get<DatReaderWriter.DBObjs.Setup>(playerEntity.SourceGfxObjOrSetupId); }
|
||||
if (playerSetup is not null)
|
||||
_physicsDataCache.CacheSetup(playerEntity.SourceGfxObjOrSetupId, playerSetup);
|
||||
_playerController.StepUpHeight = (playerSetup is not null && playerSetup.StepUpHeight > 0f)
|
||||
|
|
@ -8765,8 +8996,12 @@ public sealed class GameWindow : IDisposable
|
|||
long cpuP95HundredthsUs = TerrainDiagPercentile95Micros(_terrainCpuSamples);
|
||||
double cpuMedUs = cpuMedHundredthsUs / 100.0;
|
||||
double cpuP95Us = cpuP95HundredthsUs / 100.0;
|
||||
// A.5 T23: flag when terrain dispatcher median exceeds 1.0ms budget
|
||||
// (Phase A.5 spec §2 acceptance criterion 6). Grep-friendly prefix.
|
||||
const double TerrainBudgetUs = 1000.0;
|
||||
string terrainBudgetFlag = cpuMedUs > TerrainBudgetUs ? " BUDGET_OVER" : "";
|
||||
Console.WriteLine(
|
||||
$"[TERRAIN-DIAG] cpu_us={cpuMedUs:F2}m/{cpuP95Us:F2}p95 " +
|
||||
$"[TERRAIN-DIAG]{terrainBudgetFlag} cpu_us={cpuMedUs:F2}m/{cpuP95Us:F2}p95 " +
|
||||
$"draws={_terrain?.VisibleSlots ?? 0}/frame " +
|
||||
$"visible={_terrain?.VisibleSlots ?? 0} " +
|
||||
$"loaded={_terrain?.LoadedSlots ?? 0} " +
|
||||
|
|
@ -8802,6 +9037,17 @@ public sealed class GameWindow : IDisposable
|
|||
return copy[copy.Length - 1 - offset];
|
||||
}
|
||||
|
||||
/// <summary>A.5 T22: parse a float environment variable, returning
|
||||
/// <paramref name="defaultValue"/> when the variable is absent or unparseable.</summary>
|
||||
private static float ParseEnvFloat(string name, float defaultValue)
|
||||
{
|
||||
var s = System.Environment.GetEnvironmentVariable(name);
|
||||
if (s is not null && float.TryParse(s, System.Globalization.NumberStyles.Float,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out var v))
|
||||
return v;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
private void OnClosing()
|
||||
{
|
||||
// Phase A.1: join the streamer worker thread before tearing down GL
|
||||
|
|
|
|||
|
|
@ -80,8 +80,11 @@ void main() {
|
|||
vec4 color = texture(tex, vec3(vTexCoord, float(vTextureLayer)));
|
||||
|
||||
// Two-pass alpha-test (N.5 Decision 2).
|
||||
// A.5 T20: opaque pass writes alpha as-sampled so GL_SAMPLE_ALPHA_TO_COVERAGE
|
||||
// derives the MSAA sample mask from it — ClipMap foliage edges become smooth.
|
||||
// Discard only fully-transparent (α < 0.05); the GPU handles coverage masking.
|
||||
if (uRenderPass == 0) {
|
||||
if (color.a < 0.95) discard; // opaque pass
|
||||
if (color.a < 0.05) discard; // opaque pass — kill truly empty only (A2C)
|
||||
} else {
|
||||
if (color.a >= 0.95) discard; // transparent pass
|
||||
if (color.a < 0.05) discard; // skip totally-empty
|
||||
|
|
|
|||
|
|
@ -183,13 +183,17 @@ public sealed unsafe class TerrainAtlas : IDisposable
|
|||
layerIdx++;
|
||||
}
|
||||
|
||||
gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
|
||||
// A.5 T19: generate mipmaps + trilinear + 16x anisotropic for distant-LB quality.
|
||||
gl.GenerateMipmap(TextureTarget.Texture2DArray);
|
||||
gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
|
||||
gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
|
||||
gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
|
||||
gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
|
||||
// GL_TEXTURE_MAX_ANISOTROPY = 0x84FE (GL_EXT_texture_filter_anisotropic / ARB_texture_filter_anisotropic).
|
||||
gl.TexParameter(TextureTarget.Texture2DArray, (TextureParameterName)0x84FE, 16.0f);
|
||||
gl.BindTexture(TextureTarget.Texture2DArray, 0);
|
||||
|
||||
Console.WriteLine($"TerrainAtlas: {layerCount} terrain layers at {maxW}x{maxH}");
|
||||
Console.WriteLine($"TerrainAtlas: {layerCount} terrain layers at {maxW}x{maxH} (mipmaps+aniso16x)");
|
||||
|
||||
// ---- Alpha atlas (new in Phase 3c.2) ----
|
||||
// texMerge is guaranteed non-null here: the early return above exited
|
||||
|
|
@ -411,6 +415,43 @@ public sealed unsafe class TerrainAtlas : IDisposable
|
|||
Array.Empty<uint>(), Array.Empty<uint>(), Array.Empty<uint>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A.5 T22.5: update GL_TEXTURE_MAX_ANISOTROPY on the terrain atlas at
|
||||
/// runtime (called by <see cref="GameWindow.ReapplyQualityPreset"/> when
|
||||
/// the user changes Quality preset mid-session). Idempotent — calling with
|
||||
/// the same level as the current setting is safe and produces no visual
|
||||
/// change. The texture must not be resident-bindless when its parameters
|
||||
/// are mutated; we temporarily make it non-resident if needed.
|
||||
/// </summary>
|
||||
public void SetAnisotropic(int level)
|
||||
{
|
||||
// If bindless handles are live we must make them non-resident before
|
||||
// mutating texture state, then re-resident after.
|
||||
bool wasResident = _handlesGenerated && _bindless is not null;
|
||||
if (wasResident)
|
||||
{
|
||||
_bindless!.MakeNonResident(_terrainHandle);
|
||||
// Alpha texture is not affected by anisotropic but we must keep
|
||||
// residency symmetric — re-generate both handles after.
|
||||
_bindless.MakeNonResident(_alphaHandle);
|
||||
_handlesGenerated = false;
|
||||
}
|
||||
|
||||
_gl.BindTexture(TextureTarget.Texture2DArray, GlTexture);
|
||||
// GL_TEXTURE_MAX_ANISOTROPY = 0x84FE
|
||||
_gl.TexParameter(TextureTarget.Texture2DArray, (TextureParameterName)0x84FE, (float)level);
|
||||
_gl.BindTexture(TextureTarget.Texture2DArray, 0);
|
||||
|
||||
// Re-generate bindless handles if they were live before.
|
||||
if (wasResident)
|
||||
{
|
||||
// GetBindlessHandles regenerates and makes resident.
|
||||
_ = GetBindlessHandles();
|
||||
}
|
||||
|
||||
Console.WriteLine($"TerrainAtlas: anisotropic updated to {level}x");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Phase 1: release bindless residency BEFORE deleting textures.
|
||||
|
|
|
|||
|
|
@ -35,6 +35,10 @@ public sealed unsafe class TerrainModernRenderer : IDisposable
|
|||
private readonly Shader _shader;
|
||||
private readonly TerrainAtlas _atlas;
|
||||
|
||||
/// <summary>A.5 T22.5: exposes the terrain atlas so callers can update
|
||||
/// anisotropic level mid-session via <see cref="TerrainAtlas.SetAnisotropic"/>.</summary>
|
||||
public TerrainAtlas Atlas => _atlas;
|
||||
|
||||
private readonly TerrainSlotAllocator _alloc;
|
||||
|
||||
// Per-slot live data (index by slot integer; null entries are unused slots).
|
||||
|
|
@ -89,6 +93,18 @@ public sealed unsafe class TerrainModernRenderer : IDisposable
|
|||
_indirectBuffer = _gl.GenBuffer();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Two-tier streaming entry point. Accepts a prebuilt mesh from
|
||||
/// <see cref="LandblockStreamResult.Loaded.MeshData"/> built on the worker
|
||||
/// thread, together with the world-space origin computed by the caller
|
||||
/// (render-thread GameWindow derives it from landblockId + liveCenterX/Y).
|
||||
///
|
||||
/// Delegates to <see cref="AddLandblock(uint,LandblockMeshData,Vector3)"/>
|
||||
/// so both paths share one upload path. Per Phase A.5 spec T15.
|
||||
/// </summary>
|
||||
public void AddLandblockWithMesh(uint landblockId, LandblockMeshData meshData, Vector3 worldOrigin)
|
||||
=> AddLandblock(landblockId, meshData, worldOrigin);
|
||||
|
||||
public void AddLandblock(uint landblockId, LandblockMeshData meshData, Vector3 worldOrigin)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(meshData);
|
||||
|
|
|
|||
|
|
@ -128,6 +128,12 @@ public sealed class EntitySpawnAdapter
|
|||
}
|
||||
}
|
||||
|
||||
// A.5 T18: populate cached AABB so WalkEntities reads from the cache
|
||||
// rather than recomputing Position±5 per frame. Called here because
|
||||
// all entity-state initialization (position, rotation) is complete
|
||||
// by this point via the WorldEntity passed in.
|
||||
entity.RefreshAabb();
|
||||
|
||||
// Build the per-entity AnimatedEntityState. The sequencer factory
|
||||
// may return a stub (in tests) or a fully-constructed sequencer from
|
||||
// the MotionTable (in production). Factory must not return null —
|
||||
|
|
|
|||
|
|
@ -68,6 +68,15 @@ public sealed unsafe class WbDrawDispatcher : IDisposable
|
|||
|
||||
private readonly BindlessSupport _bindless;
|
||||
|
||||
/// <summary>
|
||||
/// A.5 T22.5: gate for GL_SAMPLE_ALPHA_TO_COVERAGE around the opaque pass.
|
||||
/// Default true matches T20 behavior. Set false for Low/Medium presets that
|
||||
/// have MsaaSamples=0 (A2C is a no-op without MSAA, but turning it off
|
||||
/// avoids the unnecessary GL state thrash and is cleaner diagnostics).
|
||||
/// Can be toggled mid-session via <see cref="GameWindow.ReapplyQualityPreset"/>.
|
||||
/// </summary>
|
||||
public bool AlphaToCoverage { get; set; } = true;
|
||||
|
||||
// SSBO buffer ids
|
||||
private uint _instanceSsbo;
|
||||
private uint _batchSsbo;
|
||||
|
|
@ -100,6 +109,11 @@ public sealed unsafe class WbDrawDispatcher : IDisposable
|
|||
private readonly Dictionary<GroupKey, InstanceGroup> _groups = new();
|
||||
private readonly List<InstanceGroup> _opaqueDraws = new();
|
||||
private readonly List<InstanceGroup> _translucentDraws = new();
|
||||
// A.5 T26 follow-up (Bug B): WalkEntities populates this scratch list
|
||||
// instead of allocating a fresh List<(WorldEntity, int)> per frame. At
|
||||
// ~10K entities × ~3 mesh refs = ~30K tuples × 16 bytes = ~480 KB / frame
|
||||
// of GC pressure on the render thread under the original T17 shape.
|
||||
private readonly List<(WorldEntity Entity, int MeshRefIndex)> _walkScratch = new();
|
||||
|
||||
// Per-entity-cull AABB radius. Conservative — covers most entities; large
|
||||
// outliers (long banners, tall columns) are still landblock-culled.
|
||||
|
|
@ -157,9 +171,142 @@ public sealed unsafe class WbDrawDispatcher : IDisposable
|
|||
Matrix4x4 restPose)
|
||||
=> restPose * animOverride * entityWorld;
|
||||
|
||||
/// <summary>
|
||||
/// Entry for <see cref="WalkEntities"/> per-landblock iteration.
|
||||
/// Mirrors the shape yielded by <c>GpuWorldState.LandblockEntries</c>.
|
||||
/// </summary>
|
||||
public readonly record struct LandblockEntry(
|
||||
uint LandblockId,
|
||||
Vector3 AabbMin,
|
||||
Vector3 AabbMax,
|
||||
IReadOnlyList<WorldEntity> Entities,
|
||||
IReadOnlyDictionary<uint, WorldEntity>? AnimatedById);
|
||||
|
||||
/// <summary>
|
||||
/// Result of <see cref="WalkEntities"/> — the list of (entity, meshRef index)
|
||||
/// pairs that passed all visibility filters, plus a diagnostic walk count.
|
||||
/// </summary>
|
||||
public struct WalkResult
|
||||
{
|
||||
public int EntitiesWalked;
|
||||
public List<(WorldEntity Entity, int MeshRefIndex)> ToDraw;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pure-CPU visibility filter over <paramref name="landblockEntries"/>.
|
||||
/// Separated from <see cref="Draw"/> so tests can exercise it without GL state.
|
||||
///
|
||||
/// <para>
|
||||
/// A.5 T17 Change #1: when an LB is frustum-culled AND
|
||||
/// <paramref name="animatedEntityIds"/> is non-empty, the OLD path walked
|
||||
/// every entity in the LB just to find the few animated ones. This helper
|
||||
/// fixes that: if the LB is invisible, we iterate
|
||||
/// <paramref name="animatedEntityIds"/> directly and look each up in
|
||||
/// <c>entry.AnimatedById</c> (typically <50 animated, up to ~10K total).
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// A.5 T18 Change #2: per-entity AABB cull reads from the cached
|
||||
/// <see cref="WorldEntity.AabbMin"/>/<see cref="WorldEntity.AabbMax"/>
|
||||
/// (refreshed lazily if <see cref="WorldEntity.AabbDirty"/>), instead of
|
||||
/// recomputing Position±5 each frame.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <summary>
|
||||
/// Test-friendly overload that allocates a fresh ToDraw list per call.
|
||||
/// Production code (<see cref="Draw"/>) uses the no-alloc overload below
|
||||
/// with a caller-provided scratch list.
|
||||
/// </summary>
|
||||
internal static WalkResult WalkEntities(
|
||||
IEnumerable<LandblockEntry> landblockEntries,
|
||||
FrustumPlanes? frustum,
|
||||
uint? neverCullLandblockId,
|
||||
HashSet<uint>? visibleCellIds,
|
||||
HashSet<uint>? animatedEntityIds)
|
||||
{
|
||||
var scratch = new List<(WorldEntity Entity, int MeshRefIndex)>();
|
||||
var result = new WalkResult { ToDraw = scratch };
|
||||
WalkEntitiesInto(
|
||||
landblockEntries, frustum, neverCullLandblockId,
|
||||
visibleCellIds, animatedEntityIds, scratch, ref result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// No-alloc overload: clears + populates the caller-provided <paramref name="scratch"/>
|
||||
/// list. <see cref="Draw"/> reuses a per-dispatcher scratch field across frames to
|
||||
/// avoid the 480+ KB / frame GC pressure that the test-friendly overload incurs.
|
||||
/// Returns walk count via <paramref name="result"/>'s <c>EntitiesWalked</c> field.
|
||||
/// </summary>
|
||||
internal static void WalkEntitiesInto(
|
||||
IEnumerable<LandblockEntry> landblockEntries,
|
||||
FrustumPlanes? frustum,
|
||||
uint? neverCullLandblockId,
|
||||
HashSet<uint>? visibleCellIds,
|
||||
HashSet<uint>? animatedEntityIds,
|
||||
List<(WorldEntity Entity, int MeshRefIndex)> scratch,
|
||||
ref WalkResult result)
|
||||
{
|
||||
scratch.Clear();
|
||||
result.EntitiesWalked = 0;
|
||||
result.ToDraw = scratch;
|
||||
|
||||
foreach (var entry in landblockEntries)
|
||||
{
|
||||
bool landblockVisible = frustum is null
|
||||
|| entry.LandblockId == neverCullLandblockId
|
||||
|| FrustumCuller.IsAabbVisible(frustum.Value, entry.AabbMin, entry.AabbMax);
|
||||
|
||||
if (!landblockVisible)
|
||||
{
|
||||
// A.5 T17 Change #1: walk only animated entities, not all entities.
|
||||
// Avoids O(N_entities) scan when only O(N_animated) work is needed.
|
||||
if (animatedEntityIds is null || animatedEntityIds.Count == 0) continue;
|
||||
if (entry.AnimatedById is null) continue;
|
||||
foreach (var animatedId in animatedEntityIds)
|
||||
{
|
||||
if (!entry.AnimatedById.TryGetValue(animatedId, out var entity)) continue;
|
||||
if (entity.MeshRefs.Count == 0) continue;
|
||||
if (entity.ParentCellId.HasValue && visibleCellIds is not null
|
||||
&& !visibleCellIds.Contains(entity.ParentCellId.Value)) continue;
|
||||
result.EntitiesWalked++;
|
||||
for (int i = 0; i < entity.MeshRefs.Count; i++)
|
||||
scratch.Add((entity, i));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var entity in entry.Entities)
|
||||
{
|
||||
if (entity.MeshRefs.Count == 0) continue;
|
||||
|
||||
if (entity.ParentCellId.HasValue && visibleCellIds is not null
|
||||
&& !visibleCellIds.Contains(entity.ParentCellId.Value))
|
||||
continue;
|
||||
|
||||
// Per-entity AABB frustum cull (perf #3). Animated entities bypass —
|
||||
// they're tracked at landblock level + need per-frame work regardless.
|
||||
// A.5 T18 Change #2: read cached AABB, refresh lazily on AabbDirty.
|
||||
bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true;
|
||||
if (frustum is not null && !isAnimated && entry.LandblockId != neverCullLandblockId)
|
||||
{
|
||||
if (entity.AabbDirty) entity.RefreshAabb();
|
||||
if (!FrustumCuller.IsAabbVisible(frustum.Value, entity.AabbMin, entity.AabbMax))
|
||||
continue;
|
||||
}
|
||||
|
||||
result.EntitiesWalked++;
|
||||
for (int i = 0; i < entity.MeshRefs.Count; i++)
|
||||
scratch.Add((entity, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(
|
||||
ICamera camera,
|
||||
IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, IReadOnlyList<WorldEntity> Entities)> landblockEntries,
|
||||
IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax,
|
||||
IReadOnlyList<WorldEntity> Entities,
|
||||
IReadOnlyDictionary<uint, WorldEntity>? AnimatedById)> landblockEntries,
|
||||
FrustumPlanes? frustum = null,
|
||||
uint? neverCullLandblockId = null,
|
||||
HashSet<uint>? visibleCellIds = null,
|
||||
|
|
@ -194,97 +341,85 @@ public sealed unsafe class WbDrawDispatcher : IDisposable
|
|||
var metaTable = _meshAdapter.MetadataTable;
|
||||
uint anyVao = 0;
|
||||
|
||||
foreach (var entry in landblockEntries)
|
||||
// Project the 5-tuple enumerable into LandblockEntry records for WalkEntities.
|
||||
static IEnumerable<LandblockEntry> ToEntries(
|
||||
IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax,
|
||||
IReadOnlyList<WorldEntity> Entities,
|
||||
IReadOnlyDictionary<uint, WorldEntity>? AnimatedById)> src)
|
||||
{
|
||||
bool landblockVisible = frustum is null
|
||||
|| entry.LandblockId == neverCullLandblockId
|
||||
|| FrustumCuller.IsAabbVisible(frustum.Value, entry.AabbMin, entry.AabbMax);
|
||||
foreach (var e in src)
|
||||
yield return new LandblockEntry(e.LandblockId, e.AabbMin, e.AabbMax, e.Entities, e.AnimatedById);
|
||||
}
|
||||
|
||||
if (!landblockVisible && (animatedEntityIds is null || animatedEntityIds.Count == 0))
|
||||
continue;
|
||||
// A.5 T26 follow-up (Bug B): use the no-alloc WalkEntitiesInto overload
|
||||
// that populates _walkScratch (a per-dispatcher field reused across frames)
|
||||
// instead of allocating a fresh List<(WorldEntity, int)> per frame.
|
||||
var walkResult = default(WalkResult);
|
||||
WalkEntitiesInto(
|
||||
ToEntries(landblockEntries),
|
||||
frustum,
|
||||
neverCullLandblockId,
|
||||
visibleCellIds,
|
||||
animatedEntityIds,
|
||||
_walkScratch,
|
||||
ref walkResult);
|
||||
|
||||
foreach (var entity in entry.Entities)
|
||||
foreach (var (entity, partIdx) in _walkScratch)
|
||||
{
|
||||
if (diag) _entitiesSeen++;
|
||||
|
||||
var entityWorld =
|
||||
Matrix4x4.CreateFromQuaternion(entity.Rotation) *
|
||||
Matrix4x4.CreateTranslation(entity.Position);
|
||||
|
||||
// Compute palette-override hash ONCE per entity (perf #4).
|
||||
// Reused across every (part, batch) lookup so the FNV-1a fold
|
||||
// over SubPalettes runs once instead of N times. Zero when the
|
||||
// entity has no palette override (trees, scenery).
|
||||
ulong palHash = 0;
|
||||
if (entity.PaletteOverride is not null)
|
||||
palHash = TextureCache.HashPaletteOverride(entity.PaletteOverride);
|
||||
|
||||
// Note: GameWindow's spawn path already applies
|
||||
// AnimPartChanges + GfxObjDegradeResolver (Issue #47 fix —
|
||||
// close-detail mesh swap for humanoids) to MeshRefs. We
|
||||
// trust MeshRefs as the source of truth here. AnimatedEntityState's
|
||||
// overrides become relevant only for hot-swap (0xF625
|
||||
// ObjDescEvent) which today rebuilds MeshRefs anyway.
|
||||
var meshRef = entity.MeshRefs[partIdx];
|
||||
ulong gfxObjId = meshRef.GfxObjId;
|
||||
|
||||
var renderData = _meshAdapter.TryGetRenderData(gfxObjId);
|
||||
if (renderData is null)
|
||||
{
|
||||
if (entity.MeshRefs.Count == 0) continue;
|
||||
|
||||
bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true;
|
||||
if (!landblockVisible && !isAnimated) continue;
|
||||
|
||||
if (entity.ParentCellId.HasValue && visibleCellIds is not null
|
||||
&& !visibleCellIds.Contains(entity.ParentCellId.Value))
|
||||
continue;
|
||||
|
||||
// Per-entity AABB frustum cull (perf #3). Skips work for distant
|
||||
// entities even when their landblock is visible. Animated
|
||||
// entities bypass — they're tracked at landblock level + need
|
||||
// per-frame work for animation regardless. Conservative 5m
|
||||
// radius covers typical entity bounds.
|
||||
if (frustum is not null && !isAnimated && entry.LandblockId != neverCullLandblockId)
|
||||
{
|
||||
var p = entity.Position;
|
||||
var aMin = new Vector3(p.X - PerEntityCullRadius, p.Y - PerEntityCullRadius, p.Z - PerEntityCullRadius);
|
||||
var aMax = new Vector3(p.X + PerEntityCullRadius, p.Y + PerEntityCullRadius, p.Z + PerEntityCullRadius);
|
||||
if (!FrustumCuller.IsAabbVisible(frustum.Value, aMin, aMax))
|
||||
continue;
|
||||
}
|
||||
|
||||
if (diag) _entitiesSeen++;
|
||||
|
||||
var entityWorld =
|
||||
Matrix4x4.CreateFromQuaternion(entity.Rotation) *
|
||||
Matrix4x4.CreateTranslation(entity.Position);
|
||||
|
||||
// Compute palette-override hash ONCE per entity (perf #4).
|
||||
// Reused across every (part, batch) lookup so the FNV-1a fold
|
||||
// over SubPalettes runs once instead of N times. Zero when the
|
||||
// entity has no palette override (trees, scenery).
|
||||
ulong palHash = 0;
|
||||
if (entity.PaletteOverride is not null)
|
||||
palHash = TextureCache.HashPaletteOverride(entity.PaletteOverride);
|
||||
|
||||
bool drewAny = false;
|
||||
for (int partIdx = 0; partIdx < entity.MeshRefs.Count; partIdx++)
|
||||
{
|
||||
// Note: GameWindow's spawn path already applies
|
||||
// AnimPartChanges + GfxObjDegradeResolver (Issue #47 fix —
|
||||
// close-detail mesh swap for humanoids) to MeshRefs. We
|
||||
// trust MeshRefs as the source of truth here. AnimatedEntityState's
|
||||
// overrides become relevant only for hot-swap (0xF625
|
||||
// ObjDescEvent) which today rebuilds MeshRefs anyway.
|
||||
var meshRef = entity.MeshRefs[partIdx];
|
||||
ulong gfxObjId = meshRef.GfxObjId;
|
||||
|
||||
var renderData = _meshAdapter.TryGetRenderData(gfxObjId);
|
||||
if (renderData is null)
|
||||
{
|
||||
if (diag) _meshesMissing++;
|
||||
continue;
|
||||
}
|
||||
drewAny = true;
|
||||
if (anyVao == 0) anyVao = renderData.VAO;
|
||||
|
||||
if (renderData.IsSetup && renderData.SetupParts.Count > 0)
|
||||
{
|
||||
foreach (var (partGfxObjId, partTransform) in renderData.SetupParts)
|
||||
{
|
||||
var partData = _meshAdapter.TryGetRenderData(partGfxObjId);
|
||||
if (partData is null) continue;
|
||||
|
||||
var model = ComposePartWorldMatrix(
|
||||
entityWorld, meshRef.PartTransform, partTransform);
|
||||
|
||||
ClassifyBatches(partData, partGfxObjId, model, entity, meshRef, palHash, metaTable);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var model = meshRef.PartTransform * entityWorld;
|
||||
ClassifyBatches(renderData, gfxObjId, model, entity, meshRef, palHash, metaTable);
|
||||
}
|
||||
}
|
||||
|
||||
if (diag && drewAny) _entitiesDrawn++;
|
||||
if (diag) _meshesMissing++;
|
||||
continue;
|
||||
}
|
||||
if (anyVao == 0) anyVao = renderData.VAO;
|
||||
|
||||
bool drewAny = false;
|
||||
if (renderData.IsSetup && renderData.SetupParts.Count > 0)
|
||||
{
|
||||
foreach (var (partGfxObjId, partTransform) in renderData.SetupParts)
|
||||
{
|
||||
var partData = _meshAdapter.TryGetRenderData(partGfxObjId);
|
||||
if (partData is null) continue;
|
||||
|
||||
var model = ComposePartWorldMatrix(
|
||||
entityWorld, meshRef.PartTransform, partTransform);
|
||||
|
||||
ClassifyBatches(partData, partGfxObjId, model, entity, meshRef, palHash, metaTable);
|
||||
drewAny = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var model = meshRef.PartTransform * entityWorld;
|
||||
ClassifyBatches(renderData, gfxObjId, model, entity, meshRef, palHash, metaTable);
|
||||
drewAny = true;
|
||||
}
|
||||
|
||||
if (diag && drewAny) _entitiesDrawn++;
|
||||
}
|
||||
|
||||
// Nothing visible — skip the GL pass entirely.
|
||||
|
|
@ -402,6 +537,12 @@ public sealed unsafe class WbDrawDispatcher : IDisposable
|
|||
{
|
||||
_gl.Disable(EnableCap.Blend);
|
||||
_gl.DepthMask(true);
|
||||
// A.5 T20: enable A2C for ClipMap foliage — GPU derives sample mask
|
||||
// from the alpha written by mesh_modern.frag so foliage edges are
|
||||
// smooth under MSAA 4x. A no-op for fully-opaque (α=1) batches.
|
||||
// A.5 T22.5: gated by AlphaToCoverage property so Low/Medium presets
|
||||
// (no MSAA) skip the unnecessary GL state change.
|
||||
if (AlphaToCoverage) _gl.Enable(EnableCap.SampleAlphaToCoverage);
|
||||
_shader.SetInt("uRenderPass", 0);
|
||||
_gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer);
|
||||
if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryOpaque);
|
||||
|
|
@ -412,6 +553,7 @@ public sealed unsafe class WbDrawDispatcher : IDisposable
|
|||
(uint)_opaqueDrawCount,
|
||||
(uint)DrawCommandStride);
|
||||
if (diag && _gpuQueriesInitialized) _gl.EndQuery(QueryTarget.TimeElapsed);
|
||||
if (AlphaToCoverage) _gl.Disable(EnableCap.SampleAlphaToCoverage);
|
||||
}
|
||||
|
||||
// ── Phase 8: transparent pass ────────────────────────────────────────
|
||||
|
|
@ -492,8 +634,12 @@ public sealed unsafe class WbDrawDispatcher : IDisposable
|
|||
long cpuP95 = Percentile95Micros(_cpuSamples);
|
||||
long gpuMed = MedianMicros(_gpuSamples);
|
||||
long gpuP95 = Percentile95Micros(_gpuSamples);
|
||||
// A.5 T23: flag when entity dispatcher median exceeds 2.0ms budget
|
||||
// (Phase A.5 spec §2 acceptance criterion 6). Grep-friendly prefix.
|
||||
const long BudgetUs = 2000;
|
||||
string budgetFlag = cpuMed > BudgetUs ? " BUDGET_OVER" : "";
|
||||
Console.WriteLine(
|
||||
$"[WB-DIAG] entSeen={_entitiesSeen} entDrawn={_entitiesDrawn} meshMissing={_meshesMissing} drawsIssued={_drawsIssued} instances={_instancesIssued} groups={_groups.Count} " +
|
||||
$"[WB-DIAG]{budgetFlag} entSeen={_entitiesSeen} entDrawn={_entitiesDrawn} meshMissing={_meshesMissing} drawsIssued={_drawsIssued} instances={_instancesIssued} groups={_groups.Count} " +
|
||||
$"cpu_us={cpuMed}m/{cpuP95}p95 gpu_us={gpuMed}m/{gpuP95}p95");
|
||||
_entitiesSeen = _entitiesDrawn = _meshesMissing = _drawsIssued = _instancesIssued = 0;
|
||||
_lastLogTick = now;
|
||||
|
|
|
|||
|
|
@ -106,17 +106,33 @@ public sealed class GpuWorldState
|
|||
/// Per-landblock iteration with AABB data for use by the frustum-culling
|
||||
/// draw path. Landblocks without a stored AABB yield <see cref="Vector3.Zero"/>
|
||||
/// for both corners, which the culler will conservatively treat as visible.
|
||||
///
|
||||
/// <para>
|
||||
/// A.5 T17: also yields an <c>AnimatedById</c> dictionary built on the fly
|
||||
/// from the landblock's entity list. This lets <see cref="WbDrawDispatcher"/>
|
||||
/// skip the full entity walk when the landblock is frustum-culled but animated
|
||||
/// entities inside it must still be processed (Change #1).
|
||||
/// Building the dict per-yield is cheap (~132 entities/LB max). A caching
|
||||
/// layer is out of A.5 scope.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, IReadOnlyList<WorldEntity> Entities)> LandblockEntries
|
||||
public IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax,
|
||||
IReadOnlyList<WorldEntity> Entities,
|
||||
IReadOnlyDictionary<uint, WorldEntity>? AnimatedById)> LandblockEntries
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (var kvp in _loaded)
|
||||
{
|
||||
// Build AnimatedById on the fly — cheap (~132 entities/LB max).
|
||||
var byId = new Dictionary<uint, WorldEntity>(kvp.Value.Entities.Count);
|
||||
foreach (var e in kvp.Value.Entities)
|
||||
byId[e.Id] = e;
|
||||
|
||||
if (_aabbs.TryGetValue(kvp.Key, out var aabb))
|
||||
yield return (kvp.Key, aabb.Min, aabb.Max, kvp.Value.Entities);
|
||||
yield return (kvp.Key, aabb.Min, aabb.Max, kvp.Value.Entities, byId);
|
||||
else
|
||||
yield return (kvp.Key, Vector3.Zero, Vector3.Zero, kvp.Value.Entities);
|
||||
yield return (kvp.Key, Vector3.Zero, Vector3.Zero, kvp.Value.Entities, byId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -339,6 +355,73 @@ public sealed class GpuWorldState
|
|||
bucket.Add(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drop all entities from a landblock without removing the terrain. Used
|
||||
/// by two-tier streaming when a landblock crosses Near→Far hysteresis.
|
||||
/// Per Phase A.5 spec §4.4.
|
||||
///
|
||||
/// <para>
|
||||
/// <b>Persistent-entity rescue is intentionally omitted</b> (unlike
|
||||
/// <see cref="RemoveLandblock"/>): demote-tier entities are atlas-tier
|
||||
/// only (procedural scenery, dat-static stabs/buildings) — they never
|
||||
/// have <c>ServerGuid != 0</c> and so can never be in <see cref="_persistentGuids"/>.
|
||||
/// The local player and other live server-spawned entities live in their
|
||||
/// landblock via <c>RelocateEntity</c> per frame and are not affected
|
||||
/// by Near→Far demotion of dat-static landblock layers.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public void RemoveEntitiesFromLandblock(uint landblockId)
|
||||
{
|
||||
// A.5 T14 follow-up: canonicalize for symmetry with AppendLiveEntity.
|
||||
// Streaming callers always pass canonical (0xAAAA0xFFFF) ids; this
|
||||
// protects against future callers that mirror AppendLiveEntity's
|
||||
// cell-resolved-id pattern.
|
||||
uint canonical = (landblockId & 0xFFFF0000u) | 0xFFFFu;
|
||||
if (!_loaded.TryGetValue(canonical, out var lb)) return;
|
||||
if (_wbSpawnAdapter is not null)
|
||||
_wbSpawnAdapter.OnLandblockUnloaded(canonical);
|
||||
_loaded[canonical] = new LoadedLandblock(lb.LandblockId, lb.Heightmap, System.Array.Empty<WorldEntity>());
|
||||
_pendingByLandblock.Remove(canonical);
|
||||
RebuildFlatView();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Merge entities into an existing-loaded landblock. Used by two-tier
|
||||
/// streaming for the Far→Near promotion case (terrain already loaded;
|
||||
/// entity layer streaming in). Falls back to the pending bucket if the
|
||||
/// landblock isn't loaded yet (handles the rare "promote arrives before
|
||||
/// far load completes" race).
|
||||
/// Per Phase A.5 spec §4.4.
|
||||
///
|
||||
/// <para>
|
||||
/// <b>Landblock id is canonicalized</b> (low 16 bits forced to 0xFFFF) —
|
||||
/// callers may pass cell-resolved ids and they will key correctly.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public void AddEntitiesToExistingLandblock(uint landblockId, IReadOnlyList<WorldEntity> entities)
|
||||
{
|
||||
// A.5 T14 follow-up: canonicalize for symmetry with AppendLiveEntity.
|
||||
uint canonical = (landblockId & 0xFFFF0000u) | 0xFFFFu;
|
||||
if (!_loaded.TryGetValue(canonical, out var lb))
|
||||
{
|
||||
// Park as pending — same pattern as AppendLiveEntity for not-yet-loaded LBs.
|
||||
if (!_pendingByLandblock.TryGetValue(canonical, out var bucket))
|
||||
{
|
||||
bucket = new List<WorldEntity>();
|
||||
_pendingByLandblock[canonical] = bucket;
|
||||
}
|
||||
bucket.AddRange(entities);
|
||||
return;
|
||||
}
|
||||
var merged = new List<WorldEntity>(lb.Entities.Count + entities.Count);
|
||||
merged.AddRange(lb.Entities);
|
||||
merged.AddRange(entities);
|
||||
_loaded[canonical] = new LoadedLandblock(lb.LandblockId, lb.Heightmap, merged);
|
||||
if (_wbSpawnAdapter is not null)
|
||||
_wbSpawnAdapter.OnLandblockLoaded(_loaded[canonical]);
|
||||
RebuildFlatView();
|
||||
}
|
||||
|
||||
private void RebuildFlatView()
|
||||
{
|
||||
_flatEntities = _loaded.Values.SelectMany(lb => lb.Entities).ToArray();
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using AcDream.Core.Terrain;
|
||||
using AcDream.Core.World;
|
||||
|
||||
namespace AcDream.App.Streaming;
|
||||
|
|
@ -10,7 +12,7 @@ namespace AcDream.App.Streaming;
|
|||
/// </summary>
|
||||
public abstract record LandblockStreamJob(uint LandblockId)
|
||||
{
|
||||
public sealed record Load(uint LandblockId) : LandblockStreamJob(LandblockId);
|
||||
public sealed record Load(uint LandblockId, LandblockStreamJobKind Kind) : LandblockStreamJob(LandblockId);
|
||||
public sealed record Unload(uint LandblockId) : LandblockStreamJob(LandblockId);
|
||||
}
|
||||
|
||||
|
|
@ -22,7 +24,29 @@ public abstract record LandblockStreamJob(uint LandblockId)
|
|||
/// </summary>
|
||||
public abstract record LandblockStreamResult(uint LandblockId)
|
||||
{
|
||||
public sealed record Loaded(uint LandblockId, LoadedLandblock Landblock) : LandblockStreamResult(LandblockId);
|
||||
/// <summary>
|
||||
/// A landblock load completed. <see cref="Tier"/> distinguishes Far
|
||||
/// (terrain only) from Near (terrain + entities). <see cref="MeshData"/>
|
||||
/// is built off the render thread on the streaming worker.
|
||||
/// </summary>
|
||||
public sealed record Loaded(
|
||||
uint LandblockId,
|
||||
LandblockStreamTier Tier,
|
||||
LoadedLandblock Landblock,
|
||||
LandblockMeshData MeshData
|
||||
) : LandblockStreamResult(LandblockId);
|
||||
|
||||
/// <summary>
|
||||
/// A previously-Far-resident landblock was promoted to Near. Terrain
|
||||
/// mesh is already on the GPU; the result carries the entity layer
|
||||
/// (stabs, buildings, scenery) to merge into the existing GpuWorldState
|
||||
/// entry.
|
||||
/// </summary>
|
||||
public sealed record Promoted(
|
||||
uint LandblockId,
|
||||
IReadOnlyList<WorldEntity> Entities
|
||||
) : LandblockStreamResult(LandblockId);
|
||||
|
||||
public sealed record Failed(uint LandblockId, string Error) : LandblockStreamResult(LandblockId);
|
||||
public sealed record Unloaded(uint LandblockId) : LandblockStreamResult(LandblockId);
|
||||
|
||||
|
|
|
|||
28
src/AcDream.App/Streaming/LandblockStreamTier.cs
Normal file
28
src/AcDream.App/Streaming/LandblockStreamTier.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
namespace AcDream.App.Streaming;
|
||||
|
||||
/// <summary>
|
||||
/// Streaming-tier classification for a landblock. <see cref="Far"/> means
|
||||
/// terrain mesh only; <see cref="Near"/> means terrain + scenery + EnvCells +
|
||||
/// entity registration with the WB dispatcher. Per Phase A.5 spec §3.
|
||||
/// </summary>
|
||||
public enum LandblockStreamTier
|
||||
{
|
||||
Far,
|
||||
Near,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What work the streaming worker should perform for a given job. Distinct
|
||||
/// from <see cref="LandblockStreamTier"/> because <see cref="PromoteToNear"/>
|
||||
/// reads only the entity layer (terrain mesh already loaded), while
|
||||
/// <see cref="LoadNear"/> reads everything from scratch. Per Phase A.5 spec §4.3.
|
||||
/// </summary>
|
||||
public enum LandblockStreamJobKind
|
||||
{
|
||||
/// <summary>Read LandBlock heightmap, build mesh, no entity layer.</summary>
|
||||
LoadFar,
|
||||
/// <summary>Read LandBlock + LandBlockInfo, generate scenery, build mesh, full entity layer.</summary>
|
||||
LoadNear,
|
||||
/// <summary>Read LandBlockInfo + scenery only — terrain already loaded for this LB.</summary>
|
||||
PromoteToNear,
|
||||
}
|
||||
|
|
@ -8,28 +8,27 @@ using AcDream.Core.World;
|
|||
namespace AcDream.App.Streaming;
|
||||
|
||||
/// <summary>
|
||||
/// Services landblock load/unload requests by invoking a caller-supplied
|
||||
/// load delegate (the production instance wraps
|
||||
/// <see cref="LandblockLoader.Load"/>) and posting results to an outbox
|
||||
/// the render thread drains once per OnUpdate.
|
||||
/// Services landblock load/unload requests by invoking caller-supplied
|
||||
/// factory delegates (the production instance wraps
|
||||
/// <see cref="LandblockLoader.Load"/> for loading and
|
||||
/// <see cref="AcDream.Core.Terrain.LandblockMesh.Build"/> for the terrain
|
||||
/// mesh) and posting results to an outbox the render thread drains once
|
||||
/// per OnUpdate.
|
||||
///
|
||||
/// <para>
|
||||
/// <b>Currently runs synchronously on the calling thread.</b> The original
|
||||
/// Phase A.1 design ran loads on a dedicated worker thread, but DatReaderWriter's
|
||||
/// <c>DatCollection</c> is not thread-safe — concurrent reads from a worker
|
||||
/// and the render thread (animation tick, live spawn handlers) corrupt
|
||||
/// internal buffer state and produce half-populated <c>LandBlock.Height[]</c>
|
||||
/// arrays which render as wildly distorted terrain. Until Phase A.3 introduces
|
||||
/// a thread-safe dat wrapper, loads are synchronous: <see cref="EnqueueLoad"/>
|
||||
/// invokes the load delegate inline and writes the result to the outbox in
|
||||
/// a single call. This causes a frame hitch when crossing landblock
|
||||
/// boundaries, but the rendering is correct.
|
||||
/// <b>Thread model (Phase A.5 T11+):</b> <see cref="Start"/> spawns a
|
||||
/// dedicated background worker thread. <see cref="EnqueueLoad"/> and
|
||||
/// <see cref="EnqueueUnload"/> write non-blocking to the inbox
|
||||
/// <see cref="Channel{T}"/>; the worker drains it and posts
|
||||
/// <see cref="LandblockStreamResult"/> records to the outbox.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// The Channel-based outbox + <see cref="DrainCompletions"/> API is
|
||||
/// preserved so the move back to async loading is a single-class change
|
||||
/// when DatCollection thread safety lands.
|
||||
/// <b>DatCollection thread safety</b> is provided by the caller:
|
||||
/// GameWindow's <c>_datLock</c> (Phase A.5 T10) serialises all
|
||||
/// <c>DatCollection.Get<T></c> calls. Both factory closures passed at
|
||||
/// construction acquire that lock before reading dats. The worker never
|
||||
/// touches <c>DatCollection</c> directly — it only calls the factories.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
|
|
@ -39,8 +38,9 @@ namespace AcDream.App.Streaming;
|
|||
/// </para>
|
||||
///
|
||||
/// <remarks>
|
||||
/// Threading: synchronous mode means all methods must be called from the
|
||||
/// same thread (the render thread in production).
|
||||
/// Threading: <see cref="DrainCompletions"/> must be called from a single
|
||||
/// consumer thread (the render thread in production). All other public
|
||||
/// methods are thread-safe.
|
||||
/// </remarks>
|
||||
/// </summary>
|
||||
public sealed class LandblockStreamer : IDisposable
|
||||
|
|
@ -53,49 +53,72 @@ public sealed class LandblockStreamer : IDisposable
|
|||
public const int DefaultDrainBatchSize = 4;
|
||||
|
||||
private readonly Func<uint, LoadedLandblock?> _loadLandblock;
|
||||
private readonly Func<uint, LoadedLandblock?, AcDream.Core.Terrain.LandblockMeshData?> _buildMeshOrNull;
|
||||
private readonly Channel<LandblockStreamJob> _inbox;
|
||||
private readonly Channel<LandblockStreamResult> _outbox;
|
||||
private readonly CancellationTokenSource _cancel = new();
|
||||
#pragma warning disable CS0649 // _worker stays declared for the future async path; unused in synchronous mode.
|
||||
private Thread? _worker;
|
||||
#pragma warning restore CS0649
|
||||
private int _disposed;
|
||||
|
||||
public LandblockStreamer(Func<uint, LoadedLandblock?> loadLandblock)
|
||||
public LandblockStreamer(
|
||||
Func<uint, LoadedLandblock?> loadLandblock,
|
||||
Func<uint, LoadedLandblock?, AcDream.Core.Terrain.LandblockMeshData?>? buildMeshOrNull = null)
|
||||
{
|
||||
_loadLandblock = loadLandblock;
|
||||
_inbox = Channel.CreateUnbounded<LandblockStreamJob>(
|
||||
// Default: no mesh build (returns null → Failed result). Production
|
||||
// wires in LandblockMesh.Build via the T12 construction site.
|
||||
_buildMeshOrNull = buildMeshOrNull ?? ((_, _) => null);
|
||||
_inbox = Channel.CreateUnbounded<LandblockStreamJob>(
|
||||
new UnboundedChannelOptions { SingleReader = true, SingleWriter = false });
|
||||
_outbox = Channel.CreateUnbounded<LandblockStreamResult>(
|
||||
new UnboundedChannelOptions { SingleReader = true, SingleWriter = true });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// No-op in synchronous mode. Preserved on the API surface so callers
|
||||
/// don't need to change when async loading returns in Phase A.3.
|
||||
/// Activate the dedicated background worker thread. Idempotent and
|
||||
/// thread-safe: concurrent callers will only spawn one worker; subsequent
|
||||
/// calls are no-ops. Atomic via <see cref="Interlocked.CompareExchange{T}(ref T, T, T)"/>.
|
||||
/// </summary>
|
||||
public void Start()
|
||||
{
|
||||
if (System.Threading.Volatile.Read(ref _disposed) != 0)
|
||||
throw new ObjectDisposedException(nameof(LandblockStreamer));
|
||||
// No worker thread in synchronous mode.
|
||||
|
||||
// A.5 T10-T12 follow-up: atomically install the worker so concurrent
|
||||
// Start() callers don't both pass the null check and spawn duplicate
|
||||
// threads. Construct the candidate; CAS it into _worker; if we lost
|
||||
// the race, the candidate goes unstarted and is GCed.
|
||||
var candidate = new Thread(WorkerLoop)
|
||||
{
|
||||
IsBackground = true,
|
||||
Name = "acdream.streaming.worker",
|
||||
};
|
||||
if (Interlocked.CompareExchange(ref _worker, candidate, null) == null)
|
||||
candidate.Start();
|
||||
// else: another caller won the race; their thread is running.
|
||||
}
|
||||
|
||||
public void EnqueueLoad(uint landblockId)
|
||||
/// <summary>
|
||||
/// Non-blocking enqueue. The worker drains the inbox and posts a
|
||||
/// <see cref="LandblockStreamResult.Loaded"/> (or
|
||||
/// <see cref="LandblockStreamResult.Failed"/>) to the outbox.
|
||||
/// </summary>
|
||||
public void EnqueueLoad(uint landblockId, LandblockStreamJobKind kind = LandblockStreamJobKind.LoadNear)
|
||||
{
|
||||
if (System.Threading.Volatile.Read(ref _disposed) != 0)
|
||||
throw new ObjectDisposedException(nameof(LandblockStreamer));
|
||||
// Synchronous mode: invoke the load delegate inline. The result lands
|
||||
// in the outbox and DrainCompletions picks it up later in the same
|
||||
// (or next) frame.
|
||||
HandleJob(new LandblockStreamJob.Load(landblockId));
|
||||
_inbox.Writer.TryWrite(new LandblockStreamJob.Load(landblockId, kind));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Non-blocking enqueue. The worker posts a
|
||||
/// <see cref="LandblockStreamResult.Unloaded"/> to the outbox.
|
||||
/// </summary>
|
||||
public void EnqueueUnload(uint landblockId)
|
||||
{
|
||||
if (System.Threading.Volatile.Read(ref _disposed) != 0)
|
||||
throw new ObjectDisposedException(nameof(LandblockStreamer));
|
||||
HandleJob(new LandblockStreamJob.Unload(landblockId));
|
||||
_inbox.Writer.TryWrite(new LandblockStreamJob.Unload(landblockId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -118,17 +141,14 @@ public sealed class LandblockStreamer : IDisposable
|
|||
{
|
||||
try
|
||||
{
|
||||
// Synchronous read loop via .WaitToReadAsync + ReadAllAsync
|
||||
// would be idiomatic but requires async; the blocking reader
|
||||
// is simpler and the thread is dedicated anyway.
|
||||
// Safe to block: this is a dedicated worker thread with no
|
||||
// SynchronizationContext, so .Result/.GetResult cannot deadlock
|
||||
// against any captured continuation. Using the sync pattern
|
||||
// here keeps the loop linear; an async-enumerable alternative
|
||||
// would force WorkerLoop to be async Task and lose the
|
||||
// simple thread-start shape.
|
||||
while (!_cancel.Token.IsCancellationRequested)
|
||||
{
|
||||
// Safe to block: this is a dedicated worker thread with no
|
||||
// SynchronizationContext, so .Result/.GetResult cannot deadlock
|
||||
// against any captured continuation. Using the sync pattern
|
||||
// here keeps the loop linear; an async-enumerable alternative
|
||||
// would force WorkerLoop to be async Task and lose the
|
||||
// simple thread-start shape.
|
||||
if (!_inbox.Reader.WaitToReadAsync(_cancel.Token).AsTask().GetAwaiter().GetResult())
|
||||
break;
|
||||
|
||||
|
|
@ -157,15 +177,47 @@ public sealed class LandblockStreamer : IDisposable
|
|||
switch (job)
|
||||
{
|
||||
case LandblockStreamJob.Load load:
|
||||
// A.5 T26 follow-up (Bug A): far-tier LBs must NOT contribute
|
||||
// entities to GpuWorldState — that defeats the whole purpose of
|
||||
// the two-tier split. The factory still builds the full entity
|
||||
// layer (LandblockLoader + scenery generation + interior cells)
|
||||
// regardless of Kind because it doesn't know about JobKind today.
|
||||
// We strip Entities here for far-tier results so the render-
|
||||
// thread dispatcher walks only near-tier (~10K) entities, not
|
||||
// all (~71K) entities at radius=12.
|
||||
//
|
||||
// Wasted worker-thread CPU is acceptable (it's off the render
|
||||
// thread). A future optimization (TODO N.6 or A.6) plumbs Kind
|
||||
// through BuildLandblockForStreaming so the dat read + scenery
|
||||
// generation are skipped entirely for far-tier.
|
||||
try
|
||||
{
|
||||
var lb = _loadLandblock(load.LandblockId);
|
||||
if (lb is null)
|
||||
{
|
||||
_outbox.Writer.TryWrite(new LandblockStreamResult.Failed(
|
||||
load.LandblockId, "LandblockLoader.Load returned null"));
|
||||
else
|
||||
_outbox.Writer.TryWrite(new LandblockStreamResult.Loaded(
|
||||
load.LandblockId, lb));
|
||||
break;
|
||||
}
|
||||
var mesh = _buildMeshOrNull(load.LandblockId, lb);
|
||||
if (mesh is null)
|
||||
{
|
||||
_outbox.Writer.TryWrite(new LandblockStreamResult.Failed(
|
||||
load.LandblockId, "buildMeshOrNull returned null"));
|
||||
break;
|
||||
}
|
||||
var tier = load.Kind == LandblockStreamJobKind.LoadFar
|
||||
? LandblockStreamTier.Far : LandblockStreamTier.Near;
|
||||
if (tier == LandblockStreamTier.Far && lb.Entities.Count > 0)
|
||||
{
|
||||
// Strip entities — far-tier ships terrain only.
|
||||
lb = new LoadedLandblock(
|
||||
lb.LandblockId,
|
||||
lb.Heightmap,
|
||||
System.Array.Empty<AcDream.Core.World.WorldEntity>());
|
||||
}
|
||||
_outbox.Writer.TryWrite(new LandblockStreamResult.Loaded(
|
||||
load.LandblockId, tier, lb, mesh));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AcDream.Core.Terrain;
|
||||
using AcDream.Core.World;
|
||||
|
||||
namespace AcDream.App.Streaming;
|
||||
|
|
@ -16,15 +17,33 @@ namespace AcDream.App.Streaming;
|
|||
/// </summary>
|
||||
public sealed class StreamingController
|
||||
{
|
||||
private readonly Action<uint> _enqueueLoad;
|
||||
private readonly Action<uint, LandblockStreamJobKind> _enqueueLoad;
|
||||
private readonly Action<uint> _enqueueUnload;
|
||||
private readonly Func<int, IReadOnlyList<LandblockStreamResult>> _drainCompletions;
|
||||
private readonly Action<LoadedLandblock> _applyTerrain;
|
||||
private readonly Action<LoadedLandblock, LandblockMeshData> _applyTerrain;
|
||||
private readonly Action<uint>? _removeTerrain;
|
||||
private readonly GpuWorldState _state;
|
||||
private StreamingRegion? _region;
|
||||
|
||||
public int Radius { get; set; }
|
||||
/// <summary>
|
||||
/// Near-tier radius (LBs from observer that load full detail: terrain +
|
||||
/// scenery + entities). Set at construction; readable thereafter.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Mutating after the first <see cref="Tick"/> has no effect — the
|
||||
/// internal <see cref="StreamingRegion"/> snapshots both radii on its
|
||||
/// constructor. Treat as init-only post-Tick.
|
||||
/// </remarks>
|
||||
public int NearRadius { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Far-tier radius (LBs from observer that load terrain only). Set at
|
||||
/// construction; readable thereafter.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Mutating after the first <see cref="Tick"/> has no effect — see <see cref="NearRadius"/>.
|
||||
/// </remarks>
|
||||
public int FarRadius { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Cap on completions drained per <see cref="Tick"/> call. The cap is
|
||||
|
|
@ -45,12 +64,13 @@ public sealed class StreamingController
|
|||
public int MaxCompletionsPerFrame { get; set; } = 4;
|
||||
|
||||
public StreamingController(
|
||||
Action<uint> enqueueLoad,
|
||||
Action<uint, LandblockStreamJobKind> enqueueLoad,
|
||||
Action<uint> enqueueUnload,
|
||||
Func<int, IReadOnlyList<LandblockStreamResult>> drainCompletions,
|
||||
Action<LoadedLandblock> applyTerrain,
|
||||
Action<LoadedLandblock, LandblockMeshData> applyTerrain,
|
||||
GpuWorldState state,
|
||||
int radius,
|
||||
int nearRadius,
|
||||
int farRadius,
|
||||
Action<uint>? removeTerrain = null)
|
||||
{
|
||||
_enqueueLoad = enqueueLoad;
|
||||
|
|
@ -59,29 +79,42 @@ public sealed class StreamingController
|
|||
_applyTerrain = applyTerrain;
|
||||
_removeTerrain = removeTerrain;
|
||||
_state = state;
|
||||
Radius = radius;
|
||||
NearRadius = nearRadius;
|
||||
FarRadius = farRadius;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Advance one frame. <paramref name="observerCx"/>/<paramref name="observerCy"/>
|
||||
/// are landblock coordinates (0..255) of the current viewer — the camera
|
||||
/// in offline mode, the server-sent player position in live.
|
||||
///
|
||||
/// <para>Two-tier model (Phase A.5 T13):</para>
|
||||
/// <list type="bullet">
|
||||
/// <item><see cref="TwoTierDiff.ToLoadFar"/> → enqueue LoadFar (terrain only, no entities)</item>
|
||||
/// <item><see cref="TwoTierDiff.ToLoadNear"/> → enqueue LoadNear (terrain + entities)</item>
|
||||
/// <item><see cref="TwoTierDiff.ToPromote"/> → enqueue PromoteToNear (entity layer for already-loaded terrain)</item>
|
||||
/// <item><see cref="TwoTierDiff.ToDemote"/> → drop entities on render thread immediately (terrain stays)</item>
|
||||
/// <item><see cref="TwoTierDiff.ToUnload"/> → enqueue full unload</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
public void Tick(int observerCx, int observerCy)
|
||||
{
|
||||
// First-tick bootstrap: no region yet, so the whole visible window
|
||||
// is a load diff.
|
||||
if (_region is null)
|
||||
{
|
||||
_region = new StreamingRegion(observerCx, observerCy, Radius);
|
||||
foreach (var id in _region.Visible)
|
||||
_enqueueLoad(id);
|
||||
_region = new StreamingRegion(observerCx, observerCy, NearRadius, FarRadius);
|
||||
var bootstrap = _region.ComputeFirstTickDiff();
|
||||
foreach (var id in bootstrap.ToLoadFar) _enqueueLoad(id, LandblockStreamJobKind.LoadFar);
|
||||
foreach (var id in bootstrap.ToLoadNear) _enqueueLoad(id, LandblockStreamJobKind.LoadNear);
|
||||
_region.MarkResidentFromBootstrap();
|
||||
}
|
||||
else if (_region.CenterX != observerCx || _region.CenterY != observerCy)
|
||||
{
|
||||
var diff = _region.RecenterTo(observerCx, observerCy);
|
||||
foreach (var id in diff.ToLoad) _enqueueLoad(id);
|
||||
foreach (var id in diff.ToUnload) _enqueueUnload(id);
|
||||
foreach (var id in diff.ToLoadFar) _enqueueLoad(id, LandblockStreamJobKind.LoadFar);
|
||||
foreach (var id in diff.ToLoadNear) _enqueueLoad(id, LandblockStreamJobKind.LoadNear);
|
||||
foreach (var id in diff.ToPromote) _enqueueLoad(id, LandblockStreamJobKind.PromoteToNear);
|
||||
foreach (var id in diff.ToDemote) _state.RemoveEntitiesFromLandblock(id);
|
||||
foreach (var id in diff.ToUnload) _enqueueUnload(id);
|
||||
}
|
||||
|
||||
// Drain up to N completions per frame so a big diff doesn't spike
|
||||
|
|
@ -92,9 +125,12 @@ public sealed class StreamingController
|
|||
switch (result)
|
||||
{
|
||||
case LandblockStreamResult.Loaded loaded:
|
||||
_applyTerrain(loaded.Landblock);
|
||||
_applyTerrain(loaded.Landblock, loaded.MeshData);
|
||||
_state.AddLandblock(loaded.Landblock);
|
||||
break;
|
||||
case LandblockStreamResult.Promoted promoted:
|
||||
_state.AddEntitiesToExistingLandblock(promoted.LandblockId, promoted.Entities);
|
||||
break;
|
||||
case LandblockStreamResult.Unloaded unloaded:
|
||||
_state.RemoveLandblock(unloaded.LandblockId);
|
||||
_removeTerrain?.Invoke(unloaded.LandblockId);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace AcDream.App.Streaming;
|
||||
|
||||
|
|
@ -10,9 +11,11 @@ namespace AcDream.App.Streaming;
|
|||
/// </summary>
|
||||
public sealed class StreamingRegion
|
||||
{
|
||||
public int CenterX { get; private set; }
|
||||
public int CenterY { get; private set; }
|
||||
public int Radius { get; }
|
||||
public int CenterX { get; private set; }
|
||||
public int CenterY { get; private set; }
|
||||
public int Radius { get; }
|
||||
public int NearRadius { get; }
|
||||
public int FarRadius { get; }
|
||||
|
||||
// Strictly the (2r+1)×(2r+1) window (clamped to world bounds).
|
||||
private readonly HashSet<uint> _visible = new();
|
||||
|
|
@ -20,6 +23,16 @@ public sealed class StreamingRegion
|
|||
// Everything currently loaded: window + hysteresis-retained landblocks.
|
||||
private readonly HashSet<uint> _resident = new();
|
||||
|
||||
// Two-tier residence tracking: maps each resident LB to its current tier.
|
||||
private readonly Dictionary<uint, TierResidence> _tierResidence = new();
|
||||
|
||||
// Set true after MarkResidentFromBootstrap. The two-tier RecenterTo
|
||||
// requires this state to be seeded; calling RecenterTo before the
|
||||
// bootstrap silently emits the entire window as fresh loads (no demotes,
|
||||
// no unloads, since _tierResidence is empty), which is a correctness
|
||||
// hazard. The flag converts that into a loud InvalidOperationException.
|
||||
private bool _bootstrapped;
|
||||
|
||||
/// <summary>
|
||||
/// Landblock IDs in the current visible window in the AC 8.8 coordinate
|
||||
/// form: <c>(lbX << 24) | (lbY << 16) | 0xFFFF</c>. The trailing
|
||||
|
|
@ -43,12 +56,16 @@ public sealed class StreamingRegion
|
|||
/// </summary>
|
||||
public IReadOnlyCollection<uint> Resident => _resident;
|
||||
|
||||
public StreamingRegion(int cx, int cy, int radius)
|
||||
public StreamingRegion(int centerX, int centerY, int nearRadius, int farRadius)
|
||||
{
|
||||
Radius = radius;
|
||||
Recenter(cx, cy);
|
||||
NearRadius = nearRadius;
|
||||
FarRadius = farRadius;
|
||||
Radius = farRadius; // outer ring drives Resident bookkeeping
|
||||
Recenter(centerX, centerY);
|
||||
}
|
||||
|
||||
public StreamingRegion(int cx, int cy, int radius) : this(cx, cy, radius, radius) { }
|
||||
|
||||
private void Recenter(int cx, int cy)
|
||||
{
|
||||
CenterX = cx;
|
||||
|
|
@ -81,13 +98,197 @@ public sealed class StreamingRegion
|
|||
internal static uint EncodeLandblockId(int lbX, int lbY)
|
||||
=> ((uint)lbX << 24) | ((uint)lbY << 16) | 0xFFFFu;
|
||||
|
||||
/// <summary>
|
||||
/// First-tick bootstrap: emit ToLoadNear for every LB in the inner ring,
|
||||
/// ToLoadFar for every LB in the outer ring (between near and far). Used
|
||||
/// by <see cref="StreamingController.Tick"/> on the first call before any
|
||||
/// RecenterTo.
|
||||
/// </summary>
|
||||
public TwoTierDiff ComputeFirstTickDiff()
|
||||
{
|
||||
var near = new List<uint>();
|
||||
var far = new List<uint>();
|
||||
for (int dx = -FarRadius; dx <= FarRadius; dx++)
|
||||
{
|
||||
for (int dy = -FarRadius; dy <= FarRadius; dy++)
|
||||
{
|
||||
int nx = CenterX + dx;
|
||||
int ny = CenterY + dy;
|
||||
if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue;
|
||||
int absDx = System.Math.Abs(dx);
|
||||
int absDy = System.Math.Abs(dy);
|
||||
var id = EncodeLandblockId(nx, ny);
|
||||
if (absDx <= NearRadius && absDy <= NearRadius)
|
||||
near.Add(id);
|
||||
else
|
||||
far.Add(id);
|
||||
}
|
||||
}
|
||||
return new TwoTierDiff(
|
||||
ToLoadFar: far,
|
||||
ToLoadNear: near,
|
||||
ToPromote: System.Array.Empty<uint>(),
|
||||
ToDemote: System.Array.Empty<uint>(),
|
||||
ToUnload: System.Array.Empty<uint>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call once after <see cref="ComputeFirstTickDiff"/> to seed
|
||||
/// <c>_tierResidence</c> with the initial window. Every LB in the inner
|
||||
/// ring (Chebyshev ≤ NearRadius) is marked Near; everything else Far.
|
||||
/// </summary>
|
||||
public void MarkResidentFromBootstrap()
|
||||
{
|
||||
if (_bootstrapped)
|
||||
throw new InvalidOperationException(
|
||||
"MarkResidentFromBootstrap was already called; calling it again would " +
|
||||
"reset accumulated tier-residence state and silently drop differential " +
|
||||
"data built up by interim RecenterTo calls.");
|
||||
|
||||
_tierResidence.Clear();
|
||||
for (int dx = -FarRadius; dx <= FarRadius; dx++)
|
||||
{
|
||||
for (int dy = -FarRadius; dy <= FarRadius; dy++)
|
||||
{
|
||||
int nx = CenterX + dx;
|
||||
int ny = CenterY + dy;
|
||||
if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue;
|
||||
int absDx = Math.Abs(dx);
|
||||
int absDy = Math.Abs(dy);
|
||||
var id = EncodeLandblockId(nx, ny);
|
||||
_tierResidence[id] = (absDx <= NearRadius && absDy <= NearRadius)
|
||||
? TierResidence.Near
|
||||
: TierResidence.Far;
|
||||
}
|
||||
}
|
||||
_bootstrapped = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test-visible wrapper around <see cref="EncodeLandblockId"/> so test
|
||||
/// assemblies can build expected IDs without duplicating the encoding rule.
|
||||
/// </summary>
|
||||
internal static uint EncodeLandblockIdForTest(int lbX, int lbY)
|
||||
=> EncodeLandblockId(lbX, lbY);
|
||||
|
||||
/// <summary>
|
||||
/// Two-tier recenter: computes the 5-list diff per Phase A.5 spec §4.2.
|
||||
/// Hysteresis: NearRadius+2 for Near→Far demote; FarRadius+2 for Far→null
|
||||
/// unload. Requires <see cref="MarkResidentFromBootstrap"/> (or a prior
|
||||
/// call to this method) to have seeded <c>_tierResidence</c>.
|
||||
/// </summary>
|
||||
public TwoTierDiff RecenterTo(int newCx, int newCy)
|
||||
{
|
||||
if (!_bootstrapped)
|
||||
throw new InvalidOperationException(
|
||||
"Two-tier RecenterTo called before MarkResidentFromBootstrap. " +
|
||||
"First call ComputeFirstTickDiff to enqueue the bootstrap loads, " +
|
||||
"then MarkResidentFromBootstrap to seed _tierResidence, then RecenterTo " +
|
||||
"for subsequent observer moves.");
|
||||
|
||||
int nearUnloadThreshold = NearRadius + 2;
|
||||
int farUnloadThreshold = FarRadius + 2;
|
||||
|
||||
var toLoadFar = new List<uint>();
|
||||
var toLoadNear = new List<uint>();
|
||||
var toPromote = new List<uint>();
|
||||
var toDemote = new List<uint>();
|
||||
var toUnload = new List<uint>();
|
||||
|
||||
// Pass 1: walk new far window — emit ToLoadFar / ToLoadNear / ToPromote.
|
||||
var newCenterIds = new HashSet<uint>();
|
||||
for (int dx = -FarRadius; dx <= FarRadius; dx++)
|
||||
{
|
||||
for (int dy = -FarRadius; dy <= FarRadius; dy++)
|
||||
{
|
||||
int nx = newCx + dx;
|
||||
int ny = newCy + dy;
|
||||
if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue;
|
||||
int absDx = Math.Abs(dx);
|
||||
int absDy = Math.Abs(dy);
|
||||
bool inNear = absDx <= NearRadius && absDy <= NearRadius;
|
||||
var id = EncodeLandblockId(nx, ny);
|
||||
newCenterIds.Add(id);
|
||||
|
||||
if (!_tierResidence.TryGetValue(id, out var current))
|
||||
{
|
||||
// Not resident at all — fresh load.
|
||||
if (inNear) toLoadNear.Add(id);
|
||||
else toLoadFar.Add(id);
|
||||
_tierResidence[id] = inNear ? TierResidence.Near : TierResidence.Far;
|
||||
}
|
||||
else if (current == TierResidence.Far && inNear)
|
||||
{
|
||||
// Was Far, now inside Near ring — promote.
|
||||
toPromote.Add(id);
|
||||
_tierResidence[id] = TierResidence.Near;
|
||||
}
|
||||
// Near→Near and Far→Far are no-ops.
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 2: check previously-resident LBs for demote / unload.
|
||||
foreach (var kvp in _tierResidence.ToArray())
|
||||
{
|
||||
var id = kvp.Key;
|
||||
var current = kvp.Value;
|
||||
int lbX = (int)((id >> 24) & 0xFFu);
|
||||
int lbY = (int)((id >> 16) & 0xFFu);
|
||||
int absDx = Math.Abs(lbX - newCx);
|
||||
int absDy = Math.Abs(lbY - newCy);
|
||||
int distance = Math.Max(absDx, absDy); // Chebyshev
|
||||
|
||||
if (newCenterIds.Contains(id))
|
||||
{
|
||||
// Still in the far window — only Near→Far demote possible here.
|
||||
if (current == TierResidence.Near && (absDx > NearRadius || absDy > NearRadius))
|
||||
{
|
||||
if (distance > nearUnloadThreshold)
|
||||
{
|
||||
toDemote.Add(id);
|
||||
_tierResidence[id] = TierResidence.Far;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Outside the new window — demote / unload by threshold.
|
||||
if (current == TierResidence.Near)
|
||||
{
|
||||
if (distance > nearUnloadThreshold)
|
||||
{
|
||||
toDemote.Add(id);
|
||||
_tierResidence[id] = TierResidence.Far;
|
||||
if (distance > farUnloadThreshold)
|
||||
{
|
||||
toUnload.Add(id);
|
||||
_tierResidence.Remove(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (current == TierResidence.Far)
|
||||
{
|
||||
if (distance > farUnloadThreshold)
|
||||
{
|
||||
toUnload.Add(id);
|
||||
_tierResidence.Remove(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CenterX = newCx;
|
||||
CenterY = newCy;
|
||||
|
||||
return new TwoTierDiff(toLoadFar, toLoadNear, toPromote, toDemote, toUnload);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recompute the visible window around a new center and return the
|
||||
/// delta vs. the previous state. Hysteresis: landblocks aren't unloaded
|
||||
/// until they're further than <c>Radius + 2</c> from the new center,
|
||||
/// so boundary crossings don't thrash.
|
||||
/// </summary>
|
||||
public RegionDiff RecenterTo(int newCx, int newCy)
|
||||
public RegionDiff RecenterToSingleTier(int newCx, int newCy)
|
||||
{
|
||||
// Snapshot the old resident set so we can diff against it.
|
||||
var oldResident = new HashSet<uint>(_resident);
|
||||
|
|
@ -126,7 +327,7 @@ public sealed class StreamingRegion
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Output of <see cref="StreamingRegion.RecenterTo"/>: the landblocks to
|
||||
/// Output of <see cref="StreamingRegion.RecenterToSingleTier"/>: the landblocks to
|
||||
/// start loading (newly entered the visible window) and the landblocks to
|
||||
/// unload (fell outside the unload threshold, which is <c>Radius + 2</c>).
|
||||
/// Both lists are disjoint from the current <see cref="StreamingRegion.Visible"/>
|
||||
|
|
@ -135,3 +336,10 @@ public sealed class StreamingRegion
|
|||
public readonly record struct RegionDiff(
|
||||
IReadOnlyList<uint> ToLoad,
|
||||
IReadOnlyList<uint> ToUnload);
|
||||
|
||||
/// <summary>
|
||||
/// Tracks which tier a landblock currently occupies in the two-tier streaming
|
||||
/// model. Absence from the dictionary encodes "not resident"; the enum has no
|
||||
/// None member to avoid suggesting a third runtime state.
|
||||
/// </summary>
|
||||
internal enum TierResidence { Far, Near }
|
||||
|
|
|
|||
15
src/AcDream.App/Streaming/TwoTierDiff.cs
Normal file
15
src/AcDream.App/Streaming/TwoTierDiff.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace AcDream.App.Streaming;
|
||||
|
||||
/// <summary>
|
||||
/// Output of <see cref="StreamingRegion.RecenterTo"/> for the two-tier model.
|
||||
/// Five disjoint lists describe what changed since the previous Tick. Per
|
||||
/// Phase A.5 spec §4.2.
|
||||
/// </summary>
|
||||
public readonly record struct TwoTierDiff(
|
||||
IReadOnlyList<uint> ToLoadFar, // entered far window from null (terrain only)
|
||||
IReadOnlyList<uint> ToLoadNear, // entered near window from null (terrain + entities — first-tick or teleport)
|
||||
IReadOnlyList<uint> ToPromote, // entered near window from far-resident (entities only)
|
||||
IReadOnlyList<uint> ToDemote, // exited near window past hysteresis (drop entities)
|
||||
IReadOnlyList<uint> ToUnload); // exited far window past hysteresis (drop terrain)
|
||||
|
|
@ -46,7 +46,7 @@ public static class LandblockMesh
|
|||
uint landblockY,
|
||||
float[] heightTable,
|
||||
TerrainBlendingContext ctx,
|
||||
Dictionary<uint, SurfaceInfo> surfaceCache)
|
||||
System.Collections.Generic.IDictionary<uint, SurfaceInfo> surfaceCache)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(block);
|
||||
ArgumentNullException.ThrowIfNull(heightTable);
|
||||
|
|
@ -105,6 +105,10 @@ public static class LandblockMesh
|
|||
uint palCode = TerrainBlending.GetPalCode(
|
||||
rBL, rBR, rTR, rTL, ttBL, ttBR, ttTR, ttTL);
|
||||
|
||||
// Lookup-or-build pattern. Not atomic under concurrent access
|
||||
// (TryGetValue then assign), but BuildSurface is deterministic —
|
||||
// two workers building the same palCode produce equal SurfaceInfo,
|
||||
// last-write-wins is benign.
|
||||
if (!surfaceCache.TryGetValue(palCode, out var surf))
|
||||
{
|
||||
surf = TerrainBlending.BuildSurface(palCode, ctx);
|
||||
|
|
|
|||
|
|
@ -42,28 +42,32 @@ public static class LandblockLoader
|
|||
{
|
||||
if (!IsSupported(stab.Id))
|
||||
continue;
|
||||
result.Add(new WorldEntity
|
||||
var stabEntity = new WorldEntity
|
||||
{
|
||||
Id = nextId++,
|
||||
SourceGfxObjOrSetupId = stab.Id,
|
||||
Position = stab.Frame.Origin,
|
||||
Rotation = stab.Frame.Orientation,
|
||||
MeshRefs = Array.Empty<MeshRef>(),
|
||||
});
|
||||
};
|
||||
stabEntity.RefreshAabb(); // A.5 T18: populate cached AABB at construction
|
||||
result.Add(stabEntity);
|
||||
}
|
||||
|
||||
foreach (var building in info.Buildings)
|
||||
{
|
||||
if (!IsSupported(building.ModelId))
|
||||
continue;
|
||||
result.Add(new WorldEntity
|
||||
var buildingEntity = new WorldEntity
|
||||
{
|
||||
Id = nextId++,
|
||||
SourceGfxObjOrSetupId = building.ModelId,
|
||||
Position = building.Frame.Origin,
|
||||
Rotation = building.Frame.Orientation,
|
||||
MeshRefs = Array.Empty<MeshRef>(),
|
||||
});
|
||||
};
|
||||
buildingEntity.RefreshAabb(); // A.5 T18: populate cached AABB at construction
|
||||
result.Add(buildingEntity);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -71,6 +71,30 @@ public sealed class WorldEntity
|
|||
/// present. Zero (no parts hidden) is the default.
|
||||
/// </summary>
|
||||
public ulong HiddenPartsMask { get; init; }
|
||||
|
||||
// Per Phase A.5 spec §4.6 Change #2 — cache per-entity AABB so the
|
||||
// dispatcher's frustum cull is a memory read, not a per-frame recompute.
|
||||
// AabbDirty starts true so the dispatcher calls RefreshAabb on first read
|
||||
// (AabbMin/AabbMax are Vector3.Zero until refreshed).
|
||||
public Vector3 AabbMin { get; private set; }
|
||||
public Vector3 AabbMax { get; private set; }
|
||||
public bool AabbDirty { get; private set; } = true;
|
||||
|
||||
private const float DefaultAabbRadius = 5.0f;
|
||||
|
||||
public void RefreshAabb()
|
||||
{
|
||||
var p = Position;
|
||||
AabbMin = new Vector3(p.X - DefaultAabbRadius, p.Y - DefaultAabbRadius, p.Z - DefaultAabbRadius);
|
||||
AabbMax = new Vector3(p.X + DefaultAabbRadius, p.Y + DefaultAabbRadius, p.Z + DefaultAabbRadius);
|
||||
AabbDirty = false;
|
||||
}
|
||||
|
||||
public void SetPosition(Vector3 pos)
|
||||
{
|
||||
Position = pos;
|
||||
AabbDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using AcDream.UI.Abstractions.Settings;
|
||||
|
||||
namespace AcDream.UI.Abstractions.Panels.Settings;
|
||||
|
||||
|
|
@ -20,7 +21,8 @@ public sealed record DisplaySettings(
|
|||
bool VSync,
|
||||
float FieldOfView,
|
||||
float Gamma,
|
||||
bool ShowFps)
|
||||
bool ShowFps,
|
||||
QualityPreset Quality)
|
||||
{
|
||||
/// <summary>Values used on first launch / when settings.json is absent.
|
||||
/// All defaults pinned to the pre-L.0 runtime state — Resolution
|
||||
|
|
@ -35,7 +37,8 @@ public sealed record DisplaySettings(
|
|||
VSync: false,
|
||||
FieldOfView: 60f,
|
||||
Gamma: 1.0f,
|
||||
ShowFps: true);
|
||||
ShowFps: true,
|
||||
Quality: QualityPreset.High);
|
||||
|
||||
/// <summary>16:9 resolution presets offered in the dropdown.</summary>
|
||||
public static IReadOnlyList<string> AvailableResolutions { get; } = new[]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AcDream.UI.Abstractions.Input;
|
||||
using AcDream.UI.Abstractions.Settings;
|
||||
|
||||
namespace AcDream.UI.Abstractions.Panels.Settings;
|
||||
|
||||
|
|
@ -219,10 +220,23 @@ public sealed class SettingsPanel : IPanel
|
|||
if (renderer.Checkbox("Show FPS", ref showFps))
|
||||
_vm.SetDisplay(d with { ShowFps = showFps });
|
||||
|
||||
// A.5 T22.5: Quality preset dropdown. Drives streaming radii, MSAA,
|
||||
// anisotropic level, A2C, and max completions-per-frame as a unit.
|
||||
// Resolution + anisotropic + A2C + completions apply immediately via
|
||||
// ReapplyQualityPreset; MSAA samples require a restart (GL context
|
||||
// cannot change sample count at runtime).
|
||||
var presets = s_qualityPresetNames;
|
||||
int qIdx = (int)d.Quality;
|
||||
if (qIdx < 0 || qIdx >= presets.Length) qIdx = (int)QualityPreset.High;
|
||||
if (renderer.Combo("Quality", ref qIdx, presets))
|
||||
_vm.SetDisplay(d with { Quality = (QualityPreset)qIdx });
|
||||
|
||||
renderer.Spacing();
|
||||
renderer.TextWrapped(
|
||||
"Resolution / Fullscreen / V-Sync apply on Save. FOV + Gamma "
|
||||
+ "preview live as you drag; Cancel reverts to the saved value.");
|
||||
+ "preview live as you drag; Cancel reverts to the saved value. "
|
||||
+ "Quality preset applies streaming radius, anisotropic, and A2C "
|
||||
+ "immediately on Save; MSAA sample count requires a restart.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -446,6 +460,11 @@ public sealed class SettingsPanel : IPanel
|
|||
+ "round-trip lands.");
|
||||
}
|
||||
|
||||
// A.5 T22.5: preset label array parallel to QualityPreset enum values.
|
||||
// Order must match the enum (Low=0, Medium=1, High=2, Ultra=3).
|
||||
private static readonly string[] s_qualityPresetNames =
|
||||
{ "Low", "Medium", "High", "Ultra" };
|
||||
|
||||
private void RenderSection(IPanelRenderer renderer, string label, InputAction[] actions)
|
||||
{
|
||||
// Movement defaults open; other sections collapsed for first-run UX.
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using AcDream.UI.Abstractions.Settings;
|
||||
|
||||
namespace AcDream.UI.Abstractions.Panels.Settings;
|
||||
|
||||
|
|
@ -62,12 +63,13 @@ public sealed class SettingsStore
|
|||
|
||||
var d = DisplaySettings.Default;
|
||||
return new DisplaySettings(
|
||||
Resolution: ReadString (disp, "resolution", d.Resolution),
|
||||
Fullscreen: ReadBool (disp, "fullscreen", d.Fullscreen),
|
||||
VSync: ReadBool (disp, "vsync", d.VSync),
|
||||
FieldOfView: ReadFloat (disp, "fieldOfView", d.FieldOfView),
|
||||
Gamma: ReadFloat (disp, "gamma", d.Gamma),
|
||||
ShowFps: ReadBool (disp, "showFps", d.ShowFps));
|
||||
Resolution: ReadString (disp, "resolution", d.Resolution),
|
||||
Fullscreen: ReadBool (disp, "fullscreen", d.Fullscreen),
|
||||
VSync: ReadBool (disp, "vsync", d.VSync),
|
||||
FieldOfView: ReadFloat (disp, "fieldOfView", d.FieldOfView),
|
||||
Gamma: ReadFloat (disp, "gamma", d.Gamma),
|
||||
ShowFps: ReadBool (disp, "showFps", d.ShowFps),
|
||||
Quality: ReadQuality (disp, "quality", d.Quality));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -327,6 +329,7 @@ public sealed class SettingsStore
|
|||
["fieldOfView"] = d.FieldOfView,
|
||||
["fullscreen"] = d.Fullscreen,
|
||||
["gamma"] = d.Gamma,
|
||||
["quality"] = d.Quality.ToString(),
|
||||
["resolution"] = d.Resolution,
|
||||
["showFps"] = d.ShowFps,
|
||||
["vsync"] = d.VSync,
|
||||
|
|
@ -405,4 +408,12 @@ public sealed class SettingsStore
|
|||
private static float ReadFloat(JsonElement obj, string name, float fallback)
|
||||
=> obj.TryGetProperty(name, out var el) && el.ValueKind == JsonValueKind.Number
|
||||
? el.GetSingle() : fallback;
|
||||
|
||||
private static QualityPreset ReadQuality(JsonElement obj, string name, QualityPreset fallback)
|
||||
{
|
||||
if (!obj.TryGetProperty(name, out var el) || el.ValueKind != JsonValueKind.String)
|
||||
return fallback;
|
||||
var s = el.GetString();
|
||||
return Enum.TryParse<QualityPreset>(s, ignoreCase: true, out var v) ? v : fallback;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
67
src/AcDream.UI.Abstractions/Settings/QualityPreset.cs
Normal file
67
src/AcDream.UI.Abstractions/Settings/QualityPreset.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
namespace AcDream.UI.Abstractions.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// A.5 T22.5: single user-facing quality knob that drives streaming radii,
|
||||
/// MSAA samples, anisotropic level, alpha-to-coverage, and max completions
|
||||
/// per frame in a single setting. Individual fields can still be overridden
|
||||
/// by env vars (see <see cref="QualitySettings.WithEnvOverrides"/>).
|
||||
/// </summary>
|
||||
public enum QualityPreset { Low, Medium, High, Ultra }
|
||||
|
||||
/// <summary>
|
||||
/// Resolved per-preset quality parameters. Constructed via
|
||||
/// <see cref="From(QualityPreset)"/> then optionally overridden with
|
||||
/// <see cref="WithEnvOverrides(QualitySettings)"/> before applying to the
|
||||
/// renderer and streaming controller.
|
||||
/// </summary>
|
||||
public readonly record struct QualitySettings(
|
||||
int NearRadius,
|
||||
int FarRadius,
|
||||
int MsaaSamples, // 0 = off, 2, 4, 8
|
||||
int AnisotropicLevel, // 1 = off, 4, 8, 16
|
||||
bool AlphaToCoverage,
|
||||
int MaxCompletionsPerFrame)
|
||||
{
|
||||
/// <summary>
|
||||
/// Return the default <see cref="QualitySettings"/> for <paramref name="preset"/>.
|
||||
/// Unknown enum values fall back to <see cref="QualityPreset.High"/>.
|
||||
/// </summary>
|
||||
public static QualitySettings From(QualityPreset preset) => preset switch
|
||||
{
|
||||
QualityPreset.Low => new(NearRadius: 2, FarRadius: 5, MsaaSamples: 0, AnisotropicLevel: 4, AlphaToCoverage: false, MaxCompletionsPerFrame: 2),
|
||||
QualityPreset.Medium => new(NearRadius: 3, FarRadius: 8, MsaaSamples: 2, AnisotropicLevel: 8, AlphaToCoverage: false, MaxCompletionsPerFrame: 3),
|
||||
QualityPreset.High => new(NearRadius: 4, FarRadius: 12, MsaaSamples: 4, AnisotropicLevel: 16, AlphaToCoverage: true, MaxCompletionsPerFrame: 4),
|
||||
QualityPreset.Ultra => new(NearRadius: 5, FarRadius: 15, MsaaSamples: 4, AnisotropicLevel: 16, AlphaToCoverage: true, MaxCompletionsPerFrame: 6),
|
||||
_ => From(QualityPreset.High),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Apply env-var overrides to a preset's resolved settings. Per-field
|
||||
/// env vars beat the preset (so devs can spot-test a single dimension).
|
||||
/// Unset or empty env vars leave the preset default unchanged.
|
||||
/// </summary>
|
||||
public static QualitySettings WithEnvOverrides(QualitySettings baseSettings)
|
||||
{
|
||||
int nearRadius = TryParseEnvInt("ACDREAM_NEAR_RADIUS", baseSettings.NearRadius);
|
||||
int farRadius = TryParseEnvInt("ACDREAM_FAR_RADIUS", baseSettings.FarRadius);
|
||||
int msaa = TryParseEnvInt("ACDREAM_MSAA_SAMPLES", baseSettings.MsaaSamples);
|
||||
int aniso = TryParseEnvInt("ACDREAM_ANISOTROPIC", baseSettings.AnisotropicLevel);
|
||||
// Bool override: any non-empty value other than "0"/"false" enables A2C.
|
||||
// Empty / unset → keep preset default.
|
||||
var a2cEnv = System.Environment.GetEnvironmentVariable("ACDREAM_A2C");
|
||||
bool a2c = a2cEnv switch
|
||||
{
|
||||
null or "" => baseSettings.AlphaToCoverage,
|
||||
"0" or "false" or "False" or "FALSE" => false,
|
||||
_ => true,
|
||||
};
|
||||
int completions = TryParseEnvInt("ACDREAM_MAX_COMPLETIONS_PER_FRAME", baseSettings.MaxCompletionsPerFrame);
|
||||
return new QualitySettings(nearRadius, farRadius, msaa, aniso, a2c, completions);
|
||||
}
|
||||
|
||||
private static int TryParseEnvInt(string name, int defaultValue)
|
||||
{
|
||||
var s = System.Environment.GetEnvironmentVariable(name);
|
||||
return s is not null && int.TryParse(s, out var v) ? v : defaultValue;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
using AcDream.App.Rendering.Wb;
|
||||
using AcDream.Core.Meshing;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.Rendering.Wb;
|
||||
|
||||
/// <summary>
|
||||
/// A.5 T21: lock in the depth-write attribution per translucency kind.
|
||||
/// <para>
|
||||
/// <c>WbDrawDispatcher.Draw</c> uses a two-pass structure:
|
||||
/// <list type="bullet">
|
||||
/// <item>Opaque pass — <c>DepthMask(true)</c>: writes depth so that
|
||||
/// later transparent geometry sorts correctly against solid surfaces.</item>
|
||||
/// <item>Transparent pass — <c>DepthMask(false)</c>: reads depth but
|
||||
/// does NOT write it, so alpha-blended surfaces don't occlude each
|
||||
/// other by Z-fighting.</item>
|
||||
/// </list>
|
||||
/// The partition that decides which pass a batch enters is
|
||||
/// <see cref="WbDrawDispatcher.IsOpaquePublic"/>:
|
||||
/// <c>Opaque</c> and <c>ClipMap</c> go to the opaque pass (depth write);
|
||||
/// <c>AlphaBlend</c>, <c>Additive</c>, <c>InvAlpha</c> go to the
|
||||
/// transparent pass (no depth write).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed class WbDispatcherDepthMaskTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(TranslucencyKind.Opaque, true)] // opaque pass — depth write
|
||||
[InlineData(TranslucencyKind.ClipMap, true)] // foliage — depth write (binary alpha / A2C)
|
||||
[InlineData(TranslucencyKind.AlphaBlend, false)] // transparent — no depth write
|
||||
[InlineData(TranslucencyKind.Additive, false)]
|
||||
[InlineData(TranslucencyKind.InvAlpha, false)]
|
||||
public void IsOpaquePartition_ImpliesDepthWriteAttribution(
|
||||
TranslucencyKind kind, bool expectsDepthWrite)
|
||||
{
|
||||
bool isOpaque = WbDrawDispatcher.IsOpaquePublic(kind);
|
||||
Assert.Equal(expectsDepthWrite, isOpaque);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,354 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using AcDream.App.Rendering;
|
||||
using AcDream.App.Rendering.Wb;
|
||||
using AcDream.Core.Meshing;
|
||||
using AcDream.Core.World;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.Rendering.Wb;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for <see cref="WbDrawDispatcher.WalkEntities"/> — the pure-CPU
|
||||
/// visibility filter extracted in A.5 T17. These tests exercise the two
|
||||
/// key perf changes from Phase A.5 spec §4.6:
|
||||
///
|
||||
/// <list type="bullet">
|
||||
/// <item>Change #1 (T17): invisible LB + animated set → iterate
|
||||
/// <c>animatedEntityIds</c> directly, not the full entity list.</item>
|
||||
/// <item>Change #2 (T18): per-entity AABB cull reads the cached AABB
|
||||
/// (<see cref="WorldEntity.AabbMin"/>/<c>AabbMax</c>) rather than
|
||||
/// recomputing Position±5 per frame.</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
public sealed class WbDrawDispatcherBucketingTests
|
||||
{
|
||||
// ── helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
private static WorldEntity MakeEntity(uint id, Vector3 position)
|
||||
=> new WorldEntity
|
||||
{
|
||||
Id = id,
|
||||
SourceGfxObjOrSetupId = 0,
|
||||
Position = position,
|
||||
Rotation = Quaternion.Identity,
|
||||
MeshRefs = System.Array.Empty<MeshRef>(),
|
||||
};
|
||||
|
||||
private static WorldEntity MakeEntityWithMesh(uint id, Vector3 position)
|
||||
=> new WorldEntity
|
||||
{
|
||||
Id = id,
|
||||
SourceGfxObjOrSetupId = 0,
|
||||
Position = position,
|
||||
Rotation = Quaternion.Identity,
|
||||
// Single dummy MeshRef so it passes the MeshRefs.Count == 0 guard.
|
||||
MeshRefs = new[] { new MeshRef { GfxObjId = 0x01000001u } },
|
||||
};
|
||||
|
||||
private static Dictionary<uint, WorldEntity> BuildById(IEnumerable<WorldEntity> entities)
|
||||
{
|
||||
var d = new Dictionary<uint, WorldEntity>();
|
||||
foreach (var e in entities) d[e.Id] = e;
|
||||
return d;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A frustum positioned at (1e6+1, 1e6+1, 1e6+1) looking toward (1e6, 1e6, 1e6)
|
||||
/// with a very narrow near/far. Any AABB near the origin (0..20000) is
|
||||
/// far behind the near plane and fails all six planes.
|
||||
/// </summary>
|
||||
private static FrustumPlanes MakeFarAwayFrustum()
|
||||
{
|
||||
var view = Matrix4x4.CreateLookAt(
|
||||
new Vector3(1e6f + 1f, 1e6f + 1f, 1e6f + 1f),
|
||||
new Vector3(1e6f, 1e6f, 1e6f),
|
||||
Vector3.UnitZ);
|
||||
var proj = Matrix4x4.CreatePerspectiveFieldOfView(
|
||||
MathF.PI / 4f, 1f, 0.1f, 1f);
|
||||
return FrustumPlanes.FromViewProjection(view * proj);
|
||||
}
|
||||
|
||||
// ── T17 Change #1 tests ───────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void WalkEntities_InvisibleLb_NoAnimated_SkipsEntireBlock()
|
||||
{
|
||||
// When LB is invisible AND animatedEntityIds is empty/null,
|
||||
// WalkEntities should not walk any entities at all.
|
||||
var entities = new List<WorldEntity>();
|
||||
for (int i = 0; i < 500; i++)
|
||||
entities.Add(MakeEntityWithMesh((uint)i, new Vector3(i, 0, 0)));
|
||||
|
||||
var byId = BuildById(entities);
|
||||
var entries = new[]
|
||||
{
|
||||
new WbDrawDispatcher.LandblockEntry(
|
||||
0xAAAA_FFFFu,
|
||||
new Vector3(10000, 10000, 10000),
|
||||
new Vector3(20000, 20000, 20000),
|
||||
entities,
|
||||
byId),
|
||||
};
|
||||
|
||||
var result = WbDrawDispatcher.WalkEntities(
|
||||
entries,
|
||||
frustum: MakeFarAwayFrustum(),
|
||||
neverCullLandblockId: null,
|
||||
visibleCellIds: null,
|
||||
animatedEntityIds: null);
|
||||
|
||||
Assert.Equal(0, result.EntitiesWalked);
|
||||
Assert.Empty(result.ToDraw);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WalkEntities_InvisibleLb_AnimatedSet_WalksOnlyAnimatedEntities()
|
||||
{
|
||||
// 1000 entities in an LB whose AABB is far outside the frustum.
|
||||
// Only entity Id=42 is in animatedEntityIds.
|
||||
// Pre-T17 behavior: walk all 1000 entities just to find #42.
|
||||
// Post-T17: walk only the 1 animated entity (EntitiesWalked == 1).
|
||||
const int Total = 1000;
|
||||
var entities = new List<WorldEntity>(Total);
|
||||
for (int i = 0; i < Total; i++)
|
||||
entities.Add(MakeEntityWithMesh((uint)i, new Vector3(i, 0, 0)));
|
||||
|
||||
var byId = BuildById(entities);
|
||||
var animatedSet = new HashSet<uint> { 42 };
|
||||
|
||||
var entries = new[]
|
||||
{
|
||||
new WbDrawDispatcher.LandblockEntry(
|
||||
0xAAAA_FFFFu,
|
||||
new Vector3(10000, 10000, 10000),
|
||||
new Vector3(20000, 20000, 20000),
|
||||
entities,
|
||||
byId),
|
||||
};
|
||||
|
||||
var result = WbDrawDispatcher.WalkEntities(
|
||||
entries,
|
||||
frustum: MakeFarAwayFrustum(),
|
||||
neverCullLandblockId: null,
|
||||
visibleCellIds: null,
|
||||
animatedEntityIds: animatedSet);
|
||||
|
||||
// Only the 1 animated entity should be walked — not 1000.
|
||||
Assert.Equal(1, result.EntitiesWalked);
|
||||
Assert.Single(result.ToDraw);
|
||||
Assert.Equal(42u, result.ToDraw[0].Entity.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WalkEntities_InvisibleLb_AnimatedIdAbsent_ZeroWalked()
|
||||
{
|
||||
// Animated entity ids 200 and 300 are NOT in this LB (which only
|
||||
// has ids 0..99). Should produce zero walks.
|
||||
var entities = new List<WorldEntity>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
entities.Add(MakeEntityWithMesh((uint)i, Vector3.Zero));
|
||||
|
||||
var byId = BuildById(entities);
|
||||
var animatedSet = new HashSet<uint> { 200, 300 }; // not in this LB
|
||||
|
||||
var entries = new[]
|
||||
{
|
||||
new WbDrawDispatcher.LandblockEntry(
|
||||
0xBBBB_FFFFu,
|
||||
new Vector3(10000, 10000, 10000),
|
||||
new Vector3(20000, 20000, 20000),
|
||||
entities,
|
||||
byId),
|
||||
};
|
||||
|
||||
var result = WbDrawDispatcher.WalkEntities(
|
||||
entries,
|
||||
frustum: MakeFarAwayFrustum(),
|
||||
neverCullLandblockId: null,
|
||||
visibleCellIds: null,
|
||||
animatedEntityIds: animatedSet);
|
||||
|
||||
Assert.Equal(0, result.EntitiesWalked);
|
||||
Assert.Empty(result.ToDraw);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WalkEntities_NeverCullLb_WalksAllEntitiesRegardlessOfFrustum()
|
||||
{
|
||||
// neverCullLandblockId bypasses the LB AABB check entirely.
|
||||
// All entities with at least one MeshRef should be walked.
|
||||
var entities = new List<WorldEntity>
|
||||
{
|
||||
MakeEntityWithMesh(1, Vector3.Zero),
|
||||
MakeEntityWithMesh(2, Vector3.Zero),
|
||||
MakeEntityWithMesh(3, Vector3.Zero),
|
||||
};
|
||||
|
||||
var byId = BuildById(entities);
|
||||
const uint lbId = 0xCCCC_FFFFu;
|
||||
|
||||
var entries = new[]
|
||||
{
|
||||
new WbDrawDispatcher.LandblockEntry(
|
||||
lbId,
|
||||
new Vector3(10000, 10000, 10000), // AABB would fail frustum
|
||||
new Vector3(20000, 20000, 20000),
|
||||
entities,
|
||||
byId),
|
||||
};
|
||||
|
||||
var result = WbDrawDispatcher.WalkEntities(
|
||||
entries,
|
||||
frustum: MakeFarAwayFrustum(),
|
||||
neverCullLandblockId: lbId, // exempt from LB cull
|
||||
visibleCellIds: null,
|
||||
animatedEntityIds: null);
|
||||
|
||||
Assert.Equal(3, result.EntitiesWalked);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WalkEntities_NullFrustum_WalksEntitiesWithMeshRefs()
|
||||
{
|
||||
// Null frustum means no culling — all entities with MeshRefs pass.
|
||||
// Entities without MeshRefs are still filtered out.
|
||||
var entities = new List<WorldEntity>
|
||||
{
|
||||
MakeEntityWithMesh(1, Vector3.Zero),
|
||||
MakeEntity(2, Vector3.Zero), // no MeshRefs — must be skipped
|
||||
MakeEntityWithMesh(3, Vector3.Zero),
|
||||
};
|
||||
|
||||
var byId = BuildById(entities);
|
||||
var entries = new[]
|
||||
{
|
||||
new WbDrawDispatcher.LandblockEntry(
|
||||
0xDDDD_FFFFu, Vector3.Zero, Vector3.Zero,
|
||||
entities, byId),
|
||||
};
|
||||
|
||||
var result = WbDrawDispatcher.WalkEntities(
|
||||
entries,
|
||||
frustum: null,
|
||||
neverCullLandblockId: null,
|
||||
visibleCellIds: null,
|
||||
animatedEntityIds: null);
|
||||
|
||||
Assert.Equal(2, result.EntitiesWalked);
|
||||
Assert.Equal(2, result.ToDraw.Count);
|
||||
}
|
||||
|
||||
// ── T18 Change #2 tests ───────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void WalkEntities_VisibleLb_EntityFarAway_CulledViaCachedAabb()
|
||||
{
|
||||
// LB passes the LB-level cull; entity AABB is far from the frustum.
|
||||
// After RefreshAabb the entity should be culled by the per-entity check.
|
||||
var entity = MakeEntityWithMesh(1, new Vector3(50000, 50000, 50000));
|
||||
entity.RefreshAabb(); // populate cached AABB at (50000±5)
|
||||
|
||||
var byId = BuildById(new[] { entity });
|
||||
var entries = new[]
|
||||
{
|
||||
// LB AABB near origin so it passes the LB cull; entity is far away.
|
||||
new WbDrawDispatcher.LandblockEntry(
|
||||
0xEEEE_FFFFu,
|
||||
new Vector3(-10, -10, -10),
|
||||
new Vector3(10, 10, 10),
|
||||
new List<WorldEntity> { entity },
|
||||
byId),
|
||||
};
|
||||
|
||||
// Frustum centered at origin, range ±100.
|
||||
var view = Matrix4x4.CreateLookAt(new Vector3(-50, 0, 0), Vector3.Zero, Vector3.UnitZ);
|
||||
var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 2f, 1f, 0.5f, 100f);
|
||||
var tightFrustum = FrustumPlanes.FromViewProjection(view * proj);
|
||||
|
||||
var result = WbDrawDispatcher.WalkEntities(
|
||||
entries,
|
||||
frustum: tightFrustum,
|
||||
neverCullLandblockId: null,
|
||||
visibleCellIds: null,
|
||||
animatedEntityIds: null);
|
||||
|
||||
// Entity at (50000,50000,50000) is outside the frustum — should be culled.
|
||||
Assert.Equal(0, result.EntitiesWalked);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WalkEntities_AnimatedEntity_BypassesPerEntityAabbCull()
|
||||
{
|
||||
// Animated entities must always pass even if their AABB would be culled.
|
||||
var entity = MakeEntityWithMesh(7, new Vector3(50000, 50000, 50000));
|
||||
entity.RefreshAabb();
|
||||
|
||||
var byId = BuildById(new[] { entity });
|
||||
var entries = new[]
|
||||
{
|
||||
new WbDrawDispatcher.LandblockEntry(
|
||||
0xEEEF_FFFFu,
|
||||
new Vector3(-10, -10, -10),
|
||||
new Vector3(10, 10, 10),
|
||||
new List<WorldEntity> { entity },
|
||||
byId),
|
||||
};
|
||||
|
||||
var view = Matrix4x4.CreateLookAt(new Vector3(-50, 0, 0), Vector3.Zero, Vector3.UnitZ);
|
||||
var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 2f, 1f, 0.5f, 100f);
|
||||
var tightFrustum = FrustumPlanes.FromViewProjection(view * proj);
|
||||
|
||||
var animatedSet = new HashSet<uint> { 7 };
|
||||
|
||||
var result = WbDrawDispatcher.WalkEntities(
|
||||
entries,
|
||||
frustum: tightFrustum,
|
||||
neverCullLandblockId: null,
|
||||
visibleCellIds: null,
|
||||
animatedEntityIds: animatedSet);
|
||||
|
||||
// Animated entity bypasses per-entity cull.
|
||||
Assert.Equal(1, result.EntitiesWalked);
|
||||
Assert.Single(result.ToDraw);
|
||||
Assert.Equal(7u, result.ToDraw[0].Entity.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WalkEntities_AabbDirty_RefreshedLazilyBeforeCull()
|
||||
{
|
||||
// An entity with AabbDirty=true (initial state) should get its AABB
|
||||
// refreshed lazily by WalkEntities before the cull check.
|
||||
var entity = MakeEntityWithMesh(5, new Vector3(0, 0, 0));
|
||||
// AabbDirty starts true by default — do NOT call RefreshAabb manually.
|
||||
Assert.True(entity.AabbDirty);
|
||||
|
||||
var byId = BuildById(new[] { entity });
|
||||
var entries = new[]
|
||||
{
|
||||
new WbDrawDispatcher.LandblockEntry(
|
||||
0xF0F0_FFFFu,
|
||||
new Vector3(-10, -10, -10),
|
||||
new Vector3(10, 10, 10),
|
||||
new List<WorldEntity> { entity },
|
||||
byId),
|
||||
};
|
||||
|
||||
// A frustum that accepts things near origin.
|
||||
var view = Matrix4x4.CreateLookAt(new Vector3(-50, 0, 0), Vector3.Zero, Vector3.UnitZ);
|
||||
var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 2f, 1f, 0.1f, 200f);
|
||||
var nearOriginFrustum = FrustumPlanes.FromViewProjection(view * proj);
|
||||
|
||||
var result = WbDrawDispatcher.WalkEntities(
|
||||
entries,
|
||||
frustum: nearOriginFrustum,
|
||||
neverCullLandblockId: null,
|
||||
visibleCellIds: null,
|
||||
animatedEntityIds: null);
|
||||
|
||||
// Entity at origin is inside the frustum after lazy RefreshAabb.
|
||||
Assert.Equal(1, result.EntitiesWalked);
|
||||
// AabbDirty should have been cleared by the lazy refresh.
|
||||
Assert.False(entity.AabbDirty);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
using System.Linq;
|
||||
using AcDream.App.Streaming;
|
||||
using AcDream.Core.World;
|
||||
using DatReaderWriter.DBObjs;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.Streaming;
|
||||
|
||||
public class GpuWorldStateTwoTierTests
|
||||
{
|
||||
private static LoadedLandblock MakeStubLandblock(uint canonicalId, params WorldEntity[] entities)
|
||||
=> new(canonicalId, new LandBlock(), entities);
|
||||
|
||||
private static WorldEntity MakeStubEntity(uint id)
|
||||
=> new()
|
||||
{
|
||||
Id = id,
|
||||
SourceGfxObjOrSetupId = 0x01000001u,
|
||||
Position = System.Numerics.Vector3.Zero,
|
||||
Rotation = System.Numerics.Quaternion.Identity,
|
||||
MeshRefs = System.Array.Empty<MeshRef>(),
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void RemoveEntitiesFromLandblock_KeepsLandblockButDropsEntities()
|
||||
{
|
||||
var state = new GpuWorldState();
|
||||
var lb = MakeStubLandblock(0xAAAAFFFFu,
|
||||
MakeStubEntity(1),
|
||||
MakeStubEntity(2));
|
||||
state.AddLandblock(lb);
|
||||
Assert.Equal(2, state.Entities.Count);
|
||||
|
||||
state.RemoveEntitiesFromLandblock(0xAAAAFFFFu);
|
||||
|
||||
Assert.Empty(state.Entities);
|
||||
Assert.True(state.IsLoaded(0xAAAAFFFFu)); // landblock still resident
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddEntitiesToExistingLandblock_MergesIntoExistingRecord()
|
||||
{
|
||||
var state = new GpuWorldState();
|
||||
var lb = MakeStubLandblock(0xAAAAFFFFu, MakeStubEntity(1));
|
||||
state.AddLandblock(lb);
|
||||
|
||||
state.AddEntitiesToExistingLandblock(0xAAAAFFFFu, new[]
|
||||
{
|
||||
MakeStubEntity(2),
|
||||
MakeStubEntity(3),
|
||||
});
|
||||
|
||||
Assert.Equal(3, state.Entities.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddEntitiesToExistingLandblock_LandblockNotYetLoaded_ParksInPending()
|
||||
{
|
||||
var state = new GpuWorldState();
|
||||
|
||||
// Landblock not loaded yet.
|
||||
state.AddEntitiesToExistingLandblock(0xAAAAFFFFu, new[]
|
||||
{
|
||||
MakeStubEntity(1),
|
||||
MakeStubEntity(2),
|
||||
});
|
||||
|
||||
// Nothing in the flat view yet.
|
||||
Assert.Empty(state.Entities);
|
||||
Assert.Equal(2, state.PendingLiveEntityCount);
|
||||
|
||||
// Now load the landblock — pending entities should merge in.
|
||||
state.AddLandblock(MakeStubLandblock(0xAAAAFFFFu));
|
||||
Assert.Equal(2, state.Entities.Count);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,9 +19,13 @@ public class LandblockStreamerTests
|
|||
0xA9B4FFFEu,
|
||||
new LandBlock(),
|
||||
System.Array.Empty<WorldEntity>());
|
||||
var stubMesh = new AcDream.Core.Terrain.LandblockMeshData(
|
||||
System.Array.Empty<AcDream.Core.Terrain.TerrainVertex>(),
|
||||
System.Array.Empty<uint>());
|
||||
|
||||
using var streamer = new LandblockStreamer(
|
||||
loadLandblock: id => id == 0xA9B4FFFEu ? stubLandblock : null);
|
||||
loadLandblock: id => id == 0xA9B4FFFEu ? stubLandblock : null,
|
||||
buildMeshOrNull: (_, _) => stubMesh);
|
||||
|
||||
streamer.Start();
|
||||
streamer.EnqueueLoad(0xA9B4FFFEu);
|
||||
|
|
@ -62,6 +66,39 @@ public class LandblockStreamerTests
|
|||
Assert.IsType<LandblockStreamResult.Failed>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Load_WhenBuildMeshReturnsNull_ReportsFailed()
|
||||
{
|
||||
// Phase A.5 T10-T12 follow-up: the mesh-build factory may return
|
||||
// null (e.g., LandBlock dat missing or corrupt). The worker must
|
||||
// emit Failed in that case instead of constructing Loaded with a
|
||||
// null MeshData (which would NRE downstream).
|
||||
var stubLandblock = new LoadedLandblock(
|
||||
0xABCDFFFEu,
|
||||
new LandBlock(),
|
||||
System.Array.Empty<WorldEntity>());
|
||||
|
||||
using var streamer = new LandblockStreamer(
|
||||
loadLandblock: _ => stubLandblock,
|
||||
buildMeshOrNull: (_, _) => null); // mesh-build returns null
|
||||
|
||||
streamer.Start();
|
||||
streamer.EnqueueLoad(0xABCDFFFEu);
|
||||
|
||||
LandblockStreamResult? result = null;
|
||||
for (int i = 0; i < SpinMaxIterations && result is null; i++)
|
||||
{
|
||||
var drained = streamer.DrainCompletions(LandblockStreamer.DefaultDrainBatchSize);
|
||||
if (drained.Count > 0) result = drained[0];
|
||||
else await Task.Delay(SpinStepMs);
|
||||
}
|
||||
|
||||
Assert.NotNull(result);
|
||||
var failed = Assert.IsType<LandblockStreamResult.Failed>(result);
|
||||
Assert.Equal(0xABCDFFFEu, failed.LandblockId);
|
||||
Assert.Contains("mesh", failed.Error, System.StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Load_WhenLoaderThrows_ReportsFailedWithMessage()
|
||||
{
|
||||
|
|
@ -104,37 +141,46 @@ public class LandblockStreamerTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void Load_ExecutesLoaderSynchronously_OnCallingThread()
|
||||
public async Task Load_ExecutesLoaderOnWorkerThread()
|
||||
{
|
||||
// Streamer was made synchronous after Phase A.1 visual verification
|
||||
// exposed concurrent dat reads as the cause of "ball of spikes"
|
||||
// terrain corruption — DatReaderWriter's DatCollection isn't
|
||||
// thread-safe and locking around every dat read on every render-
|
||||
// thread code path was too invasive. Until Phase A.3 introduces a
|
||||
// thread-safe dat wrapper, the load delegate runs on the calling
|
||||
// thread and the result is in the outbox by the time EnqueueLoad
|
||||
// returns. This test pins that contract.
|
||||
// Phase A.5 T11: the load delegate now runs on the dedicated worker
|
||||
// thread (not the calling/render thread). This test verifies the
|
||||
// async hand-off: EnqueueLoad returns immediately and the result
|
||||
// appears in the outbox only after the worker processes the inbox.
|
||||
int testThreadId = System.Environment.CurrentManagedThreadId;
|
||||
int? loaderThreadId = null;
|
||||
var stubLandblock = new LoadedLandblock(
|
||||
0x77770FFEu,
|
||||
new LandBlock(),
|
||||
System.Array.Empty<WorldEntity>());
|
||||
var stubMesh = new AcDream.Core.Terrain.LandblockMeshData(
|
||||
System.Array.Empty<AcDream.Core.Terrain.TerrainVertex>(),
|
||||
System.Array.Empty<uint>());
|
||||
|
||||
using var streamer = new LandblockStreamer(loadLandblock: id =>
|
||||
{
|
||||
loaderThreadId = System.Environment.CurrentManagedThreadId;
|
||||
return stubLandblock;
|
||||
});
|
||||
using var streamer = new LandblockStreamer(
|
||||
loadLandblock: id =>
|
||||
{
|
||||
loaderThreadId = System.Environment.CurrentManagedThreadId;
|
||||
return stubLandblock;
|
||||
},
|
||||
buildMeshOrNull: (_, _) => stubMesh);
|
||||
|
||||
streamer.Start();
|
||||
streamer.EnqueueLoad(0x77770FFEu);
|
||||
|
||||
// Result is already in the outbox — no spinning needed.
|
||||
var drained = streamer.DrainCompletions(LandblockStreamer.DefaultDrainBatchSize);
|
||||
// Spin until the worker produces a completion.
|
||||
LandblockStreamResult? result = null;
|
||||
for (int i = 0; i < SpinMaxIterations && result is null; i++)
|
||||
{
|
||||
var drained = streamer.DrainCompletions(LandblockStreamer.DefaultDrainBatchSize);
|
||||
if (drained.Count > 0) result = drained[0];
|
||||
else await Task.Delay(SpinStepMs);
|
||||
}
|
||||
|
||||
Assert.Single(drained);
|
||||
Assert.IsType<LandblockStreamResult.Loaded>(drained[0]);
|
||||
Assert.Equal(testThreadId, loaderThreadId);
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<LandblockStreamResult.Loaded>(result);
|
||||
// The loader MUST have run on a different thread than the test thread.
|
||||
Assert.NotNull(loaderThreadId);
|
||||
Assert.NotEqual(testThreadId, loaderThreadId.Value);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ public class StreamingControllerTests
|
|||
public List<uint> Unloads { get; } = new();
|
||||
public Queue<LandblockStreamResult> Pending { get; } = new();
|
||||
|
||||
public void EnqueueLoad(uint id) => Loads.Add(id);
|
||||
public void EnqueueLoad(uint id, LandblockStreamJobKind _) => Loads.Add(id);
|
||||
public void EnqueueUnload(uint id) => Unloads.Add(id);
|
||||
public IReadOnlyList<LandblockStreamResult> DrainCompletions(int max)
|
||||
{
|
||||
|
|
@ -34,14 +34,15 @@ public class StreamingControllerTests
|
|||
enqueueLoad: fake.EnqueueLoad,
|
||||
enqueueUnload: fake.EnqueueUnload,
|
||||
drainCompletions: fake.DrainCompletions,
|
||||
applyTerrain: _ => { },
|
||||
applyTerrain: (_, _) => { },
|
||||
state: state,
|
||||
radius: 2);
|
||||
nearRadius: 2,
|
||||
farRadius: 2);
|
||||
|
||||
// Center at (50, 50); no landblocks loaded yet.
|
||||
controller.Tick(observerCx: 50, observerCy: 50);
|
||||
|
||||
// 5×5 window = 25 loads enqueued, 0 unloads.
|
||||
// 5×5 window = 25 loads enqueued (nearRadius==farRadius so all go to ToLoadNear), 0 unloads.
|
||||
Assert.Equal(25, fake.Loads.Count);
|
||||
Assert.Empty(fake.Unloads);
|
||||
}
|
||||
|
|
@ -53,7 +54,7 @@ public class StreamingControllerTests
|
|||
var fake = new FakeStreamer();
|
||||
var controller = new StreamingController(
|
||||
fake.EnqueueLoad, fake.EnqueueUnload, fake.DrainCompletions,
|
||||
_ => { }, state, radius: 2);
|
||||
(_, _) => { }, state, nearRadius: 2, farRadius: 2);
|
||||
|
||||
controller.Tick(50, 50);
|
||||
fake.Loads.Clear();
|
||||
|
|
@ -72,13 +73,19 @@ public class StreamingControllerTests
|
|||
var applied = new List<LoadedLandblock>();
|
||||
var controller = new StreamingController(
|
||||
fake.EnqueueLoad, fake.EnqueueUnload, fake.DrainCompletions,
|
||||
applied.Add, state, radius: 2);
|
||||
(lb, _) => applied.Add(lb), state, nearRadius: 2, farRadius: 2);
|
||||
|
||||
// Note: LoadedLandblock's actual fields are LandblockId, Heightmap,
|
||||
// Entities (positional record). Adjust if the first positional arg
|
||||
// name differs.
|
||||
var lb = new LoadedLandblock(0x32320FFEu, new LandBlock(), System.Array.Empty<WorldEntity>());
|
||||
fake.Pending.Enqueue(new LandblockStreamResult.Loaded(0x32320FFEu, lb));
|
||||
// A.5 T10-T12 follow-up: use a real empty mesh instance instead of
|
||||
// default! so any future test that flows MeshData through the apply
|
||||
// callback gets a non-null reference to inspect rather than an NRE.
|
||||
var stubMesh = new AcDream.Core.Terrain.LandblockMeshData(
|
||||
System.Array.Empty<AcDream.Core.Terrain.TerrainVertex>(),
|
||||
System.Array.Empty<uint>());
|
||||
fake.Pending.Enqueue(new LandblockStreamResult.Loaded(0x32320FFEu, LandblockStreamTier.Near, lb, stubMesh));
|
||||
|
||||
controller.Tick(50, 50);
|
||||
|
||||
|
|
@ -93,7 +100,7 @@ public class StreamingControllerTests
|
|||
var fake = new FakeStreamer();
|
||||
var controller = new StreamingController(
|
||||
fake.EnqueueLoad, fake.EnqueueUnload, fake.DrainCompletions,
|
||||
_ => { }, state, radius: 2);
|
||||
(_, _) => { }, state, nearRadius: 2, farRadius: 2);
|
||||
|
||||
var lb = new LoadedLandblock(0x32320FFEu, new LandBlock(), System.Array.Empty<WorldEntity>());
|
||||
state.AddLandblock(lb);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,134 @@
|
|||
using System.Collections.Generic;
|
||||
using AcDream.App.Streaming;
|
||||
using AcDream.Core.Terrain;
|
||||
using AcDream.Core.World;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.Streaming;
|
||||
|
||||
public class StreamingControllerTwoTierTests
|
||||
{
|
||||
[Fact]
|
||||
public void Tick_FirstCall_EnqueuesNearAndFarLoadsByTier()
|
||||
{
|
||||
var loads = new List<(uint Id, LandblockStreamJobKind Kind)>();
|
||||
var unloads = new List<uint>();
|
||||
var state = new GpuWorldState();
|
||||
|
||||
var ctrl = new StreamingController(
|
||||
enqueueLoad: (id, kind) => loads.Add((id, kind)),
|
||||
enqueueUnload: unloads.Add,
|
||||
drainCompletions: _ => System.Array.Empty<LandblockStreamResult>(),
|
||||
applyTerrain: (_, _) => { },
|
||||
state: state,
|
||||
nearRadius: 1,
|
||||
farRadius: 3);
|
||||
|
||||
ctrl.Tick(observerCx: 100, observerCy: 100);
|
||||
|
||||
int nearCount = 0, farCount = 0;
|
||||
foreach (var (_, kind) in loads)
|
||||
{
|
||||
if (kind == LandblockStreamJobKind.LoadNear) nearCount++;
|
||||
else if (kind == LandblockStreamJobKind.LoadFar) farCount++;
|
||||
}
|
||||
Assert.Equal(9, nearCount); // 3x3 inner ring (radius=1)
|
||||
Assert.Equal(40, farCount); // 7x7 - 3x3 outer ring (radius=3)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tick_PlayerWalksOutOfNear_ToDemoteRoutesToRemoveEntities()
|
||||
{
|
||||
// Setup: bootstrap region at (100,100) with near=1, far=3.
|
||||
// The bootstrap puts LB (100,100) in the near tier.
|
||||
// Walking 4+ east drops LB (100,100) past the near-hysteresis
|
||||
// threshold (NearRadius+2 = 3); ToDemote should fire.
|
||||
|
||||
var loads = new List<(uint, LandblockStreamJobKind)>();
|
||||
var unloads = new List<uint>();
|
||||
var state = new GpuWorldState();
|
||||
|
||||
// Pre-load LB (100,100) so RemoveEntitiesFromLandblock has something
|
||||
// to find. The actual entity content doesn't matter for routing.
|
||||
var lb100 = new LoadedLandblock(
|
||||
(100u << 24) | (100u << 16) | 0xFFFFu,
|
||||
Heightmap: null!,
|
||||
Entities: new[] { new WorldEntity {
|
||||
Id = 1, SourceGfxObjOrSetupId = 0,
|
||||
Position = System.Numerics.Vector3.Zero,
|
||||
Rotation = System.Numerics.Quaternion.Identity,
|
||||
MeshRefs = System.Array.Empty<MeshRef>() } });
|
||||
state.AddLandblock(lb100);
|
||||
Assert.Equal(1, state.Entities.Count);
|
||||
|
||||
var ctrl = new StreamingController(
|
||||
enqueueLoad: (id, kind) => loads.Add((id, kind)),
|
||||
enqueueUnload: unloads.Add,
|
||||
drainCompletions: _ => System.Array.Empty<LandblockStreamResult>(),
|
||||
applyTerrain: (_, _) => { },
|
||||
state: state,
|
||||
nearRadius: 1,
|
||||
farRadius: 3);
|
||||
|
||||
ctrl.Tick(observerCx: 100, observerCy: 100); // bootstrap
|
||||
loads.Clear();
|
||||
|
||||
// Walk 4 east — LB (100,100) is now Chebyshev distance 4 from new
|
||||
// center (104,100). NearRadius+2 = 3, so 4 > 3 fires the demote.
|
||||
ctrl.Tick(observerCx: 104, observerCy: 100);
|
||||
|
||||
// ToDemote runs synchronously on the render thread (no enqueue).
|
||||
// The visible effect is RemoveEntitiesFromLandblock dropping the entity.
|
||||
Assert.Empty(state.Entities);
|
||||
// Terrain stays loaded (demote != unload).
|
||||
Assert.True(state.IsLoaded((100u << 24) | (100u << 16) | 0xFFFFu));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tick_DrainingPromoted_RoutesToAddEntitiesToExisting()
|
||||
{
|
||||
var loads = new List<(uint, LandblockStreamJobKind)>();
|
||||
var unloads = new List<uint>();
|
||||
var state = new GpuWorldState();
|
||||
|
||||
// Pre-load a far-tier-style LB record (terrain only, no entities).
|
||||
// Id must be in canonical form (low 16 bits = 0xFFFF) since
|
||||
// AddEntitiesToExistingLandblock canonicalizes incoming ids.
|
||||
uint lbId = 0x3232FFFFu;
|
||||
var lb = new LoadedLandblock(lbId, Heightmap: null!, Entities: System.Array.Empty<WorldEntity>());
|
||||
state.AddLandblock(lb);
|
||||
Assert.Empty(state.Entities);
|
||||
|
||||
// Streamer pushes a Promoted result carrying the entity layer.
|
||||
var promoted = new LandblockStreamResult.Promoted(
|
||||
lbId,
|
||||
new[] { new WorldEntity {
|
||||
Id = 7, SourceGfxObjOrSetupId = 0,
|
||||
Position = System.Numerics.Vector3.Zero,
|
||||
Rotation = System.Numerics.Quaternion.Identity,
|
||||
MeshRefs = System.Array.Empty<MeshRef>() } });
|
||||
var queue = new Queue<LandblockStreamResult>();
|
||||
queue.Enqueue(promoted);
|
||||
|
||||
var ctrl = new StreamingController(
|
||||
enqueueLoad: (id, kind) => loads.Add((id, kind)),
|
||||
enqueueUnload: unloads.Add,
|
||||
drainCompletions: max =>
|
||||
{
|
||||
var batch = new List<LandblockStreamResult>();
|
||||
while (batch.Count < max && queue.Count > 0) batch.Add(queue.Dequeue());
|
||||
return batch;
|
||||
},
|
||||
applyTerrain: (_, _) => { },
|
||||
state: state,
|
||||
nearRadius: 2,
|
||||
farRadius: 2);
|
||||
|
||||
ctrl.Tick(50, 50); // drains the Promoted result
|
||||
|
||||
// Promoted routes to AddEntitiesToExistingLandblock — the entity is now
|
||||
// merged into the existing LB record.
|
||||
Assert.Equal(1, state.Entities.Count);
|
||||
Assert.Equal(7u, state.Entities[0].Id);
|
||||
}
|
||||
}
|
||||
|
|
@ -36,7 +36,7 @@ public class StreamingRegionTests
|
|||
{
|
||||
var region = new StreamingRegion(cx: 50, cy: 50, radius: 2);
|
||||
|
||||
var diff = region.RecenterTo(50, 50);
|
||||
var diff = region.RecenterToSingleTier(50, 50);
|
||||
|
||||
Assert.Empty(diff.ToLoad);
|
||||
Assert.Empty(diff.ToUnload);
|
||||
|
|
@ -52,7 +52,7 @@ public class StreamingRegionTests
|
|||
// the radius+2 threshold, so it stays loaded (hysteresis keeps radius+2).
|
||||
var region = new StreamingRegion(cx: 50, cy: 50, radius: 2);
|
||||
|
||||
var diff = region.RecenterTo(51, 50);
|
||||
var diff = region.RecenterToSingleTier(51, 50);
|
||||
|
||||
Assert.Equal(5, diff.ToLoad.Count);
|
||||
Assert.Empty(diff.ToUnload);
|
||||
|
|
@ -71,7 +71,7 @@ public class StreamingRegionTests
|
|||
// x=48 is now 5 away, > radius+2 = 4 → unload. x=49 is 4 away, not > 4 → keep. x=50 is 3 away, not > 4 → keep.
|
||||
var region = new StreamingRegion(cx: 50, cy: 50, radius: 2);
|
||||
|
||||
var diff = region.RecenterTo(53, 50);
|
||||
var diff = region.RecenterToSingleTier(53, 50);
|
||||
|
||||
Assert.Equal(15, diff.ToLoad.Count);
|
||||
Assert.Equal(5, diff.ToUnload.Count);
|
||||
|
|
@ -82,7 +82,7 @@ public class StreamingRegionTests
|
|||
{
|
||||
var region = new StreamingRegion(cx: 50, cy: 50, radius: 2);
|
||||
|
||||
var diff = region.RecenterTo(200, 200);
|
||||
var diff = region.RecenterToSingleTier(200, 200);
|
||||
|
||||
Assert.Equal(25, diff.ToLoad.Count);
|
||||
Assert.Equal(25, diff.ToUnload.Count);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,166 @@
|
|||
using AcDream.App.Streaming;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.Streaming;
|
||||
|
||||
public class StreamingRegionTwoTierTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_TwoRadii_ExposesNearAndFarRadii()
|
||||
{
|
||||
var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 4, farRadius: 12);
|
||||
|
||||
Assert.Equal(4, region.NearRadius);
|
||||
Assert.Equal(12, region.FarRadius);
|
||||
Assert.Equal(100, region.CenterX);
|
||||
Assert.Equal(100, region.CenterY);
|
||||
// Radius (used by existing single-radius hysteresis math) must alias to
|
||||
// FarRadius — the outer ring drives "everything currently loaded" bookkeeping.
|
||||
// If a future change mistakenly aliases Radius to NearRadius, hysteresis
|
||||
// becomes (NearRadius+2) for the far-tier unload, which is wrong.
|
||||
Assert.Equal(region.FarRadius, region.Radius);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeFirstTickDiff_FirstTick_SplitsLoadIntoNearAndFar()
|
||||
{
|
||||
// near=1, far=3 → near window is 3×3=9, far window is 7×7-3×3=40 LBs.
|
||||
var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3);
|
||||
var diff = region.ComputeFirstTickDiff();
|
||||
|
||||
Assert.Equal(9, diff.ToLoadNear.Count);
|
||||
Assert.Equal(40, diff.ToLoadFar.Count);
|
||||
Assert.Empty(diff.ToPromote);
|
||||
Assert.Empty(diff.ToDemote);
|
||||
Assert.Empty(diff.ToUnload);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecenterTo_PlayerWalks_NullToFar_AppearsInToLoadFar()
|
||||
{
|
||||
var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3);
|
||||
_ = region.ComputeFirstTickDiff();
|
||||
region.MarkResidentFromBootstrap();
|
||||
|
||||
// Walk one LB east — center (100,100) → (101,100). LB column at lbX=104
|
||||
// (relative dx=+3 from new center) enters the far window from null.
|
||||
var diff = region.RecenterTo(newCx: 101, newCy: 100);
|
||||
|
||||
foreach (var y in new[] { 97, 98, 99, 100, 101, 102, 103 })
|
||||
{
|
||||
var id = StreamingRegion.EncodeLandblockIdForTest(104, y);
|
||||
Assert.Contains(id, diff.ToLoadFar);
|
||||
}
|
||||
Assert.Empty(diff.ToLoadNear);
|
||||
// The 3 LBs at x=102, y in {99,100,101} were Far from old center
|
||||
// (distance 2) and are now Near from new center (distance ≤1).
|
||||
// They should land in ToPromote.
|
||||
Assert.Equal(3, diff.ToPromote.Count);
|
||||
// All resident LBs from the old window are within hysteresis of
|
||||
// the new center (max distance 4 ≤ FarRadius+2=5), so nothing unloads.
|
||||
Assert.Empty(diff.ToUnload);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecenterTo_PlayerWalks_FarToNear_AppearsInToPromote()
|
||||
{
|
||||
var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3);
|
||||
_ = region.ComputeFirstTickDiff();
|
||||
region.MarkResidentFromBootstrap();
|
||||
|
||||
// Walk 2 east — center (102, 100). LB (102, 100) was at distance 2 (Far)
|
||||
// from (100,100); now at distance 0 → Near. That's a Promote.
|
||||
var diff = region.RecenterTo(newCx: 102, newCy: 100);
|
||||
|
||||
var promotedId = StreamingRegion.EncodeLandblockIdForTest(102, 100);
|
||||
Assert.Contains(promotedId, diff.ToPromote);
|
||||
Assert.DoesNotContain(promotedId, diff.ToLoadNear);
|
||||
Assert.DoesNotContain(promotedId, diff.ToLoadFar);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecenterTo_PlayerTeleports_NullToNear_AppearsInToLoadNear()
|
||||
{
|
||||
var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3);
|
||||
_ = region.ComputeFirstTickDiff();
|
||||
region.MarkResidentFromBootstrap();
|
||||
|
||||
// Teleport to (200, 200) — entirely new region.
|
||||
var diff = region.RecenterTo(newCx: 200, newCy: 200);
|
||||
|
||||
Assert.Equal(9, diff.ToLoadNear.Count);
|
||||
Assert.Equal(40, diff.ToLoadFar.Count);
|
||||
Assert.Empty(diff.ToPromote);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecenterTo_NearToFar_DemoteOnlyAfterHysteresis()
|
||||
{
|
||||
// near=2, far=4 → near hysteresis threshold = 4.
|
||||
var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 2, farRadius: 4);
|
||||
_ = region.ComputeFirstTickDiff();
|
||||
region.MarkResidentFromBootstrap();
|
||||
|
||||
// LB (100,100) was Near. Walk 3 east → distance 3 > NearRadius=2 but ≤ 4. No demote yet.
|
||||
var diff1 = region.RecenterTo(newCx: 103, newCy: 100);
|
||||
var lb100 = StreamingRegion.EncodeLandblockIdForTest(100, 100);
|
||||
Assert.DoesNotContain(lb100, diff1.ToDemote);
|
||||
|
||||
// Walk 2 more east → distance 5 > 4. Demote.
|
||||
var diff2 = region.RecenterTo(newCx: 105, newCy: 100);
|
||||
Assert.Contains(lb100, diff2.ToDemote);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecenterTo_FarToNull_UnloadOnlyAfterHysteresis()
|
||||
{
|
||||
var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3);
|
||||
_ = region.ComputeFirstTickDiff();
|
||||
region.MarkResidentFromBootstrap();
|
||||
|
||||
// LB (97, 100) was at distance 3 (Far). Walk 1 east → distance 4. ≤ FarRadius+2=5.
|
||||
var diff1 = region.RecenterTo(newCx: 101, newCy: 100);
|
||||
var lb97 = StreamingRegion.EncodeLandblockIdForTest(97, 100);
|
||||
Assert.DoesNotContain(lb97, diff1.ToUnload);
|
||||
|
||||
// Walk 2 more east → distance 6 > 5. Unload.
|
||||
var diff2 = region.RecenterTo(newCx: 103, newCy: 100);
|
||||
Assert.Contains(lb97, diff2.ToUnload);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecenterTo_PlayerOscillates_NoThrashWithinHysteresis()
|
||||
{
|
||||
// Start the region centered on (103,100) so the oscillation
|
||||
// between (102,100) and (103,100) never crosses a hysteresis boundary.
|
||||
// NearRadius=2, farRadius=4 → nearUnloadThreshold=4.
|
||||
// Chebyshev distance from (102,100) or (103,100) to any LB in the
|
||||
// initial 9×9 window of (103,100) is ≤ NearRadius+2=4 for all LBs
|
||||
// in the near zone, so no demotes should fire during pure oscillation.
|
||||
var region = new StreamingRegion(centerX: 103, centerY: 100, nearRadius: 2, farRadius: 4);
|
||||
_ = region.ComputeFirstTickDiff();
|
||||
region.MarkResidentFromBootstrap();
|
||||
|
||||
// Bounce between (103,100) and (102,100). All resident LBs stay
|
||||
// within the hysteresis window — no demotes expected.
|
||||
int totalDemotes = 0;
|
||||
int totalPromotes = 0;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
var d1 = region.RecenterTo(102, 100);
|
||||
totalDemotes += d1.ToDemote.Count;
|
||||
totalPromotes += d1.ToPromote.Count;
|
||||
var d2 = region.RecenterTo(103, 100);
|
||||
totalDemotes += d2.ToDemote.Count;
|
||||
totalPromotes += d2.ToPromote.Count;
|
||||
}
|
||||
|
||||
// The first step from (103,100) to (102,100) legitimately promotes the
|
||||
// x=100 near-column (5 LBs) that were Far from (103) into Near. After
|
||||
// that initial settle they stay Near for all subsequent oscillations.
|
||||
// So the ceiling is 5 promotes total (not per oscillation).
|
||||
Assert.Equal(0, totalDemotes);
|
||||
Assert.True(totalPromotes <= 5,
|
||||
$"Expected ≤5 promotes across 5 oscillations; got {totalPromotes}");
|
||||
}
|
||||
}
|
||||
47
tests/AcDream.Core.Tests/World/WorldEntityAabbTests.cs
Normal file
47
tests/AcDream.Core.Tests/World/WorldEntityAabbTests.cs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
using System.Numerics;
|
||||
using AcDream.Core.World;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.World;
|
||||
|
||||
public class WorldEntityAabbTests
|
||||
{
|
||||
[Fact]
|
||||
public void Aabb_DefaultRadius_PositionPlusMinus5()
|
||||
{
|
||||
var entity = new WorldEntity
|
||||
{
|
||||
Id = 1,
|
||||
SourceGfxObjOrSetupId = 0,
|
||||
Position = new Vector3(10, 20, 30),
|
||||
Rotation = System.Numerics.Quaternion.Identity,
|
||||
MeshRefs = System.Array.Empty<MeshRef>(),
|
||||
};
|
||||
entity.RefreshAabb();
|
||||
|
||||
Assert.Equal(new Vector3(5, 15, 25), entity.AabbMin);
|
||||
Assert.Equal(new Vector3(15, 25, 35), entity.AabbMax);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Aabb_DirtyFlag_SetByMutator_ClearedByRefresh()
|
||||
{
|
||||
var entity = new WorldEntity
|
||||
{
|
||||
Id = 1,
|
||||
SourceGfxObjOrSetupId = 0,
|
||||
Position = new Vector3(10, 20, 30),
|
||||
Rotation = System.Numerics.Quaternion.Identity,
|
||||
MeshRefs = System.Array.Empty<MeshRef>(),
|
||||
};
|
||||
entity.RefreshAabb();
|
||||
Assert.False(entity.AabbDirty);
|
||||
|
||||
entity.SetPosition(new Vector3(100, 200, 300));
|
||||
Assert.True(entity.AabbDirty);
|
||||
|
||||
entity.RefreshAabb();
|
||||
Assert.False(entity.AabbDirty);
|
||||
Assert.Equal(new Vector3(95, 195, 295), entity.AabbMin);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
using AcDream.UI.Abstractions.Settings;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.UI.Abstractions.Tests.Panels.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// A.5 T22.5: <see cref="QualitySettings"/> preset table + env-var override
|
||||
/// coverage. Env-var tests clear their variables in <c>finally</c> blocks so
|
||||
/// parallel runners cannot bleed state between tests.
|
||||
/// </summary>
|
||||
public class QualityPresetTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(QualityPreset.Low, 2, 5, 0)]
|
||||
[InlineData(QualityPreset.Medium, 3, 8, 2)]
|
||||
[InlineData(QualityPreset.High, 4, 12, 4)]
|
||||
[InlineData(QualityPreset.Ultra, 5, 15, 4)]
|
||||
public void From_Preset_ProducesExpectedRadiiAndMsaa(
|
||||
QualityPreset preset, int n1, int n2, int msaa)
|
||||
{
|
||||
var s = QualitySettings.From(preset);
|
||||
Assert.Equal(n1, s.NearRadius);
|
||||
Assert.Equal(n2, s.FarRadius);
|
||||
Assert.Equal(msaa, s.MsaaSamples);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(QualityPreset.Low, 4, false)]
|
||||
[InlineData(QualityPreset.Medium, 8, false)]
|
||||
[InlineData(QualityPreset.High, 16, true)]
|
||||
[InlineData(QualityPreset.Ultra, 16, true)]
|
||||
public void From_Preset_ProducesExpectedAnisoAndA2C(
|
||||
QualityPreset preset, int aniso, bool a2c)
|
||||
{
|
||||
var s = QualitySettings.From(preset);
|
||||
Assert.Equal(aniso, s.AnisotropicLevel);
|
||||
Assert.Equal(a2c, s.AlphaToCoverage);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(QualityPreset.Low, 2)]
|
||||
[InlineData(QualityPreset.Medium, 3)]
|
||||
[InlineData(QualityPreset.High, 4)]
|
||||
[InlineData(QualityPreset.Ultra, 6)]
|
||||
public void From_Preset_ProducesExpectedMaxCompletions(
|
||||
QualityPreset preset, int expected)
|
||||
{
|
||||
var s = QualitySettings.From(preset);
|
||||
Assert.Equal(expected, s.MaxCompletionsPerFrame);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_NearRadius_OverridesPreset()
|
||||
{
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_NEAR_RADIUS", "2");
|
||||
try
|
||||
{
|
||||
var s = QualitySettings.From(QualityPreset.High); // High = NearRadius=4 normally
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.Equal(2, resolved.NearRadius);
|
||||
Assert.Equal(12, resolved.FarRadius); // FarRadius unaffected
|
||||
}
|
||||
finally { System.Environment.SetEnvironmentVariable("ACDREAM_NEAR_RADIUS", null); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_FarRadius_OverridesPreset()
|
||||
{
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_FAR_RADIUS", "20");
|
||||
try
|
||||
{
|
||||
var s = QualitySettings.From(QualityPreset.High);
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.Equal(4, resolved.NearRadius); // NearRadius unaffected
|
||||
Assert.Equal(20, resolved.FarRadius);
|
||||
}
|
||||
finally { System.Environment.SetEnvironmentVariable("ACDREAM_FAR_RADIUS", null); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_AlphaToCoverage_BooleanParsing()
|
||||
{
|
||||
// Ensure "0" and "false" disable; other values enable.
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_A2C", "0");
|
||||
try
|
||||
{
|
||||
var s = QualitySettings.From(QualityPreset.High); // High has A2C=true
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.False(resolved.AlphaToCoverage);
|
||||
}
|
||||
finally { System.Environment.SetEnvironmentVariable("ACDREAM_A2C", null); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_AlphaToCoverage_FalseString_Disables()
|
||||
{
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_A2C", "false");
|
||||
try
|
||||
{
|
||||
var s = QualitySettings.From(QualityPreset.High);
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.False(resolved.AlphaToCoverage);
|
||||
}
|
||||
finally { System.Environment.SetEnvironmentVariable("ACDREAM_A2C", null); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_AlphaToCoverage_NonZeroEnables()
|
||||
{
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_A2C", "1");
|
||||
try
|
||||
{
|
||||
var s = QualitySettings.From(QualityPreset.Low); // Low has A2C=false
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.True(resolved.AlphaToCoverage);
|
||||
}
|
||||
finally { System.Environment.SetEnvironmentVariable("ACDREAM_A2C", null); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_Unset_LeavesPresetDefault()
|
||||
{
|
||||
// Ensure no env vars are set for this test's fields.
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_NEAR_RADIUS", null);
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_FAR_RADIUS", null);
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_A2C", null);
|
||||
|
||||
var s = QualitySettings.From(QualityPreset.High);
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.Equal(s, resolved);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void From_UndefinedPreset_FallsBackToHigh()
|
||||
{
|
||||
var s = QualitySettings.From((QualityPreset)99);
|
||||
Assert.Equal(4, s.NearRadius); // High default
|
||||
Assert.Equal(12, s.FarRadius);
|
||||
Assert.Equal(4, s.MsaaSamples);
|
||||
Assert.True(s.AlphaToCoverage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_MaxCompletionsPerFrame_OverridesPreset()
|
||||
{
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_MAX_COMPLETIONS_PER_FRAME", "8");
|
||||
try
|
||||
{
|
||||
var s = QualitySettings.From(QualityPreset.High); // High = 4
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.Equal(8, resolved.MaxCompletionsPerFrame);
|
||||
}
|
||||
finally { System.Environment.SetEnvironmentVariable("ACDREAM_MAX_COMPLETIONS_PER_FRAME", null); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_MsaaSamples_OverridesPreset()
|
||||
{
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_MSAA_SAMPLES", "8");
|
||||
try
|
||||
{
|
||||
var s = QualitySettings.From(QualityPreset.High); // High = 4
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.Equal(8, resolved.MsaaSamples);
|
||||
}
|
||||
finally { System.Environment.SetEnvironmentVariable("ACDREAM_MSAA_SAMPLES", null); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnvVar_Anisotropic_OverridesPreset()
|
||||
{
|
||||
System.Environment.SetEnvironmentVariable("ACDREAM_ANISOTROPIC", "4");
|
||||
try
|
||||
{
|
||||
var s = QualitySettings.From(QualityPreset.High); // High = 16
|
||||
var resolved = QualitySettings.WithEnvOverrides(s);
|
||||
Assert.Equal(4, resolved.AnisotropicLevel);
|
||||
}
|
||||
finally { System.Environment.SetEnvironmentVariable("ACDREAM_ANISOTROPIC", null); }
|
||||
}
|
||||
}
|
||||
|
|
@ -44,7 +44,8 @@ public sealed class SettingsStoreTests : System.IDisposable
|
|||
VSync: false,
|
||||
FieldOfView: 100f,
|
||||
Gamma: 1.4f,
|
||||
ShowFps: true);
|
||||
ShowFps: true,
|
||||
Quality: AcDream.UI.Abstractions.Settings.QualityPreset.Ultra);
|
||||
|
||||
store.SaveDisplay(original);
|
||||
var loaded = store.LoadDisplay();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue