- Introduced `Handle<T>` and `Identifier<T>` for lightweight, strongly-typed resource identifiers. - Replaced `BitSet` with `UnsafeBitSet` for improved performance and memory safety. - Refactored `Mesh` and `Material` into `MeshClass` and `MaterialClass` for better GPU resource handling. - Added `D3D12ResourceDatabase` to centralize GPU resource tracking and lifecycle management. - Updated `D3D12ShaderCompiler` to load shaders from disk and dynamically populate constant buffers and textures. - Enhanced `ICommandBuffer` with new upload operations for buffers and textures. - Refactored `Vertex` struct for simplified memory layout and better performance. - Updated `MeshBuilder` and rendering logic to align with new resource and shader structures. - Added `BindlessDescriptor` support to `TextureHandle` and `BufferHandle`. - Removed unused classes and performed general cleanup. - Updated unit tests and demos to reflect the new architecture.
388 lines
12 KiB
C#
388 lines
12 KiB
C#
using Ghost.Graphics.D3D12;
|
|
using Ghost.Graphics.RHI;
|
|
using Misaki.HighPerformance.LowLevel.Buffer;
|
|
using Misaki.HighPerformance.LowLevel.Collections;
|
|
using Misaki.HighPerformance.LowLevel.Utilities;
|
|
using Misaki.HighPerformance.Mathematics;
|
|
using Misaki.HighPerformance.Mathematics.Geometry;
|
|
using System.Runtime.CompilerServices;
|
|
using Win32.Graphics.Direct3D12;
|
|
using Win32.Graphics.Dxgi.Common;
|
|
|
|
namespace Ghost.Graphics.Data;
|
|
|
|
public struct Mesh
|
|
{
|
|
public UnsafeList<Vertex> vertices;
|
|
public UnsafeList<uint> indices;
|
|
public AABB boundingBox;
|
|
public BufferHandle vertexBuffer;
|
|
public BufferHandle indexBuffer;
|
|
|
|
public Mesh()
|
|
{
|
|
vertexBuffer = BufferHandle.Invalid;
|
|
indexBuffer = BufferHandle.Invalid;
|
|
}
|
|
|
|
internal Mesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices, BufferHandle vertexBuffer, BufferHandle indexBuffer)
|
|
{
|
|
this.vertices = new(vertices.Length, Allocator.Persistent);
|
|
this.indices = new(indices.Length, Allocator.Persistent);
|
|
this.vertices.CopyFrom(vertices);
|
|
this.indices.CopyFrom(indices);
|
|
this.vertexBuffer = vertexBuffer;
|
|
this.indexBuffer = indexBuffer;
|
|
|
|
ComputeBounds();
|
|
}
|
|
|
|
public void ComputeBounds()
|
|
{
|
|
if (vertices.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var min = new float3(float.MaxValue);
|
|
var max = new float3(float.MinValue);
|
|
foreach (var vertex in vertices)
|
|
{
|
|
var pos = vertex.position.xyz;
|
|
min = math.min(min, pos);
|
|
max = math.max(max, pos);
|
|
}
|
|
|
|
boundingBox = new AABB(min, max);
|
|
}
|
|
|
|
public void ReleaseCpuResources()
|
|
{
|
|
vertices.Dispose();
|
|
indices.Dispose();
|
|
}
|
|
}
|
|
|
|
public unsafe sealed class MeshClass : IDisposable
|
|
{
|
|
private UnsafeList<Vertex> _vertices;
|
|
private UnsafeList<int> _indices;
|
|
|
|
private AABB _boundingBox;
|
|
|
|
private IBuffer? _vertexBuffer;
|
|
private IBuffer? _indexBuffer;
|
|
|
|
public Span<Vertex> Vertices => _vertices.AsSpan();
|
|
public Span<int> Indices => _indices.AsSpan();
|
|
public AABB BoundingBox => _boundingBox;
|
|
|
|
public uint VertexCount => (uint)_vertices.Count;
|
|
public uint IndexCount => (uint)_indices.Count;
|
|
|
|
public uint VertexBufferDescriptorIndex
|
|
{
|
|
get
|
|
{
|
|
if (_vertexBuffer == null || !_vertexBuffer.Handle.IsValid)
|
|
{
|
|
throw new InvalidOperationException("Vertex buffer is not created.");
|
|
}
|
|
|
|
var bindlessDesc = _vertexBuffer.Handle.BindlessDescriptor;
|
|
if (!bindlessDesc.IsValid)
|
|
{
|
|
throw new InvalidOperationException("Vertex buffer is not created with bindless.");
|
|
}
|
|
|
|
return bindlessDesc.Index;
|
|
}
|
|
}
|
|
|
|
public uint IndexBufferDescriptorIndex
|
|
{
|
|
get
|
|
{
|
|
if (_indexBuffer == null || !_indexBuffer.Handle.IsValid)
|
|
{
|
|
throw new InvalidOperationException("Index buffer is not created.");
|
|
}
|
|
|
|
var bindlessDesc = _indexBuffer.Handle.BindlessDescriptor;
|
|
if (!bindlessDesc.IsValid)
|
|
{
|
|
throw new InvalidOperationException("Index buffer is not created with bindless.");
|
|
}
|
|
|
|
return bindlessDesc.Index;
|
|
}
|
|
}
|
|
|
|
public MeshClass(int initialVertexCapacity = 256, int initialIndexCapacity = 512)
|
|
{
|
|
_vertices = new(initialVertexCapacity, Allocator.Persistent);
|
|
_indices = new(initialIndexCapacity, Allocator.Persistent);
|
|
}
|
|
|
|
public MeshClass(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<int> indices)
|
|
: this(vertices.Length, indices.Length)
|
|
{
|
|
_vertices = new(vertices.Length, Allocator.Persistent);
|
|
_indices = new(indices.Length, Allocator.Persistent);
|
|
|
|
_vertices.CopyFrom(vertices);
|
|
_indices.CopyFrom(indices);
|
|
}
|
|
|
|
~MeshClass()
|
|
{
|
|
Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a vertex to the mesh with the specified attributes.
|
|
/// </summary>
|
|
/// <param name="vertex">The vertex data to add</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void AddVertex(Vertex vertex)
|
|
{
|
|
_vertices.Add(vertex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a triangle to the mesh by specifying the indices of its three vertices.
|
|
/// </summary>
|
|
/// <param name="index0">The index of the first vertex in the triangle. Must be within the range of the current vertex count.</param>
|
|
/// <param name="index1">The index of the second vertex in the triangle. Must be within the range of the current vertex count.</param>
|
|
/// <param name="index2">The index of the third vertex in the triangle. Must be within the range of the current vertex count.</param>
|
|
/// <exception cref="ArgumentOutOfRangeException">Thrown if any of the specified indices are out of range for the current vertex count.</exception>
|
|
public void AddTriangle(int index0, int index1, int index2)
|
|
{
|
|
if (index0 >= _vertices.Count || index1 >= _vertices.Count || index2 >= _vertices.Count)
|
|
{
|
|
throw new ArgumentOutOfRangeException("Index out of range for the current vertex count.");
|
|
}
|
|
|
|
_indices.Add(index0);
|
|
_indices.Add(index1);
|
|
_indices.Add(index2);
|
|
}
|
|
|
|
public void AddTriangles(params ReadOnlySpan<int> indices)
|
|
{
|
|
if (indices.Length % 3 != 0)
|
|
{
|
|
throw new ArgumentException("The number of indices must be a multiple of 3 to form triangles.");
|
|
}
|
|
|
|
foreach (var index in indices)
|
|
{
|
|
if (index < 0 || index >= _vertices.Count)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(indices), "Index out of range for the current vertex count.");
|
|
}
|
|
|
|
_indices.Add(index);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reduces the memory usage of the internal collections by resizing them to match their current element count.
|
|
/// </summary>
|
|
public void TrimExcess()
|
|
{
|
|
_vertices.Resize(_vertices.Count);
|
|
_indices.Resize(_indices.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Auto-compute smooth per-vertex normals.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Call this method before vertices and indices are valid.
|
|
/// </remarks>
|
|
public void ComputeNormal()
|
|
{
|
|
if (!_vertices.IsCreated || _vertices.Count < 3
|
|
|| !_indices.IsCreated || _indices.Count < 3)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (var i = 0; i < _indices.Count; i += 3)
|
|
{
|
|
var i0 = _indices[i];
|
|
var i1 = _indices[i + 1];
|
|
var i2 = _indices[i + 2];
|
|
|
|
var v0 = _vertices[i0];
|
|
var v1 = _vertices[i1];
|
|
var v2 = _vertices[i2];
|
|
|
|
var edge1 = v1.position - v0.position;
|
|
var edge2 = v2.position - v0.position;
|
|
var faceNormal = math.cross(edge1.xyz, edge2.xyz);
|
|
|
|
_vertices[i0].normal.xyz += faceNormal;
|
|
_vertices[i1].normal.xyz += faceNormal;
|
|
_vertices[i2].normal.xyz += faceNormal;
|
|
}
|
|
|
|
for (var i = 0; i < _vertices.Count; i++)
|
|
{
|
|
_vertices[i].normal = math.normalize(_vertices[i].normal);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Auto-compute per-vertex tangents.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Call this method before vertices, normals, and UVs are valid.
|
|
/// </remarks>
|
|
public void ComputeTangents()
|
|
{
|
|
var bitangents = new float4[_vertices.Count];
|
|
|
|
for (var i = 0; i < _indices.Count; i += 3)
|
|
{
|
|
var i0 = _indices[i];
|
|
var i1 = _indices[i + 1];
|
|
var i2 = _indices[i + 2];
|
|
|
|
var v0 = _vertices[i0];
|
|
var v1 = _vertices[i1];
|
|
var v2 = _vertices[i2];
|
|
|
|
var uv0 = _vertices[i0].uv;
|
|
var uv1 = _vertices[i1].uv;
|
|
var uv2 = _vertices[i2].uv;
|
|
|
|
var deltaPos1 = v1.position - v0.position;
|
|
var deltaPos2 = v2.position - v0.position;
|
|
var deltaUV1 = uv1 - uv0;
|
|
var deltaUV2 = uv2 - uv0;
|
|
|
|
var r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
|
|
var tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
|
|
var bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x) * r;
|
|
|
|
for (var j = 0; j < 3; j++)
|
|
{
|
|
var idx = _indices[i + j];
|
|
var t = _vertices[idx].tangent;
|
|
_vertices[idx].tangent.xyz = t.xyz + tangent.xyz;
|
|
|
|
bitangents[idx] += bitangent;
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < _vertices.Count; i++)
|
|
{
|
|
var n = _vertices[i].normal;
|
|
var t = _vertices[i].tangent;
|
|
|
|
var proj = n * math.dot(n, t);
|
|
t = math.normalize(t - proj);
|
|
|
|
var b = bitangents[i];
|
|
var w = math.dot(math.cross(n.xyz, t.xyz), b.xyz) < 0.0f ? -1.0f : 1.0f;
|
|
|
|
_vertices[i].tangent = new float4(t.x, t.y, t.z, w);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes the bounding box of the mesh based on its vertices.
|
|
/// </summary>
|
|
public void ComputeBounds()
|
|
{
|
|
if (_vertices.Count == 0)
|
|
{
|
|
_boundingBox = AABB.Zero;
|
|
return;
|
|
}
|
|
|
|
var min = new float3(float.MaxValue);
|
|
var max = new float3(float.MinValue);
|
|
foreach (var vertex in _vertices)
|
|
{
|
|
var pos = vertex.position.xyz;
|
|
min = math.min(min, pos);
|
|
max = math.max(max, pos);
|
|
}
|
|
|
|
_boundingBox = new AABB(min, max);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uploads the mesh data to GPU resources.
|
|
/// </summary>
|
|
public unsafe void UploadMeshData()
|
|
{
|
|
if (VertexCount == 0 || IndexCount == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
DisposeGpuResources();
|
|
|
|
var vertexBufferSize = (uint)(VertexCount * sizeof(Vertex));
|
|
var indexBufferSize = IndexCount * sizeof(int);
|
|
_vertexBuffer = GraphicsBuffer.Create(vertexBufferSize, GraphicsBuffer.Usage.CopyDestination);
|
|
_indexBuffer = GraphicsBuffer.Create(indexBufferSize, GraphicsBuffer.Usage.CopyDestination);
|
|
|
|
var uploadBatch = GraphicsPipeline.UploadBatch;
|
|
uploadBatch.Upload(_vertexBuffer.NativeResource, _vertices.AsSpan());
|
|
uploadBatch.Upload(_indexBuffer.NativeResource, _indices.AsSpan());
|
|
uploadBatch.Transition(_vertexBuffer.NativeResource, ResourceStates.CopyDest, ResourceStates.VertexAndConstantBuffer);
|
|
uploadBatch.Transition(_indexBuffer.NativeResource, ResourceStates.CopyDest, ResourceStates.IndexBuffer);
|
|
|
|
// Create bindless descriptors for vertex and index buffers
|
|
CreateBindlessDescriptors();
|
|
}
|
|
|
|
internal void MarkNoLongerReadable()
|
|
{
|
|
_vertices.Dispose();
|
|
_indices.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears all vertex and index data and releases associated GPU resources.
|
|
/// </summar>
|
|
public void Clear()
|
|
{
|
|
_vertices.Clear();
|
|
_indices.Clear();
|
|
DisposeGpuResources();
|
|
}
|
|
|
|
private void DisposeGpuResources()
|
|
{
|
|
_vertexBuffer?.Dispose();
|
|
_vertexBuffer = null;
|
|
|
|
_indexBuffer?.Dispose();
|
|
_indexBuffer = null;
|
|
|
|
if (_vertexBufferDescriptor.IsValid)
|
|
{
|
|
RenderSystem.GraphicsEngine.DescriptorAllocator.Release(_vertexBufferDescriptor);
|
|
}
|
|
|
|
if (_indexBufferDescriptor.IsValid)
|
|
{
|
|
RenderSystem.GraphicsEngine.DescriptorAllocator.Release(_indexBufferDescriptor);
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_vertices.Dispose();
|
|
_indices.Dispose();
|
|
DisposeGpuResources();
|
|
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
} |