diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs b/src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs index c8a7c92..21cc893 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs @@ -361,6 +361,42 @@ public readonly record struct AquariumFieldResourceDeclaration( NativeHandle: IntPtr.Zero, NativeHandleKind: "local-asset", SourceUri: sourceUri); + + public static AquariumFieldResourceDeclaration SurfacePage( + string resourceKey, + int width, + int height, + string format = "R16Float", + ulong version = 0, + string sourceUri = "", + long validFromNs = 0, + long validUntilNs = 0) => + new( + ResourceKey: resourceKey, + Kind: AquariumFieldResourceKind.SurfacePage, + Residency: AquariumFieldResourceResidency.GpuResident, + Access: AquariumFieldShaderAccess.ShaderResource, + Format: format, + Width: Math.Max(1, width), + Height: Math.Max(1, height), + DepthOrCount: 1, + StrideBytes: FormatStrideBytes(format), + ValidFromNs: validFromNs, + ValidUntilNs: validUntilNs, + Version: version, + NativeHandle: IntPtr.Zero, + NativeHandleKind: string.IsNullOrWhiteSpace(sourceUri) ? "fensalir-surface-page" : "local-asset", + SourceUri: sourceUri); + + private static int FormatStrideBytes(string format) => + format switch + { + "R8Unorm" or "R8_UNorm" or "R8_UNORM" => 1, + "R16Float" or "R16_Float" or "R16_FLOAT" => 2, + "R32Float" or "R32_Float" or "R32_FLOAT" or "Float32" => 4, + "Rgba16Float" or "R16G16B16A16_Float" or "R16G16B16A16_FLOAT" => 8, + _ => 4, + }; } public sealed class AquariumFieldEvidenceFrame diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs b/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs index 2e699b1..0112241 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs @@ -39,6 +39,9 @@ public static class AquariumFieldScriptCompiler case "texture2d": BindTexture2D(args, resourceAliases, resources, lineIndex); break; + case "surfacepage": + BindSurfacePage(args, resourceAliases, resources, lineIndex); + break; case "domain": var domain = ParseFieldDomain(args, lineIndex); if (domainKeys.Add(domain.DomainKey)) @@ -101,6 +104,30 @@ public static class AquariumFieldScriptCompiler } } + private static void BindSurfacePage( + IReadOnlyDictionary args, + IDictionary resourceAliases, + ICollection resources, + int lineIndex) + { + var id = Required(args, "id", lineIndex); + var key = StringValue(args, "key", $"aquarium:resource:surface-page:{id}"); + var resource = AquariumFieldResourceDeclaration.SurfacePage( + key, + Int(args, "width", 1, lineIndex), + Int(args, "height", 1, lineIndex), + StringValue(args, "format", "R16Float"), + UInt64(args, "version", 0, lineIndex), + StringValue(args, "path", "")); + + resourceAliases[id] = resource; + resourceAliases[key] = resource; + if (resources.All(existing => !string.Equals(existing.ResourceKey, resource.ResourceKey, StringComparison.Ordinal))) + { + resources.Add(resource); + } + } + public static AquariumBufferFieldFrame Compile( string source, IReadOnlyDictionary textureBindings, diff --git a/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs b/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs index 9f3050b..dcc9809 100644 --- a/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs +++ b/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs @@ -8,10 +8,11 @@ internal readonly record struct D3D12FieldResourceStats( int Resolved, int StructuredBuffers, int Texture2D, + int SurfacePages, int Unsupported, int StaleRemoved) { - public static D3D12FieldResourceStats Empty { get; } = new(0, 0, 0, 0, 0, 0); + public static D3D12FieldResourceStats Empty { get; } = new(0, 0, 0, 0, 0, 0, 0); } internal sealed class D3D12FieldResourceRegistry : IDisposable @@ -36,6 +37,7 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable var resolved = 0; var structuredBufferCount = 0; var textureCount = 0; + var surfacePageCount = 0; var unsupported = 0; foreach (var declaration in declarations) { @@ -72,6 +74,25 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable continue; } + if (declaration.Kind == AquariumFieldResourceKind.SurfacePage) + { + surfacePageCount++; + if (!declaration.HasSourceAsset && ResolveSurfacePage(device, declaration)) + { + resolved++; + } + else if (declaration.HasSourceAsset) + { + // Local asset surface pages need a command list and are loaded lazily by TryResolveSurfacePage. + } + else + { + unsupported++; + } + + continue; + } + unsupported++; } @@ -82,6 +103,7 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable Resolved: resolved, StructuredBuffers: structuredBufferCount, Texture2D: textureCount, + SurfacePages: surfacePageCount, Unsupported: unsupported, StaleRemoved: staleRemoved); } @@ -127,6 +149,19 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable return false; } + public bool TryGetSurfacePage(string resourceKey, out D3D12FieldTexture2D surfacePage) + { + if (textures.TryGetValue(resourceKey, out var slot) && + slot.Kind == AquariumFieldResourceKind.SurfacePage) + { + surfacePage = slot.Texture; + return true; + } + + surfacePage = null!; + return false; + } + public bool TryResolveTexture2D( ID3D12Device device, ID3D12GraphicsCommandList commandList, @@ -159,7 +194,7 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable try { texture = D3D12FieldTexture2D.LoadLocalAsset(device, commandList, declaration); - textures[declaration.ResourceKey] = new Texture2DSlot(texture, declaration.SourceUri, declaration.Version); + textures[declaration.ResourceKey] = new Texture2DSlot(texture, declaration.Kind, declaration.SourceUri, declaration.Version); return true; } catch (IOException) @@ -189,6 +224,81 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable } } + public bool TryResolveSurfacePage( + ID3D12Device device, + ID3D12GraphicsCommandList commandList, + AquariumFieldResourceDeclaration declaration, + out D3D12FieldTexture2D surfacePage) + { + surfacePage = null!; + if (declaration.Kind != AquariumFieldResourceKind.SurfacePage || + declaration.Residency != AquariumFieldResourceResidency.GpuResident || + declaration.Access != AquariumFieldShaderAccess.ShaderResource) + { + return false; + } + + if (textures.TryGetValue(declaration.ResourceKey, out var existing) && + existing.Kind == AquariumFieldResourceKind.SurfacePage && + existing.Version == declaration.Version && + string.Equals(existing.SourceUri, declaration.SourceUri, StringComparison.Ordinal)) + { + surfacePage = existing.Texture; + return true; + } + + if (existing is not null) + { + existing.Texture.Dispose(); + textures.Remove(declaration.ResourceKey); + } + + try + { + surfacePage = declaration.HasSourceAsset + ? D3D12FieldTexture2D.LoadLocalAsset(device, commandList, declaration) + : D3D12FieldTexture2D.TryCreateEmpty(device, declaration, out var emptySurfacePage) + ? emptySurfacePage + : null!; + if (surfacePage is null) + { + return false; + } + + textures[declaration.ResourceKey] = new Texture2DSlot( + surfacePage, + AquariumFieldResourceKind.SurfacePage, + declaration.SourceUri, + declaration.Version); + return true; + } + catch (IOException) + { + surfacePage = null!; + return false; + } + catch (InvalidOperationException) + { + surfacePage = null!; + return false; + } + catch (UnauthorizedAccessException) + { + surfacePage = null!; + return false; + } + catch (ArgumentException) + { + surfacePage = null!; + return false; + } + catch (SharpGenException) + { + surfacePage = null!; + return false; + } + } + private bool ResolveStructuredBuffer( ID3D12Device device, D3D12ResourceRegistry resourceRegistry, @@ -239,6 +349,43 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable return true; } + private bool ResolveSurfacePage( + ID3D12Device device, + AquariumFieldResourceDeclaration declaration) + { + if (declaration.Residency != AquariumFieldResourceResidency.GpuResident || + declaration.Access != AquariumFieldShaderAccess.ShaderResource) + { + return false; + } + + if (textures.TryGetValue(declaration.ResourceKey, out var existing) && + existing.Kind == AquariumFieldResourceKind.SurfacePage && + existing.Version == declaration.Version && + string.Equals(existing.SourceUri, declaration.SourceUri, StringComparison.Ordinal)) + { + return true; + } + + if (existing is not null) + { + existing.Texture.Dispose(); + textures.Remove(declaration.ResourceKey); + } + + if (!D3D12FieldTexture2D.TryCreateEmpty(device, declaration, out var surfacePage)) + { + return false; + } + + textures[declaration.ResourceKey] = new Texture2DSlot( + surfacePage, + AquariumFieldResourceKind.SurfacePage, + declaration.SourceUri, + declaration.Version); + return true; + } + private int RemoveStaleStructuredBuffers(D3D12ResourceRegistry resourceRegistry, HashSet currentKeys) { var removed = 0; @@ -318,6 +465,7 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable private sealed record Texture2DSlot( D3D12FieldTexture2D Texture, + AquariumFieldResourceKind Kind, string SourceUri, ulong Version); } diff --git a/src/Aquarium.Engine/Render/D3D12FieldTexture2D.cs b/src/Aquarium.Engine/Render/D3D12FieldTexture2D.cs index 7069c22..486616d 100644 --- a/src/Aquarium.Engine/Render/D3D12FieldTexture2D.cs +++ b/src/Aquarium.Engine/Render/D3D12FieldTexture2D.cs @@ -82,6 +82,44 @@ internal sealed unsafe class D3D12FieldTexture2D : IDisposable $"Aquarium D3D12 Field Texture2D {declaration.ResourceKey}"); } + public static bool TryCreateEmpty( + ID3D12Device device, + AquariumFieldResourceDeclaration declaration, + out D3D12FieldTexture2D texture) + { + texture = null!; + if (!TryFormat(declaration.Format, out var format)) + { + return false; + } + + var width = Math.Max(1, declaration.Width); + var height = Math.Max(1, declaration.Height); + var resource = device.CreateCommittedResource( + HeapType.Default, + ResourceDescription.Texture2D( + format, + (uint)width, + (uint)height, + 1, + 1, + 1, + 0, + ResourceFlags.None), + ResourceStates.PixelShaderResource, + null); + + texture = new D3D12FieldTexture2D( + resource, + uploadResource: null, + width, + height, + format, + $"Aquarium D3D12 Field Texture2D {declaration.ResourceKey}"); + texture.State = ResourceStates.PixelShaderResource; + return true; + } + public void CreateShaderResourceView(ID3D12Device device, D3D12DescriptorSlot descriptor) { device.CreateShaderResourceView( @@ -219,4 +257,18 @@ internal sealed unsafe class D3D12FieldTexture2D : IDisposable { return (value + alignment - 1) & ~(alignment - 1); } + + private static bool TryFormat(string formatName, out Format format) + { + format = formatName switch + { + "R8Unorm" or "R8_UNorm" or "R8_UNORM" => Format.R8_UNorm, + "R16Float" or "R16_Float" or "R16_FLOAT" => Format.R16_Float, + "R32Float" or "R32_Float" or "R32_FLOAT" or "Float32" => Format.R32_Float, + "Rgba8Unorm" or "R8G8B8A8_UNorm" or "R8G8B8A8_UNORM" => Format.R8G8B8A8_UNorm, + "Rgba16Float" or "R16G16B16A16_Float" or "R16G16B16A16_FLOAT" => Format.R16G16B16A16_Float, + _ => Format.Unknown, + }; + return format != Format.Unknown; + } } diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index fbd29bf..3db5236 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -3030,6 +3030,7 @@ public sealed class D3D12Renderer : IAquariumRenderer $"resolved resources {activeFieldResourceStats.Resolved:N0}; " + $"structured buffers {activeFieldResourceStats.StructuredBuffers:N0}; " + $"texture2d {activeFieldResourceStats.Texture2D:N0}; " + + $"surface pages {activeFieldResourceStats.SurfacePages:N0}; " + $"unsupported resources {activeFieldResourceStats.Unsupported:N0}; " + $"claims {activeFieldEvidenceFrame.Claims.Count:N0}; " + $"candidates {activeFieldEvidenceFrame.Candidates.Count:N0}; " + diff --git a/state/evidence.jsonl b/state/evidence.jsonl index db068c8..c6d63bc 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -239,3 +239,4 @@ {"lesson":"TubeField rendering now has a dedicated D3D12 render pipeline rather than reusing generic spline material. Compute expansion records one draw batch per lowering, and the TubeField pixel shader binds the matching source buffer/constants and resamples the rolling buffer per pixel by column+spline coordinate with the cheap 5-tap path. The initial material is opaque field geometry with fallback black-to-warm ramp, squared normalized emission, and fake Fresnel center-bright/edge-dark shaping. Real ramp texture binding, indirect draw packets, and non-buffer resolvers remain open. Headless dev reload, Aquarium.Engine.Tests, Mimir build, and both Mimir Fensalir field smokes passed.","timestamp":"2026-05-26T20:22:00+01:00","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl","state/map.yaml"],"source":"continued GPU-resident TubeField material implementation","topic":"d3d12-tubefield-pixel-material"} {"lesson":"Texture2D local assets are now first-class field resources. AquariumFieldResourceDeclaration carries SourceUri and has a LocalTexture2D factory; the field DSL accepts `texture2d id=... key=... path=...` and TubeField `ramp=...` resolves aliases into RampResourceKey. D3D12FieldResourceRegistry lazily uploads local Rgba8 assets into GPU-resident textures, TubeField rendering binds the ramp SRV plus linear clamp sampler, and unresolved ramps use a Fensalir-owned 0-1 fallback texture. This replaces raw ramp paths as live authority while keeping them as compatibility metadata. Fensalir build/tests/headless reload and Mimir build/field smokes passed.","timestamp":"2026-05-26T20:40:00+01:00","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs","src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs","src/Aquarium.Engine/Render/D3D12FieldTexture2D.cs","src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl","tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs","state/map.yaml"],"source":"user requested Texture2D local asset resource declaration and automatic DSL binding","topic":"field-texture2d-resource-binding"} {"lesson":"TubeField draw dimensions are now emitted as GPU-resident indirect draw packets. The expansion compute shader writes one D3D12 DrawIndexedArguments packet per resolved TubeField batch into a UAV argument buffer; render binds the matching source/ramp material state and calls ExecuteIndirect instead of CPU-manufacturing DrawIndexedInstanced counts. CPU still schedules one indirect call per material/resource batch because each batch has distinct root bindings, but draw count/start index authority now lives in the GPU expansion output. Fensalir build/tests/headless reload and Mimir build/field smokes passed.","timestamp":"2026-05-26T20:59:00+01:00","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl","state/map.yaml"],"source":"continued migration plan after Texture2D resolver cut","topic":"d3d12-tubefield-indirect-draw-packets"} +{"lesson":"SurfacePage is now a resolvable field resource kind rather than just a planner enum. AquariumFieldResourceDeclaration has a SurfacePage factory, the field evidence DSL accepts `surfacepage id=... key=... width=... height=... format=...`, and D3D12FieldResourceRegistry allocates empty GPU-resident shader-readable page textures or lazily loads local source-backed pages through the same texture upload path. This does not finish terrain/heightfield lowering; it creates the resource authority that future Height/Sdf2D/Material packets can bind. Fensalir build/tests/headless reload and Mimir build/field smokes passed.","timestamp":"2026-05-26T21:05:00+01:00","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs","src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs","src/Aquarium.Engine/Render/D3D12FieldTexture2D.cs","src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs","state/map.yaml"],"source":"continued field resource resolver migration","topic":"field-surface-page-resource-resolver"} diff --git a/state/map.yaml b/state/map.yaml index aad893d..7dac306 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -97,7 +97,12 @@ next_actions: executes the packet with ExecuteIndirect. The first material is opaque field geometry with squared normalized emission, fake Fresnel center-bright/ edge-dark shaping, and requested/dispatched/truncated/indirect-draw segment - reporting. Remaining cuts are mesh/surface-page/volume resolvers. + reporting. SurfacePage resources now have a first resolver: contracts and + DSL can declare GPU-resident 2D pages with width, height, format, version, + and optional local source, and the D3D12 registry allocates/imports them as + shader-readable page textures. Remaining cuts are mesh and volume resolvers, + plus render lowerings that consume surface pages as height/SDF/material + domains. - Move any remaining client-only Epiphany state found here back to `E:\Projects\Aquarium-Engine`. - Keep Mimir app/adapters out of this repo; Fensalir may expose generic sensor-fusion contracts only. - Package contracts/host deliberately before replacing sibling source references in client repos. diff --git a/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs b/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs index 2d89d07..13cbd09 100644 --- a/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs +++ b/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs @@ -127,4 +127,24 @@ public sealed class FieldScriptCompilerTests Assert.Equal(ramp.ResourceKey, lowering.RampResourceKey); Assert.Equal(ramp.SourceUri, lowering.RampTexturePath); } + + [Fact] + public void CompileEvidenceDeclaresSurfacePagesAsGpuResources() + { + var frame = AquariumFieldScriptCompiler.CompileEvidence( + """ + surfacepage id=height key=aquarium:resource:surface-page:heightfield width=128 height=128 format=R16Float version=3 + """, + new Dictionary(StringComparer.Ordinal)); + + var resource = Assert.Single(frame.Resources); + Assert.Equal("aquarium:resource:surface-page:heightfield", resource.ResourceKey); + Assert.Equal(AquariumFieldResourceKind.SurfacePage, resource.Kind); + Assert.Equal(AquariumFieldResourceResidency.GpuResident, resource.Residency); + Assert.Equal(AquariumFieldShaderAccess.ShaderResource, resource.Access); + Assert.Equal(128, resource.Width); + Assert.Equal(128, resource.Height); + Assert.Equal("R16Float", resource.Format); + Assert.Equal("fensalir-surface-page", resource.NativeHandleKind); + } }