Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/batched-model-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

`Renderer.DrawModels` and the model portion of `RenderQueue` now flatten and
group opaque submeshes by shared model, material and mesh. Limits remain 128
objects and 4096 total meshes. The bounded sort uses 64 KiB of extra static
scratch and allocates nothing per frame. Global stable back-to-front
translucency remains a separate pass; it is not reordered by material.
objects and 4096 total meshes. Opaque and translucent submissions share one
64 KiB static table, growing from opposite ends within that combined mesh limit.
This replaces two separate 64 KiB tables and saves 65,536 bytes of native static
memory without allocating per frame. Global stable back-to-front translucency
remains a separate pass; it is not reordered by material. Equal-depth translucent
meshes retain input order, including batches at the 4096-mesh limit.
The saving applies to executables that link this model batching path; builds
using section garbage collection can already omit the table when they only use
ordinary meshes or individual model draws.

Within one native batch, compatible repeated materials reuse GX depth/blend,
alpha, TEV, sampler and texture-object state. Object/normal matrices, mirrored
Expand Down
178 changes: 178 additions & 0 deletions docs/memory-reduction-audit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Memory reduction audit

Baseline: SDK 318, revision `4c8cd26`. Managed allocation measurements below use
the host .NET 8 CLR. They are distinct from native section sizes and from the
GameCube managed heap. The changes retain the existing public APIs.

## Transform hierarchy caches

Each transform previously allocated its TRS values (`float[10]`), local matrix
(`float[16]`), world matrix (`float[16]`), a child `List<Transform>`, and that
list's empty backing array. Most batch transforms and root game objects never
use hierarchy storage.

The world matrix and TRS values remain eager, and each transform retains its
own stable world array. A never-parented transform now builds TRS directly into
that world array. Its separate local matrix is reserved when it is first
parented, before changing the hierarchy. A child list with initial capacity
four is reserved only on the first attachment to a parent. Caches remain
available after detachment so repeated hierarchy updates do not allocate.

Validation of cycles and complete subtree depth still precedes allocation and
link changes. The new parent's insertion finishes before removal from the old
parent, preserving the original graph if allocating those buffers fails. A root without a
local cache keeps its local matrix dirty even after computing world state, so
its first attachment recomputes current local TRS. Repeated changes and matrix
reads allocate no additional buffers.

| Warmed CLR allocation workload | Before | After | Reduction |
| --- | ---: | ---: | ---: |
| 128 root transforms, including their owning reference array | 47,128 B | 28,696 B | 18,432 B |
| 128 children attached to one existing root, including owning array and parent list growth | 49,288 B | 42,152 B | 7,136 B |

Each never-parented transform eliminates three allocations and 64 B of matrix
element payload. The host CLR reduction is 144 B per transform, including
object/array overhead. For Hypercube's 128 batch poses, this removes 8,192 B of
matrix payload and 384 allocation objects; the measured CLR constructor
reduction is 18,432 B. Parented leaves retain both matrix arrays but no longer
allocate their own empty child list and empty backing array.

The PPC32 layout gives a different overhead reduction. From `dnd_managed.h`,
the managed object header is 8 B and the array header is 24 B. The allocator
rounds objects to eight-byte boundaries and prefixes each with a 16 B heap
block. The generated `List<Transform>` instance has 16 B of fields after the
object header. Removing one 64 B matrix array (104 B including headers), one
empty list object (40 B), and its zero-length backing array (40 B) therefore
avoids **184 B per never-parented transform**, or **23,552 B for 128**. This is
a layout calculation, not a hardware heap measurement. It assumes fresh
allocations; already fragmented free blocks can consume different amounts.

Reproduce the host measurements from the repository root:

```sh
python3 scripts/measure_transform_allocations.py --revision 4c8cd26
python3 scripts/measure_transform_allocations.py
dotnet run --project tests/TransformMemoryTests -c Release
```

The probe exports the selected source revision into a temporary project and
does not modify the checkout. Focused CLR tests cover first attachment,
detachment/edit/reattachment, propagation to descendants, rejected cycles and
depth overflow, stable distinct world caches, and allocation-free repeated
updates. `ModelSmoke/TransformChecks.cs` exercises the same critical hierarchy
transitions through AOT with production native math. The graphics CI domain
runs the CLR checks and the model acceptance script runs native/AOT checks,
including managed GC stress.

## Further candidates and tradeoffs

* **Material sampling options:** options are already lazy, but assigning an
explicit default sampler or UV transform still allocates `float[40]` (160 B
element payload). Hypercube intentionally preallocates these arrays before
batching to avoid frame allocations. Skipping default assignments alone
would move a future nondefault allocation into material registration or
updates. A shared default representation needs careful reset semantics
because the renderer's native sampler state is global.
* **Managed model overrides:** every `ModelInstance` owns 10-float node and
four-float color scratch arrays (56 B payload) even if no override is used.
Making them lazy saves unused storage but adds a first-override allocation.
The native color ABI requires exactly four floats, so sharing the 10-float
buffer requires an ABI change. Native override storage is already lazy.
* **Crossfade poses:** `DndInstance.other` reserves `40 * nodeCount` B even
when no crossfade is played. Laziness would defer allocation/budget failure
from instance creation to the first fade. It needs explicit rollback tests
for failed fade admission and animation state before implementation.
* **Render queue storage:** each queue entry is a separately allocated object;
default capacity is 512. Inline struct entries could remove those objects
and the reference array indirection, but all mutating paths and AOT reference
scanning must be verified. Changing the two bounded sort index arrays to
`ushort` would save `4 * capacity` B (2,048 B at default capacity), with
compiler signedness and maximum-capacity coverage required.
* **Bulk draw matrices:** replacing transforms with one shared mutable world
array is incompatible with `DrawMeshes` and `RenderQueue`, which borrow
multiple distinct matrices in one submission. A flat matrix submission API
could reduce object overhead while preserving that lifetime contract, but
requires separate API and renderer validation.
* **Streaming textures and native reads:** both texture images protect active
GPU readers during staging/publication. `Stage` retains its native source
until completion, and publication checks the destination fence. AssetStream
native reads already write directly into the destination; their 4 KiB bounce
buffers are temporary stack storage. Removing an image or the conservative
`NativeBuffer` release wait needs a new ownership contract, including callers
that expose raw addresses to GX.
* **Native alignment and shared scratch:** resource allocations may reserve up
to `alignment-1` extra bytes (usually 31 B, up to 4,095 B); a portable aligned
allocator and matching free/accounting path need validation before changing
this. Model overrides and ordinary mesh drawing also reserve separate
768-vertex workspaces, a possible 27,648 B saving. Sharing them requires an
explicit executor/reentrancy contract; the larger immediate/glyph workspace
has different capacity requirements.

Native persistent and scratch reductions are described below. Their presence
in an executable depends on the native linker retaining the relevant code;
they must not be added unconditionally to the managed savings above.

## Native model batch submission scratch

The existing 4,096-mesh batch limit applies to the total number of opaque and
alpha meshes. Previously, each pass reserved its own 4,096-entry table even
though both could not be full at the same time. A single table now grows opaque
entries from its front and alpha entries from its back. Opaque and alpha slices
are still sorted independently. Original sequence numbers preserve alpha order
for equal depths despite insertion from the back.

`ModelSubmission` is 16 B, so replacing two tables with one saves **65,536 B of
static native storage** when `dnd_model_draw_models` is retained by the linker.
Production `model_graphics.o`, built with the recording fixture flags, changed
from 158,904 to 93,368 B of BSS. Its text changed from 16,296 to 16,307 B (+11 B)
on the host compiler. Those object-file figures are compiler-specific, and
linker section collection can omit this scratch entirely from an application
which only draws raw meshes.

Recording tests verify every submitted mesh for 4,096 all-opaque, all-alpha and
mixed batches in both cached and uncached state modes. They also check stable
equal-depth order and rejection of a 4,097-mesh batch before any draw occurs.
No heap allocation was added to submissions.

## Native model skin workspace

Skin evaluation previously reserved space for 256 position matrices and 256
normal matrices for each skinned model instance: `256 * 32 * sizeof(float)`,
or 32,768 B of payload. Model skins are immutable after loading and each has a
validated joint count from one to 256. Each new workspace allocation caches the
maximum joint count across the model's skins and reserves both palettes using
that count. Their separate matrix strides remain intact.

| Maximum skin joint count | Previous workspace charge | New workspace charge | Workspace reduction |
| --- | ---: | ---: | ---: |
| 1 | 32,800 B | 160 B | 32,640 B |
| 2 | 32,800 B | 288 B | 32,512 B |
| 256 | 32,800 B | 32,800 B | 0 B |

The native allocator charges a 32 B allocation header plus a rounded payload;
these sizes include that charge. The cached maximum adds four bytes to the
PPC instance structure (eight bytes after host pointer alignment), so a model
with two-joint skins saves **32,508 B net per instance on PPC**, or 32,504 B on
the host. A maximum-size skin retains its previous workspace size and adds
only the cached field. Instances without evaluated skins do not allocate a
workspace.

The maximum is computed only when allocating workspace, avoiding a scan on
ordinary updates.
Native byte accounting, budgets and allocator alignment remain in use. The
workspace retains instance ownership and is freed by vertex dropping, model
unload and instance disposal. Dropping the workspace also clears its cached
stride; recreation derives a fresh bound from the resident model's validated
skins.

`tests/model_skin_workspace_test.c` uses production allocation and animation
code. It verifies one-, two- and 256-joint skins, mixed one-/256-joint skins,
position and normal transforms under nonuniform scale, reuse without new
allocation, drop/recreation with changed valid skin counts, exact-budget
admission, and one-byte-under-budget failure cleanup. Run the complete native and AOT model acceptance with generated
fixtures:

```sh
dotnet run --project tests/ModelAssetTests -c Release -- /tmp/dnd-model-fixtures
python3 scripts/ci_model_tests.py /tmp/dnd-model-fixtures
```
1 change: 1 addition & 0 deletions include/dnd_model_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ typedef struct {
unsigned char *done;
float **vertices;
float *workspace;
unsigned palette_joints;
unsigned *draw_order;
float *draw_depth;
} DndInstance;
Expand Down
6 changes: 5 additions & 1 deletion scripts/ci_model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ def main(fixtures):
work.mkdir(parents=True, exist_ok=True)
cc = os.environ.get("CC", "cc")
flags = ["-pthread", "-O1", "-g", "-std=c11", "-Wall", "-Wextra", "-Werror", "-Iinclude"]
dead_strip = "-Wl,-dead_strip" if sys.platform == "darwin" else "-Wl,--gc-sections"
common = ["source/managed_runtime.c", "source/async_runtime.c"]
model = sorted(str(p.relative_to(ROOT)) for p in (ROOT / "source").glob("model*.c"))
runtime = common + ["source/storage.c", "source/contiguous_write.c", "source/file_optimize.c","source/verified_copy.c"] + model + [
"source/graphics_device.c", "source/draw_resources.c", "source/graphics_gx.c", "source/performance.c"]
skin = work / "model-skin-workspace"
run(cc, *flags, "-ffunction-sections", "-fdata-sections", "tests/model_skin_workspace_test.c",
"source/model.c", "source/model_animation.c", dead_strip, "-lm", "-o", skin)
run(skin)
inflate = work / "model-inflate"
run(cc, *flags, "tests/model_inflate_test.c", "source/model_ufbx.c", "-lm", "-o", inflate)
run(inflate)
Expand All @@ -50,7 +55,6 @@ def main(fixtures):
"-c", source, "-o", obj)
objects.append(obj)
recording = work / "model-recording"
dead_strip = "-Wl,-dead_strip" if sys.platform == "darwin" else "-Wl,--gc-sections"
run(cc, *flags, *objects, dead_strip, "-lm", "-o", recording)
run(recording)

Expand Down
1 change: 1 addition & 0 deletions scripts/ci_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def main(area):
run("dotnet","run","--project","tests/ModelAssetTests","-c","Release","--",tmp)
run("python3","scripts/ci_model_tests.py",tmp)
test("tests/TevMaterialTests")
test("tests/TransformMemoryTests")
for p in ("GraphicsSmoke","SystemFirmwareSmoke","IplFontSmoke","ModelSmoke","ModelAsyncSmoke"):
build("tests/"+p+"/"+p+".csproj")
elif area=="assets":
Expand Down
29 changes: 29 additions & 0 deletions scripts/measure_transform_allocations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
"""Measure warmed host CLR allocations for SDK transforms; not a PPC heap probe."""
import argparse
from pathlib import Path
import shutil
import subprocess
import tempfile

ROOT = Path(__file__).resolve().parents[1]
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--revision", help="Git source revision; omit for the working tree")
args = parser.parse_args()
sources = [
"src/DolphinDotNet.GameCube/Dolphin/Graphics/Transform.cs",
"src/DolphinDotNet.GameCube/Dolphin/Graphics/Vector3.cs",
"src/DolphinDotNet.GameCube/Dolphin/Graphics/Quaternion.cs",
"src/DolphinDotNet.Core/Dolphin/Collections/List.cs",
"src/DolphinDotNet.Core/Dolphin/Collections/ReadOnlyCollection.cs",
]
with tempfile.TemporaryDirectory(prefix="dnd-transform-memory-") as temporary:
work = Path(temporary)
for source in sources:
content = subprocess.check_output(["git", "show", args.revision + ":" + source], cwd=ROOT) if args.revision else (ROOT / source).read_bytes()
(work / Path(source).name).write_bytes(content)
for name in ("Program.cs", "NativeModel.cs"):
shutil.copyfile(ROOT / "tests/TransformMemoryTests" / name, work / name)
(work / "Probe.csproj").write_text('<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net8.0</TargetFramework><Nullable>enable</Nullable></PropertyGroup></Project>')
print("Source:", args.revision or "working tree", flush=True)
subprocess.run(["dotnet", "run", "--project", str(work / "Probe.csproj"), "-c", "Release", "--", "--measure-only"], check=True)
11 changes: 9 additions & 2 deletions source/model_animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ static void global(DndInstance *in, unsigned node) {
void dnd_model_drop_vertices(DndInstance *in) {
dnd_model_free(in->workspace, &in->bytes);
in->workspace = NULL;
in->palette_joints = 0;
if (in->vertices)
for (unsigned i = 0; i < in->model->meshes; i++) {
dnd_model_free(in->vertices[i], &in->bytes);
Expand Down Expand Up @@ -234,12 +235,18 @@ int dnd_model_evaluate(DndInstance *in) {
}
DndSkin *skin = &m->skin[mesh->skin];
if (!in->workspace) {
in->workspace = dnd_model_alloc(256 * 32 * 4, &in->bytes, in->budget);
/* Validated skins have 1..256 joints. Cache their largest bound with this
* allocation, shared by both palette halves and every skinned mesh. */
in->palette_joints = 0;
for (unsigned j = 0; j < m->skins; j++)
if ((unsigned)m->skin[j].count > in->palette_joints)
in->palette_joints = (unsigned)m->skin[j].count;
in->workspace = dnd_model_alloc(in->palette_joints * 32 * sizeof(float), &in->bytes, in->budget);
if (!in->workspace)
return -2;
}
float(*palette)[16] = (float(*)[16])in->workspace,
(*normal)[16] = (float(*)[16])(in->workspace + 256 * 16);
(*normal)[16] = (float(*)[16])(in->workspace + in->palette_joints * 16);
for (int j = 0; j < skin->count; j++) {
const unsigned char *joint = skin->data + (unsigned)j * 68;
float bind[16];
Expand Down
Loading
Loading