forked from Misaki/GhostEngine
Added `InternalsVisibleTo` attribute for "Ghost.Graphics" and "Ghost.Editor" in `AssemblyInfo.cs`. Added a new `EngineAssemblyAttribute` in `EngineAssemblyAttribute.cs`. Added a reference to `Misaki.HighPerformance.Unsafe` in `Ghost.Core.csproj`. Added a new `Bounds` struct to represent axis-aligned bounding boxes in `Bounds.cs`. Added new `Color32` and `Color128` structs for color representation in `Color.cs`. Changed the namespace from `Ghost.Editor.Controls` to `Ghost.Editor.Core.Controls` in multiple files. Changed the implicit conversion operator in `ConstPtr<T>` to use a more descriptive parameter name in `ConstPtr.cs`. Changed the `Mesh` class to use `Color128` instead of `Color32` for color representation. Enhanced the `TypeCache` class to load types from assemblies marked with `EngineAssemblyAttribute`. Enhanced the `ProjectService` class to improve the `GetAllProjectAsync` method by filtering out bad projects. Enhanced the `GraphicsPipeline` class to support both DX12 and D3D12 graphics APIs. Enhanced the `Shader` class to include methods for compiling HLSL shaders and managing root signatures. Enhanced the `MeshRenderPass` class to utilize the new shader compilation methods. Refactored the `AppStateMachine` class to use private fields instead of static fields for state management. Refactored the `ComponentDataView` class to use the new namespace and improve organization. Refactored project references in `Ghost.Graphics.csproj` to include new dependencies and remove outdated ones. Made various adjustments to ensure consistency and improve code quality across multiple files.
201 lines
5.1 KiB
C#
201 lines
5.1 KiB
C#
using Ghost.Core;
|
|
using Ghost.Graphics.Contracts;
|
|
using Ghost.Graphics.D3D12;
|
|
using Ghost.Graphics.Data;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Ghost.Graphics;
|
|
|
|
public static class GraphicsPipeline
|
|
{
|
|
internal const int _FRAME_COUNT = 2;
|
|
|
|
private static IGraphicsDevice? _graphicsDevice;
|
|
private static IResourceAllocator? _resourceAllocator;
|
|
|
|
private static Thread? _renderThread;
|
|
private static AutoResetEvent[]? _cpuReadyEvent;
|
|
private static AutoResetEvent[]? _gpuReadyEvent;
|
|
|
|
private static uint _cpuFenceValue;
|
|
private static uint _gpuFenceValue;
|
|
|
|
private static bool _initialized;
|
|
private static bool _isRunning;
|
|
|
|
internal static uint CPUFenceValue => _cpuFenceValue;
|
|
internal static uint GPUFenceValue => _gpuFenceValue;
|
|
internal static bool IsRunning => _isRunning;
|
|
|
|
internal static IGraphicsDevice GraphicsDevice
|
|
{
|
|
get
|
|
{
|
|
if (_graphicsDevice == null)
|
|
{
|
|
throw new InvalidOperationException("Graphics pipeline is not initialized.");
|
|
}
|
|
|
|
return _graphicsDevice;
|
|
}
|
|
}
|
|
|
|
internal static IResourceAllocator ResourceAllocator
|
|
{
|
|
get
|
|
{
|
|
if (_resourceAllocator == null)
|
|
{
|
|
throw new InvalidOperationException("Resource allocator is not initialized.");
|
|
}
|
|
|
|
return _resourceAllocator;
|
|
}
|
|
}
|
|
|
|
public static GraphicsAPI CurrentAPI
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
internal static void Initialize(GraphicsAPI api)
|
|
{
|
|
switch (api)
|
|
{
|
|
case GraphicsAPI.D3D12:
|
|
_graphicsDevice = new D3D12GraphicsDevice();
|
|
_resourceAllocator = new D3D12ResourceAllocator();
|
|
break;
|
|
default:
|
|
throw new NotSupportedException($"Graphics API {api} is not supported.");
|
|
}
|
|
|
|
_renderThread = new Thread(RenderLoop)
|
|
{
|
|
IsBackground = true,
|
|
Name = "Graphics Render Thread",
|
|
Priority = ThreadPriority.Normal
|
|
};
|
|
|
|
_cpuReadyEvent = new AutoResetEvent[_FRAME_COUNT];
|
|
_gpuReadyEvent = new AutoResetEvent[_FRAME_COUNT];
|
|
for (var i = 0; i < _FRAME_COUNT; i++)
|
|
{
|
|
_cpuReadyEvent[i] = new(false);
|
|
_gpuReadyEvent[i] = new(true);
|
|
}
|
|
|
|
CurrentAPI = api;
|
|
|
|
_initialized = true;
|
|
}
|
|
|
|
private static void RenderLoop()
|
|
{
|
|
while (_isRunning)
|
|
{
|
|
if (_graphicsDevice == null)
|
|
{
|
|
throw new ArgumentException("Renderer has been disposed or is not initialized.");
|
|
}
|
|
|
|
var eventIndex = (int)(_gpuFenceValue % _FRAME_COUNT);
|
|
_cpuReadyEvent![eventIndex].WaitOne();
|
|
|
|
_graphicsDevice.InitializePendingRenderers();
|
|
|
|
foreach (var renderer in _graphicsDevice.Renderers)
|
|
{
|
|
renderer.ExecutePendingResize();
|
|
renderer.Render();
|
|
}
|
|
|
|
_gpuFenceValue++;
|
|
_gpuReadyEvent![eventIndex].Set();
|
|
|
|
_resourceAllocator!.ReleaseTempResource();
|
|
}
|
|
}
|
|
|
|
internal static bool IsGpuReady()
|
|
{
|
|
return _gpuFenceValue >= _cpuFenceValue;
|
|
}
|
|
|
|
|
|
internal static void WaitForGPUReady()
|
|
{
|
|
if (_gpuReadyEvent == null)
|
|
{
|
|
throw new InvalidOperationException("Graphics pipeline is not initialized.");
|
|
}
|
|
|
|
var eventIndex = (int)(_cpuFenceValue % _FRAME_COUNT);
|
|
_gpuReadyEvent[eventIndex].WaitOne();
|
|
}
|
|
|
|
internal static void SignalCPUReady()
|
|
{
|
|
if (_cpuReadyEvent == null)
|
|
{
|
|
throw new InvalidOperationException("Graphics pipeline is not initialized.");
|
|
}
|
|
|
|
var eventIndex = (int)(_cpuFenceValue % _FRAME_COUNT);
|
|
_cpuFenceValue++;
|
|
_cpuReadyEvent[eventIndex].Set();
|
|
}
|
|
|
|
internal static void Start()
|
|
{
|
|
if (_isRunning || !_initialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_isRunning = true;
|
|
_renderThread!.Start();
|
|
}
|
|
|
|
internal static void Stop()
|
|
{
|
|
_isRunning = false;
|
|
_renderThread?.Join();
|
|
}
|
|
|
|
internal static void Shutdown()
|
|
{
|
|
Stop();
|
|
|
|
_graphicsDevice?.Dispose();
|
|
_resourceAllocator?.Dispose();
|
|
|
|
_graphicsDevice = null;
|
|
_renderThread = null;
|
|
_initialized = false;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
internal static T GetGraphicsDevice<T>()
|
|
where T : class, IGraphicsDevice
|
|
{
|
|
if (T.TargetAPI != CurrentAPI)
|
|
{
|
|
throw new InvalidOperationException($"No graphics device of type {typeof(T)} available for the current API.");
|
|
}
|
|
|
|
return Unsafe.As<T>(GraphicsDevice);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static Result CheckAPI(GraphicsAPI expectedAPI)
|
|
{
|
|
if (CurrentAPI != expectedAPI)
|
|
{
|
|
return Result.Failure($"Expected API {expectedAPI}, but got {CurrentAPI}.");
|
|
}
|
|
|
|
return Result.Success();
|
|
}
|
|
} |