Updated multiple components to improve encapsulation, maintainability, and performance. Key changes include: - Upgraded package dependencies in project files. - Refactored `Mesh` and `RenderingContext` to use properties and added support for per-object constant buffers. - Improved resource management in `D3D12CommandBuffer`, `D3D12CommandQueue`, and `D3D12ResourceAllocator` with better encapsulation and disposal handling. - Added validation for constant buffer sizes in `D3D12PipelineLibrary`. - Simplified `MeshBuilder` methods to accept allocators and removed hardcoded values. - Enhanced debugging with `GPUResourceLeakException` and resource tracking updates. - Updated shaders and rendering logic for testing, including hardcoded triangle rendering. - Removed redundant base classes and interfaces for cleaner code structure.
175 lines
4.3 KiB
C#
175 lines
4.3 KiB
C#
using Ghost.Core;
|
|
using Ghost.Graphics.RHI;
|
|
using Ghost.Graphics.Utilities;
|
|
using Misaki.HighPerformance.LowLevel.Buffer;
|
|
using Misaki.HighPerformance.LowLevel.Collections;
|
|
using Misaki.HighPerformance.LowLevel.Utilities;
|
|
using Misaki.HighPerformance.Mathematics;
|
|
using Misaki.HighPerformance.Mathematics.Geometry;
|
|
|
|
namespace Ghost.Graphics.Core;
|
|
|
|
public struct Mesh : IResourceReleasable, IHandleType
|
|
{
|
|
internal bool IsMeshDataDirty
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the collection of vertices that define the geometry.
|
|
/// </summary>
|
|
public UnsafeList<Vertex> Vertices
|
|
{
|
|
readonly get => field;
|
|
set
|
|
{
|
|
field = value;
|
|
VertexCount = value.Count;
|
|
IsMeshDataDirty = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the collection of indices that define the order of vertices.
|
|
/// </summary>
|
|
public UnsafeList<uint> Indices
|
|
{
|
|
readonly get => field;
|
|
set
|
|
{
|
|
field = value;
|
|
IndexCount = value.Count;
|
|
IsMeshDataDirty = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the number of vertices in the mesh.
|
|
/// </summary>
|
|
public int VertexCount
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the number of indices in the mesh.
|
|
/// </summary>
|
|
public int IndexCount
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the axis-aligned bounding box (AABB) of the mesh.
|
|
/// </summary>
|
|
public AABB BoundingBox
|
|
{
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the handle to the vertex buffer on the GPU.
|
|
/// </summary>
|
|
public Handle<GraphicsBuffer> VertexBuffer
|
|
{
|
|
get; internal set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the handle to the index buffer on the GPU.
|
|
/// </summary>
|
|
public Handle<GraphicsBuffer> IndexBuffer
|
|
{
|
|
get; internal set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the handle to the mesh data buffer on the GPU.
|
|
/// </summary>
|
|
public Handle<GraphicsBuffer> ObjectDataBuffer
|
|
{
|
|
get; internal set;
|
|
}
|
|
|
|
public Mesh()
|
|
{
|
|
VertexBuffer = Handle<GraphicsBuffer>.Invalid;
|
|
IndexBuffer = Handle<GraphicsBuffer>.Invalid;
|
|
}
|
|
|
|
internal Mesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices, Handle<GraphicsBuffer> vertexBuffer, Handle<GraphicsBuffer> indexBuffer)
|
|
{
|
|
Vertices = new(vertices.Length, Allocator.Persistent);
|
|
Indices = new(indices.Length, Allocator.Persistent);
|
|
Vertices.CopyFrom(vertices);
|
|
Indices.CopyFrom(indices);
|
|
VertexBuffer = vertexBuffer;
|
|
IndexBuffer = indexBuffer;
|
|
|
|
this.ComputeBounds();
|
|
}
|
|
|
|
public readonly void ReleaseCpuResources()
|
|
{
|
|
Vertices.Dispose();
|
|
Indices.Dispose();
|
|
}
|
|
|
|
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
|
{
|
|
ReleaseCpuResources();
|
|
|
|
database.ReleaseResource(VertexBuffer.AsResource());
|
|
database.ReleaseResource(IndexBuffer.AsResource());
|
|
database.ReleaseResource(ObjectDataBuffer.AsResource());
|
|
}
|
|
}
|
|
|
|
public static class MeshExtension
|
|
{
|
|
/// <summary>
|
|
/// Computes the bounding box of the mesh based on its vertices.
|
|
/// </summary>
|
|
public static void ComputeBounds(ref this Mesh mesh)
|
|
{
|
|
if (mesh.Vertices.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var min = new float3(float.MaxValue);
|
|
var max = new float3(float.MinValue);
|
|
foreach (var vertex in mesh.Vertices)
|
|
{
|
|
var pos = vertex.position.xyz;
|
|
min = math.min(min, pos);
|
|
max = math.max(max, pos);
|
|
}
|
|
|
|
mesh.BoundingBox = new AABB(min, max);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Auto-compute smooth per-vertex normals.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Call this method before vertices and indices are valid.
|
|
/// </remarks>
|
|
public static void ComputeNormal(ref this Mesh mesh)
|
|
{
|
|
MeshBuilder.ComputeNormal(mesh.Vertices, mesh.Indices);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Auto-compute per-vertex tangents.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Call this method before vertices, normals, and UVs are valid.
|
|
/// </remarks>
|
|
public static void ComputeTangents(ref this Mesh mesh)
|
|
{
|
|
MeshBuilder.ComputeTangents(mesh.Vertices, mesh.Indices);
|
|
}
|
|
}
|