forked from Misaki/GhostEngine
Refactor and enhance codebase for maintainability
Refactored and reorganized the codebase to improve readability, performance, and maintainability. Introduced new interfaces and structs for better resource management, updated project configuration files, and refactored shader and graphics pipeline management. Improved error handling, code formatting, and removed unused code and namespaces. Updated DLL references and method signatures for consistency and maintainability.
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.DirectX;
|
||||
|
||||
namespace Ghost.Graphics.RHI;
|
||||
|
||||
/// <summary>
|
||||
@@ -16,391 +12,4 @@ public interface IRootSignature : IDisposable
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pipeline types
|
||||
/// </summary>
|
||||
public enum PipelineType
|
||||
{
|
||||
Graphics,
|
||||
Compute
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct ResourceDesc
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public TextureDesc textureDescription;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public BufferDesc bufferDescription;
|
||||
|
||||
public static ResourceDesc Buffer(BufferDesc desc)
|
||||
{
|
||||
return new ResourceDesc
|
||||
{
|
||||
bufferDescription = desc
|
||||
};
|
||||
}
|
||||
|
||||
public static ResourceDesc Texture(TextureDesc desc)
|
||||
{
|
||||
return new ResourceDesc
|
||||
{
|
||||
textureDescription = desc
|
||||
};
|
||||
}
|
||||
|
||||
internal static ResourceDesc FromD3D12(D3D12_RESOURCE_DESC desc)
|
||||
{
|
||||
if (desc.Dimension == D3D12_RESOURCE_DIMENSION.D3D12_RESOURCE_DIMENSION_BUFFER)
|
||||
{
|
||||
return Buffer(new BufferDesc
|
||||
{
|
||||
Size = (uint)desc.Width,
|
||||
Stride = 0,
|
||||
Usage = BufferUsage.None,
|
||||
MemoryType = MemoryType.Default
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
return Texture(new TextureDesc
|
||||
{
|
||||
Width = (uint)desc.Width,
|
||||
Height = desc.Height,
|
||||
Slice = desc.DepthOrArraySize,
|
||||
Format = desc.Format.ToTextureFormat(),
|
||||
Dimension = desc.Dimension.ToTextureDimension(),
|
||||
MipLevels = desc.MipLevels,
|
||||
Usage = TextureUsage.None,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Render target description
|
||||
/// Supports either color OR depth rendering, not both
|
||||
/// </summary>
|
||||
public struct RenderTargetDesc
|
||||
{
|
||||
/// <summary>
|
||||
/// Width of the render target
|
||||
/// </summary>
|
||||
public uint Width
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Height of the render target
|
||||
/// </summary>
|
||||
public uint Height
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Slice of the render target
|
||||
/// </summary>
|
||||
public uint Slice
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Type of render target
|
||||
/// </summary>
|
||||
public RenderTargetType Type
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Target texture format
|
||||
/// </summary>
|
||||
public TextureFormat Format
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Texture dimension
|
||||
/// </summary>
|
||||
public TextureDimension Dimension
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creation flags for the render target
|
||||
/// </summary>
|
||||
public RenderTargetCreationFlags CreationFlags
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Number of mip levels. 0 to generate full mip chain
|
||||
/// </summary>
|
||||
public uint MipLevels
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Number of samples for MSAA
|
||||
/// </summary>
|
||||
public uint SampleCount
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a color render target
|
||||
/// </summary>
|
||||
public static RenderTargetDesc Color(uint width, uint height, uint slice = 1,
|
||||
TextureFormat format = TextureFormat.R8G8B8A8_UNorm, TextureDimension dimension = TextureDimension.Texture2D,
|
||||
RenderTargetCreationFlags creationFlags = RenderTargetCreationFlags.AllowUAV | RenderTargetCreationFlags.DynamicallyResolution | RenderTargetCreationFlags.GenerateMips,
|
||||
uint mipLevels = 0u, uint sampleCount = 1)
|
||||
{
|
||||
return new RenderTargetDesc
|
||||
{
|
||||
Width = width,
|
||||
Height = height,
|
||||
Slice = slice,
|
||||
Type = RenderTargetType.Color,
|
||||
Format = format,
|
||||
Dimension = dimension,
|
||||
CreationFlags = creationFlags,
|
||||
MipLevels = mipLevels,
|
||||
SampleCount = sampleCount
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a depth render target
|
||||
/// </summary>
|
||||
public static RenderTargetDesc Depth(uint width, uint height, uint slice = 1,
|
||||
TextureFormat format = TextureFormat.D24_UNorm_S8_UInt, TextureDimension dimension = TextureDimension.Texture2D,
|
||||
RenderTargetCreationFlags creationFlags = RenderTargetCreationFlags.AllowUAV | RenderTargetCreationFlags.DynamicallyResolution,
|
||||
uint mipLevels = 0u, uint sampleCount = 1)
|
||||
{
|
||||
return new RenderTargetDesc
|
||||
{
|
||||
Width = width,
|
||||
Height = height,
|
||||
Slice = slice,
|
||||
Type = RenderTargetType.Depth,
|
||||
Format = format,
|
||||
Dimension = dimension,
|
||||
CreationFlags = creationFlags,
|
||||
MipLevels = mipLevels,
|
||||
SampleCount = sampleCount
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static TextureDesc ToTextureDescripton(RenderTargetDesc desc)
|
||||
{
|
||||
var usage = desc.Type == RenderTargetType.Color ? TextureUsage.RenderTarget : TextureUsage.DepthStencil;
|
||||
if (desc.CreationFlags.HasFlag(RenderTargetCreationFlags.AllowUAV))
|
||||
{
|
||||
usage |= TextureUsage.UnorderedAccess;
|
||||
}
|
||||
|
||||
return new TextureDesc
|
||||
{
|
||||
Width = desc.Width,
|
||||
Height = desc.Height,
|
||||
Slice = desc.Slice,
|
||||
Format = desc.Format,
|
||||
Dimension = desc.Dimension,
|
||||
MipLevels = desc.MipLevels,
|
||||
Usage = usage,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Texture description
|
||||
/// </summary>
|
||||
public struct TextureDesc
|
||||
{
|
||||
/// <summary>
|
||||
/// Width of the texture
|
||||
/// </summary>
|
||||
public uint Width
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Height of the texture
|
||||
/// </summary>
|
||||
public uint Height
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Slice of the texture
|
||||
/// </summary>
|
||||
public uint Slice
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Texture format
|
||||
/// </summary>
|
||||
public TextureFormat Format
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Texture dimension
|
||||
/// </summary>
|
||||
public TextureDimension Dimension
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Number of mip levels. 0 to generate full mip chain
|
||||
/// </summary>
|
||||
public uint MipLevels
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Texture usage flags
|
||||
/// </summary>
|
||||
public TextureUsage Usage
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Buffer description
|
||||
/// </summary>
|
||||
public struct BufferDesc
|
||||
{
|
||||
/// <summary>
|
||||
/// Size of the buffer in bytes
|
||||
/// </summary>
|
||||
public uint Size
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public uint Stride
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Buffer usage flags
|
||||
/// </summary>
|
||||
public BufferUsage Usage
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Memory type for the buffer
|
||||
/// </summary>
|
||||
public MemoryType MemoryType
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Texture usage flags
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum TextureUsage
|
||||
{
|
||||
None = 0,
|
||||
ShaderResource = 1 << 0,
|
||||
RenderTarget = 1 << 1,
|
||||
DepthStencil = 1 << 2,
|
||||
UnorderedAccess = 1 << 3
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dimensions of the texture
|
||||
/// </summary>
|
||||
public enum TextureDimension
|
||||
{
|
||||
Unknown = -1,
|
||||
None = 0,
|
||||
Texture2D = 1,
|
||||
Texture3D = 2,
|
||||
TextureCube = 3,
|
||||
Texture2DArray = 4,
|
||||
TextureCubeArray = 5
|
||||
}
|
||||
|
||||
public static class TextureDimensionExtension
|
||||
{
|
||||
public static TextureDimension ToTextureDimension(this D3D12_RESOURCE_DIMENSION dimension)
|
||||
{
|
||||
return dimension switch
|
||||
{
|
||||
D3D12_RESOURCE_DIMENSION.D3D12_RESOURCE_DIMENSION_TEXTURE1D => TextureDimension.Texture2D,
|
||||
D3D12_RESOURCE_DIMENSION.D3D12_RESOURCE_DIMENSION_TEXTURE2D => TextureDimension.Texture2D,
|
||||
D3D12_RESOURCE_DIMENSION.D3D12_RESOURCE_DIMENSION_TEXTURE3D => TextureDimension.Texture3D,
|
||||
_ => TextureDimension.Unknown,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Render target creation flags
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum RenderTargetCreationFlags
|
||||
{
|
||||
None = 0,
|
||||
AllowUAV = 1 << 0,
|
||||
AllowMSAA = 1 << 1,
|
||||
DynamicallyResolution = 1 << 2,
|
||||
GenerateMips = 1 << 3
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Memory types for resources
|
||||
/// </summary>
|
||||
public enum MemoryType
|
||||
{
|
||||
Default, // GPU memory
|
||||
Upload, // CPU-to-GPU memory
|
||||
Readback // GPU-to-CPU memory
|
||||
}
|
||||
Reference in New Issue
Block a user