forked from Misaki/GhostEngine
Add mesh shader support to rendering context and fix some bugs.
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user