forked from Misaki/GhostEngine
Refactor and enhance resource management and rendering
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.
This commit is contained in:
@@ -18,7 +18,9 @@ internal struct CBufferCache : IResourceReleasable
|
||||
public readonly Handle<GraphicsBuffer> GpuResource => _gpuResource;
|
||||
public readonly uint AlignedSize => _alignedSize;
|
||||
|
||||
public unsafe CBufferCache(Handle<GraphicsBuffer> buffer, uint bufferSize)
|
||||
public readonly bool IsCreated => _gpuResource.IsValid && _cpuData.IsCreated;
|
||||
|
||||
public CBufferCache(Handle<GraphicsBuffer> buffer, uint bufferSize)
|
||||
{
|
||||
_alignedSize = (bufferSize + 255u) & ~255u;
|
||||
|
||||
@@ -65,6 +67,11 @@ public struct Material : IResourceReleasable, IHandleType
|
||||
var pass = database.GetShaderPass(shader.GetPassKey(i));
|
||||
var cbufferInfo = pass.CBuffer;
|
||||
|
||||
if (cbufferInfo.SizeInBytes == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var desc = new BufferDesc
|
||||
{
|
||||
Size = cbufferInfo.SizeInBytes,
|
||||
@@ -91,7 +98,7 @@ public struct Material : IResourceReleasable, IHandleType
|
||||
public ref struct MaterialAccessor
|
||||
{
|
||||
private ref Material _materialData;
|
||||
private Shader _shader;
|
||||
private readonly Shader _shader;
|
||||
|
||||
private readonly IResourceDatabase _resourceDatabase;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
@@ -10,42 +11,118 @@ namespace Ghost.Graphics.Core;
|
||||
|
||||
public struct Mesh : IResourceReleasable, IHandleType
|
||||
{
|
||||
public UnsafeList<Vertex> vertices;
|
||||
public UnsafeList<uint> indices;
|
||||
public AABB boundingBox;
|
||||
public Handle<GraphicsBuffer> vertexBuffer;
|
||||
public Handle<GraphicsBuffer> indexBuffer;
|
||||
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;
|
||||
VertexBuffer = Handle<GraphicsBuffer>.Invalid;
|
||||
IndexBuffer = Handle<GraphicsBuffer>.Invalid;
|
||||
}
|
||||
|
||||
internal Mesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices, Handle<GraphicsBuffer> vertexBuffer, Handle<GraphicsBuffer> 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;
|
||||
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 void ReleaseCpuResources()
|
||||
public readonly void ReleaseCpuResources()
|
||||
{
|
||||
vertices.Dispose();
|
||||
indices.Dispose();
|
||||
Vertices.Dispose();
|
||||
Indices.Dispose();
|
||||
}
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
ReleaseCpuResources();
|
||||
|
||||
database.ReleaseResource(vertexBuffer.AsResource());
|
||||
database.ReleaseResource(indexBuffer.AsResource());
|
||||
database.ReleaseResource(VertexBuffer.AsResource());
|
||||
database.ReleaseResource(IndexBuffer.AsResource());
|
||||
database.ReleaseResource(ObjectDataBuffer.AsResource());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,21 +133,21 @@ public static class MeshExtension
|
||||
/// </summary>
|
||||
public static void ComputeBounds(ref this Mesh mesh)
|
||||
{
|
||||
if (mesh.vertices.Count == 0)
|
||||
if (mesh.Vertices.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var min = new float3(float.MaxValue);
|
||||
var max = new float3(float.MinValue);
|
||||
foreach (var vertex in mesh.vertices)
|
||||
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);
|
||||
mesh.BoundingBox = new AABB(min, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -81,35 +158,7 @@ public static class MeshExtension
|
||||
/// </remarks>
|
||||
public static void ComputeNormal(ref this Mesh mesh)
|
||||
{
|
||||
if (!mesh.vertices.IsCreated || mesh.vertices.Count < 3
|
||||
|| !mesh.indices.IsCreated || mesh.indices.Count < 3)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < mesh.indices.Count; i += 3)
|
||||
{
|
||||
var i0 = mesh.indices[i];
|
||||
var i1 = mesh.indices[i + 1];
|
||||
var i2 = mesh.indices[i + 2];
|
||||
|
||||
var v0 = mesh.vertices[i0];
|
||||
var v1 = mesh.vertices[i1];
|
||||
var v2 = mesh.vertices[i2];
|
||||
|
||||
var edge1 = v1.position - v0.position;
|
||||
var edge2 = v2.position - v0.position;
|
||||
var faceNormal = math.cross(edge1.xyz, edge2.xyz);
|
||||
|
||||
mesh.vertices[i0].normal.xyz += faceNormal;
|
||||
mesh.vertices[i1].normal.xyz += faceNormal;
|
||||
mesh.vertices[i2].normal.xyz += faceNormal;
|
||||
}
|
||||
|
||||
for (var i = 0; i < mesh.vertices.Count; i++)
|
||||
{
|
||||
mesh.vertices[i].normal = math.normalize(mesh.vertices[i].normal);
|
||||
}
|
||||
MeshBuilder.ComputeNormal(mesh.Vertices, mesh.Indices);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -120,53 +169,6 @@ public static class MeshExtension
|
||||
/// </remarks>
|
||||
public static void ComputeTangents(ref this Mesh mesh)
|
||||
{
|
||||
var bitangents = new float4[mesh.vertices.Count];
|
||||
|
||||
for (var i = 0; i < mesh.indices.Count; i += 3)
|
||||
{
|
||||
var i0 = mesh.indices[i];
|
||||
var i1 = mesh.indices[i + 1];
|
||||
var i2 = mesh.indices[i + 2];
|
||||
|
||||
var v0 = mesh.vertices[i0];
|
||||
var v1 = mesh.vertices[i1];
|
||||
var v2 = mesh.vertices[i2];
|
||||
|
||||
var uv0 = mesh.vertices[i0].uv;
|
||||
var uv1 = mesh.vertices[i1].uv;
|
||||
var uv2 = mesh.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 = mesh.indices[i + j];
|
||||
var t = mesh.vertices[idx].tangent;
|
||||
mesh.vertices[idx].tangent.xyz = t.xyz + tangent.xyz;
|
||||
|
||||
bitangents[idx] += bitangent;
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < mesh.vertices.Count; i++)
|
||||
{
|
||||
var n = mesh.vertices[i].normal;
|
||||
var t = mesh.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;
|
||||
|
||||
mesh.vertices[i].tangent = new float4(t.x, t.y, t.z, w);
|
||||
}
|
||||
MeshBuilder.ComputeTangents(mesh.Vertices, mesh.Indices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
@@ -72,6 +73,11 @@ public unsafe readonly ref struct RenderingContext
|
||||
return CreateMesh(vertexList, indexList);
|
||||
}
|
||||
|
||||
public MaterialAccessor GetMaterialAccessor(Handle<Material> material)
|
||||
{
|
||||
return new MaterialAccessor(material, ResourceDatabase);
|
||||
}
|
||||
|
||||
// TODO: Make one memory pool for upload.
|
||||
|
||||
/// <summary>
|
||||
@@ -82,32 +88,32 @@ public unsafe readonly ref struct RenderingContext
|
||||
public void UploadMesh(Handle<Mesh> mesh, bool markMeshStatic)
|
||||
{
|
||||
ref var meshData = ref ResourceDatabase.GetMeshReference(mesh);
|
||||
var vertexState = ResourceDatabase.GetResourceState(meshData.vertexBuffer.AsResource());
|
||||
var indexState = ResourceDatabase.GetResourceState(meshData.indexBuffer.AsResource());
|
||||
var vertexState = ResourceDatabase.GetResourceState(meshData.VertexBuffer.AsResource());
|
||||
var indexState = ResourceDatabase.GetResourceState(meshData.IndexBuffer.AsResource());
|
||||
var needVertexTransition = vertexState != ResourceState.CopyDest;
|
||||
var needIndexTransition = indexState != ResourceState.CopyDest;
|
||||
|
||||
if (needVertexTransition)
|
||||
{
|
||||
_directCmd.ResourceBarrier(meshData.vertexBuffer.AsResource(), vertexState, ResourceState.CopyDest);
|
||||
_directCmd.ResourceBarrier(meshData.VertexBuffer.AsResource(), vertexState, ResourceState.CopyDest);
|
||||
}
|
||||
|
||||
if (needIndexTransition)
|
||||
{
|
||||
_directCmd.ResourceBarrier(meshData.indexBuffer.AsResource(), indexState, ResourceState.CopyDest);
|
||||
_directCmd.ResourceBarrier(meshData.IndexBuffer.AsResource(), indexState, ResourceState.CopyDest);
|
||||
}
|
||||
|
||||
_directCmd.UploadBuffer(meshData.vertexBuffer, meshData.vertices.AsSpan());
|
||||
_directCmd.UploadBuffer(meshData.indexBuffer, meshData.indices.AsSpan());
|
||||
_directCmd.UploadBuffer(meshData.VertexBuffer, meshData.Vertices.AsSpan());
|
||||
_directCmd.UploadBuffer(meshData.IndexBuffer, meshData.Indices.AsSpan());
|
||||
|
||||
if (needVertexTransition)
|
||||
{
|
||||
_directCmd.ResourceBarrier(meshData.vertexBuffer.AsResource(), ResourceState.CopyDest, vertexState);
|
||||
_directCmd.ResourceBarrier(meshData.VertexBuffer.AsResource(), ResourceState.CopyDest, ResourceState.VertexAndConstantBuffer);
|
||||
}
|
||||
|
||||
if (needIndexTransition)
|
||||
{
|
||||
_directCmd.ResourceBarrier(meshData.indexBuffer.AsResource(), ResourceState.CopyDest, indexState);
|
||||
_directCmd.ResourceBarrier(meshData.IndexBuffer.AsResource(), ResourceState.CopyDest, ResourceState.IndexBuffer);
|
||||
}
|
||||
|
||||
if (markMeshStatic)
|
||||
@@ -116,6 +122,34 @@ public unsafe readonly ref struct RenderingContext
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateObjectData(Handle<Mesh> mesh, float4x4 localToWorld)
|
||||
{
|
||||
ref var meshData = ref ResourceDatabase.GetMeshReference(mesh);
|
||||
var data = new PerObjectData
|
||||
{
|
||||
localToWorld = localToWorld,
|
||||
worldBoundsMin = meshData.BoundingBox.Min,
|
||||
worldBoundsMax = meshData.BoundingBox.Max,
|
||||
vertexBuffer = (uint)_engine.ResourceDatabase.GetBindlessIndex(meshData.VertexBuffer.AsResource()),
|
||||
indexBuffer = (uint)_engine.ResourceDatabase.GetBindlessIndex(meshData.IndexBuffer.AsResource()),
|
||||
};
|
||||
|
||||
var bufferHandle = meshData.ObjectDataBuffer.AsResource();
|
||||
var state = ResourceDatabase.GetResourceState(bufferHandle);
|
||||
var needTransition = state != ResourceState.CopyDest;
|
||||
if (needTransition)
|
||||
{
|
||||
_directCmd.ResourceBarrier(bufferHandle, state, ResourceState.CopyDest);
|
||||
}
|
||||
|
||||
_directCmd.UploadBuffer(meshData.ObjectDataBuffer, [data]);
|
||||
|
||||
if (needTransition)
|
||||
{
|
||||
_directCmd.ResourceBarrier(bufferHandle, ResourceState.CopyDest, ResourceState.VertexAndConstantBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
public Handle<Texture> CreateTexture(ref readonly TextureDesc desc, bool tempResource = false)
|
||||
{
|
||||
return ResourceAllocator.CreateTexture(in desc, tempResource);
|
||||
@@ -126,13 +160,6 @@ public unsafe readonly ref struct RenderingContext
|
||||
var desc = ResourceDatabase.GetResourceDescription(texture.AsResource());
|
||||
desc.TextureDescription.Format.GetSurfaceInfo((int)desc.TextureDescription.Width, (int)desc.TextureDescription.Height, out var rowPitch, out var slicePitch, out _);
|
||||
|
||||
var subresourceData = new SubResourceData
|
||||
{
|
||||
pData = data.GetUnsafePtr(),
|
||||
rowPitch = rowPitch,
|
||||
slicePitch = slicePitch
|
||||
};
|
||||
|
||||
var sateBefore = ResourceDatabase.GetResourceState(texture.AsResource());
|
||||
var needTransition = sateBefore != ResourceState.CopyDest;
|
||||
|
||||
@@ -141,7 +168,17 @@ public unsafe readonly ref struct RenderingContext
|
||||
_directCmd.ResourceBarrier(texture.AsResource(), sateBefore, ResourceState.CopyDest);
|
||||
}
|
||||
|
||||
_directCmd.UploadTexture(texture, subresourceData);
|
||||
fixed (byte* pData = data)
|
||||
{
|
||||
var subresourceData = new SubResourceData
|
||||
{
|
||||
pData = pData,
|
||||
rowPitch = rowPitch,
|
||||
slicePitch = slicePitch
|
||||
};
|
||||
|
||||
_directCmd.UploadTexture(texture, subresourceData);
|
||||
}
|
||||
|
||||
if (needTransition)
|
||||
{
|
||||
@@ -169,9 +206,14 @@ public unsafe readonly ref struct RenderingContext
|
||||
var pipelineKey = hash.GetKey();
|
||||
_directCmd.SetPipelineState(pipelineKey);
|
||||
|
||||
_directCmd.SetConstantBufferView(RootSignatureLayout.PER_OBJECT_BUFFER_SLOT, meshRef.ObjectDataBuffer);
|
||||
|
||||
// NOTE: We use fixed root signature layout for bindless rendering.
|
||||
ref var cache = ref materialRef.GetPassCache(passIndex);
|
||||
_directCmd.SetConstantBufferView(RootSignatureLayout.PER_MATERIAL_BUFFER_SLOT, cache.GpuResource);
|
||||
if (cache.IsCreated)
|
||||
{
|
||||
_directCmd.SetConstantBufferView(RootSignatureLayout.PER_MATERIAL_BUFFER_SLOT, cache.GpuResource);
|
||||
}
|
||||
|
||||
// NOTE: Since we are using true bindless resources, we only need to set the descriptor heaps, not individual tables.
|
||||
// TODO: Maybe handle the traditional bindless model?
|
||||
@@ -180,7 +222,7 @@ public unsafe readonly ref struct RenderingContext
|
||||
_commandList.Get()->SetGraphicsRootDescriptorTable(rootParamIndex, samplerGpuHandle);
|
||||
#endif
|
||||
|
||||
var threadGroupCountX = ((uint)meshRef.indices.Count + numThreadsX - 1) / numThreadsX;
|
||||
_directCmd.DispatchMesh(threadGroupCountX, 1, 1);
|
||||
//var threadGroupCountX = ((uint)meshRef.IndexCount + numThreadsX - 1) / numThreadsX;
|
||||
_directCmd.DispatchMesh(1, 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
/// <summary>
|
||||
@@ -40,4 +43,14 @@ public static class RootSignatureLayout
|
||||
4
|
||||
#endif
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public struct PerObjectData
|
||||
{
|
||||
public float4x4 localToWorld;
|
||||
public float3 worldBoundsMin;
|
||||
public uint vertexBuffer;
|
||||
public float3 worldBoundsMax;
|
||||
public uint indexBuffer;
|
||||
};
|
||||
@@ -19,10 +19,11 @@ public class ShaderPass : IResourceReleasable
|
||||
{
|
||||
_cbufferInfo = info;
|
||||
|
||||
_propertyLookup = new Dictionary<string, int>(info.Properties.Count);
|
||||
for (var i = 0; i < info.Properties.Count; i++)
|
||||
var capacity = info.Properties?.Count ?? 0;
|
||||
_propertyLookup = new Dictionary<string, int>(capacity);
|
||||
for (var i = 0; i < capacity; i++)
|
||||
{
|
||||
_propertyLookup[info.Properties[i].Name] = i;
|
||||
_propertyLookup[info.Properties![i].Name] = i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user