diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs b/src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs index 0363e83..4240b6d 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs @@ -96,6 +96,23 @@ public enum AquariumFieldShaderAccess AccelerationStructure = 6, } +public enum AquariumFieldMeshTopology +{ + Unknown = 0, + TriangleList = 1, + TriangleStrip = 2, + LineList = 3, + LineStrip = 4, + PointList = 5, +} + +public enum AquariumFieldMeshIndexFormat +{ + Unknown = 0, + UInt16 = 1, + UInt32 = 2, +} + public enum AquariumFieldInvalidationCode { None = 0, @@ -238,6 +255,36 @@ public readonly record struct AquariumFieldBackendPacket( Support.HasSupport; } +public readonly record struct AquariumFieldMeshBuffer( + string BufferKey, + int Count, + int StrideBytes, + IntPtr NativeHandle, + string NativeHandleKind) +{ + public bool HasShape => Count > 0 && StrideBytes > 0; +} + +public readonly record struct AquariumFieldMeshResource( + AquariumFieldMeshBuffer Vertices, + AquariumFieldMeshBuffer Indices, + AquariumFieldMeshTopology Topology, + AquariumFieldMeshIndexFormat IndexFormat, + Vector3 BoundsMin, + Vector3 BoundsMax, + int SubmeshCount) +{ + public bool IsValid => + Vertices.HasShape && + Indices.HasShape && + Topology != AquariumFieldMeshTopology.Unknown && + IndexFormat != AquariumFieldMeshIndexFormat.Unknown && + SubmeshCount > 0 && + BoundsMax.X >= BoundsMin.X && + BoundsMax.Y >= BoundsMin.Y && + BoundsMax.Z >= BoundsMin.Z; +} + public readonly record struct AquariumFieldTubeSplineLowering( string LoweringKey, string ClaimKey, @@ -316,7 +363,8 @@ public readonly record struct AquariumFieldResourceDeclaration( ulong Version, IntPtr NativeHandle, string NativeHandleKind, - string SourceUri = "") + string SourceUri = "", + AquariumFieldMeshResource Mesh = default) { public bool HasIdentity => !string.IsNullOrWhiteSpace(ResourceKey); @@ -324,7 +372,7 @@ public readonly record struct AquariumFieldResourceDeclaration( Kind != AquariumFieldResourceKind.Unknown && Residency != AquariumFieldResourceResidency.Unknown && Access != AquariumFieldShaderAccess.Unknown && - (Width > 0 || DepthOrCount > 0 || StrideBytes > 0 || HasSourceAsset); + (Width > 0 || DepthOrCount > 0 || StrideBytes > 0 || HasSourceAsset || Mesh.IsValid); public bool HasSourceAsset => !string.IsNullOrWhiteSpace(SourceUri); @@ -414,6 +462,30 @@ public readonly record struct AquariumFieldResourceDeclaration( NativeHandleKind: "fensalir-volume-texture", SourceUri: ""); + public static AquariumFieldResourceDeclaration MeshPackage( + string resourceKey, + AquariumFieldMeshResource mesh, + ulong version = 0, + long validFromNs = 0, + long validUntilNs = 0) => + new( + ResourceKey: resourceKey, + Kind: AquariumFieldResourceKind.Mesh, + Residency: AquariumFieldResourceResidency.GpuResident, + Access: AquariumFieldShaderAccess.ShaderResource, + Format: "FieldMesh", + Width: Math.Max(1, mesh.Vertices.Count), + Height: Math.Max(1, mesh.SubmeshCount), + DepthOrCount: Math.Max(1, mesh.Indices.Count), + StrideBytes: Math.Max(1, mesh.Vertices.StrideBytes), + ValidFromNs: validFromNs, + ValidUntilNs: validUntilNs, + Version: version, + NativeHandle: IntPtr.Zero, + NativeHandleKind: "fensalir-mesh-package", + SourceUri: "", + Mesh: mesh); + private static int FormatStrideBytes(string format) => format switch { @@ -554,6 +626,11 @@ public static class AquariumFieldEvidenceValidator issues.Add(Error(resource.ResourceKey, "Field resource declaration is missing kind, residency, access, or shape.")); } + if (resource.Kind == AquariumFieldResourceKind.Mesh && !resource.Mesh.IsValid) + { + issues.Add(Error(resource.ResourceKey, "Mesh field resource is missing vertex buffer, index buffer, topology, index format, bounds, or submesh count.")); + } + if (!resource.IsGpuVisible) { issues.Add(Warning(resource.ResourceKey, "Field resource is not GPU-visible; shader lowering must import or upload before use.")); diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs b/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs index 986a6eb..e117555 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs @@ -45,6 +45,9 @@ public static class AquariumFieldScriptCompiler case "volumetexture": BindVolumeTexture(args, resourceAliases, resources, lineIndex); break; + case "mesh": + BindMesh(args, resourceAliases, resources, lineIndex); + break; case "domain": var domain = ParseFieldDomain(args, lineIndex); if (domainKeys.Add(domain.DomainKey)) @@ -155,6 +158,40 @@ public static class AquariumFieldScriptCompiler } } + private static void BindMesh( + IReadOnlyDictionary args, + IDictionary resourceAliases, + ICollection resources, + int lineIndex) + { + var id = Required(args, "id", lineIndex); + var key = StringValue(args, "key", $"aquarium:resource:mesh:{id}"); + var vertexCount = Int(args, "vertices", 1, lineIndex); + var vertexStride = Int(args, "vertexStride", 32, lineIndex); + var indexCount = Int(args, "indices", 3, lineIndex); + var indexFormat = Enum.Parse(StringValue(args, "indexFormat", nameof(AquariumFieldMeshIndexFormat.UInt32)), ignoreCase: true); + var indexStride = indexFormat == AquariumFieldMeshIndexFormat.UInt16 ? 2 : 4; + var mesh = new AquariumFieldMeshResource( + Vertices: new AquariumFieldMeshBuffer($"{key}:vertices", vertexCount, vertexStride, IntPtr.Zero, "fensalir-mesh-vertices"), + Indices: new AquariumFieldMeshBuffer($"{key}:indices", indexCount, indexStride, IntPtr.Zero, "fensalir-mesh-indices"), + Topology: Enum.Parse(StringValue(args, "topology", nameof(AquariumFieldMeshTopology.TriangleList)), ignoreCase: true), + IndexFormat: indexFormat, + BoundsMin: Vec3(args, "min", -Vector3.One, lineIndex), + BoundsMax: Vec3(args, "max", Vector3.One, lineIndex), + SubmeshCount: Int(args, "submeshes", 1, lineIndex)); + var resource = AquariumFieldResourceDeclaration.MeshPackage( + key, + mesh, + UInt64(args, "version", 0, lineIndex)); + + 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/D3D12FieldMesh.cs b/src/Aquarium.Engine/Render/D3D12FieldMesh.cs new file mode 100644 index 0000000..7a2f838 --- /dev/null +++ b/src/Aquarium.Engine/Render/D3D12FieldMesh.cs @@ -0,0 +1,65 @@ +using Vortice.Direct3D12; +using Vortice.DXGI; +using Vortice.Direct3D; + +namespace Aquarium.Engine.Render; + +internal sealed class D3D12FieldMesh : IDisposable +{ + public D3D12FieldMesh( + D3D12StructuredBuffer vertices, + D3D12StructuredBuffer indices, + AquariumFieldMeshTopology topology, + AquariumFieldMeshIndexFormat indexFormat, + int submeshCount, + ulong version) + { + Vertices = vertices; + Indices = indices; + Topology = topology; + IndexFormat = indexFormat; + SubmeshCount = submeshCount; + Version = version; + } + + public D3D12StructuredBuffer Vertices { get; } + + public D3D12StructuredBuffer Indices { get; } + + public AquariumFieldMeshTopology Topology { get; } + + public AquariumFieldMeshIndexFormat IndexFormat { get; } + + public int SubmeshCount { get; } + + public ulong Version { get; } + + public PrimitiveTopology D3DTopology => Topology switch + { + AquariumFieldMeshTopology.TriangleStrip => PrimitiveTopology.TriangleStrip, + AquariumFieldMeshTopology.LineList => PrimitiveTopology.LineList, + AquariumFieldMeshTopology.LineStrip => PrimitiveTopology.LineStrip, + AquariumFieldMeshTopology.PointList => PrimitiveTopology.PointList, + _ => PrimitiveTopology.TriangleList, + }; + + public Format D3DIndexFormat => IndexFormat == AquariumFieldMeshIndexFormat.UInt16 + ? Format.R16_UInt + : Format.R32_UInt; + + public VertexBufferView VertexBufferView => new( + Vertices.Resource.GPUVirtualAddress, + (uint)Vertices.SizeBytes, + (uint)Vertices.StrideBytes); + + public IndexBufferView IndexBufferView => new( + Indices.Resource.GPUVirtualAddress, + (uint)Indices.SizeBytes, + D3DIndexFormat); + + public void Dispose() + { + Indices.Dispose(); + Vertices.Dispose(); + } +} diff --git a/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs b/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs index 629379b..8be78b8 100644 --- a/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs +++ b/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs @@ -10,10 +10,11 @@ internal readonly record struct D3D12FieldResourceStats( int Texture2D, int SurfacePages, int VolumeTextures, + int Meshes, int Unsupported, int StaleRemoved) { - public static D3D12FieldResourceStats Empty { get; } = new(0, 0, 0, 0, 0, 0, 0, 0); + public static D3D12FieldResourceStats Empty { get; } = new(0, 0, 0, 0, 0, 0, 0, 0, 0); } internal sealed class D3D12FieldResourceRegistry : IDisposable @@ -23,6 +24,7 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable private readonly Dictionary structuredBuffers = new(StringComparer.Ordinal); private readonly Dictionary textures = new(StringComparer.Ordinal); private readonly Dictionary volumeTextures = new(StringComparer.Ordinal); + private readonly Dictionary meshes = new(StringComparer.Ordinal); private readonly HashSet liveKeys = new(StringComparer.Ordinal); public D3D12FieldResourceStats Resolve( @@ -41,6 +43,7 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable var textureCount = 0; var surfacePageCount = 0; var volumeTextureCount = 0; + var meshCount = 0; var unsupported = 0; foreach (var declaration in declarations) { @@ -111,12 +114,28 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable continue; } + if (declaration.Kind == AquariumFieldResourceKind.Mesh) + { + meshCount++; + if (ResolveMesh(device, declaration)) + { + resolved++; + } + else + { + unsupported++; + } + + continue; + } + unsupported++; } var staleRemoved = RemoveStaleStructuredBuffers(resourceRegistry, liveKeys); staleRemoved += RemoveStaleTextures(liveKeys); staleRemoved += RemoveStaleVolumeTextures(liveKeys); + staleRemoved += RemoveStaleMeshes(liveKeys); return new D3D12FieldResourceStats( Declared: declarations.Count, Resolved: resolved, @@ -124,6 +143,7 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable Texture2D: textureCount, SurfacePages: surfacePageCount, VolumeTextures: volumeTextureCount, + Meshes: meshCount, Unsupported: unsupported, StaleRemoved: staleRemoved); } @@ -145,9 +165,15 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable slot.Texture.Dispose(); } + foreach (var mesh in meshes.Values) + { + mesh.Dispose(); + } + structuredBuffers.Clear(); textures.Clear(); volumeTextures.Clear(); + meshes.Clear(); liveKeys.Clear(); } @@ -200,6 +226,17 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable return false; } + public bool TryGetMesh(string resourceKey, out D3D12FieldMesh mesh) + { + if (meshes.TryGetValue(resourceKey, out mesh!)) + { + return true; + } + + mesh = null!; + return false; + } + public bool TryResolveTexture2D( ID3D12Device device, ID3D12GraphicsCommandList commandList, @@ -463,6 +500,54 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable return true; } + private bool ResolveMesh( + ID3D12Device device, + AquariumFieldResourceDeclaration declaration) + { + var mesh = declaration.Mesh; + if (!mesh.IsValid || declaration.Residency != AquariumFieldResourceResidency.GpuResident) + { + return false; + } + + if (meshes.TryGetValue(declaration.ResourceKey, out var existing) && + existing.Version == declaration.Version && + existing.Vertices.ElementCount == mesh.Vertices.Count && + existing.Vertices.StrideBytes == mesh.Vertices.StrideBytes && + existing.Indices.ElementCount == mesh.Indices.Count && + existing.Indices.StrideBytes == mesh.Indices.StrideBytes && + existing.Topology == mesh.Topology && + existing.IndexFormat == mesh.IndexFormat) + { + return true; + } + + if (existing is not null) + { + existing.Dispose(); + meshes.Remove(declaration.ResourceKey); + } + + var vertices = new D3D12StructuredBuffer( + device, + mesh.Vertices.Count, + mesh.Vertices.StrideBytes, + $"Aquarium D3D12 Field Mesh Vertices {declaration.ResourceKey}"); + var indices = new D3D12StructuredBuffer( + device, + mesh.Indices.Count, + mesh.Indices.StrideBytes, + $"Aquarium D3D12 Field Mesh Indices {declaration.ResourceKey}"); + meshes[declaration.ResourceKey] = new D3D12FieldMesh( + vertices, + indices, + mesh.Topology, + mesh.IndexFormat, + mesh.SubmeshCount, + declaration.Version); + return true; + } + private int RemoveStaleStructuredBuffers(D3D12ResourceRegistry resourceRegistry, HashSet currentKeys) { var removed = 0; @@ -518,6 +603,24 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable return removed; } + private int RemoveStaleMeshes(HashSet currentKeys) + { + var removed = 0; + foreach (var key in meshes.Keys.ToArray()) + { + if (currentKeys.Contains(key)) + { + continue; + } + + meshes[key].Dispose(); + meshes.Remove(key); + removed++; + } + + return removed; + } + private static string RegistryKey(string resourceKey) => RegistryPrefix + resourceKey; private static D3D12StructuredBuffer? OpenSharedStructuredBuffer( diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index b219cf6..5aa9ade 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -3032,6 +3032,7 @@ public sealed class D3D12Renderer : IAquariumRenderer $"texture2d {activeFieldResourceStats.Texture2D:N0}; " + $"surface pages {activeFieldResourceStats.SurfacePages:N0}; " + $"volume textures {activeFieldResourceStats.VolumeTextures:N0}; " + + $"meshes {activeFieldResourceStats.Meshes: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 d1beb6d..80c4941 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -241,3 +241,4 @@ {"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"} {"lesson":"VolumeTexture is now a resolvable field resource kind. AquariumFieldResourceDeclaration has a VolumeTexture factory, the field evidence DSL accepts `volumetexture id=... key=... width=... height=... depth=... format=...`, and D3D12FieldResourceRegistry allocates empty GPU-resident shader-readable Texture3D resources with format/dimension/version tracking. This creates resource authority for future density/extinction/SDF3D lowerings without pretending those render paths exist yet. Fensalir build/tests/headless reload and Mimir build/field smokes passed.","timestamp":"2026-05-26T21:11:00+01:00","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs","src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs","src/Aquarium.Engine/Render/D3D12FieldTexture3D.cs","src/Aquarium.Engine/Render/D3D12FieldTextureFormat.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-volume-texture-resource-resolver"} +{"lesson":"Mesh is now a resolvable field resource package rather than loose scalar handles. AquariumFieldMeshResource owns Mesh.Vertices, Mesh.Indices, topology, index format, bounds, submesh count, and version; the field evidence DSL accepts `mesh id=... key=... vertices=... vertexStride=... indices=... indexFormat=... topology=...`; and D3D12FieldResourceRegistry allocates/reuses GPU-resident vertex and index buffers under the mesh resource key. This creates resource authority for future mesh draw/compute lowerings without pretending a material or vertex-layout render path exists yet. Fensalir build/tests/headless reload passed.","timestamp":"2026-05-26T21:24:00+01:00","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs","src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs","src/Aquarium.Engine/Render/D3D12FieldMesh.cs","src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs","state/map.yaml"],"source":"user clarified Mesh package API boundary","topic":"field-mesh-resource-resolver"} diff --git a/state/map.yaml b/state/map.yaml index 993558f..b939cdf 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -103,9 +103,13 @@ next_actions: shader-readable page textures. VolumeTexture resources now have the same first resolver shape: contracts and DSL can declare GPU-resident 3D textures with dimensions, depth, format, and version, and the D3D12 registry - allocates them as shader-readable volume textures. Remaining resolver cut - is Mesh, plus render lowerings that consume surface pages and volumes as - height/SDF/material/density domains. + allocates them as shader-readable volume textures. Mesh resources are now + first-class packages with `Mesh.Vertices` and `Mesh.Indices` buffer members, + topology, index format, bounds, submesh count, and version. The DSL can + declare a GPU-resident mesh package, and the D3D12 registry allocates and + reuses matching vertex/index GPU buffers under the mesh resource key. + Remaining cuts are render lowerings that consume meshes, surface pages, and + volumes as selected geometry/height/SDF/material/density 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 e6a44d4..4df6cc9 100644 --- a/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs +++ b/tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs @@ -168,4 +168,28 @@ public sealed class FieldScriptCompilerTests Assert.Equal("R16Float", resource.Format); Assert.Equal("fensalir-volume-texture", resource.NativeHandleKind); } + + [Fact] + public void CompileEvidenceDeclaresMeshPackagesAsGpuResources() + { + var frame = AquariumFieldScriptCompiler.CompileEvidence( + """ + mesh id=quad key=aquarium:resource:mesh:quad vertices=4 vertexStride=32 indices=6 indexFormat=UInt32 topology=TriangleList min=-1,-1,0 max=1,1,0 submeshes=1 version=9 + """, + new Dictionary(StringComparer.Ordinal)); + + var resource = Assert.Single(frame.Resources); + Assert.Equal("aquarium:resource:mesh:quad", resource.ResourceKey); + Assert.Equal(AquariumFieldResourceKind.Mesh, resource.Kind); + Assert.Equal(AquariumFieldResourceResidency.GpuResident, resource.Residency); + Assert.Equal(AquariumFieldShaderAccess.ShaderResource, resource.Access); + Assert.Equal("fensalir-mesh-package", resource.NativeHandleKind); + Assert.True(resource.Mesh.IsValid); + Assert.Equal(4, resource.Mesh.Vertices.Count); + Assert.Equal(32, resource.Mesh.Vertices.StrideBytes); + Assert.Equal(6, resource.Mesh.Indices.Count); + Assert.Equal(4, resource.Mesh.Indices.StrideBytes); + Assert.Equal(AquariumFieldMeshTopology.TriangleList, resource.Mesh.Topology); + Assert.Equal(AquariumFieldMeshIndexFormat.UInt32, resource.Mesh.IndexFormat); + } }