Code updates from the DX12GameEngine repo

See https://github.com/Aminator9000/DirectX12GameEngine/commit/a9ede9996504736a8d55f2b6d42e27ae82aaadc3.
このコミットが含まれているのは:
Sergio Pedri committed 2019-08-11 00:47:51 +02:00
1 親 ad23f8764d
コミット 05b9f22cea
5ファイル変更
+79 -90

検索条件に一致するファイルはありません

+4 -13
ファイルの表示
@@ -64,7 +64,7 @@ namespace DirectX12ComputeShaderSample
array[i] = i + 1;
}
using Buffer<float> inputBuffer = Buffer.New(device, array.AsSpan(), BufferFlags.UnorderedAccess, GraphicsHeapType.Default);
using Buffer<float> gpuBuffer = Buffer.UnorderedAccess.New(device, array.AsSpan());
// Execute computer shader
@@ -73,28 +73,18 @@ namespace DirectX12ComputeShaderSample
commandList.SetPipelineState(pipelineState);
commandList.SetComputeRoot32BitConstant(0, width, 0);
commandList.SetComputeRootDescriptorTable(1, inputBuffer);
commandList.SetComputeRootDescriptorTable(1, gpuBuffer);
commandList.Dispatch(2, 2, 1);
commandList.Flush(true);
}
// Copy data back to CPU
using Buffer<float> outputBuffer = Buffer.New<float>(device, array.Length, BufferFlags.None, GraphicsHeapType.Readback);
using (CommandList copyCommandList = new CommandList(device, CommandListType.Copy))
{
copyCommandList.CopyResource(inputBuffer, outputBuffer);
copyCommandList.Flush(true);
}
// Print matrix
Console.WriteLine("Before:");
PrintMatrix(array, width, height);
outputBuffer.GetData(array.AsSpan());
gpuBuffer.GetData(array.AsSpan());
Console.WriteLine();
Console.WriteLine("After:");
@@ -118,3 +108,4 @@ namespace DirectX12ComputeShaderSample
}
}
}
+25
ファイルの表示
@@ -0,0 +1,25 @@
using System;
namespace DirectX12GameEngine.Graphics
{
public partial class Buffer
{
public static class UnorderedAccess
{
public static unsafe Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Default)
{
return 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 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 Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType);
}
}
}
}
+43 -39
ファイルの表示
@@ -33,39 +33,53 @@ namespace DirectX12GameEngine.Graphics
return data;
}
public void GetData<T>(ref T data) where T : unmanaged
public unsafe void GetData<T>(ref T data, int offsetInBytes = 0) where T : unmanaged
{
if (Usage != GraphicsHeapType.Default)
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, ref data);
MemoryHelper.Copy(MappedResource + offsetInBytes, data);
Unmap(0);
}
}
public void GetData<T>(Span<T> data) where T : unmanaged
public unsafe void SetData<T>(in T data, int offsetInBytes = 0) where T : unmanaged
{
if (Usage != GraphicsHeapType.Default)
fixed (T* pointer = &data)
{
Map(0);
MemoryHelper.Copy(MappedResource, data);
Unmap(0);
}
}
public void SetData<T>(in T data, int offsetInBytes = 0) where T : unmanaged
{
if (Usage != GraphicsHeapType.Default)
{
Map(0);
MemoryHelper.Copy(data, MappedResource + offsetInBytes);
Unmap(0);
SetData(new Span<T>(pointer, 1), offsetInBytes);
}
}
public void SetData<T>(Span<T> data, int offsetInBytes = 0) where T : unmanaged
{
if (Usage != GraphicsHeapType.Default)
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);
@@ -73,6 +87,15 @@ namespace DirectX12GameEngine.Graphics
}
}
public Buffer ToReadback()
{
BufferDescription description = Description;
description.HeapType = GraphicsHeapType.Readback;
description.Flags = BufferFlags.None;
return New(GraphicsDevice, description);
}
public static Buffer New(GraphicsDevice device, BufferDescription description)
{
return new Buffer(device).InitializeFrom(description);
@@ -101,7 +124,7 @@ namespace DirectX12GameEngine.Graphics
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.Recreate(data);
buffer.SetData(data);
return buffer;
}
@@ -136,25 +159,6 @@ namespace DirectX12GameEngine.Graphics
return this;
}
protected internal void Recreate<T>(Span<T> data) where T : unmanaged
{
if (Description.HeapType == GraphicsHeapType.Upload)
{
SetData(data);
}
else
{
using Buffer uploadBuffer = New(GraphicsDevice, data, BufferFlags.None, GraphicsHeapType.Upload);
CommandList copyCommandList = GraphicsDevice.GetOrCreateCopyCommandList();
copyCommandList.CopyResource(uploadBuffer, this);
copyCommandList.Flush(true);
GraphicsDevice.EnqueueCopyCommandList(copyCommandList);
}
}
private static ResourceDescription ConvertToNativeDescription(BufferDescription description)
{
int size = description.SizeInBytes;
+6 -35
ファイルの表示
@@ -44,18 +44,21 @@ namespace DirectX12GameEngine.Graphics
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 ICollection<IDisposable> Disposables { get; } = new List<IDisposable>();
public FeatureLevel FeatureLevel { get; }
public GraphicsPresenter? Presenter { get; set; }
internal Queue<CommandList> CopyCommandLists { get; } = new Queue<CommandList>();
internal Device NativeDevice { get; }
internal SharpDX.Direct3D11.Device NativeDirect3D11Device => NativeDirect3D11Device ?? (nativeDirect3D11Device = SharpDX.Direct3D11.Device.CreateFromDirect3D12(
@@ -141,6 +144,7 @@ namespace DirectX12GameEngine.Graphics
NativeDirectCommandQueue.Wait(NativeDirectFence, NextDirectFenceValue);
CommandList.Dispose();
CopyCommandList.Dispose();
DepthStencilViewAllocator.Dispose();
RenderTargetViewAllocator.Dispose();
@@ -159,11 +163,6 @@ namespace DirectX12GameEngine.Graphics
NativeDirectFence.Dispose();
NativeDirectFence.Dispose();
foreach (CommandList commandList in CopyCommandLists)
{
commandList.Dispose();
}
foreach (IDisposable disposable in Disposables)
{
disposable.Dispose();
@@ -243,34 +242,6 @@ namespace DirectX12GameEngine.Graphics
return fenceValue;
}
public CommandList GetOrCreateCopyCommandList()
{
CommandList commandList;
lock (CopyCommandLists)
{
if (CopyCommandLists.Count > 0)
{
commandList = CopyCommandLists.Dequeue();
commandList.Reset();
}
else
{
commandList = new CommandList(this, CommandListType.Copy);
}
}
return commandList;
}
public void EnqueueCopyCommandList(CommandList commandList)
{
lock (CopyCommandLists)
{
CopyCommandLists.Enqueue(commandList);
}
}
internal bool IsFenceComplete(Fence fence, long fenceValue)
{
return fenceValue <= fence.CompletedValue;
+1 -3
ファイルの表示
@@ -65,12 +65,10 @@ namespace DirectX12GameEngine.Graphics
textureUploadBuffer.NativeResource.WriteToSubresource(0, null, (IntPtr)pointer, ((Format)Description.Format).SizeOfInBytes() * Width, data.Length * Unsafe.SizeOf<T>());
}
CommandList copyCommandList = GraphicsDevice.GetOrCreateCopyCommandList();
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyResource(textureUploadBuffer, this);
copyCommandList.Flush(true);
GraphicsDevice.EnqueueCopyCommandList(copyCommandList);
}
public Texture InitializeFrom(TextureDescription description)