Add D3D12 renderer foothold
This commit is contained in:
parent
62862c0335
commit
e3b751bcff
9 changed files with 295 additions and 10 deletions
33
docs/d3d12-migration.md
Normal file
33
docs/d3d12-migration.md
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
# D3D12 Migration
|
||||
|
||||
Aquarium is moving to D3D12 before the transparent stochastic surface pipeline
|
||||
gets real machinery. D3D11 remains the reference renderer until the D3D12 path
|
||||
has feature parity.
|
||||
|
||||
## Current State
|
||||
|
||||
- `--renderer d3d11` is the default renderer and owns the live scene.
|
||||
- `--renderer d3d12` creates a D3D12 device, command queue, flip-discard
|
||||
swapchain, RTV heap, command allocator/list, and fence-backed present loop.
|
||||
- The D3D12 path currently clears and presents only. It exists to prove the
|
||||
backend lifecycle before shader, resource, and pass migration starts.
|
||||
- `IAquariumRenderer` is the host boundary. The host should not learn backend
|
||||
internals as the D3D12 renderer grows teeth.
|
||||
|
||||
## Migration Order
|
||||
|
||||
1. Keep D3D11 as the visual reference.
|
||||
2. Move shared renderer contracts behind explicit backend-neutral types.
|
||||
3. Port root signatures, descriptor heaps, upload/ring buffers, and pass
|
||||
resource lifetime.
|
||||
4. Port the existing passes in visible order: grid height, froxel volume, scene,
|
||||
bloom, resolve, overlay/debug.
|
||||
5. Build the transparent stochastic surface pipe in D3D12 once descriptor and
|
||||
candidate-buffer ownership is explicit.
|
||||
|
||||
## Invariants
|
||||
|
||||
- No hidden D3D11 dependency should leak into shared renderer contracts.
|
||||
- D3D12 work must be validated by actual headless runs, not just compilation.
|
||||
- D3D11 output remains the comparison target until D3D12 fully renders the scene.
|
||||
- Transparent surfaces are still events/candidates, not canonical depth.
|
||||
|
|
@ -64,7 +64,7 @@ public sealed class AquariumRuntime : IAquariumRuntime
|
|||
Console.WriteLine($"Graphics settings: debug {graphicsSettings.RenderDebugMode}, exposure {graphicsSettings.SceneExposure:0.###}, bloom {graphicsSettings.BloomIntensity:0.###}, veil {graphicsSettings.BloomVeilIntensity:0.###}");
|
||||
Console.WriteLine($"CultCache: {stateStore.CachePath}");
|
||||
Console.WriteLine($"CultNet hello: {stateStore.Hello.RuntimeKind} / {stateStore.Hello.DisplayName}");
|
||||
Console.WriteLine("Vortice D3D11 is present as the host shell; Aquarium owns the invariants.");
|
||||
Console.WriteLine("Aquarium host renderer owns the visible frame invariants.");
|
||||
}
|
||||
|
||||
public void Update(float deltaSeconds, InputState input)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
<PackageReference Include="Vortice.Direct2D1" Version="3.8.3" />
|
||||
<ProjectReference Include="..\Aquarium.Engine.Contracts\Aquarium.Engine.Contracts.csproj" />
|
||||
<PackageReference Include="Vortice.Direct3D11" Version="3.8.3" />
|
||||
<PackageReference Include="Vortice.Direct3D12" Version="3.8.3" />
|
||||
<PackageReference Include="Vortice.D3DCompiler" Version="3.8.3" />
|
||||
<PackageReference Include="Vortice.DXGI" Version="3.8.3" />
|
||||
<PackageReference Include="Vortice.Mathematics" Version="2.1.1" />
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ public static class AquariumHost
|
|||
var iconPath = Path.Combine(AppContext.BaseDirectory, "Assets", "Aquarium-Engine-Icon.ico");
|
||||
using var window = Win32Window.Create("Epiphany Aquarium Engine", width, height, input, iconPath, visible: !runtime.Options.Headless);
|
||||
window.PaintSplash("Aquarium", "Preparing runtime state");
|
||||
using var renderer = new D3D11Renderer(
|
||||
using var renderer = CreateRenderer(
|
||||
ParseRendererBackend(args),
|
||||
window.Handle,
|
||||
window.ClientWidth,
|
||||
window.ClientHeight,
|
||||
|
|
@ -144,7 +145,43 @@ public static class AquariumHost
|
|||
: null;
|
||||
}
|
||||
|
||||
private static void SyncRendererSettingsToRuntime(D3D11Renderer renderer, IAquariumRuntime runtime)
|
||||
private static string ParseRendererBackend(IReadOnlyCollection<string> args)
|
||||
{
|
||||
var values = args.ToArray();
|
||||
for (var index = 0; index < values.Length - 1; index++)
|
||||
{
|
||||
if (string.Equals(values[index], "--renderer", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return values[index + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return Environment.GetEnvironmentVariable("AQUARIUM_RENDERER") ?? "d3d11";
|
||||
}
|
||||
|
||||
private static IAquariumRenderer CreateRenderer(
|
||||
string rendererBackend,
|
||||
IntPtr windowHandle,
|
||||
int width,
|
||||
int height,
|
||||
string? shaderPath,
|
||||
GraphicsSettings graphicsSettings,
|
||||
Action<string>? startupProgress)
|
||||
{
|
||||
if (string.Equals(rendererBackend, "d3d12", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return new D3D12Renderer(windowHandle, width, height, shaderPath, graphicsSettings, startupProgress);
|
||||
}
|
||||
|
||||
if (!string.Equals(rendererBackend, "d3d11", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new ArgumentException($"Unknown renderer backend '{rendererBackend}'. Expected d3d11 or d3d12.");
|
||||
}
|
||||
|
||||
return new D3D11Renderer(windowHandle, width, height, shaderPath, graphicsSettings, startupProgress);
|
||||
}
|
||||
|
||||
private static void SyncRendererSettingsToRuntime(IAquariumRenderer renderer, IAquariumRuntime runtime)
|
||||
{
|
||||
var settings = renderer.CaptureGraphicsSettings();
|
||||
if (settings != runtime.GraphicsSettings)
|
||||
|
|
@ -153,7 +190,7 @@ public static class AquariumHost
|
|||
}
|
||||
}
|
||||
|
||||
private static void ApplyRendererDebugInput(D3D11Renderer renderer, InputState input)
|
||||
private static void ApplyRendererDebugInput(IAquariumRenderer renderer, InputState input)
|
||||
{
|
||||
if (input.IsKeyPressed(KeyCode.RenderDebugCycle))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ using System.Runtime.InteropServices;
|
|||
|
||||
namespace Aquarium.Engine.Render;
|
||||
|
||||
public sealed class D3D11Renderer : IDisposable
|
||||
public sealed class D3D11Renderer : IAquariumRenderer
|
||||
{
|
||||
private const string ShaderRelativePath = "Render/Shaders/Aquarium.hlsl";
|
||||
private const string DitherTextureRelativePath = "Assets/Textures/Aetheria-LDR_LLL1_0.r8";
|
||||
|
|
|
|||
185
src/Aquarium.Engine/Render/D3D12Renderer.cs
Normal file
185
src/Aquarium.Engine/Render/D3D12Renderer.cs
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
using Aquarium.Engine.Input;
|
||||
using SharpGen.Runtime;
|
||||
using Vortice.Direct3D;
|
||||
using Vortice.Direct3D12;
|
||||
using Vortice.DXGI;
|
||||
using Vortice.Mathematics;
|
||||
|
||||
namespace Aquarium.Engine.Render;
|
||||
|
||||
public sealed class D3D12Renderer : IAquariumRenderer
|
||||
{
|
||||
private const int BackBufferCount = 2;
|
||||
|
||||
private readonly IDXGIFactory4 factory;
|
||||
private readonly ID3D12Device device;
|
||||
private readonly ID3D12CommandQueue commandQueue;
|
||||
private readonly IDXGISwapChain3 swapChain;
|
||||
private readonly ID3D12DescriptorHeap renderTargetViewHeap;
|
||||
private readonly ID3D12Resource[] renderTargets = new ID3D12Resource[BackBufferCount];
|
||||
private readonly ID3D12CommandAllocator commandAllocator;
|
||||
private readonly ID3D12GraphicsCommandList commandList;
|
||||
private readonly ID3D12Fence fence;
|
||||
private readonly AutoResetEvent fenceEvent = new(false);
|
||||
private readonly int renderTargetViewDescriptorSize;
|
||||
private ulong fenceValue;
|
||||
private int frameIndex;
|
||||
private GraphicsSettings settings = GraphicsSettings.Default;
|
||||
private bool debugUiVisible = true;
|
||||
|
||||
public D3D12Renderer(
|
||||
IntPtr windowHandle,
|
||||
int width,
|
||||
int height,
|
||||
string? shaderPath = null,
|
||||
GraphicsSettings? graphicsSettings = null,
|
||||
Action<string>? startupProgress = null)
|
||||
{
|
||||
_ = shaderPath;
|
||||
ApplyGraphicsSettings(graphicsSettings ?? GraphicsSettings.Default);
|
||||
ReportStartupProgress(startupProgress, "Creating D3D12 device and swapchain");
|
||||
|
||||
factory = DXGI.CreateDXGIFactory2<IDXGIFactory4>(false);
|
||||
device = D3D12.D3D12CreateDevice<ID3D12Device>(IntPtr.Zero, FeatureLevel.Level_11_0);
|
||||
commandQueue = device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct));
|
||||
|
||||
var swapChainDescription = new SwapChainDescription1
|
||||
{
|
||||
BufferCount = BackBufferCount,
|
||||
Width = (uint)width,
|
||||
Height = (uint)height,
|
||||
Format = Format.B8G8R8A8_UNorm,
|
||||
BufferUsage = Usage.RenderTargetOutput,
|
||||
SwapEffect = SwapEffect.FlipDiscard,
|
||||
SampleDescription = new SampleDescription(1, 0),
|
||||
};
|
||||
|
||||
using var swapChain1 = factory.CreateSwapChainForHwnd(commandQueue, windowHandle, swapChainDescription);
|
||||
swapChain = swapChain1.QueryInterface<IDXGISwapChain3>();
|
||||
frameIndex = (int)swapChain.CurrentBackBufferIndex;
|
||||
|
||||
renderTargetViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription(DescriptorHeapType.RenderTargetView, BackBufferCount));
|
||||
renderTargetViewDescriptorSize = (int)device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
|
||||
CreateRenderTargetViews();
|
||||
|
||||
commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
|
||||
commandList = device.CreateCommandList<ID3D12GraphicsCommandList>(0, CommandListType.Direct, commandAllocator, null);
|
||||
commandList.Close();
|
||||
fence = device.CreateFence(0);
|
||||
Console.WriteLine("D3D12 device and swapchain created.");
|
||||
}
|
||||
|
||||
public int RenderDebugMode
|
||||
{
|
||||
get => settings.RenderDebugMode;
|
||||
set => settings = (settings with { RenderDebugMode = value }).Normalized();
|
||||
}
|
||||
|
||||
public bool DebugUiVisible
|
||||
{
|
||||
get => debugUiVisible;
|
||||
set => debugUiVisible = value;
|
||||
}
|
||||
|
||||
public void UpdateDebugUi(InputState input)
|
||||
{
|
||||
_ = input;
|
||||
}
|
||||
|
||||
public void CycleRenderDebugMode()
|
||||
{
|
||||
RenderDebugMode = (RenderDebugMode + 1) % (GraphicsSettings.MaxRenderDebugMode + 1);
|
||||
}
|
||||
|
||||
public GraphicsSettings CaptureGraphicsSettings()
|
||||
{
|
||||
return settings.Normalized();
|
||||
}
|
||||
|
||||
public void ApplyGraphicsSettings(GraphicsSettings graphicsSettings)
|
||||
{
|
||||
settings = graphicsSettings.Normalized();
|
||||
}
|
||||
|
||||
public void Render(AquariumFrame frame)
|
||||
{
|
||||
var clearColor = new Color4(
|
||||
0.006f + settings.SceneExposure * 0.02f,
|
||||
0.014f + settings.BloomIntensity * 0.2f,
|
||||
0.022f + settings.BloomVeilIntensity * 0.8f,
|
||||
1.0f);
|
||||
|
||||
commandAllocator.Reset();
|
||||
commandList.Reset(commandAllocator, null);
|
||||
|
||||
var renderTarget = renderTargets[frameIndex];
|
||||
commandList.ResourceBarrier(ResourceBarrier.BarrierTransition(renderTarget, ResourceStates.Present, ResourceStates.RenderTarget));
|
||||
var renderTargetView = GetRenderTargetViewHandle(frameIndex);
|
||||
commandList.ClearRenderTargetView(renderTargetView, clearColor);
|
||||
commandList.ResourceBarrier(ResourceBarrier.BarrierTransition(renderTarget, ResourceStates.RenderTarget, ResourceStates.Present));
|
||||
commandList.Close();
|
||||
|
||||
commandQueue.ExecuteCommandList(commandList);
|
||||
swapChain.Present(1, PresentFlags.None);
|
||||
MoveToNextFrame();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
WaitForGpu();
|
||||
fenceEvent.Dispose();
|
||||
fence.Dispose();
|
||||
commandList.Dispose();
|
||||
commandAllocator.Dispose();
|
||||
for (var index = 0; index < renderTargets.Length; index++)
|
||||
{
|
||||
renderTargets[index].Dispose();
|
||||
}
|
||||
renderTargetViewHeap.Dispose();
|
||||
swapChain.Dispose();
|
||||
commandQueue.Dispose();
|
||||
device.Dispose();
|
||||
factory.Dispose();
|
||||
}
|
||||
|
||||
private void CreateRenderTargetViews()
|
||||
{
|
||||
for (var index = 0; index < renderTargets.Length; index++)
|
||||
{
|
||||
renderTargets[index] = swapChain.GetBuffer<ID3D12Resource>((uint)index);
|
||||
device.CreateRenderTargetView(renderTargets[index], null, GetRenderTargetViewHandle(index));
|
||||
}
|
||||
}
|
||||
|
||||
private CpuDescriptorHandle GetRenderTargetViewHandle(int index)
|
||||
{
|
||||
return renderTargetViewHeap.GetCPUDescriptorHandleForHeapStart() + (index * renderTargetViewDescriptorSize);
|
||||
}
|
||||
|
||||
private void MoveToNextFrame()
|
||||
{
|
||||
var signalValue = ++fenceValue;
|
||||
commandQueue.Signal(fence, signalValue).CheckError();
|
||||
frameIndex = (int)swapChain.CurrentBackBufferIndex;
|
||||
|
||||
if (fence.CompletedValue < signalValue)
|
||||
{
|
||||
fence.SetEventOnCompletion(signalValue, fenceEvent.SafeWaitHandle.DangerousGetHandle()).CheckError();
|
||||
fenceEvent.WaitOne();
|
||||
}
|
||||
}
|
||||
|
||||
private void WaitForGpu()
|
||||
{
|
||||
var signalValue = ++fenceValue;
|
||||
commandQueue.Signal(fence, signalValue).CheckError();
|
||||
fence.SetEventOnCompletion(signalValue, fenceEvent.SafeWaitHandle.DangerousGetHandle()).CheckError();
|
||||
fenceEvent.WaitOne();
|
||||
}
|
||||
|
||||
private static void ReportStartupProgress(Action<string>? startupProgress, string message)
|
||||
{
|
||||
startupProgress?.Invoke(message);
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
}
|
||||
20
src/Aquarium.Engine/Render/IAquariumRenderer.cs
Normal file
20
src/Aquarium.Engine/Render/IAquariumRenderer.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using Aquarium.Engine.Input;
|
||||
|
||||
namespace Aquarium.Engine.Render;
|
||||
|
||||
public interface IAquariumRenderer : IDisposable
|
||||
{
|
||||
int RenderDebugMode { get; set; }
|
||||
|
||||
bool DebugUiVisible { get; set; }
|
||||
|
||||
void UpdateDebugUi(InputState input);
|
||||
|
||||
void CycleRenderDebugMode();
|
||||
|
||||
GraphicsSettings CaptureGraphicsSettings();
|
||||
|
||||
void ApplyGraphicsSettings(GraphicsSettings settings);
|
||||
|
||||
void Render(AquariumFrame frame);
|
||||
}
|
||||
|
|
@ -59,3 +59,5 @@
|
|||
{"area":"renderer/volumetrics","lesson":"Do not compose the final frame from the diagnostic 4x4 medium atlas. If atlas density shows frustum-slice blobs while direct ray density is continuous, final composition should use direct registered-medium ray integration until a real froxel volume replaces the diagnostic atlas. Keep atlas density as an artifact-revealing debug view only.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60"],"ts":"2026-05-09"}
|
||||
{"area":"renderer/volumetrics","lesson":"The medium atlas trial has been replaced by a first real packed froxel volume: independent froxel dimensions, 32 depth slices, 8x4 D3D11 atlas packing, per-froxel density/transmittance/in-scattering, and resolve-side front-to-back composition. Direct ray density/transmittance debug remains the correctness reference; froxel density is the volume inspection view.","files":["src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md","docs/sdf-field-renderer-plan.md","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60"],"ts":"2026-05-09"}
|
||||
{"area":"renderer/volumetrics","lesson":"The Grid is a transparent schematic overlay, not a participating-medium occluder. Keep Grid travel/field id for temporal stability, but do not clamp fog integration to Grid travel; only solid fields such as Self and planets terminate the froxel composite ray.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/scratch.md"],"verification":["Aquarium.Engine project build succeeded","Full solution/headless verification blocked by dirty external CultLib changes on E:\\Projects\\CultLib codex/cultcache-attribute-first: CultCache.cs currently fails GameCult.Caching build with System.Text.Json/IsExternalInit errors"],"ts":"2026-05-09"}
|
||||
{"area":"renderer/d3d12","lesson":"Before building the transparent stochastic surface pipe, keep D3D11 as the reference backend and add D3D12 beside it behind IAquariumRenderer. The first D3D12 pass should prove device, command queue, swapchain, RTV heap, command list, fence, clear, and present lifecycle with headless execution before porting scene passes.","files":["src/Aquarium.Engine/AquariumHost.cs","src/Aquarium.Engine/Render/IAquariumRenderer.cs","src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","docs/d3d12-migration.md","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj","Aquarium.Engine.exe --headless --renderer d3d12 with latest known-good live assembly exited 0","Aquarium.Engine.exe --headless --renderer d3d11 with latest known-good live assembly exited 0"],"ts":"2026-05-09"}
|
||||
{"area":"renderer/backend-selection","lesson":"Backend-neutral startup logging matters once D3D12 exists beside D3D11. Live runtime messages should describe Aquarium host invariants, not claim a specific graphics API that may be false for the selected renderer.","files":["src/Aquarium.Engine.Live/AquariumRuntime.cs"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60","Latest slot Aquarium.Engine.exe --headless --renderer d3d12 exited 0 and logged D3D12 device/swapchain creation"],"ts":"2026-05-09"}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
## Current Slice
|
||||
|
||||
Planning and first implementation for the SDF field renderer. The core rule:
|
||||
Aquarium/Aetheria clouds are local SDF media above, below, around, or enclosing
|
||||
the camera, never skybox-only decoration. `docs/sdf-field-renderer-plan.md`
|
||||
captures the architecture. `Aquarium.hlsl` now has a first analytic SDF cloud
|
||||
prototype integrated along the camera ray before surface composition.
|
||||
D3D12 migration foothold. D3D11 remains the visual reference renderer; the host
|
||||
now talks through `IAquariumRenderer`, and `--renderer d3d12` selects a new
|
||||
D3D12 shell that creates device/queue/swapchain/RTV heap/command list/fence and
|
||||
clears/presents headlessly. Next renderer work should port pass/resource
|
||||
ownership into D3D12 before building the stochastic transparent surface pipe.
|
||||
|
||||
## Hot Context
|
||||
|
||||
|
|
@ -206,3 +206,10 @@ prototype integrated along the camera ray before surface composition.
|
|||
stops at Self/planet solids only; Grid pixels integrate medium to the visible
|
||||
far distance so the schematic overlay cannot cut holes through fog or occlude
|
||||
itself.
|
||||
- D3D12 migration foothold: D3D11 is frozen as the reference backend, and the
|
||||
host now selects `d3d11` or `d3d12` through `--renderer` /
|
||||
`AQUARIUM_RENDERER`. The D3D12 backend currently proves lifecycle only:
|
||||
device, command queue, flip-discard swapchain, RTV heap, command allocator,
|
||||
graphics command list, fence wait, clear, and present. Do not build the
|
||||
transparent candidate pipe on D3D11 debt; port explicit D3D12 resource/pass
|
||||
ownership first.
|
||||
|
|
|
|||
Loading…
Reference in a new issue