Added code to create readonly buffers, code refactoring
このコミットが含まれているのは:
1 親
947153f392
コミット
e292d5513f
4ファイル変更
+50
-27
検索条件に一致するファイルはありません
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics.Contracts;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
@@ -17,8 +18,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
|
||||
/// <param name="heapType">The heap type for the current buffer</param>
|
||||
protected internal HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, HeapType heapType) : base(device, size, sizeInBytes, heapType) { }
|
||||
/// <param name="bufferType">The buffer type for the current buffer</param>
|
||||
private protected HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) : base(device, size, sizeInBytes, bufferType) { }
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="HlslBuffer{T}"/> instance and returns an array
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
@@ -17,29 +19,35 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
|
||||
/// <param name="heapType">The heap type for the current buffer</param>
|
||||
protected internal Buffer(GraphicsDevice device, int size, int sizeInBytes, HeapType heapType) : base(device)
|
||||
/// <param name="bufferType">The buffer type for the current buffer</param>
|
||||
internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) : base(device)
|
||||
{
|
||||
Size = size;
|
||||
SizeInBytes = sizeInBytes; // Not necessarily a multiple of the element size, as there could be padding
|
||||
ElementSizeInBytes = Unsafe.SizeOf<T>();
|
||||
HeapType = heapType;
|
||||
BufferType = bufferType;
|
||||
|
||||
ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None;
|
||||
ResourceDescription description = ResourceDescription.Buffer(SizeInBytes, flags);
|
||||
ResourceStates resourceStates = heapType switch
|
||||
// Determine the right heap type and flags
|
||||
(HeapType heapType, ResourceFlags flags, ResourceStates states) = bufferType switch
|
||||
{
|
||||
HeapType.Upload => ResourceStates.GenericRead,
|
||||
HeapType.Readback => ResourceStates.CopyDestination,
|
||||
_ => ResourceStates.Common
|
||||
BufferType.Constant => (HeapType.Upload, ResourceFlags.None, ResourceStates.GenericRead),
|
||||
BufferType.ReadOnly => (HeapType.Default, ResourceFlags.None, ResourceStates.Common),
|
||||
BufferType.ReadWrite => (HeapType.Default, ResourceFlags.AllowUnorderedAccess, ResourceStates.Common),
|
||||
BufferType.ReadBack => (HeapType.Readback, ResourceFlags.None, ResourceStates.CopyDestination),
|
||||
BufferType.Transfer => (HeapType.Upload, ResourceFlags.None, ResourceStates.GenericRead),
|
||||
_ => throw new ArgumentException($"Invalid buffer type {bufferType}", nameof(bufferType))
|
||||
};
|
||||
|
||||
NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates);
|
||||
// Create the native resource
|
||||
ResourceDescription description = ResourceDescription.Buffer(sizeInBytes, flags);
|
||||
NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, states);
|
||||
|
||||
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = heapType switch
|
||||
// Create the resource handles, if needed
|
||||
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = bufferType switch
|
||||
{
|
||||
HeapType.Default => CreateUnorderedAccessView(),
|
||||
HeapType.Upload => CreateConstantBufferView(),
|
||||
BufferType.Constant => CreateConstantBufferView(),
|
||||
BufferType.ReadOnly => CreateShaderResourceView(),
|
||||
BufferType.ReadWrite => CreateUnorderedAccessView(),
|
||||
_ => default
|
||||
};
|
||||
}
|
||||
@@ -60,9 +68,9 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
protected int ElementSizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the heap type being targeted by the current buffer
|
||||
/// Gets the buffer type for the current <see cref="Buffer{T}"/> instance
|
||||
/// </summary>
|
||||
internal HeapType HeapType { get; }
|
||||
internal BufferType BufferType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates the descriptors for a constant buffer
|
||||
@@ -86,6 +94,22 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
|
||||
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView()
|
||||
{
|
||||
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate();
|
||||
|
||||
ShaderResourceViewDescription description = new ShaderResourceViewDescription
|
||||
{
|
||||
Shader4ComponentMapping = 5768,
|
||||
Dimension = ShaderResourceViewDimension.Buffer,
|
||||
Buffer = { ElementCount = Size, StructureByteStride = ElementSizeInBytes }
|
||||
};
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, description, cpuHandle);
|
||||
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the descriptors for a read write buffer
|
||||
/// </summary>
|
||||
@@ -98,11 +122,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
UnorderedAccessViewDescription description = new UnorderedAccessViewDescription
|
||||
{
|
||||
Dimension = UnorderedAccessViewDimension.Buffer,
|
||||
Buffer =
|
||||
{
|
||||
ElementCount = Size,
|
||||
StructureByteStride = ElementSizeInBytes
|
||||
}
|
||||
Buffer = { ElementCount = Size, StructureByteStride = ElementSizeInBytes }
|
||||
};
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
|
||||
@@ -19,7 +20,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
internal ConstantBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf<T>() / 16 + 1) * 16, HeapType.Upload)
|
||||
internal ConstantBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf<T>() / 16 + 1) * 16, BufferType.Constant)
|
||||
{
|
||||
PaddedElementSizeInBytes = SizeInBytes / Size;
|
||||
IsPaddingPresent = PaddedElementSizeInBytes > ElementSizeInBytes;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
|
||||
@@ -18,7 +19,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, size * Unsafe.SizeOf<T>(), HeapType.Default) { }
|
||||
internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, size * Unsafe.SizeOf<T>(), BufferType.ReadWrite) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
@@ -34,7 +35,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// <inheritdoc/>
|
||||
public override void GetData(Span<T> span)
|
||||
{
|
||||
using Buffer<T> readbackBaffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, HeapType.Readback);
|
||||
using Buffer<T> readbackBaffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes);
|
||||
@@ -48,7 +49,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(Span<T> span)
|
||||
{
|
||||
using Buffer<T> uploadBuffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, HeapType.Upload);
|
||||
using Buffer<T> uploadBuffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer);
|
||||
|
||||
uploadBuffer.Map(0);
|
||||
MemoryHelper.Copy(span, uploadBuffer.MappedResource);
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする