Graphics project renamed, dependencies adjusted
このコミットが含まれているのは:
1 親
de864f2875
コミット
6571390ef3
18ファイル変更
+7
-8
検索条件に一致するファイルはありません
@@ -0,0 +1,18 @@
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
public struct BufferDescription
|
||||
{
|
||||
public BufferDescription(int sizeInBytes, BufferFlags bufferFlags, GraphicsHeapType heapType)
|
||||
{
|
||||
SizeInBytes = sizeInBytes;
|
||||
Flags = bufferFlags;
|
||||
HeapType = heapType;
|
||||
}
|
||||
|
||||
public int SizeInBytes;
|
||||
|
||||
public BufferFlags Flags;
|
||||
|
||||
public GraphicsHeapType HeapType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
[Flags]
|
||||
public enum BufferFlags
|
||||
{
|
||||
None = 0,
|
||||
ConstantBuffer = 1,
|
||||
IndexBuffer = 2,
|
||||
VertexBuffer = 4,
|
||||
RenderTarget = 8,
|
||||
ShaderResource = 16,
|
||||
UnorderedAccess = 32,
|
||||
StructuredBuffer = 64,
|
||||
RawBuffer = 512,
|
||||
ArgumentBuffer = 1024
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics.Buffers.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// An abstract <see langword="class"/> that represents a graphics resource object
|
||||
/// </summary>
|
||||
public abstract class GraphicsResource : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsResource"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
protected GraphicsResource(GraphicsDevice device)
|
||||
{
|
||||
GraphicsDevice = device;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance
|
||||
/// </summary>
|
||||
public GraphicsDevice GraphicsDevice { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IntPtr"/> associated to the currently mapped resource
|
||||
/// </summary>
|
||||
public IntPtr MappedResource { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="Resource"/> object currently mapped
|
||||
/// </summary>
|
||||
protected internal Resource? NativeResource { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="CpuDescriptorHandle"/> instance for the current resource
|
||||
/// </summary>
|
||||
protected internal CpuDescriptorHandle NativeCpuDescriptorHandle { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="GpuDescriptorHandle"/> instance for the current resource
|
||||
/// </summary>
|
||||
protected internal GpuDescriptorHandle NativeGpuDescriptorHandle { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Maps the current resource to a specified subresource
|
||||
/// </summary>
|
||||
/// <param name="subresource">The index of the target subresource to map</param>
|
||||
/// <returns>An <see cref="IntPtr"/> for the newly mapped resource</returns>
|
||||
public IntPtr Map(int subresource)
|
||||
{
|
||||
IntPtr mappedResource = NativeResource?.Map(subresource) ?? throw new InvalidOperationException("Missing resource");
|
||||
MappedResource = mappedResource;
|
||||
return mappedResource;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps a subresource specified by a given index
|
||||
/// </summary>
|
||||
/// <param name="subresource">The index of the subresource to unmap</param>
|
||||
public void Unmap(int subresource)
|
||||
{
|
||||
NativeResource?.Unmap(subresource);
|
||||
MappedResource = IntPtr.Zero;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual void Dispose() => NativeResource?.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics.Buffers
|
||||
{
|
||||
public partial class Buffer
|
||||
{
|
||||
public static class Constant
|
||||
{
|
||||
public static unsafe Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Upload)
|
||||
{
|
||||
return Buffer.New(device, size, BufferFlags.ConstantBuffer, heapType);
|
||||
}
|
||||
|
||||
public static unsafe Buffer<T> New<T>(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Upload) where T : unmanaged
|
||||
{
|
||||
return Buffer.New(device, data, BufferFlags.ConstantBuffer, heapType);
|
||||
}
|
||||
|
||||
public static unsafe Buffer<T> New<T>(GraphicsDevice device, Span<T> data, GraphicsHeapType heapType = GraphicsHeapType.Upload) where T : unmanaged
|
||||
{
|
||||
return Buffer.New(device, data, BufferFlags.ConstantBuffer, heapType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics.Buffers
|
||||
{
|
||||
public partial class Buffer
|
||||
{
|
||||
public static class UnorderedAccess
|
||||
{
|
||||
public static unsafe Buffers.Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Default)
|
||||
{
|
||||
return Buffers.Buffer.New(device, size, BufferFlags.UnorderedAccess, heapType);
|
||||
}
|
||||
|
||||
public static unsafe Buffer<T> New<T>(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
|
||||
{
|
||||
return Buffers.Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType);
|
||||
}
|
||||
|
||||
public static unsafe Buffer<T> New<T>(GraphicsDevice device, Span<T> data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
|
||||
{
|
||||
return Buffers.Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using DirectX12GameEngine.Graphics.Buffers.Abstract;
|
||||
using DirectX12GameEngine.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics.Buffers
|
||||
{
|
||||
public partial class Buffer : GraphicsResource
|
||||
{
|
||||
protected Buffer(GraphicsDevice device) : base(device)
|
||||
{
|
||||
}
|
||||
|
||||
public BufferDescription Description { get; private set; }
|
||||
|
||||
public int SizeInBytes => Description.SizeInBytes;
|
||||
|
||||
public BufferFlags Flags => Description.Flags;
|
||||
|
||||
public GraphicsHeapType Usage => Description.HeapType;
|
||||
|
||||
public int ElementCount { get; protected set; }
|
||||
|
||||
public T[] GetData<T>() where T : unmanaged
|
||||
{
|
||||
T[] data = new T[SizeInBytes / Unsafe.SizeOf<T>()];
|
||||
GetData(data.AsSpan());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public unsafe void GetData<T>(ref T data, int offsetInBytes = 0) where T : unmanaged
|
||||
{
|
||||
fixed (T* pointer = &data)
|
||||
{
|
||||
GetData(new Span<T>(pointer, 1), offsetInBytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void GetData<T>(Span<T> data, int offsetInBytes = 0) where T : unmanaged
|
||||
{
|
||||
if (Usage == GraphicsHeapType.Default)
|
||||
{
|
||||
using Buffer<T> readbackBaffer = New<T>(GraphicsDevice, data.Length, BufferFlags.None, GraphicsHeapType.Readback);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(this, offsetInBytes, readbackBaffer, 0, data.Length * Unsafe.SizeOf<T>());
|
||||
copyCommandList.Flush(true);
|
||||
|
||||
readbackBaffer.GetData(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
Map(0);
|
||||
MemoryHelper.Copy(MappedResource + offsetInBytes, data);
|
||||
Unmap(0);
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe void SetData<T>(in T data, int offsetInBytes = 0) where T : unmanaged
|
||||
{
|
||||
fixed (T* pointer = &data)
|
||||
{
|
||||
SetData(new Span<T>(pointer, 1), offsetInBytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetData<T>(Span<T> data, int offsetInBytes = 0) where T : unmanaged
|
||||
{
|
||||
if (Usage == GraphicsHeapType.Default)
|
||||
{
|
||||
using Buffer<T> uploadBuffer = New(GraphicsDevice, data, BufferFlags.None, GraphicsHeapType.Upload);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, offsetInBytes, data.Length * Unsafe.SizeOf<T>());
|
||||
copyCommandList.Flush(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Map(0);
|
||||
MemoryHelper.Copy(data, MappedResource + offsetInBytes);
|
||||
Unmap(0);
|
||||
}
|
||||
}
|
||||
|
||||
public Buffers.Buffer ToReadback()
|
||||
{
|
||||
BufferDescription description = Description;
|
||||
description.HeapType = GraphicsHeapType.Readback;
|
||||
description.Flags = BufferFlags.None;
|
||||
|
||||
return New(GraphicsDevice, description);
|
||||
}
|
||||
|
||||
public static Buffers.Buffer New(GraphicsDevice device, BufferDescription description)
|
||||
{
|
||||
return new Buffers.Buffer(device).InitializeFrom(description);
|
||||
}
|
||||
|
||||
public static Buffers.Buffer New(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default)
|
||||
{
|
||||
return New(device, new BufferDescription(size, bufferFlags, heapType));
|
||||
}
|
||||
|
||||
public static Buffer<T> New<T>(GraphicsDevice device, int elementCount, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
|
||||
{
|
||||
int bufferSize = elementCount * Unsafe.SizeOf<T>();
|
||||
|
||||
return new Buffer<T>(device, bufferSize, bufferFlags, heapType);
|
||||
}
|
||||
|
||||
public static unsafe Buffer<T> New<T>(GraphicsDevice device, in T data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
|
||||
{
|
||||
fixed (T* pointer = &data)
|
||||
{
|
||||
return New(device, new Span<T>(pointer, 1), bufferFlags, heapType);
|
||||
}
|
||||
}
|
||||
|
||||
public static Buffer<T> New<T>(GraphicsDevice device, Span<T> data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
|
||||
{
|
||||
Buffer<T> buffer = New<T>(device, data.Length, bufferFlags, heapType);
|
||||
buffer.SetData(data);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
protected internal Buffers.Buffer InitializeFrom(BufferDescription description)
|
||||
{
|
||||
ResourceStates resourceStates = ResourceStates.Common;
|
||||
|
||||
if (description.HeapType == GraphicsHeapType.Upload)
|
||||
{
|
||||
resourceStates = ResourceStates.GenericRead;
|
||||
}
|
||||
else if (description.HeapType == GraphicsHeapType.Readback)
|
||||
{
|
||||
resourceStates = ResourceStates.CopyDestination;
|
||||
}
|
||||
|
||||
NativeResource ??= GraphicsDevice.NativeDevice.CreateCommittedResource(
|
||||
new HeapProperties((HeapType)description.HeapType), HeapFlags.None,
|
||||
ConvertToNativeDescription(description), resourceStates);
|
||||
|
||||
Description = description;
|
||||
|
||||
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = description.Flags switch
|
||||
{
|
||||
BufferFlags.ConstantBuffer => CreateConstantBufferView(),
|
||||
BufferFlags.ShaderResource => CreateShaderResourceView(),
|
||||
BufferFlags.UnorderedAccess => CreateUnorderedAccessView(),
|
||||
_ => default
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private static ResourceDescription ConvertToNativeDescription(BufferDescription description)
|
||||
{
|
||||
int size = description.SizeInBytes;
|
||||
ResourceFlags flags = ResourceFlags.None;
|
||||
|
||||
if (description.Flags.HasFlag(BufferFlags.UnorderedAccess))
|
||||
{
|
||||
flags |= ResourceFlags.AllowUnorderedAccess;
|
||||
//flags |= ResourceFlags.AllowSimultaneousAccess;
|
||||
}
|
||||
|
||||
return ResourceDescription.Buffer(size, flags);
|
||||
}
|
||||
|
||||
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView()
|
||||
{
|
||||
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
|
||||
|
||||
int constantBufferSize = (SizeInBytes + 255) & ~255;
|
||||
|
||||
ConstantBufferViewDescription cbvDescription = new ConstantBufferViewDescription
|
||||
{
|
||||
BufferLocation = NativeResource.GPUVirtualAddress,
|
||||
SizeInBytes = constantBufferSize
|
||||
};
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateConstantBufferView(cbvDescription, cpuHandle);
|
||||
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
|
||||
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView()
|
||||
{
|
||||
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
|
||||
GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, null, cpuHandle);
|
||||
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
|
||||
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView()
|
||||
{
|
||||
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
|
||||
|
||||
var description = new UnorderedAccessViewDescription
|
||||
{
|
||||
Format = SharpDX.DXGI.Format.R32_Float,
|
||||
Dimension = UnorderedAccessViewDimension.Buffer,
|
||||
Buffer =
|
||||
{
|
||||
ElementCount = ElementCount,
|
||||
}
|
||||
};
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle);
|
||||
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
}
|
||||
|
||||
public class Buffer<T> : Buffers.Buffer where T : unmanaged
|
||||
{
|
||||
protected internal Buffer(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) : base(device)
|
||||
{
|
||||
ElementSize = Unsafe.SizeOf<T>();
|
||||
ElementCount = size / ElementSize;
|
||||
|
||||
InitializeFrom(new BufferDescription(size, bufferFlags, heapType));
|
||||
}
|
||||
|
||||
public int ElementSize { get; }
|
||||
|
||||
public T[] GetData() => GetData<T>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
internal sealed class CommandAllocatorPool : IDisposable
|
||||
{
|
||||
private readonly Queue<(CommandAllocator, long)> commandAllocatorQueue = new Queue<(CommandAllocator, long)>();
|
||||
|
||||
public CommandAllocatorPool(GraphicsDevice device, CommandListType commandListType)
|
||||
{
|
||||
GraphicsDevice = device;
|
||||
CommandListType = commandListType;
|
||||
}
|
||||
|
||||
public CommandListType CommandListType { get; }
|
||||
|
||||
public GraphicsDevice GraphicsDevice { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (commandAllocatorQueue)
|
||||
{
|
||||
foreach ((CommandAllocator commandAllocator, long _) in commandAllocatorQueue)
|
||||
{
|
||||
commandAllocator.Dispose();
|
||||
}
|
||||
|
||||
commandAllocatorQueue.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void Enqueue(CommandAllocator commandAllocator, long fenceValue)
|
||||
{
|
||||
lock (commandAllocatorQueue)
|
||||
{
|
||||
commandAllocatorQueue.Enqueue((commandAllocator, fenceValue));
|
||||
}
|
||||
}
|
||||
|
||||
public CommandAllocator GetCommandAllocator()
|
||||
{
|
||||
lock (commandAllocatorQueue)
|
||||
{
|
||||
if (commandAllocatorQueue.Count > 0)
|
||||
{
|
||||
(CommandAllocator commandAllocator, long fenceValue) = commandAllocatorQueue.Peek();
|
||||
|
||||
long completedValue = CommandListType switch
|
||||
{
|
||||
CommandListType.Bundle => GraphicsDevice.NativeDirectFence.CompletedValue,
|
||||
CommandListType.Compute => GraphicsDevice.NativeComputeFence.CompletedValue,
|
||||
CommandListType.Copy => GraphicsDevice.NativeCopyFence.CompletedValue,
|
||||
CommandListType.Direct => GraphicsDevice.NativeDirectFence.CompletedValue,
|
||||
_ => throw new NotSupportedException("This command list type is not supported.")
|
||||
};
|
||||
|
||||
if (fenceValue <= completedValue)
|
||||
{
|
||||
commandAllocatorQueue.Dequeue();
|
||||
commandAllocator.Reset();
|
||||
|
||||
return commandAllocator;
|
||||
}
|
||||
}
|
||||
|
||||
return GraphicsDevice.NativeDevice.CreateCommandAllocator((SharpDX.Direct3D12.CommandListType)CommandListType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
using System;
|
||||
using DirectX12GameEngine.Graphics.Buffers.Abstract;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
public sealed class CommandList : IDisposable
|
||||
{
|
||||
private readonly CompiledCommandList currentCommandList;
|
||||
|
||||
public CommandList(GraphicsDevice device, CommandListType commandListType)
|
||||
{
|
||||
GraphicsDevice = device;
|
||||
CommandListType = commandListType;
|
||||
|
||||
CommandAllocator commandAllocator = GetCommandAllocator();
|
||||
|
||||
GraphicsCommandList nativeCommandList = GraphicsDevice.NativeDevice.CreateCommandList((SharpDX.Direct3D12.CommandListType)CommandListType, commandAllocator, null);
|
||||
currentCommandList = new CompiledCommandList(this, commandAllocator, nativeCommandList);
|
||||
|
||||
SetDescriptorHeaps(GraphicsDevice.ShaderResourceViewAllocator.DescriptorHeap);
|
||||
}
|
||||
|
||||
public CommandListType CommandListType { get; }
|
||||
|
||||
public GraphicsDevice GraphicsDevice { get; }
|
||||
|
||||
public CompiledCommandList Close()
|
||||
{
|
||||
//foreach (var renderTarget in RenderTargets)
|
||||
//{
|
||||
// ResourceBarrierTransition(renderTarget, ResourceStates.RenderTarget, ResourceStates.Present);
|
||||
//}
|
||||
|
||||
currentCommandList.NativeCommandList.Close();
|
||||
|
||||
return currentCommandList;
|
||||
}
|
||||
|
||||
public void CopyBufferRegion(GraphicsResource source, long sourceOffset, GraphicsResource destination, long destinationOffset, long numBytes)
|
||||
{
|
||||
currentCommandList.NativeCommandList.CopyBufferRegion(destination.NativeResource, destinationOffset, source.NativeResource, sourceOffset, numBytes);
|
||||
}
|
||||
|
||||
public void CopyResource(GraphicsResource source, GraphicsResource destination)
|
||||
{
|
||||
currentCommandList.NativeCommandList.CopyResource(destination.NativeResource, source.NativeResource);
|
||||
}
|
||||
|
||||
public void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ)
|
||||
{
|
||||
currentCommandList.NativeCommandList.Dispatch(threadGroupCountX, threadGroupCountY, threadGroupCountZ);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
switch (CommandListType)
|
||||
{
|
||||
case CommandListType.Bundle:
|
||||
GraphicsDevice.BundleAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextDirectFenceValue - 1);
|
||||
break;
|
||||
case CommandListType.Direct:
|
||||
GraphicsDevice.DirectAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextDirectFenceValue - 1);
|
||||
break;
|
||||
case CommandListType.Compute:
|
||||
GraphicsDevice.ComputeAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextComputeFenceValue - 1);
|
||||
break;
|
||||
case CommandListType.Copy:
|
||||
GraphicsDevice.CopyAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextCopyFenceValue - 1);
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException("This command list type is not supported.");
|
||||
}
|
||||
|
||||
currentCommandList.NativeCommandList.Dispose();
|
||||
}
|
||||
|
||||
public void Flush(bool wait = false)
|
||||
{
|
||||
GraphicsDevice.ExecuteCommandLists(wait, Close());
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
CommandAllocator commandAllocator = GetCommandAllocator();
|
||||
|
||||
currentCommandList.NativeCommandAllocator = commandAllocator;
|
||||
currentCommandList.NativeCommandList.Reset(currentCommandList.NativeCommandAllocator, null);
|
||||
|
||||
SetDescriptorHeaps(GraphicsDevice.ShaderResourceViewAllocator.DescriptorHeap);
|
||||
}
|
||||
|
||||
public void ResourceBarrierTransition(GraphicsResource resource, ResourceStates stateBefore, ResourceStates stateAfter)
|
||||
{
|
||||
currentCommandList.NativeCommandList.ResourceBarrierTransition(resource.NativeResource, stateBefore, stateAfter);
|
||||
}
|
||||
|
||||
public void SetDescriptorHeaps(params DescriptorHeap[] descriptorHeaps)
|
||||
{
|
||||
if (CommandListType != CommandListType.Copy)
|
||||
{
|
||||
currentCommandList.NativeCommandList.SetDescriptorHeaps(descriptorHeaps);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetGraphicsRoot32BitConstant(int rootParameterIndex, int srcData, int destOffsetIn32BitValues)
|
||||
{
|
||||
currentCommandList.NativeCommandList.SetGraphicsRoot32BitConstant(rootParameterIndex, srcData, destOffsetIn32BitValues);
|
||||
}
|
||||
|
||||
public void SetGraphicsRootDescriptorTable(int rootParameterIndex, GraphicsResource resource)
|
||||
{
|
||||
SetGraphicsRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle);
|
||||
}
|
||||
|
||||
public void SetGraphicsRootDescriptorTable(int rootParameterIndex, GpuDescriptorHandle baseDescriptor)
|
||||
{
|
||||
currentCommandList.NativeCommandList.SetGraphicsRootDescriptorTable(rootParameterIndex, baseDescriptor);
|
||||
}
|
||||
|
||||
public void SetGraphicsRootSignature(RootSignature rootSignature)
|
||||
{
|
||||
currentCommandList.NativeCommandList.SetGraphicsRootSignature(rootSignature);
|
||||
}
|
||||
|
||||
public void SetComputeRoot32BitConstant(int rootParameterIndex, int srcData, int destOffsetIn32BitValues)
|
||||
{
|
||||
currentCommandList.NativeCommandList.SetComputeRoot32BitConstant(rootParameterIndex, srcData, destOffsetIn32BitValues);
|
||||
}
|
||||
|
||||
public void SetComputeRootDescriptorTable(int rootParameterIndex, GraphicsResource resource)
|
||||
{
|
||||
SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle);
|
||||
}
|
||||
|
||||
public void SetComputeRootDescriptorTable(int rootParameterIndex, GpuDescriptorHandle baseDescriptor)
|
||||
{
|
||||
currentCommandList.NativeCommandList.SetComputeRootDescriptorTable(rootParameterIndex, baseDescriptor);
|
||||
}
|
||||
|
||||
public void SetComputeRootSignature(RootSignature rootSignature)
|
||||
{
|
||||
currentCommandList.NativeCommandList.SetComputeRootSignature(rootSignature);
|
||||
}
|
||||
|
||||
public void SetComputeRootUnorderedAccessView(int rootParameterIndex, GraphicsResource resource)
|
||||
{
|
||||
currentCommandList.NativeCommandList.SetComputeRootUnorderedAccessView(rootParameterIndex, resource.NativeResource.GPUVirtualAddress);
|
||||
}
|
||||
|
||||
public void SetPipelineState(PipelineState pipelineState)
|
||||
{
|
||||
SetComputeRootSignature(pipelineState.RootSignature);
|
||||
|
||||
currentCommandList.NativeCommandList.PipelineState = pipelineState.NativePipelineState;
|
||||
}
|
||||
|
||||
private CommandAllocator GetCommandAllocator() => CommandListType switch
|
||||
{
|
||||
CommandListType.Bundle => GraphicsDevice.BundleAllocatorPool.GetCommandAllocator(),
|
||||
CommandListType.Compute => GraphicsDevice.ComputeAllocatorPool.GetCommandAllocator(),
|
||||
CommandListType.Copy => GraphicsDevice.CopyAllocatorPool.GetCommandAllocator(),
|
||||
CommandListType.Direct => GraphicsDevice.DirectAllocatorPool.GetCommandAllocator(),
|
||||
_ => throw new NotSupportedException("This command list type is not supported.")
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
public sealed class CompiledCommandList
|
||||
{
|
||||
internal CompiledCommandList(CommandList builder, CommandAllocator nativeCommandAllocator, GraphicsCommandList nativeCommandList)
|
||||
{
|
||||
Builder = builder;
|
||||
NativeCommandAllocator = nativeCommandAllocator;
|
||||
NativeCommandList = nativeCommandList;
|
||||
}
|
||||
|
||||
internal CommandList Builder { get; set; }
|
||||
|
||||
internal CommandAllocator NativeCommandAllocator { get; set; }
|
||||
|
||||
internal GraphicsCommandList NativeCommandList { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.18362.2002-preview" />
|
||||
<PackageReference Include="SharpDX.Direct3D12" Version="4.2.1-beta0-gab36f12303" />
|
||||
<PackageReference Include="System.Memory" Version="4.5.3" />
|
||||
<PackageReference Include="System.Numerics.Vectors" Version="4.6.0-preview5.19224.8" />
|
||||
<PackageReference Include="System.Runtime.InteropServices.WindowsRuntime" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
internal sealed class DescriptorAllocator : IDisposable
|
||||
{
|
||||
private const int DescriptorsPerHeap = 4096;
|
||||
|
||||
private readonly object allocatorLock = new object();
|
||||
private readonly int descriptorSize;
|
||||
|
||||
private CpuDescriptorHandle currentCpuHandle;
|
||||
private GpuDescriptorHandle currentGpuHandle;
|
||||
private int remainingHandles;
|
||||
|
||||
public DescriptorAllocator(GraphicsDevice device, DescriptorHeapType descriptorHeapType, DescriptorHeapFlags descriptorHeapFlags = DescriptorHeapFlags.None, int descriptorCount = DescriptorsPerHeap)
|
||||
{
|
||||
if (descriptorCount < 1 || descriptorCount > DescriptorsPerHeap)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(descriptorCount), $"Descriptor count must be between 1 and {DescriptorsPerHeap}.");
|
||||
}
|
||||
|
||||
descriptorSize = device.NativeDevice.GetDescriptorHandleIncrementSize(descriptorHeapType);
|
||||
|
||||
DescriptorHeapDescription rtvHeapDescription = new DescriptorHeapDescription
|
||||
{
|
||||
DescriptorCount = descriptorCount,
|
||||
Flags = descriptorHeapFlags,
|
||||
Type = descriptorHeapType
|
||||
};
|
||||
|
||||
DescriptorHeap = device.NativeDevice.CreateDescriptorHeap(rtvHeapDescription);
|
||||
|
||||
remainingHandles = descriptorCount;
|
||||
currentCpuHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart;
|
||||
currentGpuHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart;
|
||||
}
|
||||
|
||||
public DescriptorHeap DescriptorHeap { get; }
|
||||
|
||||
public (CpuDescriptorHandle, GpuDescriptorHandle) Allocate(int count)
|
||||
{
|
||||
if (count < 1 || (count > remainingHandles && remainingHandles != 0))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count), "Count must be between 1 and the remaining handles if the remaining handles are not 0.");
|
||||
}
|
||||
|
||||
lock (allocatorLock)
|
||||
{
|
||||
if (remainingHandles == 0)
|
||||
{
|
||||
remainingHandles = DescriptorHeap.Description.DescriptorCount;
|
||||
currentCpuHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart;
|
||||
currentGpuHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart;
|
||||
}
|
||||
|
||||
CpuDescriptorHandle cpuDescriptorHandle = currentCpuHandle;
|
||||
GpuDescriptorHandle gpuDescriptorHandle = currentGpuHandle;
|
||||
|
||||
currentCpuHandle += descriptorSize * count;
|
||||
currentGpuHandle += descriptorSize * count;
|
||||
remainingHandles -= count;
|
||||
|
||||
return (cpuDescriptorHandle, gpuDescriptorHandle);
|
||||
}
|
||||
}
|
||||
|
||||
public (CpuDescriptorHandle, GpuDescriptorHandle) AllocateSlot(int slot)
|
||||
{
|
||||
if (slot < 0 || slot > DescriptorHeap.Description.DescriptorCount - 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(slot), "Slot must be between 0 and the descript count - 1.");
|
||||
}
|
||||
|
||||
lock (allocatorLock)
|
||||
{
|
||||
CpuDescriptorHandle cpuDescriptorHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart + descriptorSize * slot;
|
||||
GpuDescriptorHandle gpuDescriptorHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart + descriptorSize * slot;
|
||||
|
||||
return (cpuDescriptorHandle, gpuDescriptorHandle);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
DescriptorHeap.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using SharpDX.Direct3D;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
public sealed class GraphicsDevice : IDisposable
|
||||
{
|
||||
private readonly AutoResetEvent fenceEvent = new AutoResetEvent(false);
|
||||
|
||||
public GraphicsDevice(bool enableDebugLayer = false)
|
||||
{
|
||||
#if DEBUG
|
||||
if (enableDebugLayer) DebugInterface.Get().EnableDebugLayer();
|
||||
#endif
|
||||
NativeDevice = new Device(null, FeatureLevel);
|
||||
|
||||
NativeComputeCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Compute));
|
||||
NativeCopyCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Copy));
|
||||
NativeDirectCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct));
|
||||
|
||||
BundleAllocatorPool = new CommandAllocatorPool(this, CommandListType.Bundle);
|
||||
ComputeAllocatorPool = new CommandAllocatorPool(this, CommandListType.Compute);
|
||||
CopyAllocatorPool = new CommandAllocatorPool(this, CommandListType.Copy);
|
||||
DirectAllocatorPool = new CommandAllocatorPool(this, CommandListType.Direct);
|
||||
|
||||
NativeComputeFence = NativeDevice.CreateFence(0, FenceFlags.None);
|
||||
NativeCopyFence = NativeDevice.CreateFence(0, FenceFlags.None);
|
||||
NativeDirectFence = NativeDevice.CreateFence(0, FenceFlags.None);
|
||||
|
||||
DepthStencilViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.DepthStencilView, descriptorCount: 1);
|
||||
RenderTargetViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.RenderTargetView, descriptorCount: 2);
|
||||
ShaderResourceViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, DescriptorHeapFlags.ShaderVisible);
|
||||
|
||||
CommandList = new CommandList(this, CommandListType.Direct);
|
||||
CommandList.Close();
|
||||
|
||||
CopyCommandList = new CommandList(this, CommandListType.Copy);
|
||||
CopyCommandList.Close();
|
||||
}
|
||||
|
||||
public CommandList CommandList { get; }
|
||||
|
||||
public CommandList CopyCommandList { get; }
|
||||
|
||||
public const FeatureLevel FeatureLevel = SharpDX.Direct3D.FeatureLevel.Level_12_1;
|
||||
|
||||
internal Device NativeDevice { get; }
|
||||
|
||||
internal DescriptorAllocator DepthStencilViewAllocator { get; set; }
|
||||
|
||||
internal DescriptorAllocator RenderTargetViewAllocator { get; set; }
|
||||
|
||||
internal DescriptorAllocator ShaderResourceViewAllocator { get; set; }
|
||||
|
||||
internal CommandAllocatorPool BundleAllocatorPool { get; }
|
||||
|
||||
internal CommandAllocatorPool ComputeAllocatorPool { get; }
|
||||
|
||||
internal CommandAllocatorPool CopyAllocatorPool { get; }
|
||||
|
||||
internal CommandAllocatorPool DirectAllocatorPool { get; }
|
||||
|
||||
internal CommandQueue NativeComputeCommandQueue { get; }
|
||||
|
||||
internal CommandQueue NativeCopyCommandQueue { get; }
|
||||
|
||||
internal CommandQueue NativeDirectCommandQueue { get; }
|
||||
|
||||
internal Fence NativeComputeFence { get; }
|
||||
|
||||
internal Fence NativeCopyFence { get; }
|
||||
|
||||
internal Fence NativeDirectFence { get; }
|
||||
|
||||
internal long NextComputeFenceValue { get; private set; } = 1;
|
||||
|
||||
internal long NextCopyFenceValue { get; private set; } = 1;
|
||||
|
||||
internal long NextDirectFenceValue { get; private set; } = 1;
|
||||
|
||||
public RootSignature CreateRootSignature(RootSignatureDescription rootSignatureDescription)
|
||||
{
|
||||
return NativeDevice.CreateRootSignature(rootSignatureDescription.Serialize());
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
NativeDirectCommandQueue.Signal(NativeDirectFence, NextDirectFenceValue);
|
||||
NativeDirectCommandQueue.Wait(NativeDirectFence, NextDirectFenceValue);
|
||||
|
||||
CommandList.Dispose();
|
||||
CopyCommandList.Dispose();
|
||||
|
||||
DepthStencilViewAllocator.Dispose();
|
||||
RenderTargetViewAllocator.Dispose();
|
||||
ShaderResourceViewAllocator.Dispose();
|
||||
|
||||
BundleAllocatorPool.Dispose();
|
||||
ComputeAllocatorPool.Dispose();
|
||||
CopyAllocatorPool.Dispose();
|
||||
DirectAllocatorPool.Dispose();
|
||||
|
||||
NativeComputeCommandQueue.Dispose();
|
||||
NativeCopyCommandQueue.Dispose();
|
||||
NativeDirectCommandQueue.Dispose();
|
||||
|
||||
NativeComputeFence.Dispose();
|
||||
NativeDirectFence.Dispose();
|
||||
NativeDirectFence.Dispose();
|
||||
|
||||
NativeDevice.Dispose();
|
||||
}
|
||||
|
||||
public void ExecuteCommandLists(bool wait, params CompiledCommandList[] commandLists)
|
||||
{
|
||||
Fence fence = commandLists[0].Builder.CommandListType switch
|
||||
{
|
||||
CommandListType.Direct => NativeDirectFence,
|
||||
CommandListType.Compute => NativeComputeFence,
|
||||
CommandListType.Copy => NativeCopyFence,
|
||||
_ => throw new NotSupportedException("This command list type is not supported.")
|
||||
};
|
||||
|
||||
long fenceValue = ExecuteCommandLists(commandLists);
|
||||
|
||||
if (wait)
|
||||
{
|
||||
WaitForFence(fence, fenceValue);
|
||||
}
|
||||
}
|
||||
|
||||
public long ExecuteCommandLists(params CompiledCommandList[] commandLists)
|
||||
{
|
||||
CommandAllocatorPool commandAllocatorPool;
|
||||
CommandQueue commandQueue;
|
||||
Fence fence;
|
||||
long fenceValue;
|
||||
|
||||
switch (commandLists[0].Builder.CommandListType)
|
||||
{
|
||||
case CommandListType.Compute:
|
||||
commandAllocatorPool = ComputeAllocatorPool;
|
||||
commandQueue = NativeComputeCommandQueue;
|
||||
|
||||
fence = NativeComputeFence;
|
||||
fenceValue = NextComputeFenceValue;
|
||||
NextDirectFenceValue++;
|
||||
break;
|
||||
case CommandListType.Copy:
|
||||
commandAllocatorPool = CopyAllocatorPool;
|
||||
commandQueue = NativeCopyCommandQueue;
|
||||
|
||||
fence = NativeCopyFence;
|
||||
fenceValue = NextCopyFenceValue;
|
||||
NextCopyFenceValue++;
|
||||
break;
|
||||
case CommandListType.Direct:
|
||||
commandAllocatorPool = DirectAllocatorPool;
|
||||
commandQueue = NativeDirectCommandQueue;
|
||||
|
||||
fence = NativeDirectFence;
|
||||
fenceValue = NextDirectFenceValue;
|
||||
NextDirectFenceValue++;
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException("This command list type is not supported.");
|
||||
}
|
||||
|
||||
SharpDX.Direct3D12.CommandList[] nativeCommandLists = new SharpDX.Direct3D12.CommandList[commandLists.Length];
|
||||
|
||||
for (int i = 0; i < commandLists.Length; i++)
|
||||
{
|
||||
nativeCommandLists[i] = commandLists[i].NativeCommandList;
|
||||
commandAllocatorPool.Enqueue(commandLists[i].NativeCommandAllocator, fenceValue);
|
||||
}
|
||||
|
||||
commandQueue.ExecuteCommandLists(nativeCommandLists);
|
||||
commandQueue.Signal(fence, fenceValue);
|
||||
|
||||
return fenceValue;
|
||||
}
|
||||
|
||||
internal bool IsFenceComplete(Fence fence, long fenceValue)
|
||||
{
|
||||
return fenceValue <= fence.CompletedValue;
|
||||
}
|
||||
|
||||
internal void WaitForFence(Fence fence, long fenceValue)
|
||||
{
|
||||
if (IsFenceComplete(fence, fenceValue)) return;
|
||||
|
||||
lock (fence)
|
||||
{
|
||||
fence.SetEventOnCompletion(fenceValue, fenceEvent.SafeWaitHandle.DangerousGetHandle());
|
||||
fenceEvent.WaitOne();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
public enum GraphicsHeapType
|
||||
{
|
||||
Default = 1,
|
||||
Upload = 2,
|
||||
Readback = 3,
|
||||
Custom = 4
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with some helper methods to copy data between memory areas
|
||||
/// </summary>
|
||||
public static class MemoryHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Copies the content of a <see cref="Span{T}"/> to the area pointed by an input <see cref="IntPtr"/> value
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of values in the input <see cref="Span{T}"/></typeparam>
|
||||
/// <param name="source">The source <see cref="Span{T}"/> to read</param>
|
||||
/// <param name="destination">The <see cref="IntPtr"/> for the destination memory area</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void Copy<T>(Span<T> source, IntPtr destination) where T : unmanaged
|
||||
{
|
||||
source.CopyTo(new Span<T>(destination.ToPointer(), source.Length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a memory area pointed by an <see cref="IntPtr"/> value to a target <see cref="Span{T}"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of values to read</typeparam>
|
||||
/// <param name="source">The <see cref="IntPtr"/> that indicates the memory area to read from</param>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> to write</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void Copy<T>(IntPtr source, Span<T> destination) where T : unmanaged
|
||||
{
|
||||
new Span<T>(source.ToPointer(), destination.Length).CopyTo(destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace DirectX12GameEngine.Graphics
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a custom pipeline state for a compute operation
|
||||
/// </summary>
|
||||
public sealed class PipelineState : ComputePipelineStateDescription
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="PipelineState"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> to use</param>
|
||||
/// <param name="rootSignature">The <see cref="SharpDX.Direct3D12.RootSignature"/> value for the current shader</param>
|
||||
/// <param name="computeShader">The bytecode for the compute shader to run</param>
|
||||
public PipelineState(GraphicsDevice device, RootSignature rootSignature, ShaderBytecode computeShader)
|
||||
{
|
||||
RootSignaturePointer = rootSignature;
|
||||
ComputeShader = computeShader;
|
||||
RootSignature = RootSignaturePointer;
|
||||
|
||||
NativePipelineState = device.NativeDevice.CreateComputePipelineState(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="SharpDX.Direct3D12.RootSignature"/> value for the current shader
|
||||
/// </summary>
|
||||
public RootSignature RootSignature { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="SharpDX.Direct3D12.PipelineState"/> instance for the current <see cref="PipelineState"/> object
|
||||
/// </summary>
|
||||
public SharpDX.Direct3D12.PipelineState NativePipelineState { get; }
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする