forked from Misaki/GhostEngine
feat: Implement D3D12 resource factory and improve swap chain management
- Added D3D12ResourceFactory for creating render targets, textures, and buffers. - Enhanced D3D12SwapChain to manage back buffer render targets and provide access to them. - Updated D3D12Texture to utilize resource handles for better resource management. - Removed legacy ResourceAllocator and integrated improvements for resource handling. - Introduced new interfaces for resource factory and swap chain to streamline resource creation. - Added support for mip levels and texture dimensions in render target and texture descriptions. - Created new markdown files to document allocator and swap chain improvements.
This commit is contained in:
@@ -8,17 +8,34 @@ public interface IRenderDevice : IDisposable
|
||||
/// <summary>
|
||||
/// Graphics command queue for rendering operations
|
||||
/// </summary>
|
||||
ICommandQueue GraphicsQueue { get; }
|
||||
ICommandQueue GraphicsQueue
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute command queue for compute shader operations
|
||||
/// </summary>
|
||||
ICommandQueue ComputeQueue { get; }
|
||||
ICommandQueue ComputeQueue
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copy command queue for data transfer operations
|
||||
/// </summary>
|
||||
ICommandQueue CopyQueue { get; }
|
||||
ICommandQueue CopyQueue
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the descriptor allocator for managing descriptors
|
||||
/// </summary>
|
||||
IDescriptorAllocator DescriptorAllocator
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a command buffer for recording rendering commands
|
||||
@@ -33,32 +50,6 @@ public interface IRenderDevice : IDisposable
|
||||
/// <param name="desc">Swap chain description</param>
|
||||
/// <returns>A new swap chain instance</returns>
|
||||
ISwapChain CreateSwapChain(SwapChainDesc desc);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a render target for off-screen rendering
|
||||
/// </summary>
|
||||
/// <param name="desc">Render target description</param>
|
||||
/// <returns>A new render target instance</returns>
|
||||
IRenderTarget CreateRenderTarget(RenderTargetDesc desc);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a texture resource
|
||||
/// </summary>
|
||||
/// <param name="desc">Texture description</param>
|
||||
/// <returns>A new texture instance</returns>
|
||||
ITexture CreateTexture(TextureDesc desc);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a buffer resource
|
||||
/// </summary>
|
||||
/// <param name="desc">Buffer description</param>
|
||||
/// <returns>A new buffer instance</returns>
|
||||
IBuffer CreateBuffer(BufferDesc desc);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the descriptor allocator for managing descriptors
|
||||
/// </summary>
|
||||
IDescriptorAllocator DescriptorAllocator { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -71,6 +71,21 @@ public struct RenderTargetDesc
|
||||
/// </summary>
|
||||
public TextureFormat Format;
|
||||
|
||||
/// <summary>
|
||||
/// Texture dimension
|
||||
/// </summary>
|
||||
public TextureDimension Dimension;
|
||||
|
||||
/// <summary>
|
||||
/// Creation flags for the render target
|
||||
/// </summary>
|
||||
public RenderTargetCreationFlags CreationFlags;
|
||||
|
||||
/// <summary>
|
||||
/// Number of mip levels. 0 to generate full mip chain
|
||||
/// </summary>
|
||||
public uint MipLevels;
|
||||
|
||||
/// <summary>
|
||||
/// Number of samples for MSAA
|
||||
/// </summary>
|
||||
@@ -79,7 +94,10 @@ public struct RenderTargetDesc
|
||||
/// <summary>
|
||||
/// Creates a color render target
|
||||
/// </summary>
|
||||
public static RenderTargetDesc Color(uint width, uint height, TextureFormat format, uint sampleCount = 1)
|
||||
public static RenderTargetDesc Color(uint width, uint height,
|
||||
TextureFormat format, TextureDimension dimension = TextureDimension.Texture2D,
|
||||
RenderTargetCreationFlags creationFlags = RenderTargetCreationFlags.AllowUAV | RenderTargetCreationFlags.DynamicallyScalable | RenderTargetCreationFlags.GenerateMips,
|
||||
uint mipLevels = 0u, uint sampleCount = 1)
|
||||
{
|
||||
return new RenderTargetDesc
|
||||
{
|
||||
@@ -87,6 +105,9 @@ public struct RenderTargetDesc
|
||||
Height = height,
|
||||
Type = RenderTargetType.Color,
|
||||
Format = format,
|
||||
Dimension = dimension,
|
||||
CreationFlags = creationFlags,
|
||||
MipLevels = mipLevels,
|
||||
SampleCount = sampleCount
|
||||
};
|
||||
}
|
||||
@@ -94,7 +115,10 @@ public struct RenderTargetDesc
|
||||
/// <summary>
|
||||
/// Creates a depth render target
|
||||
/// </summary>
|
||||
public static RenderTargetDesc Depth(uint width, uint height, TextureFormat format = TextureFormat.D24_UNorm_S8_UInt, uint sampleCount = 1)
|
||||
public static RenderTargetDesc Depth(uint width, uint height,
|
||||
TextureFormat format = TextureFormat.D24_UNorm_S8_UInt, TextureDimension dimension = TextureDimension.Texture2D,
|
||||
RenderTargetCreationFlags creationFlags = RenderTargetCreationFlags.AllowUAV | RenderTargetCreationFlags.DynamicallyScalable,
|
||||
uint mipLevels = 0u, uint sampleCount = 1)
|
||||
{
|
||||
return new RenderTargetDesc
|
||||
{
|
||||
@@ -102,6 +126,9 @@ public struct RenderTargetDesc
|
||||
Height = height,
|
||||
Type = RenderTargetType.Depth,
|
||||
Format = format,
|
||||
Dimension = dimension,
|
||||
CreationFlags = creationFlags,
|
||||
MipLevels = mipLevels,
|
||||
SampleCount = sampleCount
|
||||
};
|
||||
}
|
||||
@@ -128,7 +155,12 @@ public struct TextureDesc
|
||||
public TextureFormat Format;
|
||||
|
||||
/// <summary>
|
||||
/// Number of mip levels
|
||||
/// Texture dimension
|
||||
/// </summary>
|
||||
public TextureDimension Dimension;
|
||||
|
||||
/// <summary>
|
||||
/// Number of mip levels. 0 to generate full mip chain
|
||||
/// </summary>
|
||||
public uint MipLevels;
|
||||
|
||||
@@ -137,11 +169,12 @@ public struct TextureDesc
|
||||
/// </summary>
|
||||
public TextureUsage Usage;
|
||||
|
||||
public TextureDesc(uint width, uint height, TextureFormat format, uint mipLevels = 1, TextureUsage usage = TextureUsage.ShaderResource)
|
||||
public TextureDesc(uint width, uint height, TextureFormat format, TextureDimension dimension = TextureDimension.Texture2D, uint mipLevels = 0u, TextureUsage usage = TextureUsage.ShaderResource)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
Format = format;
|
||||
Dimension = dimension;
|
||||
MipLevels = mipLevels;
|
||||
Usage = usage;
|
||||
}
|
||||
@@ -188,6 +221,33 @@ public enum TextureUsage
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Render target creation flags
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum RenderTargetCreationFlags
|
||||
{
|
||||
None = 0,
|
||||
AllowUAV = 1 << 0,
|
||||
AllowMSAA = 1 << 1,
|
||||
DynamicallyScalable = 1 << 2,
|
||||
GenerateMips = 1 << 3
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Memory types for resources
|
||||
/// </summary>
|
||||
|
||||
41
Ghost.Graphics/RHI/IResourceFactory.cs
Normal file
41
Ghost.Graphics/RHI/IResourceFactory.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Ghost.Graphics.Data;
|
||||
|
||||
namespace Ghost.Graphics.RHI;
|
||||
|
||||
public interface IResourceFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a render target for off-screen rendering
|
||||
/// </summary>
|
||||
/// <param name="desc">Render target description</param>
|
||||
/// <returns>A new render target instance</returns>
|
||||
public IRenderTarget CreateRenderTarget(ref readonly RenderTargetDesc desc);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a texture resource
|
||||
/// </summary>
|
||||
/// <param name="desc">Texture description</param>
|
||||
/// <returns>A new texture handle point to the resource</returns>
|
||||
public TextureHandle CreateTextureHandle(ref readonly TextureDesc desc);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a texture resource
|
||||
/// </summary>
|
||||
/// <param name="desc">Texture description</param>
|
||||
/// <returns>A new texture instance</returns>
|
||||
public ITexture CreateTexture(ref readonly TextureDesc desc);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a buffer resource
|
||||
/// </summary>
|
||||
/// <param name="desc">Buffer description</param>
|
||||
/// <returns>A new buffer handle point to the resource</returns>
|
||||
public BufferHandle CreateBufferHandle(ref readonly BufferDesc desc);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a buffer resource
|
||||
/// </summary>
|
||||
/// <param name="desc">Buffer description</param>
|
||||
/// <returns>A new buffer instance</returns>
|
||||
public IBuffer CreateBuffer(ref readonly BufferDesc desc);
|
||||
}
|
||||
@@ -28,6 +28,12 @@ public interface ISwapChain : IDisposable
|
||||
/// <returns>Current back buffer texture</returns>
|
||||
ITexture GetCurrentBackBuffer();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current back buffer as a render target
|
||||
/// </summary>
|
||||
/// <returns>Current back buffer render target</returns>
|
||||
IRenderTarget GetCurrentBackBufferRenderTarget();
|
||||
|
||||
/// <summary>
|
||||
/// Presents the rendered frame
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user