Add D3D12 renderer foothold
This commit is contained in:
parent
62862c0335
commit
e3b751bcff
9 changed files with 295 additions and 10 deletions
|
|
@ -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);
|
||||
}
|
||||
Loading…
Reference in a new issue