Refactor descriptor handling and shader compilation

Refactored descriptor allocation and release logic by introducing `IDescriptorAllocator` and replacing `DescriptorHeapAllocator` with `D3D12DescriptorHeap`. Updated descriptor structs to include validation properties and improved memory management with `ReadOnlySpan`.

Enhanced shader compilation by introducing `ShaderStage` and `CompilerVersion` enums, enabling more flexible and maintainable shader handling.

Refactored `Mesh` to use `IBuffer` for vertex and index buffers, added bindless descriptor support, and improved resource cleanup.

Updated `RenderSystem` and other components for better initialization, error handling, and disposal logic. General improvements to code readability and maintainability.
This commit is contained in:
2025-09-13 20:07:29 +09:00
parent 1dfed83e38
commit 74bb2ccda5
23 changed files with 561 additions and 403 deletions

View File

@@ -117,6 +117,7 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer
throw new NotImplementedException();
}
// TODO: Batch draw calls by material to minimize state changes
public void DrawMesh(Mesh mesh, Material material)
{
// Bind the bindless material (sets up root signature, pipeline state, and descriptor heaps)

View File

@@ -1,5 +1,8 @@
using Ghost.Core;
using Ghost.Graphics.D3D12.Utilities;
using Ghost.Graphics.Data;
using Ghost.Graphics.RHI;
using System.Runtime.CompilerServices;
using Win32.Graphics.Direct3D12;
namespace Ghost.Graphics.D3D12;
@@ -7,13 +10,13 @@ namespace Ghost.Graphics.D3D12;
/// <summary>
/// D3D12 implementation of descriptor allocator that manages different types of descriptor heaps.
/// </summary>
internal unsafe class D3D12DescriptorAllocator : IDisposable
internal unsafe class D3D12DescriptorAllocator : IDescriptorAllocator, IDisposable
{
private readonly DescriptorHeapAllocator _rtvHeap;
private readonly DescriptorHeapAllocator _dsvHeap;
private readonly DescriptorHeapAllocator _srvHeap;
private readonly DescriptorHeapAllocator _samplerHeap;
private readonly BindlessDescriptorHeapAllocator _bindlessHeap;
private readonly D3D12DescriptorHeap _rtvHeap;
private readonly D3D12DescriptorHeap _dsvHeap;
private readonly D3D12DescriptorHeap _srvHeap;
private readonly D3D12DescriptorHeap _samplerHeap;
private readonly BindlessDescriptorHeap _bindlessHeap;
private bool _disposed;
@@ -21,11 +24,11 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
{
var pDevice = device.NativeDevice;
_rtvHeap = new DescriptorHeapAllocator("rtv", pDevice, DescriptorHeapType.Rtv, initialRtvCount);
_dsvHeap = new DescriptorHeapAllocator("dsv", pDevice, DescriptorHeapType.Dsv, initialDsvCount);
_srvHeap = new DescriptorHeapAllocator("srv", pDevice, DescriptorHeapType.CbvSrvUav, initialSrvCount);
_samplerHeap = new DescriptorHeapAllocator("sampler", pDevice, DescriptorHeapType.Sampler, initialSamplerCount);
_bindlessHeap = new BindlessDescriptorHeapAllocator(pDevice, initialBindlessCount);
_rtvHeap = new D3D12DescriptorHeap("rtv", pDevice, DescriptorHeapType.Rtv, initialRtvCount);
_dsvHeap = new D3D12DescriptorHeap("dsv", pDevice, DescriptorHeapType.Dsv, initialDsvCount);
_srvHeap = new D3D12DescriptorHeap("srv", pDevice, DescriptorHeapType.CbvSrvUav, initialSrvCount);
_samplerHeap = new D3D12DescriptorHeap("sampler", pDevice, DescriptorHeapType.Sampler, initialSamplerCount);
_bindlessHeap = new BindlessDescriptorHeap(pDevice, initialBindlessCount);
}
~D3D12DescriptorAllocator()
@@ -45,8 +48,7 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
throw new InvalidOperationException("Failed to allocate RTV descriptor");
}
var cpuHandle = _rtvHeap.GetCpuHandle(index);
return new RenderTargetDescriptor(index, cpuHandle);
return new RenderTargetDescriptor { Index = index };
}
public RenderTargetDescriptor[] AllocateRTVs(uint count)
@@ -63,30 +65,33 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
for (uint i = 0; i < count; i++)
{
var index = baseIndex + i;
var cpuHandle = _rtvHeap.GetCpuHandle(index);
descriptors[i] = new RenderTargetDescriptor(index, cpuHandle);
descriptors[i] = new RenderTargetDescriptor { Index = index };
}
return descriptors;
}
public void ReleaseRTV(RenderTargetDescriptor descriptor)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CpuDescriptorHandle GetCpuHandle(RenderTargetDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (descriptor is RenderTargetDescriptor d3d12Descriptor)
{
_rtvHeap.ReleaseDescriptor(d3d12Descriptor.Index);
}
return _rtvHeap.GetCpuHandle(descriptor.Index);
}
public void ReleaseRTVs(RenderTargetDescriptor[] descriptors)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Release(RenderTargetDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
_rtvHeap.ReleaseDescriptor(descriptor.Index);
}
public void Release(ReadOnlySpan<RenderTargetDescriptor> descriptors)
{
ObjectDisposedException.ThrowIf(_disposed, this);
foreach (var descriptor in descriptors)
{
ReleaseRTV(descriptor);
Release(descriptor);
}
}
@@ -104,8 +109,7 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
throw new InvalidOperationException("Failed to allocate DSV descriptor");
}
var cpuHandle = _dsvHeap.GetCpuHandle(index);
return new DepthStencilDescriptor(index, cpuHandle);
return new DepthStencilDescriptor { Index = index };
}
public DepthStencilDescriptor[] AllocateDSVs(uint count)
@@ -122,30 +126,31 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
for (uint i = 0; i < count; i++)
{
var index = baseIndex + i;
var cpuHandle = _dsvHeap.GetCpuHandle(index);
descriptors[i] = new DepthStencilDescriptor(index, cpuHandle);
descriptors[i] = new DepthStencilDescriptor { Index = index };
}
return descriptors;
}
public void ReleaseDSV(DepthStencilDescriptor descriptor)
public CpuDescriptorHandle GetCpuHandle(DepthStencilDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (descriptor is DepthStencilDescriptor d3d12Descriptor)
{
_dsvHeap.ReleaseDescriptor(d3d12Descriptor.Index);
}
return _dsvHeap.GetCpuHandle(descriptor.Index);
}
public void ReleaseDSVs(DepthStencilDescriptor[] descriptors)
public void Release(DepthStencilDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
_dsvHeap.ReleaseDescriptor(descriptor.Index);
}
public void Release(ReadOnlySpan<DepthStencilDescriptor> descriptors)
{
ObjectDisposedException.ThrowIf(_disposed, this);
foreach (var descriptor in descriptors)
{
ReleaseDSV(descriptor);
Release(descriptor);
}
}
@@ -163,13 +168,8 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
throw new InvalidOperationException("Failed to allocate SRV descriptor");
}
var cpuHandle = _srvHeap.GetCpuHandle(index);
var gpuHandle = _srvHeap.GetGpuHandle(index);
// Copy to shader visible heap
_srvHeap.CopyToShaderVisibleHeap(index);
return new ShaderResourceDescriptor(index, cpuHandle, gpuHandle);
return new ShaderResourceDescriptor { Index = index };
}
public ShaderResourceDescriptor[] AllocateSRVs(uint count)
@@ -186,34 +186,38 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
for (uint i = 0; i < count; i++)
{
var index = baseIndex + i;
var cpuHandle = _srvHeap.GetCpuHandle(index);
var gpuHandle = _srvHeap.GetGpuHandle(index);
descriptors[i] = new ShaderResourceDescriptor(index, cpuHandle, gpuHandle);
descriptors[i] = new ShaderResourceDescriptor { Index = index };
}
// Copy all descriptors to shader visible heap
_srvHeap.CopyToShaderVisibleHeap(baseIndex, count);
return descriptors;
}
public void ReleaseSRV(ShaderResourceDescriptor descriptor)
public CpuDescriptorHandle GetCpuHandle(ShaderResourceDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (descriptor is ShaderResourceDescriptor d3d12Descriptor)
{
_srvHeap.ReleaseDescriptor(d3d12Descriptor.Index);
}
return _srvHeap.GetCpuHandle(descriptor.Index);
}
public void ReleaseSRVs(ShaderResourceDescriptor[] descriptors)
public GpuDescriptorHandle GetGpuHandle(ShaderResourceDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
return _srvHeap.GetGpuHandle(descriptor.Index);
}
public void Release(ShaderResourceDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
_srvHeap.ReleaseDescriptor(descriptor.Index);
}
public void Release(ReadOnlySpan<ShaderResourceDescriptor> descriptors)
{
ObjectDisposedException.ThrowIf(_disposed, this);
foreach (var descriptor in descriptors)
{
ReleaseSRV(descriptor);
Release(descriptor);
}
}
@@ -231,13 +235,8 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
throw new InvalidOperationException("Failed to allocate Sampler descriptor");
}
var cpuHandle = _samplerHeap.GetCpuHandle(index);
var gpuHandle = _samplerHeap.GetGpuHandle(index);
// Copy to shader visible heap
_samplerHeap.CopyToShaderVisibleHeap(index);
return new SamplerDescriptor(index, cpuHandle, gpuHandle);
return new SamplerDescriptor { Index = index };
}
public SamplerDescriptor[] AllocateSamplers(uint count)
@@ -254,34 +253,38 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
for (uint i = 0; i < count; i++)
{
var index = baseIndex + i;
var cpuHandle = _samplerHeap.GetCpuHandle(index);
var gpuHandle = _samplerHeap.GetGpuHandle(index);
descriptors[i] = new SamplerDescriptor(index, cpuHandle, gpuHandle);
descriptors[i] = new SamplerDescriptor { Index = index };
}
// Copy all descriptors to shader visible heap
_samplerHeap.CopyToShaderVisibleHeap(baseIndex, count);
return descriptors;
}
public void ReleaseSampler(SamplerDescriptor descriptor)
public CpuDescriptorHandle GetCpuHandle(SamplerDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (descriptor is SamplerDescriptor d3d12Descriptor)
{
_samplerHeap.ReleaseDescriptor(d3d12Descriptor.Index);
}
return _samplerHeap.GetCpuHandle(descriptor.Index);
}
public void ReleaseSamplers(SamplerDescriptor[] descriptors)
public GpuDescriptorHandle GetGpuHandle(SamplerDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
return _samplerHeap.GetGpuHandle(descriptor.Index);
}
public void Release(SamplerDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
_samplerHeap.ReleaseDescriptor(descriptor.Index);
}
public void Release(ReadOnlySpan<SamplerDescriptor> descriptors)
{
ObjectDisposedException.ThrowIf(_disposed, this);
foreach (var descriptor in descriptors)
{
ReleaseSampler(descriptor);
Release(descriptor);
}
}
@@ -303,10 +306,7 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
throw new InvalidOperationException("Failed to allocate bindless descriptor");
}
var cpuHandle = _bindlessHeap.GetCpuHandle(index);
var gpuHandle = _bindlessHeap.GetGpuHandle(index);
return new BindlessDescriptor(index, cpuHandle, gpuHandle);
return new BindlessDescriptor { Index = index };
}
/// <summary>
@@ -326,37 +326,43 @@ internal unsafe class D3D12DescriptorAllocator : IDisposable
for (uint i = 0; i < count; i++)
{
var index = baseIndex + i;
var cpuHandle = _bindlessHeap.GetCpuHandle(index);
var gpuHandle = _bindlessHeap.GetGpuHandle(index);
descriptors[i] = new BindlessDescriptor(index, cpuHandle, gpuHandle);
descriptors[i] = new BindlessDescriptor { Index = index };
}
return descriptors;
}
public CpuDescriptorHandle GetCpuHandle(BindlessDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
return _bindlessHeap.GetCpuHandle(descriptor.Index);
}
public GpuDescriptorHandle GetGpuHandle(BindlessDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
return _bindlessHeap.GetGpuHandle(descriptor.Index);
}
/// <summary>
/// Releases a bindless descriptor.
/// </summary>
public void ReleaseBindless(BindlessDescriptor descriptor)
public void Release(BindlessDescriptor descriptor)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (descriptor is BindlessDescriptor d3d12Descriptor)
{
_bindlessHeap.ReleaseDescriptor(d3d12Descriptor.Index);
}
_bindlessHeap.ReleaseDescriptor(descriptor.Index);
}
/// <summary>
/// Releases multiple bindless descriptors.
/// </summary>
public void ReleaseBindless(BindlessDescriptor[] descriptors)
public void Release(ReadOnlySpan<BindlessDescriptor> descriptors)
{
ObjectDisposedException.ThrowIf(_disposed, this);
foreach (var descriptor in descriptors)
{
ReleaseBindless(descriptor);
Release(descriptor);
}
}

View File

@@ -9,16 +9,16 @@ internal unsafe class D3D12GraphicsEngine : IGraphicsEngine
#endif
private readonly D3D12RenderDevice _device;
private readonly D3D12PipelineStateController _stateController;
private readonly D3D12DescriptorAllocator _descriptorAllocator;
private readonly D3D12ResourceAllocator _resourceAllocator;
private readonly D3D12PipelineStateController _pipelineState;
private readonly D3D12DescriptorAllocator _descriptorAllocator;
private readonly D3D12PipelineStateController _stateController;
public IRenderDevice Device => _device;
public IPipelineStateController PipelineStateController => _stateController;
public IResourceAllocator ResourceAllocator => _resourceAllocator;
public IPipelineStateController PipelineStateController => _stateController;
public D3D12GraphicsEngine(RenderSystem renderSystem)
{
#if DEBUG
@@ -26,11 +26,10 @@ internal unsafe class D3D12GraphicsEngine : IGraphicsEngine
#endif
_device = new();
_stateController = new(_device);
_resourceAllocator = new(_device, renderSystem);
_pipelineState = new(_device);
_descriptorAllocator = new(_device);
_resourceAllocator = new(renderSystem, _device, _descriptorAllocator);
_stateController = new(_device);
}
public IRenderer CreateRenderer()
@@ -50,6 +49,7 @@ internal unsafe class D3D12GraphicsEngine : IGraphicsEngine
public void Dispose()
{
_stateController.Dispose();
_descriptorAllocator.Dispose();
_resourceAllocator.Dispose();
_device.Dispose();

View File

@@ -25,10 +25,6 @@ internal class D3D12ShaderPipeline : IShaderPipeline
internal unsafe class D3D12PipelineStateController : IPipelineStateController, IDisposable
{
private const string _VS_ENTRY_POINT = "VSMain";
private const string _PS_ENTRY_POINT = "PSMain";
private const string _PROFILE_VS_6_6 = "vs_6_6";
private readonly ID3D12Device14* _device;
private readonly Dictionary<Shader, D3D12ShaderPipeline> _shaderPipelines;
@@ -55,8 +51,8 @@ internal unsafe class D3D12PipelineStateController : IPipelineStateController, I
{
foreach (var kvp in _shaderPipelines)
{
var vsResult = D3D12ShaderCompiler.CompileDXC(kvp.Key, _VS_ENTRY_POINT, _PROFILE_VS_6_6);
var psResult = D3D12ShaderCompiler.CompileDXC(kvp.Key, _PS_ENTRY_POINT, _PROFILE_VS_6_6);
var vsResult = D3D12ShaderCompiler.Compile(kvp.Key, D3D12ShaderCompiler.ShaderStage.VertexShader, D3D12ShaderCompiler.CompilerVersion.SM_6_6);
var psResult = D3D12ShaderCompiler.Compile(kvp.Key, D3D12ShaderCompiler.ShaderStage.PixelShader, D3D12ShaderCompiler.CompilerVersion.SM_6_6);
kvp.Value.vsResult = vsResult;
kvp.Value.psResult = psResult;

View File

@@ -7,7 +7,6 @@ using Win32.Graphics.Direct3D12;
using Win32.Graphics.Dxgi;
using Win32.Graphics.Dxgi.Common;
using static Win32.Graphics.D3D12MemoryAllocator.Apis;
using ResourceHandle = Ghost.Graphics.Data.ResourceHandle;
namespace Ghost.Graphics.D3D12;
@@ -43,8 +42,12 @@ internal unsafe class D3D12ResourceAllocator : IResourceAllocator<ID3D12Resource
private const uint _MAX_TEXTURE2D_DIMENSION = 16384u;
private const uint _MAX_TEXTURE3D_DIMENSION = 2048u;
private readonly RenderSystem _renderSystem;
private readonly ID3D12Device14* _device;
private readonly Allocator _allocator;
private readonly RenderSystem _renderSystem;
private readonly D3D12DescriptorAllocator _descriptorAllocator;
private UnsafeList<AllocationInfo> _allocations = new(64, Misaki.HighPerformance.LowLevel.Buffer.Allocator.Persistent);
private UnsafeQueue<int> _freeSlots = new(64, Misaki.HighPerformance.LowLevel.Buffer.Allocator.Persistent);
private UnsafeQueue<ResourceHandle> _temResources = new(64, Misaki.HighPerformance.LowLevel.Buffer.Allocator.Persistent);
@@ -60,10 +63,8 @@ internal unsafe class D3D12ResourceAllocator : IResourceAllocator<ID3D12Resource
}
}
public D3D12ResourceAllocator(D3D12RenderDevice device, RenderSystem renderSystem)
public D3D12ResourceAllocator(RenderSystem renderSystem, D3D12RenderDevice device, D3D12DescriptorAllocator descriptorAllocator)
{
_renderSystem = renderSystem;
var desc = new AllocatorDesc
{
pAdapter = (IDXGIAdapter*)device.Adapter,
@@ -72,6 +73,10 @@ internal unsafe class D3D12ResourceAllocator : IResourceAllocator<ID3D12Resource
};
CreateAllocator(in desc, out _allocator);
_device = device.NativeDevice;
_renderSystem = renderSystem;
_descriptorAllocator = descriptorAllocator;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -173,13 +178,55 @@ internal unsafe class D3D12ResourceAllocator : IResourceAllocator<ID3D12Resource
Allocation allocation = default;
ThrowIfFailed(_allocator.CreateResource(&allocationDesc, in resourceDescription, initialState, null, &allocation, IID_NULL, null));
return new(TrackResource(in allocation, tempResource));
var handle = TrackResource(in allocation, tempResource);
if (desc.Usage.HasFlag(BufferUsage.ShaderResource) && desc.CreationFlags.HasFlag(BufferCreationFlags.Bindless))
{
var isRaw = desc.Usage.HasFlag(BufferUsage.Raw);
var descriptorHandle = _descriptorAllocator.AllocateBindless();
var srvDesc = new ShaderResourceViewDescription
{
ViewDimension = SrvDimension.Buffer,
Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING
};
if (isRaw)
{
srvDesc.Format = Format.R32Typeless;
srvDesc.Buffer.FirstElement = 0;
srvDesc.Buffer.NumElements = (uint)(desc.Size / 4);
srvDesc.Buffer.StructureByteStride = 0;
srvDesc.Buffer.Flags = BufferSrvFlags.Raw;
}
else
{
srvDesc.Format = Format.Unknown;
srvDesc.Buffer.FirstElement = 0;
srvDesc.Buffer.NumElements = (uint)(desc.Size / desc.Stride);
srvDesc.Buffer.StructureByteStride = desc.Stride;
srvDesc.Buffer.Flags = BufferSrvFlags.None;
}
_device->CreateShaderResourceView(allocation.Resource, &srvDesc, _descriptorAllocator.GetCpuHandle(descriptorHandle));
return new(handle, descriptorHandle);
}
return new(handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public BufferHandle CreateUploadBuffer(uint sizeInBytes, bool tempResource = false)
{
var desc = new BufferDesc(sizeInBytes, BufferUsage.Upload, MemoryType.Upload);
var desc = new BufferDesc
{
Size = sizeInBytes,
Usage = BufferUsage.Upload,
MemoryType = MemoryType.Upload
};
return CreateBufferHandle(in desc, tempResource);
}

View File

@@ -11,6 +11,20 @@ namespace Ghost.Graphics.D3D12;
internal unsafe static class D3D12ShaderCompiler
{
public enum CompilerVersion
{
SM_6_6,
SM_7_0
}
public enum ShaderStage
{
VertexShader,
PixelShader,
MeshShader,
ComputeShader
}
public struct CompileResult : IDisposable
{
public UnsafeArray<byte> bytecode;
@@ -23,7 +37,35 @@ internal unsafe static class D3D12ShaderCompiler
}
}
public static CompileResult CompileDXC(Shader shader, string entryPoint, string profile)
private static string GetProfileString(ShaderStage stage, CompilerVersion version)
{
return (stage, version) switch
{
(ShaderStage.VertexShader, CompilerVersion.SM_6_6) => "vs_6_6",
(ShaderStage.PixelShader, CompilerVersion.SM_6_6) => "ps_6_6",
(ShaderStage.MeshShader, CompilerVersion.SM_6_6) => "ms_6_6",
(ShaderStage.ComputeShader, CompilerVersion.SM_6_6) => "cs_6_6",
(ShaderStage.VertexShader, CompilerVersion.SM_7_0) => "vs_7_0",
(ShaderStage.PixelShader, CompilerVersion.SM_7_0) => "ps_7_0",
(ShaderStage.MeshShader, CompilerVersion.SM_7_0) => "ms_7_0",
(ShaderStage.ComputeShader, CompilerVersion.SM_7_0) => "cs_7_0",
_ => throw new ArgumentOutOfRangeException(nameof(stage), "Unsupported shader stage or compiler version")
};
}
private static string GetEntryPoint(ShaderStage stage)
{
return stage switch
{
ShaderStage.VertexShader => "VSMain",
ShaderStage.PixelShader => "PSMain",
ShaderStage.MeshShader => "MSMain",
ShaderStage.ComputeShader => "CSMain",
_ => throw new ArgumentOutOfRangeException(nameof(stage), "Unsupported shader stage")
};
}
public static CompileResult Compile(Shader shader, ShaderStage stage, CompilerVersion version)
{
using ComPtr<IDxcCompiler3> compiler = default;
using ComPtr<IDxcUtils> utils = default;
@@ -43,12 +85,12 @@ internal unsafe static class D3D12ShaderCompiler
// Prepare compilation arguments - NOTE: NO -Qstrip_reflect to keep reflection data
var argsArray = new string[]
{
"-T", profile, // Target profile (vs_6_6, ps_6_6)
"-E", entryPoint, // Entry point
"-HV", "2021", // HLSL version 2021 (required for SM 6.6)
"-enable-16bit-types", // Enable 16-bit types
"-O3", // Optimization level
"-Qstrip_debug" // Strip debug info but KEEP reflection
"-T", GetProfileString(stage, version), // Target profile (vs_6_6, ps_6_6)
"-E", GetEntryPoint(stage), // Entry point
"-HV", "2021", // HLSL version 2021 (required for SM 6.6)
"-enable-16bit-types", // Enable 16-bit types
"-O3", // Optimization level
"-Qstrip_debug" // Strip debug info but KEEP reflection
};
// Convert to wide strings (DXC expects LPCWSTR)

View File

@@ -1,132 +0,0 @@
using Win32.Graphics.Direct3D12;
namespace Ghost.Graphics.D3D12;
/// <summary>
/// Implementation of render target view (RTV) descriptor.
/// </summary>
public readonly struct RenderTargetDescriptor
{
public uint Index
{
get;
}
public CpuDescriptorHandle CpuHandle
{
get;
}
public RenderTargetDescriptor(uint index, CpuDescriptorHandle cpuHandle)
{
Index = index;
CpuHandle = cpuHandle;
}
}
/// <summary>
/// Implementation of depth stencil view (DSV) descriptor.
/// </summary>
public readonly struct DepthStencilDescriptor
{
public uint Index
{
get;
}
public CpuDescriptorHandle CpuHandle
{
get;
}
public DepthStencilDescriptor(uint index, CpuDescriptorHandle cpuHandle)
{
Index = index;
CpuHandle = cpuHandle;
}
}
/// <summary>
/// Implementation of shader resource view (SRV) descriptor.
/// </summary>
public sealed class ShaderResourceDescriptor
{
public uint Index
{
get;
}
public CpuDescriptorHandle CpuHandle
{
get;
}
public GpuDescriptorHandle GpuHandle
{
get;
}
public ShaderResourceDescriptor(uint index, CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle)
{
Index = index;
CpuHandle = cpuHandle;
GpuHandle = gpuHandle;
}
}
/// <summary>
/// Implementation of sampler descriptor.
/// </summary>
public sealed class SamplerDescriptor
{
public uint Index
{
get;
}
public CpuDescriptorHandle CpuHandle
{
get;
}
public GpuDescriptorHandle GpuHandle
{
get;
}
public SamplerDescriptor(uint index, CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle)
{
Index = index;
CpuHandle = cpuHandle;
GpuHandle = gpuHandle;
}
}
/// <summary>
/// Implementation of bindless descriptor for SM 6.6 rendering.
/// This descriptor maintains a 1:1 relationship between allocation indices and shader indices.
/// </summary>
public sealed class BindlessDescriptor
{
public uint Index
{
get;
}
public CpuDescriptorHandle CpuHandle
{
get;
}
public GpuDescriptorHandle GpuHandle
{
get;
}
public BindlessDescriptor(uint index, CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle)
{
Index = index;
CpuHandle = cpuHandle;
GpuHandle = gpuHandle;
}
}

View File

@@ -64,7 +64,7 @@ internal unsafe class Renderer
commandAllocator.Dispose();
commandList.Dispose();
backBuffer.Dispose();
GraphicsPipeline.DescriptorAllocator.ReleaseRTV(rtvDescriptor);
GraphicsPipeline.DescriptorAllocator.Release(rtvDescriptor);
}
}
@@ -226,7 +226,7 @@ internal unsafe class Renderer
if (frameResource.backBuffer.Get() is not null)
{
var c = frameResource.backBuffer.Reset();
GraphicsPipeline.DescriptorAllocator.ReleaseRTV(frameResource.rtvDescriptor);
GraphicsPipeline.DescriptorAllocator.Release(frameResource.rtvDescriptor);
}
frameResource.fenceValue = _frameResources[_backBufferIndex].fenceValue;

View File

@@ -10,7 +10,7 @@ namespace Ghost.Graphics.D3D12.Utilities;
/// Specialized descriptor heap allocator for SM 6.6 bindless rendering with ResourceDescriptorHeap[index].
/// This allocator maintains a 1:1 relationship between allocation indices and shader indices.
/// </summary>
internal unsafe struct BindlessDescriptorHeapAllocator : IDisposable
internal unsafe struct BindlessDescriptorHeap : IDisposable
{
private const DescriptorIndex _INVALID_DESCRIPTOR_INDEX = ~0u;
@@ -42,7 +42,7 @@ internal unsafe struct BindlessDescriptorHeapAllocator : IDisposable
public readonly ConstPtr<ID3D12DescriptorHeap> BindlessHeap => new(_bindlessHeap.Get());
public BindlessDescriptorHeapAllocator(ComPtr<ID3D12Device14> device, uint numDescriptors = 10000)
public BindlessDescriptorHeap(ComPtr<ID3D12Device14> device, uint numDescriptors = 10000)
{
_device = device;
device.Get()->AddRef();
@@ -67,7 +67,6 @@ internal unsafe struct BindlessDescriptorHeapAllocator : IDisposable
// Try to grow the heap
if (!Grow(NumDescriptors * 2))
{
Debug.WriteLine("ERROR: Failed to grow bindless descriptor heap!");
return _INVALID_DESCRIPTOR_INDEX;
}
}
@@ -88,7 +87,6 @@ internal unsafe struct BindlessDescriptorHeapAllocator : IDisposable
var newSize = Math.Max(NumDescriptors * 2, NumDescriptors + count);
if (!Grow(newSize))
{
Debug.WriteLine("ERROR: Failed to grow bindless descriptor heap!");
return _INVALID_DESCRIPTOR_INDEX;
}
}
@@ -110,7 +108,6 @@ internal unsafe struct BindlessDescriptorHeapAllocator : IDisposable
{
if (index >= NumDescriptors)
{
Debug.WriteLine("Error: Attempted to release an invalid descriptor index");
return;
}
@@ -128,7 +125,6 @@ internal unsafe struct BindlessDescriptorHeapAllocator : IDisposable
var index = baseIndex + i;
if (index >= NumDescriptors)
{
Debug.WriteLine("Error: Attempted to release an invalid descriptor index");
continue;
}
@@ -139,19 +135,19 @@ internal unsafe struct BindlessDescriptorHeapAllocator : IDisposable
}
}
public CpuDescriptorHandle GetCpuHandle(DescriptorIndex index)
public readonly CpuDescriptorHandle GetCpuHandle(DescriptorIndex index)
{
var handle = _startCpuHandle;
return handle.Offset((int)index, _stride);
}
public GpuDescriptorHandle GetGpuHandle(DescriptorIndex index)
public readonly GpuDescriptorHandle GetGpuHandle(DescriptorIndex index)
{
var handle = _startGpuHandle;
return handle.Offset((int)index, _stride);
}
public GpuDescriptorHandle GetGpuHandleStart()
public readonly GpuDescriptorHandle GetGpuHandleStart()
{
return _startGpuHandle;
}

View File

@@ -6,11 +6,11 @@ using DescriptorIndex = System.UInt32;
namespace Ghost.Graphics.D3D12.Utilities;
internal unsafe struct DescriptorHeapAllocator : IDisposable
internal unsafe struct D3D12DescriptorHeap : IDisposable
{
private const DescriptorIndex _INVALID_DESCRIPTOR_INDEX = ~0u;
private ComPtr<ID3D12Device14> _device;
private readonly ID3D12Device14* _pDevice;
private ComPtr<ID3D12DescriptorHeap> _heap;
private ComPtr<ID3D12DescriptorHeap> _shaderVisibleHeap;
@@ -50,15 +50,14 @@ internal unsafe struct DescriptorHeapAllocator : IDisposable
public readonly ID3D12DescriptorHeap* Heap => _heap.Get();
public readonly ID3D12DescriptorHeap* ShaderVisibleHeap => _shaderVisibleHeap.Get();
public DescriptorHeapAllocator(string name, ComPtr<ID3D12Device14> device, DescriptorHeapType type, uint numDescriptors)
public D3D12DescriptorHeap(string name, ID3D12Device14* device, DescriptorHeapType type, uint numDescriptors)
{
_device = device;
device.Get()->AddRef();
_pDevice = device;
HeapType = type;
NumDescriptors = numDescriptors;
ShaderVisible = type == DescriptorHeapType.CbvSrvUav || type == DescriptorHeapType.Sampler;
Stride = device.Get()->GetDescriptorHandleIncrementSize(type);
Stride = device->GetDescriptorHandleIncrementSize(type);
var success = AllocateResources(numDescriptors);
Debug.Assert(success);
@@ -154,27 +153,27 @@ internal unsafe struct DescriptorHeapAllocator : IDisposable
}
}
public CpuDescriptorHandle GetCpuHandle(DescriptorIndex index)
public readonly CpuDescriptorHandle GetCpuHandle(DescriptorIndex index)
{
var handle = _startCpuHandle;
return handle.Offset((int)index, Stride);
}
public CpuDescriptorHandle GetCpuHandleShaderVisible(DescriptorIndex index)
public readonly CpuDescriptorHandle GetCpuHandleShaderVisible(DescriptorIndex index)
{
var handle = _startCpuHandleShaderVisible;
return handle.Offset((int)index, Stride);
}
public GpuDescriptorHandle GetGpuHandle(DescriptorIndex index)
public readonly GpuDescriptorHandle GetGpuHandle(DescriptorIndex index)
{
var handle = _startGpuHandleShaderVisible;
return handle.Offset((int)index, Stride);
}
public void CopyToShaderVisibleHeap(DescriptorIndex index, uint count = 1)
public readonly void CopyToShaderVisibleHeap(DescriptorIndex index, uint count = 1)
{
_device.Get()->CopyDescriptorsSimple(count, GetCpuHandleShaderVisible(index), GetCpuHandle(index), HeapType);
_pDevice->CopyDescriptorsSimple(count, GetCpuHandleShaderVisible(index), GetCpuHandle(index), HeapType);
}
private bool AllocateResources(uint numDescriptors)
@@ -193,7 +192,7 @@ internal unsafe struct DescriptorHeapAllocator : IDisposable
fixed (void* heapPtr = &_heap)
{
var hr = _device.Get()->CreateDescriptorHeap(&heapDesc, __uuidof<ID3D12DescriptorHeap>(), (void**)heapPtr);
var hr = _pDevice->CreateDescriptorHeap(&heapDesc, __uuidof<ID3D12DescriptorHeap>(), (void**)heapPtr);
if (hr.Failure)
{
return false;
@@ -209,7 +208,7 @@ internal unsafe struct DescriptorHeapAllocator : IDisposable
fixed (void* heapPtr = &_shaderVisibleHeap)
{
var hr = _device.Get()->CreateDescriptorHeap(&heapDesc, __uuidof<ID3D12DescriptorHeap>(), (void**)heapPtr);
var hr = _pDevice->CreateDescriptorHeap(&heapDesc, __uuidof<ID3D12DescriptorHeap>(), (void**)heapPtr);
if (hr.Failure)
{
return false;
@@ -235,11 +234,11 @@ internal unsafe struct DescriptorHeapAllocator : IDisposable
return false;
}
_device.Get()->CopyDescriptorsSimple(oldSize, _startCpuHandle, oldHeap.Get()->GetCPUDescriptorHandleForHeapStart(), HeapType);
_pDevice->CopyDescriptorsSimple(oldSize, _startCpuHandle, oldHeap.Get()->GetCPUDescriptorHandleForHeapStart(), HeapType);
if (_shaderVisibleHeap.Get() is not null)
{
_device.Get()->CopyDescriptorsSimple(oldSize, _startCpuHandleShaderVisible, oldHeap.Get()->GetCPUDescriptorHandleForHeapStart(), HeapType);
_pDevice->CopyDescriptorsSimple(oldSize, _startCpuHandleShaderVisible, oldHeap.Get()->GetCPUDescriptorHandleForHeapStart(), HeapType);
}
return true;
@@ -248,8 +247,6 @@ internal unsafe struct DescriptorHeapAllocator : IDisposable
/// <inheritdoc />
public void Dispose()
{
_device.Dispose();
_heap.Dispose();
_shaderVisibleHeap.Dispose();
}