Add fractal surface page residency policy
This commit is contained in:
parent
db06ec4a8d
commit
d07a24a522
11 changed files with 306 additions and 3 deletions
|
|
@ -0,0 +1,28 @@
|
|||
namespace Aquarium.Engine.Fractal.Lod;
|
||||
|
||||
public sealed class FractalSurfacePageResidencyPlan
|
||||
{
|
||||
public FractalSurfacePageResidencyPlan(
|
||||
IReadOnlyList<AquariumFractalSurfacePage> residentPages,
|
||||
IReadOnlyList<AquariumFractalSurfacePage> missingPages,
|
||||
IReadOnlyList<AquariumFractalSurfacePage> requestedPages,
|
||||
IReadOnlyList<AquariumFractalSurfacePage> evictedPages,
|
||||
long residentBytes)
|
||||
{
|
||||
ResidentPages = residentPages;
|
||||
MissingPages = missingPages;
|
||||
RequestedPages = requestedPages;
|
||||
EvictedPages = evictedPages;
|
||||
ResidentBytes = residentBytes;
|
||||
}
|
||||
|
||||
public IReadOnlyList<AquariumFractalSurfacePage> ResidentPages { get; }
|
||||
|
||||
public IReadOnlyList<AquariumFractalSurfacePage> MissingPages { get; }
|
||||
|
||||
public IReadOnlyList<AquariumFractalSurfacePage> RequestedPages { get; }
|
||||
|
||||
public IReadOnlyList<AquariumFractalSurfacePage> EvictedPages { get; }
|
||||
|
||||
public long ResidentBytes { get; }
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
namespace Aquarium.Engine.Fractal.Lod;
|
||||
|
||||
public static class FractalSurfacePageResidencyPlanner
|
||||
{
|
||||
public static FractalSurfacePageResidencyPlan Plan(
|
||||
IReadOnlyList<AquariumFractalSurfacePage> pages,
|
||||
IFractalSurfacePageStore store,
|
||||
long maxResidentBytes,
|
||||
int maxRequests)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(pages);
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
if (maxResidentBytes < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(maxResidentBytes), maxResidentBytes, "Resident byte budget must not be negative.");
|
||||
}
|
||||
|
||||
if (maxRequests < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(maxRequests), maxRequests, "Request budget must not be negative.");
|
||||
}
|
||||
|
||||
var rows = pages
|
||||
.Select((page, index) => new PageCandidate(page, index, EstimatedBytes(page), store.IsResident(page.Key)))
|
||||
.ToArray();
|
||||
var retainedIndexes = new HashSet<int>();
|
||||
var retainedBytes = 0L;
|
||||
foreach (var candidate in rows
|
||||
.Where(candidate => candidate.IsResident)
|
||||
.OrderByDescending(PageValue)
|
||||
.ThenByDescending(candidate => candidate.Page.MaxError)
|
||||
.ThenBy(candidate => candidate.Page.Key.Value, StringComparer.Ordinal))
|
||||
{
|
||||
if (retainedBytes + candidate.EstimatedBytes > maxResidentBytes)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
retainedIndexes.Add(candidate.Index);
|
||||
retainedBytes += candidate.EstimatedBytes;
|
||||
}
|
||||
|
||||
var resident = new List<AquariumFractalSurfacePage>();
|
||||
var missing = new List<AquariumFractalSurfacePage>();
|
||||
var evicted = new List<AquariumFractalSurfacePage>();
|
||||
foreach (var candidate in rows)
|
||||
{
|
||||
if (candidate.IsResident && retainedIndexes.Contains(candidate.Index))
|
||||
{
|
||||
resident.Add(candidate.Page);
|
||||
continue;
|
||||
}
|
||||
|
||||
missing.Add(candidate.Page);
|
||||
if (candidate.IsResident)
|
||||
{
|
||||
evicted.Add(candidate.Page);
|
||||
store.Evict(candidate.Page.Key);
|
||||
}
|
||||
}
|
||||
|
||||
var requested = rows
|
||||
.Where(candidate => !candidate.IsResident)
|
||||
.Select(candidate => candidate.Page)
|
||||
.OrderByDescending(PageValue)
|
||||
.ThenByDescending(page => page.MaxError)
|
||||
.ThenBy(page => page.Key.Value, StringComparer.Ordinal)
|
||||
.Take(maxRequests)
|
||||
.ToArray();
|
||||
foreach (var page in requested)
|
||||
{
|
||||
store.Request(page);
|
||||
}
|
||||
|
||||
return new FractalSurfacePageResidencyPlan(resident, missing, requested, evicted, retainedBytes);
|
||||
}
|
||||
|
||||
public static long EstimatedBytes(AquariumFractalSurfacePage page)
|
||||
{
|
||||
if (page.Width <= 0 || page.Height <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (long)page.Width * page.Height * BytesPerPixel(page.Key.Kind);
|
||||
}
|
||||
|
||||
private static float PageValue(PageCandidate candidate)
|
||||
{
|
||||
return PageValue(candidate.Page, candidate.EstimatedBytes);
|
||||
}
|
||||
|
||||
private static float PageValue(AquariumFractalSurfacePage page)
|
||||
{
|
||||
return PageValue(page, EstimatedBytes(page));
|
||||
}
|
||||
|
||||
private static float PageValue(AquariumFractalSurfacePage page, long estimatedBytes)
|
||||
{
|
||||
return MathF.Max(page.MaxError, 0.000001f) / Math.Max(estimatedBytes, 1L);
|
||||
}
|
||||
|
||||
private static int BytesPerPixel(AquariumFractalSurfacePageKind kind)
|
||||
{
|
||||
return kind switch
|
||||
{
|
||||
AquariumFractalSurfacePageKind.Material => 4,
|
||||
AquariumFractalSurfacePageKind.Height => 2,
|
||||
AquariumFractalSurfacePageKind.SignedDistance2D => 2,
|
||||
AquariumFractalSurfacePageKind.Confidence => 2,
|
||||
_ => 4,
|
||||
};
|
||||
}
|
||||
|
||||
private readonly record struct PageCandidate(
|
||||
AquariumFractalSurfacePage Page,
|
||||
int Index,
|
||||
long EstimatedBytes,
|
||||
bool IsResident);
|
||||
}
|
||||
10
src/Aquarium.Engine.Fractal/Lod/IFractalSurfacePageStore.cs
Normal file
10
src/Aquarium.Engine.Fractal/Lod/IFractalSurfacePageStore.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
namespace Aquarium.Engine.Fractal.Lod;
|
||||
|
||||
public interface IFractalSurfacePageStore
|
||||
{
|
||||
bool IsResident(AquariumFractalSurfacePageKey key);
|
||||
|
||||
void Request(AquariumFractalSurfacePage page);
|
||||
|
||||
void Evict(AquariumFractalSurfacePageKey key);
|
||||
}
|
||||
|
|
@ -11,6 +11,8 @@ public static class ZyphosFractalTerrain
|
|||
{
|
||||
private const string PatchRelativePath = "Worlds/zyphos-first-fractal-terrain.aquageo";
|
||||
private const int SurfacePageDimension = 128;
|
||||
private const long MaxResidentSurfacePageBytes = 2L * 1024L * 1024L;
|
||||
private const int MaxSurfacePageRequests = 8;
|
||||
|
||||
private static readonly Lazy<string> PatchSource = new(() => File.ReadAllText(PatchPath));
|
||||
private static readonly Lazy<FractalOwnershipTree> Tree = new(() => FractalDslCompiler.Compile(PatchSource.Value));
|
||||
|
|
@ -69,6 +71,11 @@ public static class ZyphosFractalTerrain
|
|||
bucketPixelsPerWorld,
|
||||
new FractalXorShiftRandom(((uint)bucket ^ 0xC2B2AE35u) + (uint)selectedCut.Length));
|
||||
var surfacePages = PlanSurfacePages(selectedCut);
|
||||
var surfacePageResidency = FractalSurfacePageResidencyPlanner.Plan(
|
||||
surfacePages,
|
||||
ResidentSurfacePageStore.Instance,
|
||||
MaxResidentSurfacePageBytes,
|
||||
MaxSurfacePageRequests);
|
||||
var cutKey = CutCacheKey(selectedCut);
|
||||
AquariumHeightFieldBrush[] brushes;
|
||||
lock (PlanCacheLock)
|
||||
|
|
@ -86,8 +93,9 @@ public static class ZyphosFractalTerrain
|
|||
resourcePlan,
|
||||
structuralProbeReservoir,
|
||||
surfacePages,
|
||||
surfacePageResidency,
|
||||
bucketPixelsPerWorld,
|
||||
$"{selectedCut.Length}/{Summaries.Value.Length} cuts / {brushes.Length}/{OwnershipTree.Claims.Count} brushes / {resourcePlan.UpdateNodes.Length}/{DefaultBudget.MaxCpuUpdates} cpu updates / {resourcePlan.GpuEstimatedCost:0.0}/{DefaultBudget.MaxGpuEstimatedCost:0.0} gpu cost / {resourcePlan.Residency.ResidentNodes.Count}/{DefaultBudget.MaxResidentPayloads} resident / {resourcePlan.Residency.RequestedNodes.Count}/{DefaultBudget.MaxSsdRequests} ssd requests / {(structuralProbeReservoir.HasSample ? structuralProbeReservoir.CandidateCount : 0)} probe candidates / {surfacePages.Length} surface pages / {bucketPixelsPerWorld:0.00} px-wu");
|
||||
$"{selectedCut.Length}/{Summaries.Value.Length} cuts / {brushes.Length}/{OwnershipTree.Claims.Count} brushes / {resourcePlan.UpdateNodes.Length}/{DefaultBudget.MaxCpuUpdates} cpu updates / {resourcePlan.GpuEstimatedCost:0.0}/{DefaultBudget.MaxGpuEstimatedCost:0.0} gpu cost / {resourcePlan.Residency.ResidentNodes.Count}/{DefaultBudget.MaxResidentPayloads} resident / {resourcePlan.Residency.RequestedNodes.Count}/{DefaultBudget.MaxSsdRequests} ssd requests / {(structuralProbeReservoir.HasSample ? structuralProbeReservoir.CandidateCount : 0)} probe candidates / {surfacePageResidency.ResidentPages.Count}/{surfacePages.Length} surface pages / {bucketPixelsPerWorld:0.00} px-wu");
|
||||
}
|
||||
|
||||
private static AquariumFractalSurfacePage[] PlanSurfacePages(IReadOnlyList<AquariumSelectedCut> selectedCut)
|
||||
|
|
@ -139,6 +147,9 @@ public static class ZyphosFractalTerrain
|
|||
$" structuralProbe: {(plan.StructuralProbeReservoir.HasSample ? plan.StructuralProbeReservoir.NodeKey.Value : "none")}",
|
||||
$" structuralProbeWeight: {plan.StructuralProbeReservoir.WeightSum:0.000} candidates={plan.StructuralProbeReservoir.CandidateCount} contributionWeight={plan.StructuralProbeReservoir.ContributionWeight:0.000}",
|
||||
$" surfacePages: {plan.SurfacePages.Length} {SurfacePageDimension}x{SurfacePageDimension}",
|
||||
$" surfacePageResident: {plan.SurfacePageResidency.ResidentPages.Count}/{plan.SurfacePages.Length} bytes={plan.SurfacePageResidency.ResidentBytes}/{MaxResidentSurfacePageBytes}",
|
||||
$" surfacePageRequests: {plan.SurfacePageResidency.RequestedPages.Count}/{MaxSurfacePageRequests}",
|
||||
$" surfacePageEvictions: {plan.SurfacePageResidency.EvictedPages.Count}",
|
||||
"selected:",
|
||||
};
|
||||
|
||||
|
|
@ -172,6 +183,24 @@ public static class ZyphosFractalTerrain
|
|||
{
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ResidentSurfacePageStore : IFractalSurfacePageStore
|
||||
{
|
||||
public static ResidentSurfacePageStore Instance { get; } = new();
|
||||
|
||||
public bool IsResident(AquariumFractalSurfacePageKey key)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Request(AquariumFractalSurfacePage page)
|
||||
{
|
||||
}
|
||||
|
||||
public void Evict(AquariumFractalSurfacePageKey key)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct ZyphosFractalRenderPlan(
|
||||
|
|
@ -180,5 +209,6 @@ public readonly record struct ZyphosFractalRenderPlan(
|
|||
FractalResourcePlan ResourcePlan,
|
||||
FractalProbeReservoirSnapshot StructuralProbeReservoir,
|
||||
AquariumFractalSurfacePage[] SurfacePages,
|
||||
FractalSurfacePageResidencyPlan SurfacePageResidency,
|
||||
float PixelsPerWorld,
|
||||
string Summary);
|
||||
|
|
|
|||
Loading…
Reference in a new issue