perf(render) #429: allocation-exact streamed-mesh completion
UploadGfxObjMeshData built every completed mesh's index data three-plus times over in LINQ transients (per-batch Indices.ToArray copies plus an unsized SelectMany growth) on the render thread, up to the per-frame upload budget. The conversion now fills one exact-size retained CPUIndices array (the same one the B.4b pick path keeps) and hands the shared arena (offset, count) segments of it; CPUPositions fills by a direct pre-sized loop; the Sum/Any/FirstOrDefault transients are gone. GlobalMeshBuffer.UploadMesh takes the segment form — the staged bytes per batch are unchanged. Gate: a warmed completion must allocate near its retained-copy size (MeshPipelineDeviceSeamTests). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
5b9d0260bb
commit
0330fcd0d1
3 changed files with 183 additions and 34 deletions
|
|
@ -286,9 +286,15 @@ public sealed class GlobalMeshBuffer : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #429: index batches arrive as (offset, count) segments of one shared
|
||||
/// index array — the caller's retained CPU pick copy — instead of one
|
||||
/// managed array per batch. The bytes staged per batch are unchanged.
|
||||
/// </summary>
|
||||
internal GlobalMeshAllocation UploadMesh(
|
||||
VertexPositionNormalTexture[] vertices,
|
||||
IReadOnlyList<ushort[]> indexBatches)
|
||||
ushort[] indices,
|
||||
ReadOnlySpan<(int Offset, int Count)> indexBatches)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
_retirementLedger.RetryPendingPublications();
|
||||
|
|
@ -296,16 +302,21 @@ public sealed class GlobalMeshBuffer : IDisposable
|
|||
if (_migration is not null)
|
||||
throw new InvalidOperationException("A mesh upload cannot mutate the arena while a backing-buffer migration is in progress.");
|
||||
ArgumentNullException.ThrowIfNull(vertices);
|
||||
ArgumentNullException.ThrowIfNull(indexBatches);
|
||||
ArgumentNullException.ThrowIfNull(indices);
|
||||
if (vertices.Length == 0)
|
||||
throw new ArgumentException("A global mesh allocation requires vertices.", nameof(vertices));
|
||||
|
||||
int totalIndices = 0;
|
||||
for (int i = 0; i < indexBatches.Count; i++)
|
||||
for (int i = 0; i < indexBatches.Length; i++)
|
||||
{
|
||||
ushort[] batch = indexBatches[i]
|
||||
?? throw new ArgumentException("Index batches cannot contain null.", nameof(indexBatches));
|
||||
totalIndices = checked(totalIndices + batch.Length);
|
||||
(int offset, int count) = indexBatches[i];
|
||||
if (offset < 0 || count <= 0 || (long)offset + count > indices.Length)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"Index batch {i} ({offset}, {count}) is outside the shared index array ({indices.Length}).",
|
||||
nameof(indexBatches));
|
||||
}
|
||||
totalIndices = checked(totalIndices + count);
|
||||
}
|
||||
if (totalIndices == 0)
|
||||
throw new ArgumentException("A global mesh allocation requires indices.", nameof(indexBatches));
|
||||
|
|
@ -322,7 +333,7 @@ public sealed class GlobalMeshBuffer : IDisposable
|
|||
throw;
|
||||
}
|
||||
|
||||
var firstIndices = new int[indexBatches.Count];
|
||||
var firstIndices = new int[indexBatches.Length];
|
||||
try
|
||||
{
|
||||
// IGpuBuffer.Upload stages through a neutral binding point of the
|
||||
|
|
@ -337,18 +348,15 @@ public sealed class GlobalMeshBuffer : IDisposable
|
|||
|
||||
IGpuBuffer indexStore = RequireStore(_indexBuffer);
|
||||
int indexOffset = indexRange.Offset;
|
||||
for (int i = 0; i < indexBatches.Count; i++)
|
||||
for (int i = 0; i < indexBatches.Length; i++)
|
||||
{
|
||||
ushort[] batch = indexBatches[i];
|
||||
(int offset, int count) = indexBatches[i];
|
||||
firstIndices[i] = indexOffset;
|
||||
if (batch.Length > 0)
|
||||
{
|
||||
long indexOffsetBytes = checked((long)indexOffset * sizeof(ushort));
|
||||
indexStore.Upload(
|
||||
indexOffsetBytes,
|
||||
MemoryMarshal.AsBytes(new ReadOnlySpan<ushort>(batch)));
|
||||
indexOffset = checked(indexOffset + batch.Length);
|
||||
}
|
||||
long indexOffsetBytes = checked((long)indexOffset * sizeof(ushort));
|
||||
indexStore.Upload(
|
||||
indexOffsetBytes,
|
||||
MemoryMarshal.AsBytes(new ReadOnlySpan<ushort>(indices, offset, count)));
|
||||
indexOffset = checked(indexOffset + count);
|
||||
}
|
||||
}
|
||||
catch
|
||||
|
|
|
|||
|
|
@ -2045,13 +2045,47 @@ namespace AcDream.App.Rendering.Wb
|
|||
{
|
||||
if (meshData.Vertices.Length == 0) return null;
|
||||
|
||||
var modernIndexBatches = meshData.TextureBatches.Values
|
||||
.SelectMany(batches => batches)
|
||||
.Where(batch => batch.Indices.Count != 0)
|
||||
.Select(batch => batch.Indices.ToArray())
|
||||
.ToArray();
|
||||
// #429: allocation-exact conversion. The retained pick copies
|
||||
// (CPUPositions/CPUIndices) are the only geometry-proportional
|
||||
// allocations this method makes; the per-batch segments handed to
|
||||
// the shared arena are (offset, count) views into the same
|
||||
// retained CPUIndices array. The former LINQ chain materialized
|
||||
// every batch twice (a per-batch ToArray plus an unsized
|
||||
// SelectMany growth) — megabytes of transient garbage per mesh on
|
||||
// the render thread.
|
||||
int totalIndexCount = 0;
|
||||
int nonEmptyBatchCount = 0;
|
||||
foreach (List<TextureBatchData> formatBatches in meshData.TextureBatches.Values)
|
||||
{
|
||||
foreach (TextureBatchData formatBatch in formatBatches)
|
||||
{
|
||||
if (formatBatch.Indices.Count == 0) continue;
|
||||
totalIndexCount = checked(totalIndexCount + formatBatch.Indices.Count);
|
||||
nonEmptyBatchCount++;
|
||||
}
|
||||
}
|
||||
|
||||
var cpuIndices = new ushort[totalIndexCount];
|
||||
var indexSegments = new (int Offset, int Count)[nonEmptyBatchCount];
|
||||
{
|
||||
int fillOffset = 0;
|
||||
int segmentIndex = 0;
|
||||
foreach (List<TextureBatchData> formatBatches in meshData.TextureBatches.Values)
|
||||
{
|
||||
foreach (TextureBatchData formatBatch in formatBatches)
|
||||
{
|
||||
int count = formatBatch.Indices.Count;
|
||||
if (count == 0) continue;
|
||||
CollectionsMarshal.AsSpan(formatBatch.Indices)
|
||||
.CopyTo(cpuIndices.AsSpan(fillOffset, count));
|
||||
indexSegments[segmentIndex++] = (fillOffset, count);
|
||||
fillOffset = checked(fillOffset + count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GlobalMeshAllocation? globalAllocation = null;
|
||||
var renderBatches = new List<ObjectRenderBatch>();
|
||||
var renderBatches = new List<ObjectRenderBatch>(nonEmptyBatchCount);
|
||||
var acquiredTextures = new List<(TextureAtlasManager Atlas, TextureKey Key)>();
|
||||
|
||||
try
|
||||
|
|
@ -2068,8 +2102,13 @@ namespace AcDream.App.Rendering.Wb
|
|||
// or vertex/index buffer to build here — Vulkan bakes vertex input
|
||||
// into the pipeline, and the shared arena's stores are bound once
|
||||
// per pass (see WbDrawDispatcher.Rhi.cs's BindPipelineWithMesh).
|
||||
if (GlobalBuffer is not null && modernIndexBatches.Length != 0)
|
||||
globalAllocation = GlobalBuffer.UploadMesh(meshData.Vertices, modernIndexBatches);
|
||||
if (GlobalBuffer is not null && nonEmptyBatchCount != 0)
|
||||
{
|
||||
globalAllocation = GlobalBuffer.UploadMesh(
|
||||
meshData.Vertices,
|
||||
cpuIndices,
|
||||
indexSegments);
|
||||
}
|
||||
|
||||
foreach (var (format, batches) in meshData.TextureBatches)
|
||||
{
|
||||
|
|
@ -2093,8 +2132,25 @@ namespace AcDream.App.Rendering.Wb
|
|||
// family. Choosing an earlier reclaimed slot first
|
||||
// duplicates a layer already resident in a later array
|
||||
// every time portal churn revisits that texture.
|
||||
atlasManager = atlasList.FirstOrDefault(a => a.HasTexture(batch.Key))
|
||||
?? atlasList.FirstOrDefault(a => a.AvailableSlots > 0);
|
||||
for (int i = 0; i < atlasList.Count; i++)
|
||||
{
|
||||
if (atlasList[i].HasTexture(batch.Key))
|
||||
{
|
||||
atlasManager = atlasList[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (atlasManager is null)
|
||||
{
|
||||
for (int i = 0; i < atlasList.Count; i++)
|
||||
{
|
||||
if (atlasList[i].AvailableSlots > 0)
|
||||
{
|
||||
atlasManager = atlasList[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (atlasManager == null)
|
||||
{
|
||||
atlasManager = new TextureAtlasManager(
|
||||
|
|
@ -2180,21 +2236,34 @@ namespace AcDream.App.Rendering.Wb
|
|||
}
|
||||
}
|
||||
|
||||
// Every renderBatches entry is one non-empty texture batch, so
|
||||
// their index counts sum to exactly totalIndexCount.
|
||||
long geometryBytes = checked(
|
||||
(long)meshData.Vertices.Length * VertexPositionNormalTexture.Size
|
||||
+ renderBatches.Sum(b => (long)b.IndexCount * sizeof(ushort)));
|
||||
+ (long)totalIndexCount * sizeof(ushort));
|
||||
bool hasCutoutSubset = false;
|
||||
for (int i = 0; i < renderBatches.Count; i++)
|
||||
{
|
||||
if (renderBatches[i].Translucency
|
||||
== AcDream.Core.Meshing.TranslucencyKind.ClipMap)
|
||||
{
|
||||
hasCutoutSubset = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
var cpuPositions = new Vector3[meshData.Vertices.Length];
|
||||
for (int i = 0; i < cpuPositions.Length; i++)
|
||||
cpuPositions[i] = meshData.Vertices[i].Position;
|
||||
var renderData = new ObjectRenderData
|
||||
{
|
||||
VertexCount = meshData.Vertices.Length,
|
||||
Batches = renderBatches,
|
||||
HasCutoutSubset = renderBatches.Any(
|
||||
static batch => batch.Translucency
|
||||
== AcDream.Core.Meshing.TranslucencyKind.ClipMap),
|
||||
HasCutoutSubset = hasCutoutSubset,
|
||||
GlobalAllocation = globalAllocation,
|
||||
ParticleEmitters = meshData.ParticleEmitters,
|
||||
DIDDegrade = meshData.DIDDegrade,
|
||||
CPUPositions = meshData.Vertices.Select(v => v.Position).ToArray(),
|
||||
CPUIndices = meshData.TextureBatches.Values.SelectMany(l => l).SelectMany(b => b.Indices).ToArray(),
|
||||
CPUPositions = cpuPositions,
|
||||
CPUIndices = cpuIndices,
|
||||
CPUEdgeLines = meshData.EdgeLines,
|
||||
MemorySize = geometryBytes,
|
||||
NonArenaGpuBytes = CalculateNonArenaGeometryBytes(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue