Refactor descriptor handling and shader compilation

Refactored descriptor allocation and release logic by introducing `IDescriptorAllocator` and replacing `DescriptorHeapAllocator` with `D3D12DescriptorHeap`. Updated descriptor structs to include validation properties and improved memory management with `ReadOnlySpan`.

Enhanced shader compilation by introducing `ShaderStage` and `CompilerVersion` enums, enabling more flexible and maintainable shader handling.

Refactored `Mesh` to use `IBuffer` for vertex and index buffers, added bindless descriptor support, and improved resource cleanup.

Updated `RenderSystem` and other components for better initialization, error handling, and disposal logic. General improvements to code readability and maintainability.
This commit is contained in:
2025-09-13 20:07:29 +09:00
parent 1dfed83e38
commit 74bb2ccda5
23 changed files with 561 additions and 403 deletions

View File

@@ -1,7 +1,7 @@
namespace Ghost.Graphics.RHI;
/// <summary>
/// D3D12-style command queue interface
/// Command queue interface
/// </summary>
public interface ICommandQueue : IDisposable
{
@@ -46,7 +46,7 @@ public interface ICommandQueue : IDisposable
}
/// <summary>
/// Command queue types matching D3D12
/// Command queue types
/// </summary>
public enum CommandQueueType
{

View File

@@ -0,0 +1,64 @@
using Ghost.Graphics.Data;
using System.Runtime.CompilerServices;
using Win32.Graphics.Direct3D12;
namespace Ghost.Graphics.RHI;
public interface IDescriptorAllocator
{
public RenderTargetDescriptor AllocateRTV();
public RenderTargetDescriptor[] AllocateRTVs(uint count);
public DepthStencilDescriptor AllocateDSV();
public DepthStencilDescriptor[] AllocateDSVs(uint count);
public ShaderResourceDescriptor AllocateSRV();
public ShaderResourceDescriptor[] AllocateSRVs(uint count);
public SamplerDescriptor AllocateSampler();
public SamplerDescriptor[] AllocateSamplers(uint count);
public BindlessDescriptor AllocateBindless();
public BindlessDescriptor[] AllocateBindless(uint count);
public CpuDescriptorHandle GetCpuHandle(RenderTargetDescriptor descriptor);
public CpuDescriptorHandle GetCpuHandle(DepthStencilDescriptor descriptor);
public CpuDescriptorHandle GetCpuHandle(ShaderResourceDescriptor descriptor);
public GpuDescriptorHandle GetGpuHandle(ShaderResourceDescriptor descriptor);
public CpuDescriptorHandle GetCpuHandle(SamplerDescriptor descriptor);
public GpuDescriptorHandle GetGpuHandle(SamplerDescriptor descriptor);
public CpuDescriptorHandle GetCpuHandle(BindlessDescriptor descriptor);
public GpuDescriptorHandle GetGpuHandle(BindlessDescriptor descriptor);
public void Release(RenderTargetDescriptor descriptor);
public void Release(ReadOnlySpan<RenderTargetDescriptor> descriptors);
public void Release(DepthStencilDescriptor descriptor);
public void Release(ReadOnlySpan<DepthStencilDescriptor> descriptors);
public void Release(ShaderResourceDescriptor descriptor);
public void Release(ReadOnlySpan<ShaderResourceDescriptor> descriptors);
public void Release(SamplerDescriptor descriptor);
public void Release(ReadOnlySpan<SamplerDescriptor> descriptors);
public void Release(BindlessDescriptor descriptor);
public void Release(ReadOnlySpan<BindlessDescriptor> descriptors);
}

View File

@@ -165,7 +165,16 @@ public struct RenderTargetDesc
usage |= TextureUsage.UnorderedAccess;
}
return new TextureDesc(desc.Width, desc.Height, desc.Slice, desc.Format, desc.Dimension, desc.MipLevels, usage);
return new TextureDesc
{
Width = desc.Width,
Height = desc.Height,
Slice = desc.Slice,
Format = desc.Format,
Dimension = desc.Dimension,
MipLevels = desc.MipLevels,
Usage = usage
};
}
}
@@ -236,19 +245,6 @@ public struct TextureDesc
get;
set;
}
public TextureDesc(uint width, uint height, uint slice = 1,
TextureFormat format = TextureFormat.R8G8B8A8_UNorm, TextureDimension dimension = TextureDimension.Texture2D,
uint mipLevels = 0u, TextureUsage usage = TextureUsage.ShaderResource)
{
Width = width;
Height = height;
Slice = slice;
Format = format;
Dimension = dimension;
MipLevels = mipLevels;
Usage = usage;
}
}
/// <summary>
@@ -265,6 +261,12 @@ public struct BufferDesc
set;
}
public uint Stride
{
get;
set;
}
/// <summary>
/// Buffer usage flags
/// </summary>
@@ -274,6 +276,12 @@ public struct BufferDesc
set;
}
public BufferCreationFlags CreationFlags
{
get;
set;
}
/// <summary>
/// Memory type for the buffer
/// </summary>
@@ -282,13 +290,6 @@ public struct BufferDesc
get;
set;
}
public BufferDesc(ulong size, BufferUsage usage, MemoryType memoryType = MemoryType.Default)
{
Size = size;
Usage = usage;
MemoryType = memoryType;
}
}
/// <summary>

