diff --git a/docs/perfect-machine-architecture.md b/docs/perfect-machine-architecture.md index 613159c..c4cfff6 100644 --- a/docs/perfect-machine-architecture.md +++ b/docs/perfect-machine-architecture.md @@ -417,6 +417,14 @@ spatiotemporal machine. If a mesh, analytic SDF, tube envelope, or volume pass can write the same evidence lanes and validation guides, it belongs in the same frame contract. +For buffer-driven spline fields, the DSL should express the rolling texture +slice, axis, modulo window, surface graph, and quality budget. The interpreter +then chooses the current best lowering. Today that means direct SDF tube +geometry for moderate fields and reservoir splats when an automatic budget says +the direct representation has become too expensive. Future mesh lowering fits +the same contract; it is another backend for the same evidence, not another +authoring language. + ### 10. TAA Guide Integration TAA consumes guide signals: diff --git a/docs/temporal-spatial-evidence-reservoir.md b/docs/temporal-spatial-evidence-reservoir.md index aae6150..36641f4 100644 --- a/docs/temporal-spatial-evidence-reservoir.md +++ b/docs/temporal-spatial-evidence-reservoir.md @@ -133,6 +133,14 @@ material/field encoding, conservative bounds, and guide data sufficient for TAA and reservoir validation. A mesh that only paints pixels is a fallback draw. A mesh that emits field evidence is a first-class contributor. +The `.aquafield` script surface follows the same boundary. A script declares +intent such as "read this rolling texture slice as a spline field over this +axis, with this surface graph and quality budget." It does not need to choose +direct SDF tubes, reservoir splats, or future mesh lowering. `lowering +mode=auto` is the default contract: the renderer may emit direct SDF tube +geometry when that is cheap and legible, decimate columns/control points under +LOD pressure, or route oversized fields through the reservoir path. + ## Invariants - Reservoir math is a pure, testable core before it becomes HLSL. diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs b/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs index 11dde2e..9cee093 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs @@ -13,8 +13,7 @@ public static class AquariumFieldScriptCompiler ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(textureBindings); - AquariumFractalReservoirField reservoir = AquariumFractalReservoirField.Empty; - var useReservoirLowering = false; + var loweringPolicy = AquariumFieldLoweringPolicy.Default; var textures = new List(); var programs = new List(); var nodes = new List(); @@ -47,13 +46,8 @@ public static class AquariumFieldScriptCompiler case "splinefield": programs.Add(ParseSplineField(args, nodes, lineIndex)); break; - case "reservoir": - reservoir = ParseReservoir(args, reservoirRadius, lineIndex); - useReservoirLowering = true; - break; - case "direct": - useReservoirLowering = false; - reservoir = AquariumFractalReservoirField.Empty; + case "lowering": + loweringPolicy = ParseLoweringPolicy(args, lineIndex); break; default: throw new FormatException($"Unknown field DSL command `{tokens[0]}` at line {lineIndex + 1}."); @@ -70,18 +64,17 @@ public static class AquariumFieldScriptCompiler throw new FormatException("Field DSL must declare at least one `splinefield`."); } - if (useReservoirLowering && !reservoir.HasInput) - { - var splatCount = programs.Sum(program => Math.Max(1, program.ProbePolicy.MaxProbeCount)); - reservoir = DefaultReservoir(splatCount, reservoirRadius); - } + loweringPolicy = loweringPolicy.Normalized(); + var reservoir = DefaultReservoir( + loweringPolicy.MaxReservoirSplats, + reservoirRadius); return new AquariumBufferFieldFrame { Textures = textures, TextureSplineFields = programs, Reservoir = reservoir, - UseReservoirLowering = useReservoirLowering, + LoweringPolicy = loweringPolicy, SourceScript = source, }; } @@ -140,14 +133,14 @@ public static class AquariumFieldScriptCompiler Vec4(args, "value", Vector4.Zero, lineIndex)); } - private static AquariumFractalReservoirField ParseReservoir(IReadOnlyDictionary args, float reservoirRadius, int lineIndex) + private static AquariumFieldLoweringPolicy ParseLoweringPolicy(IReadOnlyDictionary args, int lineIndex) { - return DefaultReservoir( - Int(args, "splats", 65536, lineIndex), - reservoirRadius, - Int(args, "updates", 65536, lineIndex), - Int(args, "candidates", 2, lineIndex), - UInt(args, "seed", 0xA17EA11u, lineIndex)); + return new AquariumFieldLoweringPolicy( + Enum.Parse(StringValue(args, "mode", "auto"), ignoreCase: true), + Int(args, "maxSplines", AquariumFieldLoweringPolicy.Default.MaxDirectSplines, lineIndex), + Int(args, "maxControlPoints", AquariumFieldLoweringPolicy.Default.MaxDirectControlPoints, lineIndex), + Int(args, "maxSplats", AquariumFieldLoweringPolicy.Default.MaxReservoirSplats, lineIndex), + Float(args, "lodBias", AquariumFieldLoweringPolicy.Default.LodBias, lineIndex)); } private static AquariumFractalReservoirField DefaultReservoir( @@ -204,6 +197,11 @@ public static class AquariumFieldScriptCompiler return value; } + private static string StringValue(IReadOnlyDictionary args, string key, string fallback) => + args.TryGetValue(key, out var value) && !string.IsNullOrWhiteSpace(value) + ? value + : fallback; + private static int Int(IReadOnlyDictionary args, string key, int fallback, int lineIndex) => args.TryGetValue(key, out var value) ? int.Parse(value, CultureInfo.InvariantCulture) : fallback; diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs b/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs index a0a9f78..bd2b6a8 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs @@ -527,13 +527,45 @@ public sealed class AquariumBufferFieldFrame public AquariumFractalReservoirField Reservoir { get; init; } = AquariumFractalReservoirField.Empty; - public bool UseReservoirLowering { get; init; } + public AquariumFieldLoweringPolicy LoweringPolicy { get; init; } = AquariumFieldLoweringPolicy.Default; + + public bool UseReservoirLowering => LoweringPolicy.Normalized().Mode == AquariumFieldLoweringMode.ReservoirSplats; public string? SourceScript { get; init; } public bool HasInput => SplineTubeFields.Count > 0 || TextureSplineFields.Count > 0; } +public enum AquariumFieldLoweringMode +{ + Auto = 0, + DirectSdfTubes = 1, + ReservoirSplats = 2, + Mesh = 3, +} + +public sealed record AquariumFieldLoweringPolicy( + AquariumFieldLoweringMode Mode, + int MaxDirectSplines, + int MaxDirectControlPoints, + int MaxReservoirSplats, + float LodBias) +{ + public static AquariumFieldLoweringPolicy Default { get; } = new( + AquariumFieldLoweringMode.Auto, + MaxDirectSplines: 512, + MaxDirectControlPoints: 65_536, + MaxReservoirSplats: 131_072, + LodBias: 1.0f); + + public AquariumFieldLoweringPolicy Normalized() => new( + Mode, + Math.Clamp(MaxDirectSplines, 1, 65_536), + Math.Clamp(MaxDirectControlPoints, 2, 8_388_608), + Math.Clamp(MaxReservoirSplats, 1, 4_194_304), + Math.Clamp(LodBias, 0.05f, 16.0f)); +} + public enum AquariumTextureAxis { X = 0, @@ -677,7 +709,7 @@ public sealed class AquariumBufferFieldFrameBuilder private readonly List textures = []; private readonly List textureSplineFields = []; private AquariumFractalReservoirField reservoir = AquariumFractalReservoirField.Empty; - private bool useReservoirLowering; + private AquariumFieldLoweringPolicy loweringPolicy = AquariumFieldLoweringPolicy.Default; private string? sourceScript; public AquariumBufferFieldFrameBuilder SplineTube( @@ -726,14 +758,19 @@ public sealed class AquariumBufferFieldFrameBuilder public AquariumBufferFieldFrameBuilder Reservoir(AquariumFractalReservoirField field) { reservoir = field; - useReservoirLowering = field.HasInput; + loweringPolicy = loweringPolicy with { Mode = field.HasInput ? AquariumFieldLoweringMode.ReservoirSplats : AquariumFieldLoweringMode.Auto }; return this; } public AquariumBufferFieldFrameBuilder DirectSdfSurfaces() { - reservoir = AquariumFractalReservoirField.Empty; - useReservoirLowering = false; + loweringPolicy = loweringPolicy with { Mode = AquariumFieldLoweringMode.DirectSdfTubes }; + return this; + } + + public AquariumBufferFieldFrameBuilder Lowering(AquariumFieldLoweringPolicy policy) + { + loweringPolicy = policy.Normalized(); return this; } @@ -749,7 +786,7 @@ public sealed class AquariumBufferFieldFrameBuilder Textures = textures, TextureSplineFields = textureSplineFields, Reservoir = reservoir, - UseReservoirLowering = useReservoirLowering, + LoweringPolicy = loweringPolicy, SourceScript = sourceScript, }; } diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 50bd834..7714732 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -2189,7 +2189,8 @@ public sealed class D3D12Renderer : IAquariumRenderer gpuFusionPointCount = 0; gpuFusionPointSource = default; temporalGaussiansGpuGenerated = false; - activeFractalReservoirField = scene.BufferFieldFrame.UseReservoirLowering && scene.BufferFieldFrame.Reservoir.HasInput + var bufferFieldUsesReservoir = ShouldLowerBufferFieldToReservoir(scene.BufferFieldFrame); + activeFractalReservoirField = bufferFieldUsesReservoir && scene.BufferFieldFrame.Reservoir.HasInput ? scene.BufferFieldFrame.Reservoir : scene.FractalReservoirField.HasInput ? scene.FractalReservoirField @@ -2201,7 +2202,7 @@ public sealed class D3D12Renderer : IAquariumRenderer activeTextureFieldSamples = FlattenTextureSamples(activeBufferFieldFrame); activeSplineFrame = MergeSplineFrames( scene.SplineFrame.HasInput ? scene.SplineFrame : AquariumSplineFrame.Empty, - BuildTextureSplineFrame(activeBufferFieldFrame)); + BuildTextureSplineFrame(activeBufferFieldFrame, bufferFieldUsesReservoir)); activeFractalProgramTransforms = activeFractalReservoirField.HasInput && scene.FractalReservoirField.ProgramTransforms.Count > 0 ? scene.FractalReservoirField.ProgramTransforms as AquariumPackedFractalIfsTransform[] ?? scene.FractalReservoirField.ProgramTransforms.ToArray() : []; @@ -2398,15 +2399,32 @@ public sealed class D3D12Renderer : IAquariumRenderer return programs; } - private static AquariumSplineFrame BuildTextureSplineFrame(AquariumBufferFieldFrame frame) + private static bool ShouldLowerBufferFieldToReservoir(AquariumBufferFieldFrame frame) + { + if (!frame.HasInput || !frame.Reservoir.HasInput) + { + return false; + } + + var policy = frame.LoweringPolicy.Normalized(); + return policy.Mode switch + { + AquariumFieldLoweringMode.ReservoirSplats => true, + AquariumFieldLoweringMode.DirectSdfTubes => false, + AquariumFieldLoweringMode.Mesh => false, + _ => EstimateTextureSplineColumnCount(frame) > policy.MaxDirectSplines, + }; + } + + private static int EstimateTextureSplineColumnCount(AquariumBufferFieldFrame frame) { if (!frame.HasInput || frame.TextureSplineFields.Count == 0 || frame.Textures.Count == 0) { - return AquariumSplineFrame.Empty; + return 0; } var textures = frame.Textures.ToDictionary(texture => texture.Id, StringComparer.Ordinal); - var splines = new List(); + var count = 0; foreach (var program in frame.TextureSplineFields) { if (!textures.TryGetValue(program.TextureId, out var texture)) @@ -2414,7 +2432,51 @@ public sealed class D3D12Renderer : IAquariumRenderer continue; } - AppendTextureSplineField(splines, texture, program); + count += Math.Clamp( + program.ColumnCount, + 1, + program.FrequencyAxis == AquariumTextureAxis.X ? texture.Height : texture.Width); + } + + return count; + } + + private static AquariumSplineFrame BuildTextureSplineFrame(AquariumBufferFieldFrame frame, bool loweredToReservoir) + { + if (loweredToReservoir || !frame.HasInput || frame.TextureSplineFields.Count == 0 || frame.Textures.Count == 0) + { + return AquariumSplineFrame.Empty; + } + + var textures = frame.Textures.ToDictionary(texture => texture.Id, StringComparer.Ordinal); + var splines = new List(); + var policy = frame.LoweringPolicy.Normalized(); + var requestedColumns = Math.Max(1, EstimateTextureSplineColumnCount(frame)); + var effectiveMaxSplines = Math.Max(1, (int)MathF.Round(policy.MaxDirectSplines * policy.LodBias)); + var columnLodStride = Math.Max(1, (int)MathF.Ceiling(requestedColumns / (float)effectiveMaxSplines)); + var requestedControlPoints = 0; + foreach (var program in frame.TextureSplineFields) + { + if (!textures.TryGetValue(program.TextureId, out var texture)) + { + continue; + } + + var axisSamples = program.FrequencyAxis == AquariumTextureAxis.X ? texture.Width : texture.Height; + var columnCount = Math.Clamp(program.ColumnCount, 1, program.FrequencyAxis == AquariumTextureAxis.X ? texture.Height : texture.Width); + requestedControlPoints += Math.Max(1, axisSamples) * Math.Max(1, (int)MathF.Ceiling(columnCount / (float)columnLodStride)); + } + + var effectiveMaxControlPoints = Math.Max(2, (int)MathF.Round(policy.MaxDirectControlPoints * policy.LodBias)); + var axisLodStride = Math.Max(1, (int)MathF.Ceiling(requestedControlPoints / (float)effectiveMaxControlPoints)); + foreach (var program in frame.TextureSplineFields) + { + if (!textures.TryGetValue(program.TextureId, out var texture)) + { + continue; + } + + AppendTextureSplineField(splines, texture, program, columnLodStride, axisLodStride); } return splines.Count == 0 @@ -2425,7 +2487,9 @@ public sealed class D3D12Renderer : IAquariumRenderer private static void AppendTextureSplineField( List splines, AquariumTextureFieldBinding texture, - AquariumTextureSplineFieldProgram program) + AquariumTextureSplineFieldProgram program, + int columnLodStride, + int axisLodStride) { var axisSamples = program.FrequencyAxis == AquariumTextureAxis.X ? texture.Width : texture.Height; if (axisSamples <= 1 || texture.Channels <= 0) @@ -2440,12 +2504,16 @@ public sealed class D3D12Renderer : IAquariumRenderer program.Appearance.Alpha, program.Appearance.ZeroThreshold, program.Appearance.Feather); - for (var column = 0; column < columnCount; column++) + var columnStride = Math.Max(1, columnLodStride); + var frequencyStride = Math.Max(1, axisLodStride); + var pointCount = Math.Max(2, ((axisSamples - 1) / frequencyStride) + 1); + for (var column = 0; column < columnCount; column += columnStride) { - var vertices = new AquariumSplineVertex[axisSamples]; + var vertices = new AquariumSplineVertex[pointCount]; var hasContribution = false; - for (var frequencyIndex = 0; frequencyIndex < axisSamples; frequencyIndex++) + for (var pointIndex = 0; pointIndex < pointCount; pointIndex++) { + var frequencyIndex = Math.Min(axisSamples - 1, pointIndex * frequencyStride); var textureColumn = program.FirstColumn + column * Math.Max(1, program.ColumnStride); if (program.RollingWindowModulo > 0) { @@ -2476,7 +2544,7 @@ public sealed class D3D12Renderer : IAquariumRenderer program.Appearance.Emission, Math.Clamp(sample, 0.0f, 1.0f)); color.W = Math.Clamp(0.16f + sample * program.Appearance.Alpha, 0.0f, 1.0f); - vertices[frequencyIndex] = new AquariumSplineVertex(position, color); + vertices[pointIndex] = new AquariumSplineVertex(position, color); } if (hasContribution) diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 91847b3..6ac5c87 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -218,3 +218,4 @@ {"date":"2026-05-23","topic":"visitor-docs","lesson":"Fensalir visitor docs must name the live machine: spatio-temporal reservoirs, Form/Appearance/Transport field layers, .aquageo/IFS volumetric DSL, and AquaSynth audio integration. Boundary-only prose hides the engine's actual authority."} {"date":"2026-05-23","topic":"mimir-app-boundary","lesson":"Fensalir no longer contains the old sensor-fusion app project or app-specific tests. Mimir owns that application surface; Fensalir keeps only generic sensor-fusion contracts, GPU sensor fusion shader machinery, and temporal evidence lowerings."} {"timestamp":"2026-05-25T18:00:34+01:00","topic":"reservoir-contributor-contract","lesson":"Fensalir's spatiotemporal reservoir is a consumer of coherent surface/volume evidence, not the only geometry generator. Sensor fusion and adaptive IFS probes are producer families alongside direct SDF proxy/tube fields and traditional mesh geometry, as long as each contributor emits the shared color/travel, metadata, control, depth where applicable, and reservoir-guide lanes needed for temporal validation.","files":["README.md","docs/temporal-spatial-evidence-reservoir.md","docs/perfect-machine-architecture.md","docs/README.md"],"source":"user architecture correction"} +{"timestamp":"2026-05-25T18:25:00+01:00","topic":"field-dsl-lowering-authority","lesson":"Aquafield scripts should express rolling texture/spline/surface intent plus quality budgets; Fensalir renderer owns backend lowering and LOD. Direct SDF tubes, reservoir splats, and future mesh lowering are backend choices behind the same evidence contract, not authoring commands in client scripts.","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs","src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","docs/temporal-spatial-evidence-reservoir.md","docs/perfect-machine-architecture.md"],"source":"user architecture correction"} diff --git a/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs b/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs new file mode 100644 index 0000000..b2fbbdf --- /dev/null +++ b/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs @@ -0,0 +1,48 @@ +using Aquarium.Engine.Render; + +namespace Aquarium.Engine.Tests; + +public sealed class FieldScriptCompilerTests +{ + [Fact] + public void CompileTreatsSplineFieldAsIntentWithAutoLoweringByDefault() + { + var frame = AquariumFieldScriptCompiler.Compile( + """ + texture id=spectrum + surface id=amplitude op=texture.sample value=0,0,0,0 + splinefield id=field texture=spectrum frequencyAxis=x columns=4 + """, + new Dictionary(StringComparer.Ordinal) + { + ["spectrum"] = new("spectrum", 8, 4, 1, 0, AquariumRollingModuloMode.Rows, Enumerable.Repeat(0.5f, 32).ToArray()), + }); + + Assert.Equal(AquariumFieldLoweringMode.Auto, frame.LoweringPolicy.Mode); + Assert.True(frame.Reservoir.HasInput); + Assert.False(frame.UseReservoirLowering); + Assert.Single(frame.TextureSplineFields); + } + + [Fact] + public void CompileParsesLoweringPolicyWithoutBackendCommands() + { + var frame = AquariumFieldScriptCompiler.Compile( + """ + texture id=spectrum + splinefield id=field texture=spectrum frequencyAxis=x columns=8 + lowering mode=reservoirSplats maxSplines=2 maxControlPoints=16 maxSplats=4096 lodBias=0.5 + """, + new Dictionary(StringComparer.Ordinal) + { + ["spectrum"] = new("spectrum", 8, 8, 1, 0, AquariumRollingModuloMode.Rows, Enumerable.Repeat(0.5f, 64).ToArray()), + }); + + Assert.Equal(AquariumFieldLoweringMode.ReservoirSplats, frame.LoweringPolicy.Mode); + Assert.Equal(2, frame.LoweringPolicy.MaxDirectSplines); + Assert.Equal(16, frame.LoweringPolicy.MaxDirectControlPoints); + Assert.Equal(4096, frame.LoweringPolicy.MaxReservoirSplats); + Assert.Equal(0.5f, frame.LoweringPolicy.LodBias); + Assert.True(frame.UseReservoirLowering); + } +}