From 3154e59d48af4f20179d8ed2b8783d9a83a75434 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 9 May 2026 13:20:06 +0100 Subject: [PATCH] Audit D3D12 foundation against synthesis --- docs/d3d12-best-practices-audit.md | 4 ++- research/d3d12/synthesis.md | 30 ++++++++++--------- .../Render/D3D12RenderTarget.cs | 3 +- src/Aquarium.Engine/Render/D3D12Renderer.cs | 4 ++- state/evidence.jsonl | 1 + state/memory.json | 2 +- state/scratch.md | 7 +++-- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/docs/d3d12-best-practices-audit.md b/docs/d3d12-best-practices-audit.md index 2df4dc3..b7e6d17 100644 --- a/docs/d3d12-best-practices-audit.md +++ b/docs/d3d12-best-practices-audit.md @@ -39,6 +39,8 @@ resource graph beyond smoke-pass scaffolding. - Render targets can create UAV descriptors. The D3D12 smoke pass binds a transient per-frame UAV descriptor and writes a diagnostic target from the pixel shader, proving CBV and UAV tables can share the active transient heap. + UAV-capable resource creation is separate from persistent UAV descriptor + ownership, so diagnostic-only transient UAVs do not consume static heap slots. - The D3D12 smoke path exercises shader compilation, root signature, PSO, descriptor table, RTV/UAV binding, barriers, copy, and present. @@ -62,7 +64,7 @@ resource graph beyond smoke-pass scaffolding. ## Required Before Real Pass Migration - Upload ring capacity policy and overflow reporting for real frame data. -- Debug names for D3D12 resources, descriptors by owner, and command lists. +- Descriptor-owner diagnostics beyond heap-level capacity summaries. - A real pass migration that uses the D3D12 resource helpers without cloning smoke-pass-local policy. diff --git a/research/d3d12/synthesis.md b/research/d3d12/synthesis.md index 2217eb5..eff8c02 100644 --- a/research/d3d12/synthesis.md +++ b/research/d3d12/synthesis.md @@ -49,10 +49,11 @@ application for shader-visible heaps. The long-term Aquarium shape should be: Current Aquarium D3D12 status: -- Good: shader-visible descriptor arena exists; per-frame CBV descriptors are - not overwritten while in flight. -- Temporary: arena is fixed and smoke-only. It must become a fence-reclaimed - allocator before real pass/material resources arrive. +- Good: shader-visible descriptors are split into static and per-frame + transient arenas. Transient CBV/UAV descriptors reset only after the owning + frame fence clears, and static descriptors rebuild on resize after a GPU wait. +- Temporary: arenas are still simple bump allocators. Keep them until real + non-resize churn demands free lists, paging, or a persistent global heap. ## Upload Doctrine @@ -64,10 +65,11 @@ memory before shader use. Current Aquarium D3D12 status: -- Good: upload buffer is persistently mapped and 256-byte aligned for constant - buffer use. -- Temporary: helper is still one allocation per buffer. It should become an - upload ring with typed allocations and fence reclamation. +- Good: each frame owns a persistently mapped upload ring, and constant-buffer + writes are 256-byte aligned. +- Temporary: upload policy is still per-frame and constant-only. Structured + scene data should move through typed upload allocations and default-heap + buffers before real pass migration grows data volume. - Guardrail: keep direct shader reads from upload memory limited to small constants. Structured/large scene data should move to default memory. @@ -95,9 +97,8 @@ Current Aquarium D3D12 status: - Good: the offscreen render target tracks its current state and transitions only on change. -- Temporary: backbuffer state is still assumed around present/copy. That is fine - in the current single path, but should become tracked or wrapped once resize, - multiple passes, or alternate present paths exist. +- Good: swapchain backbuffers are wrapped as tracked resources and transition + explicitly between present and render-target states. - Watch item: rendering to an offscreen target then copying to the swapchain is useful for proving texture ownership, but final composition should avoid unnecessary full-frame copies unless a post-process graph requires them. @@ -118,9 +119,10 @@ Current Aquarium D3D12 status: ## Aquarium Next Steps 1. Add a D3D12 frame graph/resource map for named render targets and buffers. -2. Replace the fixed descriptor arena with static + transient descriptor - allocation, reclaimed by frame fence. -3. Replace one-off upload buffers with a per-frame or global upload ring. +2. Promote descriptor allocation from smoke-path arenas into renderer-wide + static/transient descriptor policy. +3. Extend the upload ring from constants into typed uploads for structured + buffers and texture staging. 4. Add D3D12MA or a deliberate placed-resource allocator before the resource count becomes serious. 5. Add debug names and PIX markers while the graph is still small. diff --git a/src/Aquarium.Engine/Render/D3D12RenderTarget.cs b/src/Aquarium.Engine/Render/D3D12RenderTarget.cs index 54669aa..f631ac7 100644 --- a/src/Aquarium.Engine/Render/D3D12RenderTarget.cs +++ b/src/Aquarium.Engine/Render/D3D12RenderTarget.cs @@ -14,6 +14,7 @@ internal sealed class D3D12RenderTarget : IDisposable D3D12DescriptorSlot renderTargetView, D3D12DescriptorSlot? shaderResourceView, D3D12DescriptorSlot? unorderedAccessView, + bool allowUnorderedAccess, Color4 clearColor, string name) { @@ -24,7 +25,7 @@ internal sealed class D3D12RenderTarget : IDisposable ShaderResourceView = shaderResourceView; UnorderedAccessView = unorderedAccessView; var resourceFlags = ResourceFlags.AllowRenderTarget; - if (unorderedAccessView.HasValue) + if (allowUnorderedAccess || unorderedAccessView.HasValue) { resourceFlags |= ResourceFlags.AllowUnorderedAccess; } diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 15605e4..3c879e6 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -254,6 +254,7 @@ public sealed class D3D12Renderer : IAquariumRenderer renderTargetViewArena.Allocate(), staticShaderDescriptorArena.Allocate(), null, + false, new Color4(0.006f, 0.014f, 0.022f, 1.0f), "Aquarium D3D12 Smoke Target"); resourceRegistry.Add("smoke-target", target); @@ -269,7 +270,8 @@ public sealed class D3D12Renderer : IAquariumRenderer Format.R8G8B8A8_UNorm, renderTargetViewArena.Allocate(), null, - staticShaderDescriptorArena.Allocate(), + null, + true, new Color4(0.0f, 0.0f, 0.0f, 1.0f), "Aquarium D3D12 Diagnostic UAV Target"); resourceRegistry.Add("diagnostic-uav-target", target); diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 145f3e9..5baec85 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -69,3 +69,4 @@ {"area":"renderer/d3d12","lesson":"D3D12 now separates static shader descriptors from per-frame transient descriptor arenas, resets transient descriptors only after frame fence wait, registers named resources, and reports capacity for upload ring, transient/static shader descriptors, and RTV descriptors. Keep arenas simple until real churn demands free lists; avoid exact-fit capacities that leave no room for immediate next resources.","files":["src/Aquarium.Engine/Render/D3D12DescriptorArena.cs","src/Aquarium.Engine/Render/D3D12ResourceRegistry.cs","src/Aquarium.Engine/Render/D3D12UploadRing.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","docs/d3d12-best-practices-audit.md","docs/d3d12-migration.md","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj","Aquarium.Engine.exe --headless --renderer d3d12 exited 0 and printed resource registry plus capacity diagnostics"],"ts":"2026-05-09"} {"area":"renderer/d3d12","lesson":"D3D12 render targets now support SRV descriptors, and the smoke target is sampled by a second fullscreen pass instead of copied as an opaque resource. The renderer interface now receives current window dimensions, allowing D3D12 to recreate swapchain buffers and dependent smoke resources on resize. Resize still consumes fixed bring-up descriptors and fails loudly before exhaustion; production descriptor reclamation/rebuild policy is still needed before serious pass migration.","files":["src/Aquarium.Engine/Render/IAquariumRenderer.cs","src/Aquarium.Engine/AquariumHost.cs","src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/D3D12RenderTarget.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Smoke.hlsl","docs/d3d12-best-practices-audit.md","docs/d3d12-migration.md","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj","Aquarium.Engine.exe --headless --renderer d3d12 exited 0 with SRV copy/display pass and capacity diagnostics"],"ts":"2026-05-09"} {"area":"renderer/d3d12","lesson":"D3D12 render targets now support UAV descriptors, and the smoke pass proves a transient per-frame UAV table by writing a diagnostic texture from the pixel shader. Pixel shader UAV registers share the render-target output namespace, so the first manual UAV must bind at u1 when SV_Target0 is present. Resize now waits for the GPU, releases swapchain-dependent resources, rebuilds static shader/RTV descriptor arenas, and recreates descriptors from a clean state instead of burning fixed slots. The D3D12 backend also ignores the D3D11 shader-source override and compiles its copied D3D12Smoke.hlsl.","files":["src/Aquarium.Engine/Render/D3D12RenderTarget.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Smoke.hlsl","docs/d3d12-best-practices-audit.md","docs/d3d12-migration.md","state/memory.json","state/map.yaml","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 exited 0 and printed UAV/static/transient capacity diagnostics"],"ts":"2026-05-09"} +{"area":"renderer/d3d12","lesson":"D3D12 resource capability must remain separate from descriptor ownership. A texture can be created with AllowUnorderedAccess for transient UAV views without allocating a persistent static UAV descriptor. The diagnostic UAV target now uses a transient per-frame UAV descriptor only, reducing static heap use and keeping descriptor ownership honest before real pass migration.","files":["src/Aquarium.Engine/Render/D3D12RenderTarget.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","research/d3d12/synthesis.md","docs/d3d12-best-practices-audit.md","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx", ".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 exited 0 and static shader descriptors dropped to 1/64"],"ts":"2026-05-09"} diff --git a/state/memory.json b/state/memory.json index 5654e88..518def9 100644 --- a/state/memory.json +++ b/state/memory.json @@ -15,7 +15,7 @@ "native_engine": [ "Aquarium Engine lives at E:\\Projects\\Aquarium-Engine and is the live C# line. Main is the development branch after the 2026-05-08 merge.", "The host owns Win32 windowing, D3D11 swapchain creation, shader compilation, input translation, and the live-runtime assembly boundary. Do not outsource the core frame protocol to a hidden framework loop.", - "D3D12 is being added beside D3D11, not by mutating the reference renderer in place. Keep descriptor ownership explicit: static descriptors for long-lived SRV/UAV tables, per-frame transient descriptors reset only after fence wait, and whole-arena rebuilds on resize until real non-resize churn demands free lists.", + "D3D12 is being added beside D3D11, not by mutating the reference renderer in place. Keep descriptor ownership explicit: static descriptors for long-lived SRV/UAV tables, per-frame transient descriptors reset only after fence wait, and whole-arena rebuilds on resize until real non-resize churn demands free lists. Resource capability flags are not descriptor ownership; do not burn static UAV descriptors just to allow a transient UAV view.", "D3D12 pixel shader UAV registers share the output namespace. If a shader writes SV_Target0, bind the first manual UAV at u1, not u0.", "D3D12 shader routing must not inherit the D3D11 --shader-source override by accident. Backend-specific HLSL should compile from its copied output path unless an explicit backend-specific override exists.", "Use Vortice at the graphics API membrane. Keep CultMath as the CPU math grammar. Do not let third-party math opinions leak inward unless the conversion is explicit.", diff --git a/state/scratch.md b/state/scratch.md index c680b71..4984ab8 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -10,8 +10,9 @@ renderer-owned offscreen render target, fullscreen root signature, and smoke-test PSO that draws a diagnostic fullscreen triangle into the offscreen target before sampling it back to the swapchain. Render targets now support SRV/UAV descriptors, the smoke pass writes a diagnostic UAV through a per-frame -transient descriptor, and resize rebuilds static shader/RTV descriptor arenas -after a GPU wait instead of consuming fixed bring-up slots. Next renderer work +transient descriptor without consuming a persistent static UAV slot, and resize +rebuilds static shader/RTV descriptor arenas after a GPU wait instead of +consuming fixed bring-up slots. Next renderer work can start porting real passes while keeping D3D11 as the visual reference. ## Hot Context @@ -262,6 +263,8 @@ can start porting real passes while keeping D3D11 as the visual reference. D3D12 smoke pass binds a transient per-frame UAV descriptor and writes a diagnostic target from the pixel shader. Pixel shader UAVs share output register namespace, so the diagnostic UAV binds at `u1` rather than `u0`. + UAV resource capability is now independent from persistent UAV descriptor + ownership, so transient diagnostic UAVs do not burn static descriptors. D3D12 ignores the D3D11 `--shader-source` override and compiles its own copied `D3D12Smoke.hlsl`. Resize now waits for the GPU, releases swapchain-dependent resources, rebuilds static shader/RTV descriptor arenas, and recreates