Removed Vortice.Windows references and files
このコミットが含まれているのは:
1 親
b67af4aaa1
コミット
4811769ebc
46ファイル変更
+1124
-3358
検索条件に一致するファイルはありません
@@ -1,89 +1,85 @@
|
||||
using System;
|
||||
using ComputeSharp.Core.Interop;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Vortice.Direct3D12;
|
||||
using TerraFX.Interop;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// An abstract <see langword="class"/> that represents a graphics resource object
|
||||
/// An abstract <see langword="class"/> that represents a graphics resource object.
|
||||
/// </summary>
|
||||
public abstract class GraphicsResource : IDisposable
|
||||
public abstract unsafe class GraphicsResource : NativeObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsResource"/> instance with the specified parameters
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
protected GraphicsResource(GraphicsDevice device)
|
||||
protected internal readonly ID3D12Resource* D3D12Resource;
|
||||
|
||||
/// <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, ID3D12Resource* d3d12resource)
|
||||
{
|
||||
D3D12Resource = d3d12resource;
|
||||
|
||||
GraphicsDevice = device;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance
|
||||
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance.
|
||||
/// </summary>
|
||||
public GraphicsDevice GraphicsDevice { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ID3D12Resource"/> object currently mapped
|
||||
/// Maps the current resource to a specified subresource.
|
||||
/// </summary>
|
||||
protected internal ID3D12Resource? 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>
|
||||
/// <returns>A <see cref="MappedResource"/> instance representing the mapped resource</returns>
|
||||
/// <returns>A <see cref="MappedResource"/> instance representing the mapped resource.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal MappedResource MapResource()
|
||||
{
|
||||
var resource = NativeResource ?? throw new InvalidOperationException("Missing resource");
|
||||
|
||||
return new MappedResource(resource);
|
||||
return new MappedResource(D3D12Resource);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual void Dispose() => NativeResource?.Dispose();
|
||||
protected override void OnDispose()
|
||||
{
|
||||
D3D12Resource->Release();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A type representing a mapped memory resource
|
||||
/// A type representing a mapped memory resource.
|
||||
/// </summary>
|
||||
internal readonly ref struct MappedResource
|
||||
{
|
||||
/// <summary>
|
||||
/// The target <see cref="ID3D12Resource"/> to map
|
||||
/// The target <see cref="ID3D12Resource"/> to map.
|
||||
/// </summary>
|
||||
private readonly ID3D12Resource Resource;
|
||||
private readonly ID3D12Resource* d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// The pointer to the mapped resource
|
||||
/// The pointer to the mapped resource.
|
||||
/// </summary>
|
||||
public readonly IntPtr Pointer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="MappedResource"/> instance for a given <see cref="ID3D12Resource"/> value
|
||||
/// Creates a new <see cref="MappedResource"/> instance for a given <see cref="ID3D12Resource"/> value.
|
||||
/// </summary>
|
||||
/// <param name="resource">The input <see cref="ID3D12Resource"/> instance to map</param>
|
||||
/// <param name="resource">The input <see cref="ID3D12Resource"/> instance to map.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MappedResource(ID3D12Resource resource)
|
||||
public MappedResource(ID3D12Resource* d3d12resource)
|
||||
{
|
||||
Resource = resource;
|
||||
Pointer = resource.Map(0);
|
||||
d3D12Resource = d3d12resource;
|
||||
|
||||
Pointer = d3d12resource->Map();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDisposable.Dispose"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
Resource.Unmap(0);
|
||||
d3D12Resource->Unmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
using ComputeSharp.Core.Interop;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// An abstract <see langword="class"/> that represents a graphics resource object.
|
||||
/// </summary>
|
||||
public abstract unsafe class GraphicsResource2 : NativeObject
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
/// </summary>
|
||||
protected internal readonly ID3D12Resource* D3D12Resource;
|
||||
|
||||
/// <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 GraphicsResource2(GraphicsDevice2 device, ID3D12Resource* d3d12resource)
|
||||
{
|
||||
D3D12Resource = d3d12resource;
|
||||
|
||||
GraphicsDevice = device;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance.
|
||||
/// </summary>
|
||||
public GraphicsDevice2 GraphicsDevice { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Maps the current resource to a specified subresource.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="MappedResource"/> instance representing the mapped resource.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal MappedResource MapResource()
|
||||
{
|
||||
return new MappedResource(D3D12Resource);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
{
|
||||
D3D12Resource->Release();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A type representing a mapped memory resource.
|
||||
/// </summary>
|
||||
internal readonly ref struct MappedResource
|
||||
{
|
||||
/// <summary>
|
||||
/// The target <see cref="ID3D12Resource"/> to map.
|
||||
/// </summary>
|
||||
private readonly ID3D12Resource* d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// The pointer to the mapped resource.
|
||||
/// </summary>
|
||||
public readonly IntPtr Pointer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="MappedResource"/> instance for a given <see cref="ID3D12Resource"/> value.
|
||||
/// </summary>
|
||||
/// <param name="resource">The input <see cref="ID3D12Resource"/> instance to map.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MappedResource(ID3D12Resource* d3d12resource)
|
||||
{
|
||||
d3D12Resource = d3d12resource;
|
||||
|
||||
Pointer = d3d12resource->Map();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDisposable.Dispose"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
d3D12Resource->Unmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics.Contracts;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory, that can be used from an HLSL shader
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public abstract class HlslBuffer2<T> : Buffer2<T> where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslBuffer2{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> 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="bufferType">The buffer type for the current buffer</param>
|
||||
private protected HlslBuffer2(GraphicsDevice2 device, int size, int sizeInBytes, BufferType bufferType)
|
||||
: base(device, size, sizeInBytes, bufferType)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="HlslBuffer2{T}"/> instance and returns an array
|
||||
/// </summary>
|
||||
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer</returns>
|
||||
[Pure]
|
||||
public T[] GetData() => GetData(0, Size);
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="HlslBuffer2{T}"/> instance in a given range and returns an array
|
||||
/// </summary>
|
||||
/// <param name="offset">The offset to start reading data from</param>
|
||||
/// <param name="count">The number of items to read</param>
|
||||
/// <returns>A <typeparamref name="T"/> array with the contents of the specified range from the current buffer</returns>
|
||||
[Pure]
|
||||
public T[] GetData(int offset, int count)
|
||||
{
|
||||
T[] data = new T[count];
|
||||
GetData(data, offset, count);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="HlslBuffer2{T}"/> instance and writes them into a target <see cref="Span{T}"/>
|
||||
/// </summary>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> to write data to</param>
|
||||
public void GetData(Span<T> span) => GetData(span, 0, Size);
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the specified range from the current <see cref="HlslBuffer2{T}"/> instance and writes them into a target <see cref="Span{T}"/>
|
||||
/// </summary>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> to write data to</param>
|
||||
/// <param name="offset">The offset to start reading data from</param>
|
||||
/// <param name="count">The number of items to read</param>
|
||||
public abstract void GetData(Span<T> span, int offset, int count);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="HlslBuffer2{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array to read data from</param>
|
||||
public void SetData(T[] array) => SetData(array.AsSpan());
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="HlslBuffer2{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array to read data from</param>
|
||||
/// <param name="offset">The offset to start writing data to</param>
|
||||
/// <param name="count">The number of items to write</param>
|
||||
public void SetData(T[] array, int offset, int count) => SetData(array.AsSpan(), offset, count);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to the current <see cref="HlslBuffer2{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="span">The input <see cref="ReadOnlySpan{T}"/> to read data from</param>
|
||||
public void SetData(ReadOnlySpan<T> span) => SetData(span, 0, Size);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="HlslBuffer2{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="span">The input <see cref="ReadOnlySpan{T}"/> to read data from</param>
|
||||
/// <param name="offset">The offset to start writing data to</param>
|
||||
/// <param name="count">The number of items to write</param>
|
||||
public abstract void SetData(ReadOnlySpan<T> span, int offset, int count);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="HlslBuffer2{T}"/> to the current <see cref="HlslBuffer2{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer2{T}"/> to read data from</param>
|
||||
public abstract void SetData(HlslBuffer2<T> buffer);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="HlslBuffer2{T}"/> to the current <see cref="HlslBuffer2{T}"/> instance, using a temporary CPU buffer
|
||||
/// </summary>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer2{T}"/> to read data from</param>
|
||||
protected void SetDataWithCpuBuffer(HlslBuffer2<T> buffer)
|
||||
{
|
||||
// Create a temporary array
|
||||
T[] array = ArrayPool<T>.Shared.Rent(buffer.Size);
|
||||
Span<T> span = array.AsSpan(0, buffer.Size);
|
||||
|
||||
// Get the unpadded data from the read write buffer
|
||||
buffer.GetData(span);
|
||||
|
||||
// Set the data, adding the padding if needed
|
||||
SetData(span);
|
||||
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
/// <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="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) { }
|
||||
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,74 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed structured buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public abstract class HlslStructuredBuffer2<T> : HlslBuffer2<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslStructuredBuffer2{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
/// <param name="bufferType">The buffer type for the current buffer</param>
|
||||
internal HlslStructuredBuffer2(GraphicsDevice2 device, int size, BufferType bufferType)
|
||||
: base(device, size, size * Unsafe.SizeOf<T>(), bufferType)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void GetData(Span<T> span, int offset, int count)
|
||||
{
|
||||
using Buffer2<T> transferBuffer = new Buffer2<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.ReadBack);
|
||||
|
||||
using (CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
|
||||
{
|
||||
copyCommandList.CopyBufferRegion(D3D12Resource, offset * ElementSizeInBytes, transferBuffer.D3D12Resource, 0, count * ElementSizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
using MappedResource resource = transferBuffer.MapResource();
|
||||
|
||||
MemoryHelper.Copy(resource.Pointer, 0, span, 0, count);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(ReadOnlySpan<T> span, int offset, int count)
|
||||
{
|
||||
using Buffer2<T> transferBuffer = new Buffer2<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer);
|
||||
|
||||
using (MappedResource resource = transferBuffer.MapResource())
|
||||
{
|
||||
MemoryHelper.Copy(span, resource.Pointer, 0, count);
|
||||
}
|
||||
|
||||
using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.CopyBufferRegion(transferBuffer.D3D12Resource, 0, D3D12Resource, offset * ElementSizeInBytes, count * ElementSizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(HlslBuffer2<T> buffer)
|
||||
{
|
||||
if (!buffer.IsPaddingPresent)
|
||||
{
|
||||
// Directly copy the input buffer
|
||||
using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using Vortice.Direct3D12;
|
||||
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
|
||||
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
@@ -20,16 +20,19 @@ 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="bufferType">The buffer type for the current buffer</param>
|
||||
internal HlslStructuredBuffer(GraphicsDevice device, int size, BufferType bufferType) : base(device, size, size * Unsafe.SizeOf<T>(), bufferType) { }
|
||||
internal HlslStructuredBuffer(GraphicsDevice device, int size, BufferType bufferType)
|
||||
: base(device, size, size * Unsafe.SizeOf<T>(), bufferType)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void GetData(Span<T> span, int offset, int count)
|
||||
public override unsafe void GetData(Span<T> span, int offset, int count)
|
||||
{
|
||||
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.ReadBack);
|
||||
|
||||
using (CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy))
|
||||
using (CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
|
||||
{
|
||||
copyCommandList.CopyBufferRegion(this, offset * ElementSizeInBytes, transferBuffer, 0, count * ElementSizeInBytes);
|
||||
copyCommandList.CopyBufferRegion(D3D12Resource, offset * ElementSizeInBytes, transferBuffer.D3D12Resource, 0, count * ElementSizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
@@ -39,7 +42,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(ReadOnlySpan<T> span, int offset, int count)
|
||||
public override unsafe void SetData(ReadOnlySpan<T> span, int offset, int count)
|
||||
{
|
||||
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer);
|
||||
|
||||
@@ -48,21 +51,21 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
MemoryHelper.Copy(span, resource.Pointer, 0, count);
|
||||
}
|
||||
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.CopyBufferRegion(transferBuffer, 0, this, offset * ElementSizeInBytes, count * ElementSizeInBytes);
|
||||
copyCommandList.CopyBufferRegion(transferBuffer.D3D12Resource, 0, D3D12Resource, offset * ElementSizeInBytes, count * ElementSizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(HlslBuffer<T> buffer)
|
||||
public override unsafe void SetData(HlslBuffer<T> buffer)
|
||||
{
|
||||
if (!buffer.IsPaddingPresent)
|
||||
{
|
||||
// Directly copy the input buffer
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using TerraFX.Interop;
|
||||
using static TerraFX.Interop.D3D12_HEAP_TYPE;
|
||||
using static TerraFX.Interop.D3D12_RESOURCE_FLAGS;
|
||||
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
|
||||
using static TerraFX.Interop.D3D12_SRV_DIMENSION;
|
||||
using static TerraFX.Interop.D3D12_UAV_DIMENSION;
|
||||
using FX = TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
|
||||
public unsafe class Buffer2<T> : GraphicsResource2
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
|
||||
/// </summary>
|
||||
protected internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
|
||||
/// </summary>
|
||||
protected internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the current buffer.
|
||||
/// </summary>
|
||||
protected readonly int SizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of each <typeparamref name="T"/> value contained in the buffer.
|
||||
/// </summary>
|
||||
protected readonly int ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the current buffer.
|
||||
/// </summary>
|
||||
internal readonly int PaddedElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The buffer type for the current <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
internal readonly BufferType BufferType;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters
|
||||
/// </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>
|
||||
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
|
||||
/// <param name="bufferType">The buffer type for the current buffer</param>
|
||||
internal Buffer2(GraphicsDevice2 device, int size, int sizeInBytes, BufferType bufferType)
|
||||
: base(device, CreateCommittedResource(device.D3D12Device, bufferType, sizeInBytes))
|
||||
{
|
||||
Size = size;
|
||||
SizeInBytes = sizeInBytes;
|
||||
ElementSizeInBytes = Unsafe.SizeOf<T>();
|
||||
BufferType = bufferType;
|
||||
PaddedElementSizeInBytes = sizeInBytes / size;
|
||||
|
||||
if (bufferType is BufferType.Constant or BufferType.ReadOnly or BufferType.ReadWrite)
|
||||
{
|
||||
GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
|
||||
switch (bufferType)
|
||||
{
|
||||
case BufferType.Constant: CreateConstantBufferView(); break;
|
||||
case BufferType.ReadOnly: CreateShaderResourceView(); break;
|
||||
case BufferType.ReadWrite: CreateUnorderedAccessView(); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
|
||||
/// </summary>
|
||||
public int Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not there is some padding between elements in the current buffer
|
||||
/// </summary>
|
||||
internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a committed resource for a given buffer type.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
|
||||
/// <param name="bufferType">The buffer type currently in use.</param>
|
||||
/// <param name="sizeInBytes">The size in bytes of the current buffer.</param>
|
||||
/// <returns>An <see cref="ID3D12Resource"/> reference for the current buffer.</returns>
|
||||
private static ID3D12Resource* CreateCommittedResource(ID3D12Device* d3D12Device, BufferType bufferType, int sizeInBytes)
|
||||
{
|
||||
(D3D12_HEAP_TYPE d3D12HeapType,
|
||||
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStates) = bufferType switch
|
||||
{
|
||||
BufferType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
|
||||
BufferType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
|
||||
BufferType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
|
||||
BufferType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
|
||||
BufferType.Transfer => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
|
||||
_ => throw null!
|
||||
};
|
||||
|
||||
return d3D12Device->CreateCommittedResource(d3D12HeapType, (uint)sizeInBytes, d3D12ResourceFlags, d3D12ResourceStates);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a constant buffer.
|
||||
/// </summary>
|
||||
private void CreateConstantBufferView()
|
||||
{
|
||||
uint constantBufferSize = (uint)((SizeInBytes + 255) & ~255);
|
||||
|
||||
D3D12_CONSTANT_BUFFER_VIEW_DESC d3D12ConstantBufferViewDescription;
|
||||
d3D12ConstantBufferViewDescription.BufferLocation = D3D12Resource->GetGPUVirtualAddress();
|
||||
d3D12ConstantBufferViewDescription.SizeInBytes = constantBufferSize;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateConstantBufferView(&d3D12ConstantBufferViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a readonly buffer.
|
||||
/// </summary>
|
||||
private void CreateShaderResourceView()
|
||||
{
|
||||
D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default;
|
||||
d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
|
||||
d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
||||
d3D12ShaderResourceViewDescription.Buffer.NumElements = (uint)Size;
|
||||
d3D12ShaderResourceViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateShaderResourceView(D3D12Resource, &d3D12ShaderResourceViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a buffer than be both read and written to.
|
||||
/// </summary>
|
||||
private void CreateUnorderedAccessView()
|
||||
{
|
||||
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
|
||||
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
|
||||
d3D12UnorderedAccessViewDescription.Buffer.NumElements = (uint)Size;
|
||||
d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,54 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using SharpGen.Runtime;
|
||||
using Vortice.Direct3D12;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using TerraFX.Interop;
|
||||
using static TerraFX.Interop.D3D12_HEAP_TYPE;
|
||||
using static TerraFX.Interop.D3D12_RESOURCE_FLAGS;
|
||||
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
|
||||
using static TerraFX.Interop.D3D12_SRV_DIMENSION;
|
||||
using static TerraFX.Interop.D3D12_UAV_DIMENSION;
|
||||
using FX = TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public class Buffer<T> : GraphicsResource where T : unmanaged
|
||||
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
|
||||
public unsafe class Buffer<T> : GraphicsResource
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
|
||||
/// </summary>
|
||||
protected internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
|
||||
/// </summary>
|
||||
protected internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the current buffer.
|
||||
/// </summary>
|
||||
protected readonly int SizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of each <typeparamref name="T"/> value contained in the buffer.
|
||||
/// </summary>
|
||||
protected readonly int ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the current buffer.
|
||||
/// </summary>
|
||||
internal readonly int PaddedElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The buffer type for the current <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
internal readonly BufferType BufferType;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
@@ -22,44 +56,26 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// <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="bufferType">The buffer type for the current buffer</param>
|
||||
internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) : base(device)
|
||||
internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType)
|
||||
: base(device, CreateCommittedResource(device.D3D12Device, bufferType, sizeInBytes))
|
||||
{
|
||||
Size = size;
|
||||
SizeInBytes = sizeInBytes; // Not necessarily a multiple of the element size, as there could be padding
|
||||
SizeInBytes = sizeInBytes;
|
||||
ElementSizeInBytes = Unsafe.SizeOf<T>();
|
||||
BufferType = bufferType;
|
||||
PaddedElementSizeInBytes = sizeInBytes / size;
|
||||
|
||||
// Determine the right heap type and flags
|
||||
(HeapType heapType, ResourceFlags flags, ResourceStates states) = bufferType switch
|
||||
if (bufferType is BufferType.Constant or BufferType.ReadOnly or BufferType.ReadWrite)
|
||||
{
|
||||
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))
|
||||
};
|
||||
GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
|
||||
// Create the native resource
|
||||
ResourceDescription description = ResourceDescription.Buffer(sizeInBytes, flags);
|
||||
Result result = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, states, out ID3D12Resource? resource);
|
||||
|
||||
if (result.Failure)
|
||||
{
|
||||
throw new COMException("Failed to create the committed resource", result.Code);
|
||||
switch (bufferType)
|
||||
{
|
||||
case BufferType.Constant: CreateConstantBufferView(); break;
|
||||
case BufferType.ReadOnly: CreateShaderResourceView(); break;
|
||||
case BufferType.ReadWrite: CreateUnorderedAccessView(); break;
|
||||
}
|
||||
}
|
||||
|
||||
NativeResource = resource!;
|
||||
|
||||
// Create the resource handles, if needed
|
||||
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = bufferType switch
|
||||
{
|
||||
BufferType.Constant => CreateConstantBufferView(),
|
||||
BufferType.ReadOnly => CreateShaderResourceView(),
|
||||
BufferType.ReadWrite => CreateUnorderedAccessView(),
|
||||
_ => default
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -67,87 +83,74 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// </summary>
|
||||
public int Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of the current buffer
|
||||
/// </summary>
|
||||
protected int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of each <typeparamref name="T"/> value contained in the buffer
|
||||
/// </summary>
|
||||
protected int ElementSizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of the current buffer
|
||||
/// </summary>
|
||||
internal int PaddedElementSizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not there is some padding between elements in the current buffer
|
||||
/// </summary>
|
||||
internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the buffer type for the current <see cref="Buffer{T}"/> instance
|
||||
/// Creates a committed resource for a given buffer type.
|
||||
/// </summary>
|
||||
internal BufferType BufferType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates the descriptors for a constant buffer
|
||||
/// </summary>
|
||||
/// <returns>The CPU and GPU handles for a constant buffer</returns>
|
||||
[Pure]
|
||||
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView()
|
||||
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
|
||||
/// <param name="bufferType">The buffer type currently in use.</param>
|
||||
/// <param name="sizeInBytes">The size in bytes of the current buffer.</param>
|
||||
/// <returns>An <see cref="ID3D12Resource"/> reference for the current buffer.</returns>
|
||||
private static ID3D12Resource* CreateCommittedResource(ID3D12Device* d3D12Device, BufferType bufferType, int sizeInBytes)
|
||||
{
|
||||
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate();
|
||||
|
||||
int constantBufferSize = (SizeInBytes + 255) & ~255;
|
||||
|
||||
ConstantBufferViewDescription description = new ConstantBufferViewDescription
|
||||
(D3D12_HEAP_TYPE d3D12HeapType,
|
||||
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStates) = bufferType switch
|
||||
{
|
||||
BufferLocation = NativeResource!.GPUVirtualAddress,
|
||||
SizeInBytes = constantBufferSize
|
||||
BufferType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
|
||||
BufferType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
|
||||
BufferType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
|
||||
BufferType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
|
||||
BufferType.Transfer => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
|
||||
_ => throw null!
|
||||
};
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateConstantBufferView(description, cpuHandle);
|
||||
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
|
||||
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView()
|
||||
{
|
||||
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate();
|
||||
|
||||
ShaderResourceViewDescription description = new ShaderResourceViewDescription
|
||||
{
|
||||
Shader4ComponentMapping = 5768,
|
||||
ViewDimension = ShaderResourceViewDimension.Buffer,
|
||||
Buffer = { NumElements = Size, StructureByteStride = ElementSizeInBytes }
|
||||
};
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, description, cpuHandle);
|
||||
|
||||
return (cpuHandle, gpuHandle);
|
||||
return d3D12Device->CreateCommittedResource(d3D12HeapType, (uint)sizeInBytes, d3D12ResourceFlags, d3D12ResourceStates);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the descriptors for a read write buffer
|
||||
/// Creates a view for a constant buffer.
|
||||
/// </summary>
|
||||
/// <returns>The CPU and GPU handles for a read write buffer</returns>
|
||||
[Pure]
|
||||
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView()
|
||||
private void CreateConstantBufferView()
|
||||
{
|
||||
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate();
|
||||
uint constantBufferSize = (uint)((SizeInBytes + 255) & ~255);
|
||||
|
||||
UnorderedAccessViewDescription description = new UnorderedAccessViewDescription
|
||||
{
|
||||
ViewDimension = UnorderedAccessViewDimension.Buffer,
|
||||
Buffer = { NumElements = Size, StructureByteStride = ElementSizeInBytes }
|
||||
};
|
||||
D3D12_CONSTANT_BUFFER_VIEW_DESC d3D12ConstantBufferViewDescription;
|
||||
d3D12ConstantBufferViewDescription.BufferLocation = D3D12Resource->GetGPUVirtualAddress();
|
||||
d3D12ConstantBufferViewDescription.SizeInBytes = constantBufferSize;
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle);
|
||||
GraphicsDevice.D3D12Device->CreateConstantBufferView(&d3D12ConstantBufferViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a readonly buffer.
|
||||
/// </summary>
|
||||
private void CreateShaderResourceView()
|
||||
{
|
||||
D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default;
|
||||
d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
|
||||
d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
||||
d3D12ShaderResourceViewDescription.Buffer.NumElements = (uint)Size;
|
||||
d3D12ShaderResourceViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
|
||||
|
||||
return (cpuHandle, gpuHandle);
|
||||
GraphicsDevice.D3D12Device->CreateShaderResourceView(D3D12Resource, &d3D12ShaderResourceViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a buffer than be both read and written to.
|
||||
/// </summary>
|
||||
private void CreateUnorderedAccessView()
|
||||
{
|
||||
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
|
||||
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
|
||||
d3D12UnorderedAccessViewDescription.Buffer.NumElements = (uint)Size;
|
||||
d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ConstantBuffer2<T> : HlslBuffer2<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ConstantBuffer2{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
internal ConstantBuffer2(GraphicsDevice2 device, int size)
|
||||
: base(device, size, size * GetPaddedSize(), BufferType.Constant)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the right padded size for <typeparamref name="T"/> elements to store in the current instance
|
||||
/// </summary>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int GetPaddedSize()
|
||||
{
|
||||
int size = Unsafe.SizeOf<T>();
|
||||
|
||||
return (size + 15) & ~15;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current constant buffer
|
||||
/// </summary>
|
||||
/// <param name="i">The index of the value to get</param>
|
||||
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
|
||||
public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ConstantBuffer<T>)}<T>[int]");
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void GetData(Span<T> span, int offset, int count)
|
||||
{
|
||||
using MappedResource resource = MapResource();
|
||||
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
ref T spanRef = ref MemoryMarshal.GetReference(span);
|
||||
ref byte resourceRef = ref Unsafe.AsRef<byte>((void*)resource.Pointer);
|
||||
|
||||
// Move to the initial ref
|
||||
resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
|
||||
|
||||
Unsafe.Add(ref spanRef, i) = Unsafe.As<byte, T>(ref targetRef);
|
||||
}
|
||||
}
|
||||
else MemoryHelper.Copy(resource.Pointer, offset, span, 0, count);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(ReadOnlySpan<T> span, int offset, int count)
|
||||
{
|
||||
using MappedResource resource = MapResource();
|
||||
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
ref T spanRef = ref MemoryMarshal.GetReference(span);
|
||||
ref byte resourceRef = ref Unsafe.AsRef<byte>((void*)resource.Pointer);
|
||||
|
||||
// Move to the initial offset
|
||||
resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
|
||||
|
||||
Unsafe.As<byte, T>(ref targetRef) = Unsafe.Add(ref spanRef, i);
|
||||
}
|
||||
}
|
||||
else MemoryHelper.Copy(span, resource.Pointer, offset, count);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(HlslBuffer2<T> buffer)
|
||||
{
|
||||
if (buffer is ConstantBuffer2<T>)
|
||||
{
|
||||
// Directly copy the input buffer, if possible
|
||||
using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,9 @@ using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using Vortice.Direct3D12;
|
||||
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
|
||||
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
@@ -16,14 +16,18 @@ namespace ComputeSharp
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ConstantBuffer<T> : HlslBuffer<T> where T : unmanaged
|
||||
public sealed class ConstantBuffer<T> : HlslBuffer<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ConstantBuffer{T}"/> instance with the specified parameters
|
||||
/// </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 * GetPaddedSize(), BufferType.Constant) { }
|
||||
internal ConstantBuffer(GraphicsDevice device, int size)
|
||||
: base(device, size, size * GetPaddedSize(), BufferType.Constant)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the right padded size for <typeparamref name="T"/> elements to store in the current instance
|
||||
@@ -91,14 +95,14 @@ namespace ComputeSharp
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(HlslBuffer<T> buffer)
|
||||
public override unsafe void SetData(HlslBuffer<T> buffer)
|
||||
{
|
||||
if (buffer is ConstantBuffer<T>)
|
||||
{
|
||||
// Directly copy the input buffer, if possible
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
|
||||
@@ -7,177 +7,194 @@ using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extension methods for the <see cref="GraphicsDevice"/> type to allocate buffers
|
||||
/// A <see langword="class"/> with extension methods for the <see cref="GraphicsDevice"/> type to allocate buffers.
|
||||
/// </summary>
|
||||
public static class BufferExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified parameters
|
||||
/// Allocates a new constant buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input array</returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input array.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, T[] array) where T : unmanaged
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, T[] array)
|
||||
where T : unmanaged
|
||||
{
|
||||
return device.AllocateConstantBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified parameters
|
||||
/// Allocates a new constant buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/></returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, Span<T> span) where T : unmanaged
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, Span<T> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
ConstantBuffer<T> buffer = new ConstantBuffer<T>(device, span.Length);
|
||||
ConstantBuffer<T> buffer = new(device, span.Length);
|
||||
|
||||
buffer.SetData(span);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified parameters
|
||||
/// Allocates a new constant buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/></returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer) where T : unmanaged
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer)
|
||||
where T : unmanaged
|
||||
{
|
||||
ConstantBuffer<T> constantBuffer = new ConstantBuffer<T>(device, buffer.Size);
|
||||
ConstantBuffer<T> constantBuffer = new(device, buffer.Size);
|
||||
|
||||
constantBuffer.SetData(buffer);
|
||||
|
||||
return constantBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly buffer with the specified parameters
|
||||
/// Allocates a new readonly buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="size">The size of the buffer to allocate</param>
|
||||
/// <returns>A zeroed <see cref="ReadOnlyBuffer{T}"/> instance of size <paramref name="size"/></returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="size">The size of the buffer to allocate.</param>
|
||||
/// <returns>A zeroed <see cref="ReadOnlyBuffer{T}"/> instance of size <paramref name="size"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, int size) where T : unmanaged
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, int size)
|
||||
where T : unmanaged
|
||||
{
|
||||
return new ReadOnlyBuffer<T>(device, size);
|
||||
return new(device, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly buffer with the specified parameters
|
||||
/// Allocates a new readonly buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input array</returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input array.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, T[] array) where T : unmanaged
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, T[] array)
|
||||
where T : unmanaged
|
||||
{
|
||||
return device.AllocateReadOnlyBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly buffer with the specified parameters
|
||||
/// Allocates a new readonly buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/></returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, Span<T> span) where T : unmanaged
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, Span<T> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
ReadOnlyBuffer<T> buffer = new ReadOnlyBuffer<T>(device, span.Length);
|
||||
ReadOnlyBuffer<T> buffer = new(device, span.Length);
|
||||
|
||||
buffer.SetData(span);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly buffer with the specified parameters
|
||||
/// Allocates a new readonly buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/></returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer) where T : unmanaged
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer)
|
||||
where T : unmanaged
|
||||
{
|
||||
ReadOnlyBuffer<T> readWriteBuffer = new ReadOnlyBuffer<T>(device, buffer.Size);
|
||||
ReadOnlyBuffer<T> readWriteBuffer = new(device, buffer.Size);
|
||||
|
||||
readWriteBuffer.SetData(buffer);
|
||||
|
||||
return readWriteBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters
|
||||
/// Allocates a new read write buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="size">The size of the buffer to allocate</param>
|
||||
/// <returns>A zeroed <see cref="ReadWriteBuffer{T}"/> instance of size <paramref name="size"/></returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="size">The size of the buffer to allocate.</param>
|
||||
/// <returns>A zeroed <see cref="ReadWriteBuffer{T}"/> instance of size <paramref name="size"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, int size) where T : unmanaged
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, int size)
|
||||
where T : unmanaged
|
||||
{
|
||||
return new ReadWriteBuffer<T>(device, size);
|
||||
return new(device, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters
|
||||
/// Allocates a new read write buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer{T}"/> instance with the contents of the input array</returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer{T}"/> instance with the contents of the input array.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, T[] array) where T : unmanaged
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, T[] array)
|
||||
where T : unmanaged
|
||||
{
|
||||
return device.AllocateReadWriteBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters
|
||||
/// Allocates a new read write buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/></returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, Span<T> span) where T : unmanaged
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, Span<T> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
ReadWriteBuffer<T> buffer = new ReadWriteBuffer<T>(device, span.Length);
|
||||
ReadWriteBuffer<T> buffer = new(device, span.Length);
|
||||
|
||||
buffer.SetData(span);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters
|
||||
/// Allocates a new read write buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/></returns>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer) where T : unmanaged
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer)
|
||||
where T : unmanaged
|
||||
{
|
||||
ReadWriteBuffer<T> readWriteBuffer = new ReadWriteBuffer<T>(device, buffer.Size);
|
||||
ReadWriteBuffer<T> readWriteBuffer = new (device, buffer.Size);
|
||||
|
||||
readWriteBuffer.SetData(buffer);
|
||||
|
||||
return readWriteBuffer;
|
||||
|
||||
@@ -1,203 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extension methods for the <see cref="GraphicsDevice2"/> type to allocate buffers.
|
||||
/// </summary>
|
||||
public static class BufferExtensions2
|
||||
{
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer2{T}"/> instance with the contents of the input array.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer2<T> AllocateConstantBuffer<T>(this GraphicsDevice2 device, T[] array)
|
||||
where T : unmanaged
|
||||
{
|
||||
return device.AllocateConstantBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer2{T}"/> instance with the contents of the input <see cref="Span{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer2<T> AllocateConstantBuffer<T>(this GraphicsDevice2 device, Span<T> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
ConstantBuffer2<T> buffer = new(device, span.Length);
|
||||
|
||||
buffer.SetData(span);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer2{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer2{T}"/> instance with the contents of the input <see cref="HlslBuffer2{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer2<T> AllocateConstantBuffer<T>(this GraphicsDevice2 device, HlslBuffer2<T> buffer)
|
||||
where T : unmanaged
|
||||
{
|
||||
ConstantBuffer2<T> constantBuffer = new(device, buffer.Size);
|
||||
|
||||
constantBuffer.SetData(buffer);
|
||||
|
||||
return constantBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="size">The size of the buffer to allocate.</param>
|
||||
/// <returns>A zeroed <see cref="ReadOnlyBuffer2{T}"/> instance of size <paramref name="size"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer2<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice2 device, int size)
|
||||
where T : unmanaged
|
||||
{
|
||||
return new(device, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer2{T}"/> instance with the contents of the input array.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer2<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice2 device, T[] array)
|
||||
where T : unmanaged
|
||||
{
|
||||
return device.AllocateReadOnlyBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer2{T}"/> instance with the contents of the input <see cref="Span{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer2<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice2 device, Span<T> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
ReadOnlyBuffer2<T> buffer = new(device, span.Length);
|
||||
|
||||
buffer.SetData(span);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer2{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer2{T}"/> instance with the contents of the input <see cref="HlslBuffer2{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer2<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice2 device, HlslBuffer2<T> buffer)
|
||||
where T : unmanaged
|
||||
{
|
||||
ReadOnlyBuffer2<T> readWriteBuffer = new(device, buffer.Size);
|
||||
|
||||
readWriteBuffer.SetData(buffer);
|
||||
|
||||
return readWriteBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="size">The size of the buffer to allocate.</param>
|
||||
/// <returns>A zeroed <see cref="ReadWriteBuffer2{T}"/> instance of size <paramref name="size"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer2<T> AllocateReadWriteBuffer<T>(this GraphicsDevice2 device, int size)
|
||||
where T : unmanaged
|
||||
{
|
||||
return new(device, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer2{T}"/> instance with the contents of the input array.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer2<T> AllocateReadWriteBuffer<T>(this GraphicsDevice2 device, T[] array)
|
||||
where T : unmanaged
|
||||
{
|
||||
return device.AllocateReadWriteBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer2{T}"/> instance with the contents of the input <see cref="Span{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer2<T> AllocateReadWriteBuffer<T>(this GraphicsDevice2 device, Span<T> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
ReadWriteBuffer2<T> buffer = new(device, span.Length);
|
||||
|
||||
buffer.SetData(span);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> instance to use to allocate the buffer.</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer.</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer2{T}"/> instance with the contents of the input <see cref="HlslBuffer2{T}"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer2<T> AllocateReadWriteBuffer<T>(this GraphicsDevice2 device, HlslBuffer2<T> buffer)
|
||||
where T : unmanaged
|
||||
{
|
||||
ReadWriteBuffer2<T> readWriteBuffer = new (device, buffer.Size);
|
||||
|
||||
readWriteBuffer.SetData(buffer);
|
||||
|
||||
return readWriteBuffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed readonly buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadOnlyBuffer2<T> : HlslStructuredBuffer2<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadOnlyBuffer2{T}"/> instance with the specified parameters
|
||||
/// </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 ReadOnlyBuffer2(GraphicsDevice2 device, int size)
|
||||
: base(device, size, BufferType.ReadOnly)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
/// </summary>
|
||||
/// <param name="i">The index of the value to get</param>
|
||||
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
|
||||
public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyBuffer<T>)}<T>[int]");
|
||||
}
|
||||
}
|
||||
@@ -9,14 +9,18 @@ namespace ComputeSharp
|
||||
/// A <see langword="class"/> representing a typed readonly buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadOnlyBuffer<T> : HlslStructuredBuffer<T> where T : unmanaged
|
||||
public sealed class ReadOnlyBuffer<T> : HlslStructuredBuffer<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadOnlyBuffer{T}"/> instance with the specified parameters
|
||||
/// </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 ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, BufferType.ReadOnly) { }
|
||||
internal ReadOnlyBuffer(GraphicsDevice device, int size)
|
||||
: base(device, size, BufferType.ReadOnly)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadWriteBuffer2<T> : HlslStructuredBuffer2<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadWriteBuffer2{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice2"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
internal ReadWriteBuffer2(GraphicsDevice2 device, int size)
|
||||
: base(device, size, BufferType.ReadWrite)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
/// </summary>
|
||||
/// <param name="i">The index of the value to get or set</param>
|
||||
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
|
||||
public ref T this[int i] => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer<T>)}<T>[int]");
|
||||
}
|
||||
}
|
||||
@@ -9,14 +9,18 @@ namespace ComputeSharp
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadWriteBuffer<T> : HlslStructuredBuffer<T> where T : unmanaged
|
||||
public sealed class ReadWriteBuffer<T> : HlslStructuredBuffer<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadWriteBuffer{T}"/> instance with the specified parameters
|
||||
/// </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, BufferType.ReadWrite) { }
|
||||
internal ReadWriteBuffer(GraphicsDevice device, int size)
|
||||
: base(device, size, BufferType.ReadWrite)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする