forked from Misaki/GhostEngine
Major architectural update to graphics/material/shader system: - Introduced strongly-typed key structs (Key64/Key128) for passes, variants, and pipelines; removed legacy key types. - Implemented robust hashing and key generation utilities for efficient variant and pipeline lookup/caching. - Shader compiler now compiles/caches all keyword variants using new key system; includes handled as lists. - Switched to push constant root signature for per-draw data; updated HLSL and C# codegen accordingly. - Refactored Material, Shader, and Pass data structures for cache efficiency and variant support. - Pipeline library and PSO management now use 128-bit keys and variant-specific caching. - Replaced WorldNode with SceneNode in editor/scene graph; introduced ComponentManager for archetype/query management. - Migrated math utilities to Misaki.HighPerformance.Mathematics; updated editor controls. - Updated all HLSL and codegen for new buffer/push constant layouts and macros. - Misc: project reference cleanup, D3D12 Work Graph support, doc updates, and code modernization.
178 lines
4.5 KiB
C#
178 lines
4.5 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
|
|
{
|
|
private UnsafeList<Vertex> _vertices;
|
|
private UnsafeList<uint> _indices;
|
|
|
|
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 => _vertices;
|
|
set
|
|
{
|
|
_vertices = 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 => _indices;
|
|
set
|
|
{
|
|
_indices = 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 UnsafeList<Vertex>(vertices.Length, Allocator.Persistent);
|
|
Indices = new UnsafeList<uint>(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);
|
|
}
|
|
}
|