forked from Misaki/GhostEngine
Add new interfaces and refactor rendering logic
Added a new `ConstPtr<T>` struct for type-safe pointers. Added a new `ICommandBuffer` interface for resource copying. Added a new `IRenderPass` interface to define render passes. Added a new `IResource` interface for GPU resources. Added a new `IResourceAllocator` interface for resource management. Added a new `ISwapChainPanelNative` struct for native interactions. Added a new `D3D12Utility` class for Direct3D 12 utilities. Added a new package reference for `Vortice.Win32.Graphics.D3D12MemoryAllocator`. Changed project file to allow unsafe code blocks. Changed `Result` struct methods to improve clarity. Changed error handling in `ProjectService` and `AssetDatabase` to use `Result.Failure()`. Changed `launchSettings.json` to enable native debugging. Changed rendering logic in `ScenePage.xaml.cs` to use `IRenderer`. Changed `IGraphicsDevice` interface to include renderer properties. Changed `IRenderView` to `IRenderer` and updated its methods. Changed `Mesh` class to use the new `IResource` interface for buffers. Changed `GraphicsAPI` enum to include a `None` value. Changed various aspects of the `GraphicsPipeline` class for new architecture. Removed the old `DX12RenderView` class and replaced it with `DX12Renderer`. Removed unnecessary code in the `ResourceView` class.
This commit is contained in:
@@ -2,4 +2,5 @@
|
||||
|
||||
public interface ICommandBuffer
|
||||
{
|
||||
public void CopyResource(IResource dstResource, uint dstOffset, IResource srcResource, uint srcOffset, uint size);
|
||||
}
|
||||
@@ -2,10 +2,17 @@
|
||||
|
||||
namespace Ghost.Graphics.Contracts;
|
||||
|
||||
public interface IGraphicsDevice : IDisposable
|
||||
internal interface IGraphicsDevice : IDisposable
|
||||
{
|
||||
public static abstract IGraphicsDevice Create();
|
||||
public static abstract GraphicsAPI TargetAPI
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public IRenderView CreateRenderView(in SwapChainPresenter swapChainSurface);
|
||||
public void OnRender();
|
||||
public ReadOnlySpan<IRenderer> Renderers
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public IRenderer CreateRenderer(in SwapChainPresenter swapChainSurface);
|
||||
}
|
||||
7
Ghost.Graphics/Contracts/IRenderPass.cs
Normal file
7
Ghost.Graphics/Contracts/IRenderPass.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Ghost.Graphics.Contracts;
|
||||
|
||||
internal interface IRenderPass : IDisposable
|
||||
{
|
||||
void Initialize(ICommandBuffer cmb);
|
||||
void Execute(ICommandBuffer cmb);
|
||||
}
|
||||
@@ -3,8 +3,13 @@
|
||||
/// <summary>
|
||||
/// Defines the contract for a render view in the graphics pipeline.
|
||||
/// </summary>
|
||||
internal interface IRenderView : IDisposable
|
||||
internal interface IRenderer : IDisposable
|
||||
{
|
||||
public ReadOnlySpan<IRenderPass> RenderPasses
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requests a resize of the render view.
|
||||
/// </summary>
|
||||
@@ -17,26 +22,17 @@ internal interface IRenderView : IDisposable
|
||||
/// </summary>
|
||||
public void ExecutePendingResize();
|
||||
|
||||
/// <summary>
|
||||
/// Begins a render operation.
|
||||
/// </summary>
|
||||
/// <returns>An ICommandBuffer instance to manage render commands.</returns>
|
||||
public ICommandBuffer BeginRender();
|
||||
/// <summary>
|
||||
/// Renders the current content to the output target.
|
||||
/// </summary>
|
||||
public void Render();
|
||||
/// <summary>
|
||||
/// Ends the current rendering operation and finalizes any pending rendering tasks.
|
||||
/// </summary>
|
||||
public void EndRender();
|
||||
|
||||
/// <summary>
|
||||
/// Waits for the next frame to be ready for rendering.
|
||||
/// </summary>
|
||||
public void WaitNextFrame();
|
||||
/// <summary>
|
||||
/// Waits for the rendering operations to complete and the GPU to be idle.
|
||||
/// Waits for the render view to become idle, ensuring all previous commands have been executed and resources are ready for the next frame.
|
||||
/// </summary>
|
||||
public void WaitIdle();
|
||||
}
|
||||
18
Ghost.Graphics/Contracts/IResource.cs
Normal file
18
Ghost.Graphics/Contracts/IResource.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace Ghost.Graphics.Contracts;
|
||||
|
||||
public interface IResource : IDisposable
|
||||
{
|
||||
public ulong GPUAddress
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public void SetData<T>(Span<T> data)
|
||||
where T : unmanaged;
|
||||
}
|
||||
11
Ghost.Graphics/Contracts/IResourceAllocator.cs
Normal file
11
Ghost.Graphics/Contracts/IResourceAllocator.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Vortice.Direct3D12;
|
||||
|
||||
namespace Ghost.Graphics.Contracts;
|
||||
|
||||
internal unsafe interface IResourceAllocator : IDisposable
|
||||
{
|
||||
public abstract static IResourceAllocator Create();
|
||||
|
||||
public IResource CreateUploadBuffer(uint sizeInBytes, ResourceFlags flags = ResourceFlags.None);
|
||||
public IResource CreateCopyDestinationBuffer(uint sizeInBytes, ResourceFlags flags = ResourceFlags.None);
|
||||
}
|
||||
61
Ghost.Graphics/Contracts/ISwapChainPanelNative.cs
Normal file
61
Ghost.Graphics/Contracts/ISwapChainPanelNative.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ghost.Graphics.Contracts;
|
||||
|
||||
[ComImport]
|
||||
[Guid("63aad0b8-7c24-40ff-85a8-640d944cc325")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface ISwapChainPanelNativeRaw
|
||||
{
|
||||
// IUnknown: QueryInterface, AddRef, Release
|
||||
void QueryInterface(in Guid riid, out IntPtr ppvObject);
|
||||
uint AddRef();
|
||||
uint Release();
|
||||
|
||||
// SetSwapChain is the 4th slot in the vtable (0-based index 3)
|
||||
int SetSwapChain(IntPtr swapChainPtr);
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
public unsafe readonly struct ISwapChainPanelNative
|
||||
{
|
||||
private readonly IntPtr _nativePtr;
|
||||
public readonly IntPtr NativePointer => _nativePtr;
|
||||
|
||||
public ISwapChainPanelNative(IntPtr nativePtr)
|
||||
{
|
||||
_nativePtr = nativePtr;
|
||||
}
|
||||
|
||||
public static ISwapChainPanelNative FromSwapChainPanel(object panel)
|
||||
{
|
||||
// Get the IUnknown/IInspectable pointer
|
||||
var unknown = Marshal.GetIUnknownForObject(panel);
|
||||
try
|
||||
{
|
||||
// Query for ISwapChainPanelNative
|
||||
var iid = typeof(ISwapChainPanelNativeRaw).GUID;
|
||||
var result = Marshal.QueryInterface(unknown, in iid, out var nativePtr);
|
||||
if (result < 0)
|
||||
{
|
||||
Marshal.ThrowExceptionForHR(result);
|
||||
}
|
||||
|
||||
return new ISwapChainPanelNative(nativePtr);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.Release(unknown);
|
||||
}
|
||||
}
|
||||
|
||||
public int SetSwapChain(IntPtr swapChainPtr)
|
||||
{
|
||||
var raw = (ISwapChainPanelNativeRaw)Marshal.GetObjectForIUnknown(_nativePtr);
|
||||
var hr = raw.SetSwapChain(swapChainPtr);
|
||||
Marshal.ReleaseComObject(raw);
|
||||
return hr;
|
||||
}
|
||||
|
||||
public void Dispose() => Marshal.Release(_nativePtr);
|
||||
}
|
||||
Reference in New Issue
Block a user