Files
GhostEngine/Ghost.Graphics/D3D12/GraphicsResource.cs
Misaki 1284bb17de Refactor graphics architecture and resource management
Added DescriptorAllocator.cs to manage descriptor allocations for Direct3D 12.
Added Texture2D.cs to handle 2D textures and GPU resource creation.
Added DescriptorAllocatorExample.cs to demonstrate the new descriptor allocator interface.

Changed project files to reference Misaki.HighPerformance.LowLevel instead of Misaki.HighPerformance.Unsafe.
Changed _renderView type from IRenderer? to Renderer? in ScenePage.xaml.cs.
Changed EngineCore.cs to remove explicit graphics API specification during initialization.
Changed Logger.cs to enhance the Assert method with a DoesNotReturnIf attribute.
Changed resource types in Mesh.cs from IResource to GraphicsResource.

Removed multiple interfaces including ICommandBuffer, IDebugLayer, IGraphicsDevice, IPipelineResource, IRenderPass, IRenderer, IResource, and IResourceAllocator to simplify the graphics architecture.
Removed D3D12DebugLayer class from DebugLayer.cs to streamline the debug layer implementation.

Updated CommandList.cs and D3D12CommandBuffer.cs to implement a new command list structure for Direct3D 12.
Updated Material.cs to improve handling of constant buffers and textures.
Updated Shader.cs to include new structures for texture and property information.
Updated GraphicsPipeline.cs to support the new graphics device and resource management system.
Updated UnitTestAppWindow.xaml.cs to reflect changes in the renderer type and ensure proper resource management.
Updated BindlessMeshRenderPass.cs and MeshRenderPass.cs to implement modern rendering techniques, including bindless textures and improved shader management.
Updated CBufferCache.cs to align with the new resource management system and improve memory handling.
2025-07-12 14:25:20 +09:00

141 lines
3.3 KiB
C#

using Ghost.Core;
using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.CompilerServices;
using Win32;
using Win32.Graphics.Direct3D12;
namespace Ghost.Graphics.D3D12;
public unsafe class GraphicsResource
{
private ComPtr<ID3D12Resource> _nativeResource;
private string _name = string.Empty;
private bool _disposed;
internal ConstPtr<ID3D12Resource> NativeResource => new(_nativeResource.Get());
public ulong GPUAddress => _nativeResource.Get()->GetGPUVirtualAddress();
public string Name
{
get => _name;
set
{
_name = value;
_nativeResource.Get()->SetName(_name);
}
}
public bool TempResource
{
get;
}
internal GraphicsResource(ComPtr<ID3D12Resource> nativeResource, bool temp = false)
{
_nativeResource = nativeResource;
TempResource = temp;
}
~GraphicsResource()
{
DisposeInternal();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetData<T>(Span<T> data)
where T : unmanaged
{
fixed (T* ptr = data)
{
SetData(ptr, (uint)data.Length);
}
}
public unsafe void SetData<T>(T* data, uint length)
where T : unmanaged
{
var size = (uint)(length * sizeof(T));
SetData((void*)data, size);
}
public unsafe void SetData(void* data, uint size)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var range = new Win32.Graphics.Direct3D12.Range(0, size);
void* mappedPtr;
ThrowIfFailed(_nativeResource.Get()->Map(0, &range, &mappedPtr));
Unsafe.CopyBlock(mappedPtr, data, size);
_nativeResource.Get()->Unmap(0, &range);
}
public UnsafeArray<T> ReadData<T>(Allocator allocator)
where T : unmanaged
{
var size = (uint)_nativeResource.Get()->GetDesc().Width;
var data = new UnsafeArray<T>((int)(size / (uint)sizeof(T)), allocator);
try
{
ReadData(data.GetUnsafePtr(), &size);
return data;
}
catch (Exception)
{
data.Dispose();
throw;
}
}
public void ReadData<T>(T* pData, uint* size)
where T : unmanaged
{
ReadData((void*)pData, size);
}
public void ReadData(void* pData, uint* size)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var range = new Win32.Graphics.Direct3D12.Range(0, (uint)_nativeResource.Get()->GetDesc().Width);
void* mappedPtr;
var hr = _nativeResource.Get()->Map(0, &range, &mappedPtr);
if (hr.Failure)
{
var message = hr.ToString();
throw new InvalidOperationException($"Failed to map resource: {message}");
}
Unsafe.CopyBlock(pData, mappedPtr, (uint)(range.End - range.Begin));
_nativeResource.Get()->Unmap(0, &range);
if (size != null)
{
*size = (uint)(range.End - range.Begin);
}
}
internal void DisposeInternal()
{
if (_disposed)
{
return;
}
_nativeResource.Dispose();
_disposed = true;
}
public void Dispose()
{
if (!TempResource)
{
DisposeInternal();
GC.SuppressFinalize(this);
}
}
}