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:
2025-09-02 19:39:34 +09:00
parent 5385141f14
commit 78cc64b1d2
20 changed files with 848 additions and 680 deletions

View File

@@ -1,6 +1,5 @@
using Ghost.Graphics.Contracts;
using Ghost.Graphics.RHI;
using Ghost.Graphics.D3D12.Utilities;
using Win32;
using Win32.Graphics.Direct3D12;
using Win32.Graphics.Dxgi;
@@ -15,16 +14,27 @@ internal unsafe class D3D12SwapChain : ISwapChain
{
private ComPtr<IDXGISwapChain4> _swapChain;
private readonly D3D12Texture[] _backBuffers;
private readonly D3D12RenderTarget[] _backBufferRenderTargets;
private uint _currentBackBufferIndex;
private bool _disposed;
public uint Width { get; private set; }
public uint Height { get; private set; }
public uint BufferCount { get; }
public uint Width
{
get; private set;
}
public uint Height
{
get; private set;
}
public uint BufferCount
{
get;
}
public D3D12SwapChain(ComPtr<IDXGIFactory7> factory, ID3D12CommandQueue* commandQueue, SwapChainDesc desc)
{
_backBuffers = new D3D12Texture[desc.BufferCount];
_backBufferRenderTargets = new D3D12RenderTarget[desc.BufferCount];
Width = desc.Width;
Height = desc.Height;
@@ -102,6 +112,9 @@ internal unsafe class D3D12SwapChain : ISwapChain
backBuffer.Get()->SetName($"SwapChain_BackBuffer_{i}");
_backBuffers[i] = new D3D12Texture(backBuffer.Move(), Width, Height, TextureFormat.B8G8R8A8_UNorm);
// Create render target wrapper for the back buffer
_backBufferRenderTargets[i] = new D3D12RenderTarget(_backBuffers[i], RenderTargetType.Color);
}
}
@@ -111,6 +124,12 @@ internal unsafe class D3D12SwapChain : ISwapChain
return _backBuffers[_currentBackBufferIndex];
}
public IRenderTarget GetCurrentBackBufferRenderTarget()
{
_currentBackBufferIndex = _swapChain.Get()->GetCurrentBackBufferIndex();
return _backBufferRenderTargets[_currentBackBufferIndex];
}
public void Present(bool vsync = true)
{
var presentFlags = PresentFlags.None;
@@ -127,9 +146,10 @@ internal unsafe class D3D12SwapChain : ISwapChain
if (Width == width && Height == height)
return;
// Release old back buffers
for (int i = 0; i < _backBuffers.Length; i++)
// Release old back buffers and render targets
for (var i = 0; i < _backBuffers.Length; i++)
{
_backBufferRenderTargets[i]?.Dispose();
_backBuffers[i]?.Dispose();
}
@@ -161,14 +181,18 @@ internal unsafe class D3D12SwapChain : ISwapChain
public void Dispose()
{
if (_disposed) return;
for (int i = 0; i < _backBuffers.Length; i++)
if (_disposed)
{
return;
}
for (var i = 0; i < _backBuffers.Length; i++)
{
_backBufferRenderTargets[i]?.Dispose();
_backBuffers[i]?.Dispose();
}
_swapChain.Dispose();
_disposed = true;
}
}
}