Code refactoring
このコミットが含まれているのは:
1 親
3977272bbe
コミット
7e4d5dff53
5ファイル変更
+82
-94
検索条件に一致するファイルはありません
@@ -120,7 +120,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
copyCommandList.Flush();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
copyCommandList.Flush();
|
||||
|
||||
readbackBaffer.Map(0);
|
||||
MemoryHelper.Copy(readbackBaffer.MappedResource, span);
|
||||
@@ -58,7 +58,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
copyCommandList.Flush();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -70,7 +70,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
copyCommandList.Flush();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
|
||||
@@ -87,13 +87,12 @@ namespace ComputeSharp.Graphics.Commands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the pending operations and optionally waits for them to be completed
|
||||
/// Executes the pending operations and waits for them to be completed
|
||||
/// </summary>
|
||||
/// <param name="wait">Indicates whether or not to wait for the completion of the scheduled operations</param>
|
||||
public void Flush(bool wait = false)
|
||||
public void Flush()
|
||||
{
|
||||
Close();
|
||||
GraphicsDevice.ExecuteCommandLists(wait, this);
|
||||
GraphicsDevice.ExecuteCommandList(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -103,6 +103,80 @@ namespace ComputeSharp.Graphics
|
||||
return NativeDevice.CreateRootSignature(rootSignatureDescription.Serialize());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a <see cref="CommandList"/> and waits for the GPU to finish processing it
|
||||
/// </summary>
|
||||
/// <param name="commandList">The input <see cref="CommandList"/> to execute</param>
|
||||
internal void ExecuteCommandList(CommandList commandList)
|
||||
{
|
||||
Fence fence = commandList.CommandListType switch
|
||||
{
|
||||
CommandListType.Direct => NativeDirectFence,
|
||||
CommandListType.Compute => NativeComputeFence,
|
||||
CommandListType.Copy => NativeCopyFence,
|
||||
_ => throw new NotSupportedException("This command list type is not supported.")
|
||||
};
|
||||
|
||||
long fenceValue = GetFenceValueForCommandList(commandList);
|
||||
|
||||
if (fenceValue <= fence.CompletedValue) return;
|
||||
|
||||
lock (fence)
|
||||
{
|
||||
fence.SetEventOnCompletion(fenceValue, fenceEvent.SafeWaitHandle.DangerousGetHandle());
|
||||
fenceEvent.WaitOne();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a <see cref="CommandList"/> and, signals and retrieves the following fence value
|
||||
/// </summary>
|
||||
/// <param name="commandList">The input <see cref="CommandList"/> to execute</param>
|
||||
/// <returns>The value of the new fence to wait for</returns>
|
||||
private long GetFenceValueForCommandList(CommandList commandList)
|
||||
{
|
||||
CommandAllocatorPool commandAllocatorPool;
|
||||
CommandQueue commandQueue;
|
||||
Fence fence;
|
||||
long fenceValue;
|
||||
|
||||
switch (commandList.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($"Unsupported command list of type {commandList.CommandListType}");
|
||||
}
|
||||
|
||||
commandAllocatorPool.Enqueue(commandList.CommandAllocator, fenceValue);
|
||||
commandQueue.ExecuteCommandLists(commandList.NativeCommandList);
|
||||
commandQueue.Signal(fence, fenceValue);
|
||||
|
||||
return fenceValue;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
NativeDirectCommandQueue.Signal(NativeDirectFence, NextDirectFenceValue);
|
||||
@@ -130,90 +204,5 @@ namespace ComputeSharp.Graphics
|
||||
|
||||
NativeDevice.Dispose();
|
||||
}
|
||||
|
||||
internal void ExecuteCommandLists(bool wait, params CommandList[] commandLists)
|
||||
{
|
||||
Fence fence = commandLists[0].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);
|
||||
}
|
||||
}
|
||||
|
||||
internal long ExecuteCommandLists(params CommandList[] commandLists)
|
||||
{
|
||||
CommandAllocatorPool commandAllocatorPool;
|
||||
CommandQueue commandQueue;
|
||||
Fence fence;
|
||||
long fenceValue;
|
||||
|
||||
switch (commandLists[0].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].CommandAllocator, 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,7 @@ namespace ComputeSharp.Shaders
|
||||
|
||||
// Dispatch and wait for completion
|
||||
commandList.Dispatch(groupsX, groupsY, groupsZ);
|
||||
commandList.Flush(true);
|
||||
commandList.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする