Files
GhostEngine/Ghost.Graphics/D3D12/D3D12Buffer.cs
Misaki 78cc64b1d2 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.
2025-09-02 19:39:34 +09:00

127 lines
2.8 KiB
C#

using Ghost.Graphics.Data;
using Ghost.Graphics.RHI;
using Win32;
using Win32.Graphics.Direct3D12;
namespace Ghost.Graphics.D3D12;
/// <summary>
/// D3D12 implementation of buffer interface using resource handles
/// </summary>
internal unsafe class D3D12Buffer : IBuffer
{
private readonly BufferHandle _handle;
private readonly ComPtr<ID3D12Resource> _externalResource; // For externally managed resources
private ResourceState _currentState;
private void* _mappedPtr;
private bool _disposed;
public BufferUsage Usage
{
get;
}
public MemoryType MemoryType
{
get;
}
public string Name
{
get => field;
set
{
field = value;
NativeResource->SetName(field);
}
} = string.Empty;
public ulong Size
{
get;
}
public ResourceState CurrentState => _currentState;
public ID3D12Resource* NativeResource => _handle.ResourceHandle.GetAllocation().Resource;
/// <summary>
/// Constructor for wrapping existing D3D12 resources
/// </summary>
public D3D12Buffer(ComPtr<ID3D12Resource> resource, ulong size, BufferUsage usage, MemoryType memoryType)
{
_handle = BufferHandle.Invalid;
_externalResource = resource.Move();
Size = size;
Usage = usage;
MemoryType = memoryType;
_currentState = ResourceState.Common;
}
/// <summary>
/// Constructor for allocator-managed buffers
/// </summary>
public D3D12Buffer(BufferHandle handle, ref readonly BufferDesc desc)
{
_handle = handle;
Size = desc.Size;
Usage = desc.Usage;
MemoryType = desc.MemoryType;
_currentState = ResourceState.Common;
}
public void* Map()
{
if (_mappedPtr != null)
return _mappedPtr;
if (MemoryType != MemoryType.Upload && MemoryType != MemoryType.Readback)
{
throw new InvalidOperationException("Only upload and readback buffers can be mapped");
}
var range = new Win32.Graphics.Direct3D12.Range { Begin = 0, End = 0 };
fixed (void** ptr = &_mappedPtr)
{
NativeResource->Map(0, &range, ptr);
}
return _mappedPtr;
}
public void Unmap()
{
if (_mappedPtr != null)
{
NativeResource->Unmap(0, null);
_mappedPtr = null;
}
}
public void SetCurrentState(ResourceState state)
{
_currentState = state;
}
public void Dispose()
{
if (_disposed)
return;
Unmap();
if (_handle.IsValid)
{
_handle.Dispose();
}
else
{
// Release external resource
_externalResource.Dispose();
}
_disposed = true;
}
}