Add mesh shader support to rendering context and fix some bugs.

This commit is contained in:
2025-11-05 09:37:54 +00:00
parent 3bcf0ad539
commit b3eeb8d366
8 changed files with 65 additions and 28 deletions

View File

@@ -25,12 +25,5 @@
<PackageReference Include="System.IO.Hashing" Version="9.0.10" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.2" />
</ItemGroup>
<ItemGroup>
<Reference Include="Misaki.HighPerformance.Unsafe">
<HintPath>..\..\Class\Misaki.HighPerformance\Misaki.HighPerformance.LowLevel\bin\Release\net9.0\Misaki.HighPerformance.LowLevel.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@@ -134,15 +134,17 @@ public unsafe readonly ref struct RenderingContext
_resourceDatabase.SetResourceState(texture.AsResource(), sateBefore);
}
}
#if false
// TODO: Ideally we should queue the draw call to our rendering system, and render it in the full rendering pipeline.
// This is just a place holder for now for testing purpose.
// TODO: Since we are using mesh shader, we should use dispatch mesh instead of draw calls.
public void RenderMesh(Handle<Mesh> mesh, Handle<Material> material, string passName)
{
//_cmd.DrawMesh(mesh, material);
ref var meshRef = ref _resourceDatabase.GetMeshReference(mesh);
ref var materialRef = ref _resourceDatabase.GetMaterialReference(material);
var shader = _resourceDatabase.GetShaderReference(materialRef.Shader);
shader.TryGetPassKey(passName, out var passKey);
shader.TryGetPassKey(passName, out var passIndex, out var passKey);
var hash = new GraphicsPipelineHash
{
id = passKey,
@@ -160,7 +162,7 @@ public unsafe readonly ref struct RenderingContext
// for (int i = 0; i < 4; i++)
{
ref var cache = ref materialRef.GetPassCache((int)passKey.value);
ref var cache = ref materialRef.GetPassCache(passIndex);
_directCmb.SetConstantBufferView(RootSignatureLayout.PER_MATERIAL_BUFFER_SLOT, cache.GpuResource);
}
@@ -177,6 +179,49 @@ public unsafe readonly ref struct RenderingContext
var triangleCount = (uint)meshRef.indices.Count / 3;
_directCmb.Draw(3, triangleCount, 0, 0);
}
#endif
// TODO: Ideally we should queue the draw call to our rendering system, and render it in the full rendering pipeline.
// This is just a place holder for now for testing purpose.
// TODO: Since we are using mesh shader, we should use dispatch mesh instead of draw calls.
public void DispatchMesh(Handle<Mesh> mesh, Handle<Material> material, string passName, uint numThreadsX)
{
ref var meshRef = ref _resourceDatabase.GetMeshReference(mesh);
ref var materialRef = ref _resourceDatabase.GetMaterialReference(material);
var shader = _resourceDatabase.GetShaderReference(materialRef.Shader);
shader.TryGetPassKey(passName, out var passIndex, out var passKey);
var hash = new GraphicsPipelineHash
{
id = passKey,
rtvCount = 1,
dsvFormat = TextureFormat.Unknown,
};
hash.rtvFormats[0] = TextureFormat.B8G8R8A8_UNorm;
var pipelineKey = hash.GetKey();
_directCmb.SetPipelineState(pipelineKey);
// FIX: Get valid root signature. In D3D12, we use fixed root signature layout for bindless rendering.
// However, our code should not assume that blindly. Each pipeline should have contained root signature info even if there are fixed.
// This ensures that future changes to root signature layout can be accommodated.
// for (int i = 0; i < 4; i++)
{
ref var cache = ref materialRef.GetPassCache(passIndex);
_directCmb.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: Matbe handle the transitional bindless model?
#if false
var samplerGpuHandle = _descriptorAllocator.GetSamplerHeap()->GetGPUDescriptorHandleForHeapStart();
_commandList.Get()->SetGraphicsRootDescriptorTable(rootParamIndex, samplerGpuHandle);
#endif
var threadGroupCountX = ((uint)meshRef.indices.Count + numThreadsX - 1) / numThreadsX;
_directCmb.DispatchMesh(threadGroupCountX, 1, 1);
}
public void ExecuteCopyCommands()
{

View File

@@ -96,6 +96,7 @@ public unsafe class ShaderPass : IResourceReleasable
public class Shader : IResourceReleasable, IIdentifierType
{
private UnsafeArray<ShaderPassKey> _passIDs;
// TODO: Optmize lookups with a better data structure if needed
private readonly Dictionary<string, int> _passLookup; // pass name to index
private readonly Dictionary<string, List<int>> _propertyLookup; // property name to pass index (property name to list of pass indices that contain the property)
@@ -137,20 +138,27 @@ public class Shader : IResourceReleasable, IIdentifierType
}
}
public int GetPassIndex(string passName)
{
return _passLookup.GetValueOrDefault(passName, -1);
}
public ShaderPassKey GetPassKey(int index)
{
return _passIDs[index];
}
public bool TryGetPassKey(string passName, out ShaderPassKey passID)
public bool TryGetPassKey(string passName, out int passIndex, out ShaderPassKey passID)
{
var index = _passLookup.GetValueOrDefault(passName, -1);
if (index == -1)
{
passIndex = -1;
passID = new(0);
return false;
}
passIndex = index;
passID = _passIDs[index];
return true;
}

View File

@@ -106,7 +106,7 @@ internal class D3D12ResourceDatabase : IResourceDatabase, IDisposable
private readonly UnsafeSlotMap<Mesh> _meshes;
private readonly UnsafeSlotMap<Material> _materials;
private readonly DynamicArray<Shader?> _shaders; // NOTE: We use a simple list since shader is not frequently added/removed. This can save 4 bytes for each ecs component.
private readonly Dictionary<ShaderPassKey, ShaderPass> _shaderPasses;
private readonly Dictionary<ShaderPassKey, ShaderPass> _shaderPasses; // NOTE: The reason we use Dictionary here is that ShaderPassKey is a presistence identifier across multiple application sessions.
private bool _disposed;

View File

@@ -22,8 +22,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ghost.Core\Ghost.Core.csproj" />
<ProjectReference Include="..\Ghost.Shader\Ghost.SDL.csproj" />
<ProjectReference Include="../Ghost.Core/Ghost.Core.csproj" />
<ProjectReference Include="../Ghost.Shader/Ghost.SDL.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -161,6 +161,7 @@ public interface ICommandBuffer : IDisposable
/// <summary>
/// Dispatches ray tracing threads
/// </summary>
// TODO: This method is not supported yet.
void DispatchRay();
/// <summary>

View File

@@ -1,10 +1,10 @@
using Ghost.Core;
using Ghost.Graphics.Core;
using Ghost.Graphics.Contracts;
using Ghost.Graphics.RHI;
using Ghost.Graphics.Utilities;
using Ghost.SDL.Compiler;
using Misaki.HighPerformance.Image;
using Ghost.Graphics.Core;
namespace Ghost.Graphics.RenderPasses;
@@ -67,7 +67,7 @@ internal unsafe class MeshRenderPass : IRenderPass
public void Execute(ref readonly RenderingContext ctx)
{
ctx.RenderMesh(_mesh, _material, "Forward");
ctx.DispatchMesh(_mesh, _material, "Forward", 8);
}
public void Cleanup(IResourceDatabase resourceDatabase)

View File

@@ -7,17 +7,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Remove="Combinators\**" />
<EmbeddedResource Remove="Combinators\**" />
<None Remove="Combinators\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Misaki.HighPerformance.Mathematics" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ghost.Core\Ghost.Core.csproj" />
<ProjectReference Include="../Ghost.Core/Ghost.Core.csproj" />
</ItemGroup>
</Project>