forked from Misaki/GhostEngine
Refactor graphics engine dependencies and structure
Removed references to `Misaki.HighPerformance.Unsafe` and replaced them with `Misaki.HighPerformance.LowLevel` in multiple files. Removed calls to `AllocationManager.Initialize()` and `AllocationManager.Dispose()` in `EngineCore.cs`. Added new methods to the `ICommandBuffer` interface for enhanced rendering capabilities. Updated the `D3D12CommandBuffer` class to implement new graphics command handling methods. Added the `Material` class to manage shader properties and caching for improved performance. Encapsulated shader compilation and reflection processes within the `Shader` class for better organization. Added the `CBufferCache` struct to optimize GPU resource management for constant buffer data. Updated the `MeshRenderPass` class to utilize the new `Material` class for dynamic mesh rendering. Updated various project files to reflect the restructuring of dependencies. Modified XAML files and code-behind for improved readability and maintainability.
This commit is contained in:
@@ -1,21 +1,103 @@
|
||||
using Win32;
|
||||
using Win32.Graphics.Direct3D12;
|
||||
using Ghost.Graphics.Contracts;
|
||||
using Ghost.Graphics.Shading;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ghost.Graphics.Data;
|
||||
|
||||
public class Material : IDisposable
|
||||
{
|
||||
// TODO: Pipeline state should be abstracted that can support multiple graphics APIs.
|
||||
private ComPtr<ID3D12PipelineState> _pipelineState;
|
||||
private readonly Dictionary<string, CBufferCache> _cbufferCaches;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
public Shader Shader
|
||||
{
|
||||
get;
|
||||
set;
|
||||
} = Shader.Empty;
|
||||
}
|
||||
|
||||
public Material(Shader shader)
|
||||
{
|
||||
Shader = shader;
|
||||
|
||||
_cbufferCaches = new();
|
||||
foreach (var cbufferInfo in shader.ConstantBuffers.Values)
|
||||
{
|
||||
_cbufferCaches.Add(cbufferInfo.Name, new CBufferCache(cbufferInfo.Size));
|
||||
}
|
||||
}
|
||||
|
||||
~Material()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetFloat(string propertyName, in float value)
|
||||
{
|
||||
WriteToCache(propertyName, in value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetVector(string propertyName, in Vector4 value)
|
||||
{
|
||||
WriteToCache(propertyName, in value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetMatrix(string propertyName, in Matrix4x4 value)
|
||||
{
|
||||
WriteToCache(propertyName, in value);
|
||||
}
|
||||
|
||||
private unsafe void WriteToCache<T>(string propertyName, in T value) where T : unmanaged
|
||||
{
|
||||
if (!Shader.Properties.TryGetValue(propertyName, out var propInfo))
|
||||
{
|
||||
throw new ArgumentException($"Property '{propertyName}' does not exist in the shader.", nameof(propertyName));
|
||||
}
|
||||
|
||||
if (propInfo.Size != sizeof(T))
|
||||
{
|
||||
throw new ArgumentException($"Property '{propertyName}' has a size mismatch. Expected {sizeof(T)} bytes, but got {propInfo.Size} bytes.", nameof(propertyName));
|
||||
}
|
||||
|
||||
var cache = _cbufferCaches[propInfo.CBufferName];
|
||||
|
||||
Unsafe.WriteUnaligned(ref cache.CpuData[(int)propInfo.ByteOffset], value);
|
||||
}
|
||||
|
||||
public void UploadAndBind(ICommandBuffer cmb)
|
||||
{
|
||||
foreach (var cache in _cbufferCaches.Values)
|
||||
{
|
||||
cache.UploadToGpu();
|
||||
}
|
||||
|
||||
foreach (var (name, cache) in _cbufferCaches)
|
||||
{
|
||||
var cbufferInfo = Shader.ConstantBuffers[name];
|
||||
cmb.SetGraphicsRootConstantBufferView(cbufferInfo.RegisterSlot, cache.GpuResource.GPUAddress);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_pipelineState.Dispose();
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var cache in _cbufferCaches.Values)
|
||||
{
|
||||
cache.Dispose();
|
||||
}
|
||||
|
||||
_cbufferCaches.Clear();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user