View File

@@ -1,3 +1,5 @@
using Ghost.Graphics.Data;
namespace Ghost.Graphics.RHI;
/// <summary>
@@ -8,17 +10,26 @@ public interface IResource : IDisposable
/// <summary>
/// Current resource state
/// </summary>
ResourceState CurrentState { get; }
ResourceState CurrentState
{
get;
}
/// <summary>
/// Resource name for debugging
/// </summary>
string Name { get; set; }
string Name
{
get; set;
}
/// <summary>
/// Size of the resource in bytes
/// </summary>
ulong Size { get; }
ulong Size
{
get;
}
}
/// <summary>
@@ -29,22 +40,34 @@ public interface ITexture : IResource
/// <summary>
/// Width of the texture in pixels
/// </summary>
uint Width { get; }
uint Width
{
get;
}
/// <summary>
/// Height of the texture in pixels
/// </summary>
uint Height { get; }
uint Height
{
get;
}
/// <summary>
/// Texture format
/// </summary>
TextureFormat Format { get; }
TextureFormat Format
{
get;
}
/// <summary>
/// Number of mip levels
/// </summary>
uint MipLevels { get; }
uint MipLevels
{
get;
}
}
/// <summary>
@@ -55,18 +78,26 @@ public interface IBuffer : IResource
/// <summary>
/// Buffer usage type
/// </summary>
BufferUsage Usage { get; }
public BufferUsage Usage
{
get;
}
public BufferHandle Handle
{
get;
}
/// <summary>
/// Maps the buffer for CPU access
/// </summary>
/// <returns>Pointer to mapped memory</returns>
unsafe void* Map();
public unsafe void* Map();
/// <summary>
/// Unmaps the buffer from CPU access
/// </summary>
void Unmap();
public void Unmap();
}
/// <summary>
@@ -78,7 +109,10 @@ public interface IRenderTarget : ITexture
/// <summary>
/// Type of render target (color or depth)
/// </summary>
RenderTargetType Type { get; }
RenderTargetType Type
{
get;
}
}
/// <summary>
@@ -117,5 +151,15 @@ public enum BufferUsage
Structured = 1 << 3,
Raw = 1 << 4,
Upload = 1 << 5,
Readback = 1 << 6
Readback = 1 << 6,
IndirectArgument = 1 << 7,
ShaderResource = Vertex | Index | Constant
}
[Flags]
public enum BufferCreationFlags
{
None = 0,
Bindless = 1 << 0
}