Merge pull request #76 from Sergio0694/feature/texture-types

Added Texture2D<T> types
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2020-12-04 00:58:52 +01:00
コミット b5129f69ac
28ファイル変更
+1267 -256

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

+1 -1
ファイルの表示
@@ -261,7 +261,7 @@ namespace ComputeSharp.BokehBlur.Processors
// Preliminary gamma highlight pass
using IMemoryOwner<Vector4> source4 = GetExposedVector4Buffer(memory);
using ReadOnlyBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(source4.Memory.Span);
using ReadOnlyBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer<Vector4>(source4.Memory.Span);
using ReadWriteBuffer<Vector4> processingBuffer = Gpu.Default.AllocateReadWriteBuffer<Vector4>(sourceBuffer.Length);
using ReadWriteBuffer<ComplexVector4> firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer<ComplexVector4>(sourceBuffer.Length);
using ReadOnlyBuffer<Complex64> kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer<Complex64>(KernelSize);
+1 -1
ファイルの表示
@@ -96,7 +96,7 @@ namespace ComputeSharp.BokehBlur.Processors
PixelOperations<TPixel>.Instance.ToVector4(Configuration, spanTPixel, spanVector4);
using ReadWriteBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(spanVector4);
using ReadWriteBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer<Vector4>(spanVector4);
using ReadWriteBuffer<Vector4> firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer<Vector4>(sourceBuffer.Length);
using ReadOnlyBuffer<float> kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel);
+36 -61
ファイルの表示
@@ -8,9 +8,6 @@ using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Extensions;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_SRV_DIMENSION;
using static TerraFX.Interop.D3D12_UAV_DIMENSION;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
@@ -29,6 +26,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
/// <remarks>This field is dynamically accessed when loading shader dispatch data.</remarks>
internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
/// <summary>
@@ -42,8 +40,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="length">The number of items to store in the current buffer.</param>
/// <param name="elementSizeInBytes">The size in bytes of each buffer item (including padding, if any).</param>
/// <param name="bufferType">The buffer type for the current buffer.</param>
internal Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, BufferType bufferType)
/// <param name="resourceType">The resource type for the current buffer.</param>
private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType)
{
device.ThrowIfDisposed();
@@ -53,20 +51,26 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
GraphicsDevice = device;
Length = length;
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, (ulong)SizeInBytes);
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes);
GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (bufferType)
switch (resourceType)
{
case BufferType.Constant: CreateConstantBufferView(d3D12CpuDescriptorHandle); break;
case BufferType.ReadOnly: CreateShaderResourceView(d3D12CpuDescriptorHandle); break;
case BufferType.ReadWrite: CreateUnorderedAccessView(d3D12CpuDescriptorHandle); break;
case ResourceType.Constant:
device.D3D12Device->CreateConstantBufferView(this.d3D12Resource, SizeInBytes, d3D12CpuDescriptorHandle);
break;
case ResourceType.ReadOnly:
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, (uint)length, elementSizeInBytes, d3D12CpuDescriptorHandle);
break;
case ResourceType.ReadWrite:
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, (uint)length, elementSizeInBytes, d3D12CpuDescriptorHandle);
break;
}
}
/// <summary>
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance.
/// Gets the <see cref="Graphics.GraphicsDevice"/> associated with the current instance.
/// </summary>
public GraphicsDevice GraphicsDevice { get; }
@@ -103,7 +107,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer.</returns>
[Pure]
public T[] GetData() => GetData(0, Length);
public T[] GetData()
{
return GetData(0, Length);
}
/// <summary>
/// Reads the contents of the current <see cref="Buffer{T}"/> instance in a given range and returns an array.
@@ -127,7 +134,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// Reads the contents of the current <see cref="Buffer{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
public void GetData(T[] destination) => GetData(destination.AsSpan(), 0);
public void GetData(T[] destination)
{
GetData(destination.AsSpan(), 0);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target array.
@@ -148,7 +158,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// The input data will be read from the start of the buffer.
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
public void GetData(Span<T> destination) => GetData(destination, 0);
public void GetData(Span<T> destination)
{
GetData(destination, 0);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
@@ -161,7 +174,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Buffer{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public void SetData(T[] source) => SetData(source.AsSpan());
public void SetData(T[] source)
{
SetData(source.AsSpan());
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Buffer{T}"/> instance.
@@ -182,7 +198,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// The input data will be written to the start of the buffer, and all input items will be copied.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
public void SetData(ReadOnlySpan<T> source) => SetData(source, 0);
public void SetData(ReadOnlySpan<T> source)
{
SetData(source, 0);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Buffer{T}"/> instance.
@@ -221,50 +240,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
}
/// <summary>
/// Creates a view for a constant buffer.
/// </summary>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
private void CreateConstantBufferView(D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
uint constantBufferSize = checked((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>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
private void CreateShaderResourceView(D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
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)Length;
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>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
private void CreateUnorderedAccessView(D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
d3D12UnorderedAccessViewDescription.Buffer.NumElements = (uint)Length;
d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle);
}
/// <inheritdoc/>
protected override void OnDispose()
{
+8 -8
ファイルの表示
@@ -23,9 +23,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="length">The number of items to store in the current buffer.</param>
/// <param name="bufferType">The buffer type for the current buffer.</param>
internal StructuredBuffer(GraphicsDevice device, int length, BufferType bufferType)
: base(device, length, (uint)Unsafe.SizeOf<T>(), bufferType)
/// <param name="resourceType">The buffer type for the current buffer.</param>
private protected StructuredBuffer(GraphicsDevice device, int length, ResourceType resourceType)
: base(device, length, (uint)Unsafe.SizeOf<T>(), resourceType)
{
}
@@ -43,11 +43,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
byteOffset = (nint)offset * ElementSizeInBytes,
byteSize = (nint)destination.Length * ElementSizeInBytes;
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.ReadBack, (ulong)byteSize);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize);
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
{
copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteSize);
copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset,(ulong)byteSize);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -70,7 +70,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
byteOffset = (nint)offset * ElementSizeInBytes,
byteSize = (nint)source.Length * ElementSizeInBytes;
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.Upload, (ulong)byteSize);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
{
@@ -79,7 +79,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteSize);
copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0,(ulong)byteSize);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -98,7 +98,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
// Directly copy the input buffer
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(source.D3D12Resource, 0, D3D12Resource, 0, (ulong)SizeInBytes);
copyCommandList.CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
else SetDataWithCpuBuffer(source);
+409
ファイルの表示
@@ -0,0 +1,409 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Interop;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Buffers.Interop;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
/// <summary>
/// A <see langword="class"/> representing a typed 2D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
public unsafe abstract class Texture2D<T> : NativeObject
where T : unmanaged
{
/// <summary>
/// The <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
/// <remarks>This field is dynamically accessed when loading shader dispatch data.</remarks>
internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
/// <summary>
/// The default <see cref="D3D12_RESOURCE_STATES"/> value for the current resource.
/// </summary>
private readonly D3D12_RESOURCE_STATES D3D12ResourceStates;
/// <summary>
/// Creates a new <see cref="Texture2D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="Graphics.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType)
{
device.ThrowIfDisposed();
Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width));
Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height));
GraphicsDevice = device;
Width = width;
Height = height;
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
resourceType,
DXGIFormatHelper.GetForType<T>(),
(uint)width,
(uint)height,
out D3D12ResourceStates);
device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (resourceType)
{
case ResourceType.ReadOnly:
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), d3D12CpuDescriptorHandle);
break;
case ResourceType.ReadWrite:
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), d3D12CpuDescriptorHandle);
break;
}
}
/// <summary>
/// Gets the <see cref="Graphics.GraphicsDevice"/> associated with the current instance.
/// </summary>
public GraphicsDevice GraphicsDevice { get; }
/// <summary>
/// Gets the width of the current texture.
/// </summary>
public int Width { get; }
/// <summary>
/// Gets the height of the current texture.
/// </summary>
public int Height { get; }
/// <summary>
/// Gets the <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{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()
{
T[,] data = new T[Height, Width];
GetData(data);
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
public void GetData(T[,] destination)
{
fixed (T* p = destination)
{
GetData(new Span<T>(p, destination.Length));
}
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
public void GetData(T[] destination)
{
GetData(destination.AsSpan(), 0, 0, Width, Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public void GetData(T[] destination, Range x, Range y)
{
GetData(destination.AsSpan(), x, y);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public void GetData(T[] destination, int x, int y, int width, int height)
{
GetData(destination.AsSpan(), x, y, width, height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
public void GetData(T[] destination, int offset)
{
GetData(destination.AsSpan(offset), 0, 0, Width, Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public void GetData(T[] destination, int offset, Range x, Range y)
{
GetData(destination.AsSpan(offset), x, y);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public void GetData(T[] destination, int offset, int x, int y, int width, int height)
{
GetData(destination.AsSpan(offset), x, y, width, height);
}
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// The input data will be read from the start of the texture.
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
public void GetData(Span<T> destination)
{
GetData(destination, 0, 0, Width, Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public void GetData(Span<T> destination, Range x, Range y)
{
var (offsetX, width) = x.GetOffsetAndLength(Width);
var (offsetY, height) = y.GetOffsetAndLength(Height);
GetData(destination, offsetX, offsetY, width, height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public void GetData(Span<T> destination, int x, int y, int width, int height)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
Guard.IsInRange(x, 0, Width, nameof(x));
Guard.IsInRange(y, 0, Height, nameof(y));
Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width));
Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height));
Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x));
Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y));
Guard.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination));
nint byteSize = (nint)width * height * Unsafe.SizeOf<T>();
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize);
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE))
{
copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceStates, D3D12_RESOURCE_STATE_COPY_SOURCE);
copyCommandList.CopyTextureRegion(d3D12Resource.Get(), (uint)Unsafe.SizeOf<T>(), D3D12Resource, DXGIFormatHelper.GetForType<T>(), (uint)x, (uint)y, (uint)width, (uint)height);
copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceStates);
copyCommandList.ExecuteAndWaitForCompletion();
}
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
MemoryHelper.Copy(resource.Pointer, 0, destination);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public void SetData(T[,] source)
{
fixed (T* p = source)
{
SetData(new Span<T>(p, source.Length));
}
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public void SetData(T[] source)
{
SetData(source.AsSpan(), 0, 0, Width, Height);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public void SetData(T[] source, Range x, Range y)
{
SetData(source.AsSpan(), x, y);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public void SetData(T[] source, int x, int y, int width, int height)
{
SetData(source.AsSpan(), x, y, width, height);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public void SetData(T[] source, int offset, Range x, Range y)
{
SetData(source.AsSpan(offset), x, y);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public void SetData(T[] source, int offset, int x, int y, int width, int height)
{
SetData(source.AsSpan(offset), x, y, width, height);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to the current <see cref="Texture2D{T}"/> instance.
/// The input data will be written to the start of the texture, and all input items will be copied.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
public void SetData(ReadOnlySpan<T> source)
{
SetData(source, 0, 0, Width, Height);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public void SetData(ReadOnlySpan<T> source, Range x, Range y)
{
var (offsetX, width) = x.GetOffsetAndLength(Width);
var (offsetY, height) = y.GetOffsetAndLength(Height);
SetData(source, offsetX, offsetY, width, height);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public void SetData(ReadOnlySpan<T> source, int x, int y, int width, int height)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
Guard.IsInRange(x, 0, Width, nameof(x));
Guard.IsInRange(y, 0, Height, nameof(y));
Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width));
Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height));
Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x));
Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y));
Guard.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source));
nint byteSize = (nint)width * height * Unsafe.SizeOf<T>();
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
{
MemoryHelper.Copy(source, resource.Pointer, 0);
}
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE);
copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceStates, D3D12_RESOURCE_STATE_COPY_DEST);
copyCommandList.CopyTextureRegion(D3D12Resource, DXGIFormatHelper.GetForType<T>(), (uint)x, (uint)y, d3D12Resource.Get(), (uint)width, (uint)height, (uint)Unsafe.SizeOf<T>());
copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceStates);
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <inheritdoc/>
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
}
/// <summary>
/// Throws a <see cref="GraphicsDeviceMismatchException"/> if the target device doesn't match the current one.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ThrowIfDeviceMismatch(GraphicsDevice device)
{
if (GraphicsDevice != device)
{
GraphicsDeviceMismatchException.Throw(this, device);
}
}
}
}
+2 -2
ファイルの表示
@@ -32,7 +32,7 @@ namespace ComputeSharp
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="length">The number of items to store in the current buffer.</param>
internal ConstantBuffer(GraphicsDevice device, int length)
: base(device, length, (uint)GetPaddedSize(), BufferType.Constant)
: base(device, length, (uint)GetPaddedSize(), ResourceType.Constant)
{
}
@@ -130,7 +130,7 @@ namespace ComputeSharp
// Directly copy the input buffer, if possible
using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(source.D3D12Resource, 0, D3D12Resource, 0, (ulong)SizeInBytes);
copyCommandList.CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
else SetDataWithCpuBuffer(source);
-38
ファイルの表示
@@ -1,38 +0,0 @@
namespace ComputeSharp.Graphics.Buffers.Enums
{
/// <summary>
/// An <see langword="enum"/> that indicates the type of a given HLSL buffer.
/// </summary>
internal enum BufferType
{
/// <summary>
/// A constant buffer, with items aligned to 16 bytes, used for individual values or very small arrays.
/// This is mapped to a <see langword="cbuffer"/> in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_UPLOAD"/>.
/// </summary>
Constant,
/// <summary>
/// A readonly buffer, that can store arbitrary arrays of values and can only be read from by the GPU.
/// This is mapped to a <see langword="StructuredBuffer&lt;T&gt;"/> in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_DEFAULT"/>.
/// </summary>
ReadOnly,
/// <summary>
/// A read write buffer, that can store arbitrary arrays with both read and write access for the GPU.
/// This is mapped to a <see langword="RWStructuredBuffer&lt;T&gt;"/> in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_DEFAULT"/>.
/// </summary>
ReadWrite,
/// <summary>
/// A readback buffer, used as temporary buffer to read data back from <see cref="ReadOnly"/> and <see cref="ReadWrite"/> buffers.
/// This has no mapping in HLSL, and is allocated in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_READBACK"/>.
/// </summary>
ReadBack,
/// <summary>
/// A transfer buffer, used as a temporary buffer to set data to <see cref="ReadOnly"/> and <see cref="ReadWrite"/> buffers.
/// This has no mapping in HLSL, and is allocated in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_UPLOAD"/>.
/// </summary>
Upload
}
}
+43
ファイルの表示
@@ -0,0 +1,43 @@
namespace ComputeSharp.Graphics.Buffers.Enums
{
/// <summary>
/// An <see langword="enum"/> that indicates the type of a given HLSL resource.
/// </summary>
internal enum ResourceType
{
/// <summary>
/// A constant resource, with items aligned to 16 bytes, used for individual values or very small arrays.
/// This is only supported for buffers and is mapped to a <see langword="cbuffer"/>.
/// Resources of this type are located in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_UPLOAD"/>.
/// </summary>
Constant,
/// <summary>
/// A readonly resource, that can store arbitrary arrays of values and can only be read from by the GPU.
/// This is mapped to either <see langword="StructuredBuffer&lt;T&gt;"/> or <see langword="TextureND&lt;T&gt;"/>.
/// Resources of this type are located in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_DEFAULT"/>.
/// </summary>
ReadOnly,
/// <summary>
/// A read write resource, that can store arbitrary arrays with both read and write access for the GPU.
/// This is mapped to either <see langword="RWStructuredBuffer&lt;T&gt;"/> or <see langword="RWTextureND&lt;T&gt;"/>.
/// Resources of this type are located in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_DEFAULT"/>.
/// </summary>
ReadWrite,
/// <summary>
/// A readback resource, used temporarily to read data back from <see cref="ReadOnly"/> and <see cref="ReadWrite"/> resources.
/// This has no direct mapping to a type in HLSL, as it's only used for copy operations.
/// Resources of this type are located in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_READBACK"/>.
/// </summary>
ReadBack,
/// <summary>
/// A transfer resource, used temporarily to set data to <see cref="ReadOnly"/> and <see cref="ReadWrite"/> resources.
/// This has no direct mapping to a type in HLSL, as it's only used for copy operations (like <see cref="ReadBack"/>).
/// Resources of this type are located in <see cref="TerraFX.Interop.D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_UPLOAD"/>.
/// </summary>
Upload
}
}
+2 -2
ファイルの表示
@@ -22,12 +22,12 @@ namespace ComputeSharp
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="length">The number of items to store in the current buffer.</param>
internal ReadOnlyBuffer(GraphicsDevice device, int length)
: base(device, length, BufferType.ReadOnly)
: base(device, length, ResourceType.ReadOnly)
{
}
/// <summary>
/// Gets a single <typeparamref name="T"/> value from the current read write buffer.
/// Gets a single <typeparamref name="T"/> value from the current readonly 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>
+43
ファイルの表示
@@ -0,0 +1,43 @@
using System.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Buffers.Views;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="class"/> representing a typed readonly 2D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
[DebuggerTypeProxy(typeof(BufferDebugView2D<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadOnlyTexture2D<T> : Texture2D<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadOnlyTexture2D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height)
: base(device, width, height, ResourceType.ReadOnly)
{
}
/// <summary>
/// Gets a single <typeparamref name="T"/> value from the current readonly texture.
/// </summary>
/// <param name="xy">The coordinates 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[UInt2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D<T>)}<T>[UInt2]");
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadOnlyTexture2D<{typeof(T)}>[{Width}, {Height}]";
}
}
}
+1 -1
ファイルの表示
@@ -22,7 +22,7 @@ namespace ComputeSharp
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="length">The number of items to store in the current buffer.</param>
internal ReadWriteBuffer(GraphicsDevice device, int length)
: base(device, length, BufferType.ReadWrite)
: base(device, length, ResourceType.ReadWrite)
{
}
+43
ファイルの表示
@@ -0,0 +1,43 @@
using System.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Buffers.Views;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="class"/> representing a typed read write 2D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
[DebuggerTypeProxy(typeof(BufferDebugView2D<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadWriteTexture2D<T> : Texture2D<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadWriteTexture2D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
internal ReadWriteTexture2D(GraphicsDevice device, int width, int height)
: base(device, width, height, ResourceType.ReadWrite)
{
}
/// <summary>
/// Gets or sets a single <typeparamref name="T"/> value from the current readonly texture.
/// </summary>
/// <param name="xy">The coordinates 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 ref T this[UInt2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D<T>)}<T>[UInt2]");
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadWriteTexture2D<{typeof(T)}>[{Width}, {Height}]";
}
}
}
+28
ファイルの表示
@@ -0,0 +1,28 @@
using System.Diagnostics;
using ComputeSharp.Graphics.Buffers.Abstract;
namespace ComputeSharp.Graphics.Buffers.Views
{
/// <summary>
/// A debug proxy used to display items in a <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items to display.</typeparam>
internal sealed class BufferDebugView2D<T>
where T : unmanaged
{
/// <summary>
/// Initializes a new instance of the <see cref="BufferDebugView2D{T}"/> class with the specified parameters.
/// </summary>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance with the items to display.</param>
public BufferDebugView2D(Texture2D<T>? texture)
{
Items = texture?.GetData();
}
/// <summary>
/// Gets the items to display for the current instance.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public T[,]? Items { get; }
}
}
+68 -3
ファイルの表示
@@ -81,16 +81,81 @@ namespace ComputeSharp.Graphics.Commands
/// <summary>
/// Copies a memory region from one resource to another.
/// </summary>
/// <param name="d3D12ResourceSource">The source <see cref="ID3D12Resource"/> to read from.</param>
/// <param name="sourceOffset">The starting offset to read the source resource from.</param>
/// <param name="d3d12ResourceDestination">The destination <see cref="ID3D12Resource"/> to write to.</param>
/// <param name="destinationOffset">The starting offset to write the destination resource from.</param>
/// <param name="d3D12ResourceSource">The source <see cref="ID3D12Resource"/> to read from.</param>
/// <param name="sourceOffset">The starting offset to read the source resource from.</param>
/// <param name="numBytes">The total number of bytes to copy from one resource to another.</param>
public readonly void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, ulong sourceOffset, ID3D12Resource* d3d12ResourceDestination, ulong destinationOffset, ulong numBytes)
public readonly void CopyBufferRegion(ID3D12Resource* d3d12ResourceDestination, ulong destinationOffset, ID3D12Resource* d3D12ResourceSource, ulong sourceOffset, ulong numBytes)
{
this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, destinationOffset, d3D12ResourceSource, sourceOffset, numBytes);
}
/// <summary>
/// Copies a texture memory region from one resource (a buffer) to another (a texture).
/// </summary>
/// <param name="d3d12ResourceDestination">The destination <see cref="ID3D12Resource"/> (a texture) to write to.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> parameter curreently in use by the texture.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="d3D12ResourceSource">The source <see cref="ID3D12Resource"/> (a buffer) to read from.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
/// <param name="elementSizeInBytes">The size of each element to copy.</param>
public readonly void CopyTextureRegion(
ID3D12Resource* d3d12ResourceDestination,
DXGI_FORMAT dxgiFormat,
uint x,
uint y,
ID3D12Resource* d3D12ResourceSource,
uint width,
uint height,
uint elementSizeInBytes)
{
this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3d12ResourceDestination, dxgiFormat, x, y, d3D12ResourceSource, width, height, elementSizeInBytes);
}
/// <summary>
/// Copies a texture memory region from one resource (a texture) to another (a buffer).
/// </summary>
/// <param name="d3d12ResourceDestination">The destination <see cref="ID3D12Resource"/> (a buffer) to write to.</param>
/// <param name="elementSizeInBytes">The size of each element to copy.</param>
/// <param name="d3D12ResourceSource">The source <see cref="ID3D12Resource"/> (a texture) to read from.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> parameter curreently in use by the texture.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public readonly void CopyTextureRegion(
ID3D12Resource* d3d12ResourceDestination,
uint elementSizeInBytes,
ID3D12Resource* d3D12ResourceSource,
DXGI_FORMAT dxgiFormat,
uint x,
uint y,
uint width,
uint height)
{
this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3d12ResourceDestination, elementSizeInBytes, d3D12ResourceSource, dxgiFormat, x, y, width, height);
}
/// <summary>
/// Creates a resource barrier to transition a resource to a specific state.
/// </summary>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to change state for.</param>
/// <param name="d3D12ResourceStatesBefore">The starting <see cref="D3D12_RESOURCE_STATES"/> value for the transition.</param>
/// <param name="d3D12ResourceStatesAfter">The destnation <see cref="D3D12_RESOURCE_STATES"/> value for the transition.</param>
public readonly void ResourceBarrier(
ID3D12Resource* d3D12Resource,
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore,
D3D12_RESOURCE_STATES d3D12ResourceStatesAfter)
{
D3D12_RESOURCE_BARRIER d3D12ResourceBarrier = D3D12_RESOURCE_BARRIER.InitTransition(d3D12Resource, d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
this.d3D12GraphicsCommandList.Get()->ResourceBarrier(1, &d3D12ResourceBarrier);
}
/// <summary>
/// Binds an input <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value to a specified root parameter.
/// </summary>
+25 -8
ファイルの表示
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.Contracts;
using System.Text;
using ComputeSharp.Core.Interop;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
@@ -23,19 +24,22 @@ namespace ComputeSharp.Exceptions
/// <summary>
/// Creates a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
/// </summary>
/// <typeparam name="T">The type of values in the input buffer.</typeparam>
/// <param name="buffer">The input <see cref="Buffer{T}"/> that was used.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance that was used.</param>
/// <param name="resource">The input <see cref="NativeObject"/> that was used.</param>
/// <param name="sourceDevice">The source <see cref="GraphicsDevice"/> instance tied to <paramref name="resource"/>.</param>
/// <param name="destinationDevice">The target <see cref="GraphicsDevice"/> instance that was used.</param>
/// <returns>A new <see cref="GraphicsDeviceMismatchException"/> instance with a formatted error message.</returns>
/// <remarks>
/// This method only takes a <see cref="NativeObject"/> instance and the associated <see cref="GraphicsDevice"/> instance as
/// <see cref="object.GetType"/> will still be available, but without the unnecessary generic type specializations for the method.
/// </remarks>
[Pure]
private static GraphicsDeviceMismatchException Create<T>(Buffer<T> buffer, GraphicsDevice device)
where T : unmanaged
private static GraphicsDeviceMismatchException Create(NativeObject resource, GraphicsDevice sourceDevice, GraphicsDevice destinationDevice)
{
StringBuilder builder = new(512);
builder.AppendLine("Invalid pairing of graphics devices used to run a compute shader and allocate memory buffers.");
builder.AppendLine($"The target device to run the compute shader is \"{device}\".");
builder.AppendLine($"The buffer of type {buffer.GetType()} was allocated on device \"{buffer.GraphicsDevice}\".");
builder.AppendLine($"The target device to run the compute shader is \"{destinationDevice}\".");
builder.AppendLine($"The buffer of type {resource.GetType()} was allocated on device \"{sourceDevice}\".");
builder.Append("Make sure to always allocate buffers on the same device used to actually run the code that accesses them.");
builder.ToString();
@@ -52,7 +56,20 @@ namespace ComputeSharp.Exceptions
internal static void Throw<T>(Buffer<T> buffer, GraphicsDevice device)
where T : unmanaged
{
throw Create(buffer, device);
throw Create(buffer, buffer.GraphicsDevice, device);
}
/// <summary>
/// Throws a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
/// </summary>
/// <typeparam name="T">The type of values in the input buffer.</typeparam>
/// <param name="buffer">The input <see cref="Texture2D{T}"/> that was used.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance that was used.</param>
[Pure]
internal static void Throw<T>(Texture2D<T> texture, GraphicsDevice device)
where T : unmanaged
{
throw Create(texture, texture.GraphicsDevice, device);
}
}
}
+174 -12
ファイルの表示
@@ -11,9 +11,12 @@ using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_FLAGS;
using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE;
using static TerraFX.Interop.D3D12_FENCE_FLAGS;
using static TerraFX.Interop.D3D12_MEMORY_POOL;
using static TerraFX.Interop.D3D12_HEAP_FLAGS;
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.Extensions
@@ -86,7 +89,7 @@ namespace ComputeSharp.Graphics.Extensions
d3d12device.CreateDescriptorHeap(
&d3d12DescriptorHeapDesc,
FX.__uuidof< ID3D12DescriptorHeap>(),
FX.__uuidof<ID3D12DescriptorHeap>(),
d3d12DescriptorHeap.GetVoidAddressOf()).Assert();
return d3d12DescriptorHeap.Move();
@@ -96,25 +99,25 @@ namespace ComputeSharp.Graphics.Extensions
/// 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="resourceType">The resource 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>
public static ComPtr<ID3D12Resource> CreateCommittedResource(
this ref ID3D12Device d3D12Device,
BufferType bufferType,
ResourceType resourceType,
ulong 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.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch
{
ResourceType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
ResourceType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
ResourceType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
_ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
};
};
using ComPtr<ID3D12Resource> d3D12Resource = default;
@@ -128,7 +131,7 @@ namespace ComputeSharp.Graphics.Extensions
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE,
D3D12_HEAP_FLAG_NONE,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
@@ -138,6 +141,165 @@ namespace ComputeSharp.Graphics.Extensions
return d3D12Resource.Move();
}
/// <summary>
/// Creates a committed resource for a given 2D texture type.
/// </summary>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="resourceType">The resource type currently in use.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="height">The height of the texture resource.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ID3D12Resource"/> reference for the current texture.</returns>
public static ComPtr<ID3D12Resource> CreateCommittedResource(
this ref ID3D12Device d3D12Device,
ResourceType resourceType,
DXGI_FORMAT dxgiFormat,
uint width,
uint height,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE),
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
_ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
};
using ComPtr<ID3D12Resource> d3D12Resource = default;
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
d3D12HeapProperties.CreationNodeMask = 1;
d3D12HeapProperties.VisibleNodeMask = 1;
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags);
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
FX.__uuidof<ID3D12Resource>(),
d3D12Resource.GetVoidAddressOf()).Assert();
return d3D12Resource.Move();
}
/// <summary>
/// Creates a view for a constant buffer.
/// </summary>
/// <param name="d3d12Device">The target <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="d3d12resource">The <see cref="ID3D12Resource"/> to create a view for.</param>
/// <param name="bufferSize">The size of the target resource.</param>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
public static void CreateConstantBufferView(
this ref ID3D12Device d3d12Device,
ID3D12Resource* d3d12resource,
nint bufferSize,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
uint constantBufferSize = checked((uint)((bufferSize + 255) & ~255));
D3D12_CONSTANT_BUFFER_VIEW_DESC d3D12ConstantBufferViewDescription;
d3D12ConstantBufferViewDescription.BufferLocation = d3d12resource->GetGPUVirtualAddress();
d3D12ConstantBufferViewDescription.SizeInBytes = constantBufferSize;
d3d12Device.CreateConstantBufferView(&d3D12ConstantBufferViewDescription, d3D12CpuDescriptorHandle);
}
/// <summary>
/// Creates a view for a readonly buffer.
/// </summary>
/// <param name="d3d12Device">The target <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="d3d12resource">The <see cref="ID3D12Resource"/> to create a view for.</param>
/// <param name="bufferSize">The size of the target resource.</param>
/// <param name="elementSize">The size in byte of each item in the resource.</param>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
public static void CreateShaderResourceView(
this ref ID3D12Device d3d12Device,
ID3D12Resource* d3d12resource,
uint bufferSize,
uint elementSize,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
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 = bufferSize;
d3D12ShaderResourceViewDescription.Buffer.StructureByteStride = elementSize;
d3d12Device.CreateShaderResourceView(d3d12resource, &d3D12ShaderResourceViewDescription, d3D12CpuDescriptorHandle);
}
/// <summary>
/// Creates a view for a readonly texture.
/// </summary>
/// <param name="d3d12Device">The target <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="d3d12resource">The <see cref="ID3D12Resource"/> to create a view for.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
public static void CreateShaderResourceView(
this ref ID3D12Device d3d12Device,
ID3D12Resource* d3d12resource,
DXGI_FORMAT dxgiFormat,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default;
d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
d3D12ShaderResourceViewDescription.Format = dxgiFormat;
d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
d3d12Device.CreateShaderResourceView(d3d12resource, &d3D12ShaderResourceViewDescription, d3D12CpuDescriptorHandle);
}
/// <summary>
/// Creates a view for a buffer that can be both read and written to.
/// </summary>
/// <param name="d3d12Device">The target <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="d3d12resource">The <see cref="ID3D12Resource"/> to create a view for.</param>
/// <param name="bufferSize">The size of the target resource.</param>
/// <param name="elementSize">The size in byte of each item in the resource.</param>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
public static void CreateUnorderedAccessView(
this ref ID3D12Device d3d12Device,
ID3D12Resource* d3d12resource,
uint bufferSize,
uint elementSize,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
d3D12UnorderedAccessViewDescription.Buffer.NumElements = bufferSize;
d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = elementSize;
d3d12Device.CreateUnorderedAccessView(d3d12resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle);
}
/// <summary>
/// Creates a view for a texture that can be both read and written to.
/// </summary>
/// <param name="d3d12Device">The target <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="d3d12resource">The <see cref="ID3D12Resource"/> to create a view for.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
public static void CreateUnorderedAccessView(
this ref ID3D12Device d3d12Device,
ID3D12Resource* d3d12resource,
DXGI_FORMAT dxgiFormat,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
d3D12UnorderedAccessViewDescription.Format = dxgiFormat;
d3d12Device.CreateUnorderedAccessView(d3d12resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle);
}
/// <summary>
/// Creates a new <see cref="ID3D12CommandAllocator"/> for a given device.
/// </summary>
+91
ファイルの表示
@@ -0,0 +1,91 @@
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics.Extensions
{
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="ID3D12GraphicsCommandList"/> type.
/// </summary>
internal static unsafe class ID3D12GraphicsCommandListExtensions
{
/// <summary>
/// Copies a texture memory region from one resource (a buffer) to another (a texture).
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> instance in use.</param>
/// <param name="d3d12ResourceDestination">The destination <see cref="ID3D12Resource"/> (a texture) to write to.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> parameter curreently in use by the texture.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="d3D12ResourceSource">The source <see cref="ID3D12Resource"/> (a buffer) to read from.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
/// <param name="elementSizeInBytes">The size of each element to copy.</param>
public static void CopyTextureRegion(
this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList,
ID3D12Resource* d3d12ResourceDestination,
DXGI_FORMAT dxgiFormat,
uint x,
uint y,
ID3D12Resource* d3D12ResourceSource,
uint width,
uint height,
uint elementSizeInBytes)
{
D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new(
dxgiFormat,
width,
height,
1,
(elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1));
D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
d3D12PlacedSubresourceFootprint.Offset = 0;
d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint;
D3D12_TEXTURE_COPY_LOCATION
d3D12TextureCopyLocationDestination = new(d3d12ResourceDestination, 0),
d3D12TextureCopyLocationSource = new(d3D12ResourceSource, d3D12PlacedSubresourceFootprint);
d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, x, y, 0, &d3D12TextureCopyLocationSource, null);
}
/// <summary>
/// Copies a texture memory region from one resource (a texture) to another (a buffer).
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> instance in use.</param>
/// <param name="d3d12ResourceDestination">The destination <see cref="ID3D12Resource"/> (a buffer) to write to.</param>
/// <param name="elementSizeInBytes">The size of each element to copy.</param>
/// <param name="d3D12ResourceSource">The source <see cref="ID3D12Resource"/> (a texture) to read from.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> parameter curreently in use by the texture.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public static void CopyTextureRegion(
this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList,
ID3D12Resource* d3d12ResourceDestination,
uint elementSizeInBytes,
ID3D12Resource* d3D12ResourceSource,
DXGI_FORMAT dxgiFormat,
uint x,
uint y,
uint width,
uint height)
{
D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new(
dxgiFormat,
width,
height,
1,
(elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1));
D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
d3D12PlacedSubresourceFootprint.Offset = 0;
d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint;
D3D12_TEXTURE_COPY_LOCATION
d3D12TextureCopyLocationDestination = new(d3d12ResourceDestination, d3D12PlacedSubresourceFootprint),
d3D12TextureCopyLocationSource = new(d3D12ResourceSource, 0);
D3D12_BOX d3D12Box = new((int)x, (int)y, (int)(x + width), (int)(y + height));
d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, 0, 0, 0, &d3D12TextureCopyLocationSource, &d3D12Box);
}
}
}
+44
ファイルの表示
@@ -0,0 +1,44 @@
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.Diagnostics;
using static TerraFX.Interop.DXGI_FORMAT;
using TerraFX.Interop;
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// A helper type with utility methods for <see cref="DXGI_FORMAT"/>.
/// </summary>
internal static class DXGIFormatHelper
{
/// <summary>
/// Gets the appropriate <see cref="DXGI_FORMAT"/> value for the input type argument.
/// </summary>
/// <typeparam name="T">The input type argument to get the corresponding <see cref="DXGI_FORMAT"/> for.</typeparam>
/// <returns>The <see cref="DXGI_FORMAT"/> value corresponding to <typeparamref name="T"/>.</returns>
/// <exception cref="System.ArgumentException">Thrown when the input type <typeparamref name="T"/> is not supported.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DXGI_FORMAT GetForType<T>()
where T : unmanaged
{
if (typeof(T) == typeof(int)) return DXGI_FORMAT_R32_SINT;
else if (typeof(T) == typeof(Int2)) return DXGI_FORMAT_R32G32_SINT;
else if (typeof(T) == typeof(Int3)) return DXGI_FORMAT_R32G32B32_SINT;
else if (typeof(T) == typeof(Int4)) return DXGI_FORMAT_R32G32B32A32_SINT;
else if (typeof(T) == typeof(uint)) return DXGI_FORMAT_R32_UINT;
else if (typeof(T) == typeof(UInt2)) return DXGI_FORMAT_R32G32_UINT;
else if (typeof(T) == typeof(UInt3)) return DXGI_FORMAT_R32G32B32_UINT;
else if (typeof(T) == typeof(UInt4)) return DXGI_FORMAT_R32G32B32A32_UINT;
else if (typeof(T) == typeof(float)) return DXGI_FORMAT_R32_FLOAT;
else if (typeof(T) == typeof(Float2)) return DXGI_FORMAT_R32G32_FLOAT;
else if (typeof(T) == typeof(Float3)) return DXGI_FORMAT_R32G32B32_FLOAT;
else if (typeof(T) == typeof(Float4)) return DXGI_FORMAT_R32G32B32A32_FLOAT;
else if (typeof(T) == typeof(Vector2)) return DXGI_FORMAT_R32G32_FLOAT;
else if (typeof(T) == typeof(Vector3)) return DXGI_FORMAT_R32G32B32_FLOAT;
else if (typeof(T) == typeof(Vector4)) return DXGI_FORMAT_R32G32B32A32_FLOAT;
else return ThrowHelper.ThrowArgumentException<DXGI_FORMAT>("Invalid texture type");
}
}
}
+26 -10
ファイルの表示
@@ -50,8 +50,8 @@ namespace ComputeSharp.Shaders.Mappings
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a known buffer type.</returns>
[Pure]
public static bool IsKnownBufferType(Type type) => IsConstantBufferType(type) ||
IsReadOnlyBufferType(type) ||
IsReadWriteBufferType(type);
IsReadOnlyResourceType(type) ||
IsReadWriteResourceType(type);
/// <summary>
/// Checks whether or not the input type is a <see cref="ConstantBuffer{T}"/> value.
@@ -63,21 +63,37 @@ namespace ComputeSharp.Shaders.Mappings
type.GetGenericTypeDefinition() == typeof(ConstantBuffer<>);
/// <summary>
/// Checks whether or not the input type is a <see cref="ReadOnlyBuffer{T}"/> value.
/// Checks whether or not the input type is a <see cref="ReadOnlyBuffer{T}"/> or <see cref="ReadOnlyTexture2D{T}"/> value.
/// </summary>
/// <param name="type">The input <see cref="Type"/> instance to check.</param>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a <see cref="ReadOnlyBuffer{T}"/> instance.</returns>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is a readonly resource type.</returns>
[Pure]
public static bool IsReadOnlyBufferType(Type type) => type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(ReadOnlyBuffer<>);
public static bool IsReadOnlyResourceType(Type type)
{
if (!type.IsGenericType) return false;
Type genericType = type.GetGenericTypeDefinition();
return
genericType == typeof(ReadOnlyBuffer<>) ||
genericType == typeof(ReadOnlyTexture2D<>);
}
/// <summary>
/// Checks whether or not the input type is a <see cref="ReadWriteBuffer{T}"/> value.
/// Checks whether or not the input type is a <see cref="ReadWriteBuffer{T}"/> or <see cref="ReadWriteTexture2D{T}"/> value.
/// </summary>
/// <param name="type">The input <see cref="Type"/> instance to check.</param>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a <see cref="ReadWriteBuffer{T}"/> instance.</returns>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is a writeable resource type.</returns>
[Pure]
public static bool IsReadWriteBufferType(Type type) => type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>);
public static bool IsReadWriteResourceType(Type type)
{
if (!type.IsGenericType) return false;
Type genericType = type.GetGenericTypeDefinition();
return
genericType == typeof(ReadWriteBuffer<>) ||
genericType == typeof(ReadWriteTexture2D<>);
}
}
}
+4 -4
ファイルの表示
@@ -60,14 +60,14 @@ namespace ComputeSharp.Shaders.Renderer
builder.AppendLine('}');
// Buffers
foreach (var buffer in info.HslsBuffersInfo)
foreach (var buffer in info.HlslResourceInfo)
{
builder.AppendLine();
switch (buffer)
{
// Constant buffer go to cbuffer fields with a dummy local
case HlslBufferInfo.Constant _:
case HlslResourceInfo.Constant _:
builder.Append("cbuffer _");
builder.Append(buffer.FieldName);
builder.Append(" : register(b");
@@ -83,10 +83,10 @@ namespace ComputeSharp.Shaders.Renderer
break;
// Structured buffer have the same syntax but different id
case HlslBufferInfo.ReadOnly _:
case HlslResourceInfo.ReadOnly _:
char registerId = 't';
goto StructuredBuffer;
case HlslBufferInfo.ReadWrite _:
case HlslResourceInfo.ReadWrite _:
registerId = 'u';
StructuredBuffer:
builder.Append(buffer.FieldType);
@@ -1,32 +1,34 @@
namespace ComputeSharp.Shaders.Renderer.Models
{
/// <summary>
/// A model that contains info on a shader buffer field.
/// A model that contains info on a shader typed resource field.
/// </summary>
/// <param name="FieldType">The type of the current field in the HLSL shader.</param>
/// <param name="FieldName">The name to use for the current field.</param>
internal abstract record HlslBufferInfo(string FieldType, string FieldName, int BufferIndex)
internal abstract record HlslResourceInfo(string FieldType, string FieldName, int BufferIndex)
: CapturedFieldInfo(FieldType, FieldName)
{
/// <inheritdoc/>
/// <summary>
/// A model that represents a captured constant buffer in a shader.
/// </summary>
/// <inheritdoc/>
public sealed record Constant(string FieldType, string FieldName, int BufferIndex)
: HlslBufferInfo(FieldType, FieldName, BufferIndex);
: HlslResourceInfo(FieldType, FieldName, BufferIndex);
/// <inheritdoc/>
/// <summary>
/// A model that represents a captured readonly buffer in a shader.
/// A model that represents a captured readonly typed resource in a shader.
/// Maps to either <see cref="ComputeSharp.ReadOnlyBuffer{T}"/> or <see cref="ComputeSharp.ReadOnlyTexture2D{T}"/>.
/// </summary>
public sealed record ReadOnly(string FieldType, string FieldName, int BufferIndex)
: HlslBufferInfo(FieldType, FieldName, BufferIndex);
/// <inheritdoc/>
public sealed record ReadOnly(string FieldType, string FieldName, int BufferIndex)
: HlslResourceInfo(FieldType, FieldName, BufferIndex);
/// <summary>
/// A model that represents a captured writeable buffer in a shader.
/// Maps to either <see cref="ComputeSharp.ReadWriteBuffer{T}"/> or <see cref="ComputeSharp.ReadWriteTexture2D{T}"/>.
/// </summary>
/// <inheritdoc/>
public sealed record ReadWrite(string FieldType, string FieldName, int BufferIndex)
: HlslBufferInfo(FieldType, FieldName, BufferIndex);
: HlslResourceInfo(FieldType, FieldName, BufferIndex);
}
}
-62
ファイルの表示
@@ -1,62 +0,0 @@
using System.Collections.Generic;
#pragma warning disable CS8618
namespace ComputeSharp.Shaders.Renderer.Models
{
/// <summary>
/// A <see langword="class"/> that contains info to render a shader source to compile.
/// </summary>
internal sealed class ShaderInfo
{
/// <summary>
/// Gets or sets the list of captured buffers being present in the current shader.
/// </summary>
public IReadOnlyList<HlslBufferInfo> BuffersList { get; init; }
/// <summary>
/// Gets or sets the list of captured variables being present in the current shader.
/// </summary>
public IReadOnlyList<CapturedFieldInfo> FieldsList { get; init; }
/// <summary>
/// Gets or sets the number of threads in the X group.
/// </summary>
public int NumThreadsX { get; init; }
/// <summary>
/// Gets or sets the number of threads in the Y group.
/// </summary>
public int NumThreadsY { get; init; }
/// <summary>
/// Gets or sets the number of threads in the Z group.
/// </summary>
public int NumThreadsZ { get; init; }
/// <summary>
/// Gets or sets the name for the threads id variable used in the shader.
/// </summary>
public string ThreadsIdsVariableName { get; init; }
/// <summary>
/// Gets or sets the entry point of the shader.
/// </summary>
public string EntryPoint { get; init; }
/// <summary>
/// Gets or sets the list of static functions used by the shader.
/// </summary>
public IReadOnlyCollection<object> FunctionsList { get; init; } // TODO
/// <summary>
/// Gets or sets the list of local functions used by the shader.
/// </summary>
public IReadOnlyCollection<string> LocalFunctionsList { get; init; }
/// <summary>
/// Gets or sets the list of dependent types for the shader.
/// </summary>
public IReadOnlyCollection<string> DeclaredTypes { get; init; }
}
}
+2 -2
ファイルの表示
@@ -14,9 +14,9 @@ namespace ComputeSharp.Shaders.Translation
public string EntryPoint { get; }
/// <summary>
/// Gets the collection of <see cref="HlslBufferInfo"/> items for the shader fields.
/// Gets the collection of <see cref="Renderer.Models.HlslResourceInfo"/> items for the shader fields.
/// </summary>
public IReadOnlyList<HlslBufferInfo> HslsBuffersInfo { get; }
public IReadOnlyList<HlslResourceInfo> HlslResourceInfo { get; }
/// <summary>
/// Gets the collection of <see cref="CapturedFieldInfo"/> items for the shader fields.
+2 -2
ファイルの表示
@@ -114,14 +114,14 @@ namespace ComputeSharp.Shaders.Translation
nameof(NativeObject.ThrowIfDisposed),
BindingFlags.Instance | BindingFlags.NonPublic)!, null);
// Call Buffer<T>.ThrowIfDeviceMismatch(GraphicsDevice)
// Call ThrowIfDeviceMismatch(GraphicsDevice)
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Ldarg_1);
il.EmitCall(OpCodes.Callvirt, fieldInfo.FieldType.GetMethod(
nameof(Buffer<byte>.ThrowIfDeviceMismatch),
BindingFlags.Instance | BindingFlags.NonPublic)!, null);
// Access Buffer<T>.D3D12GpuDescriptorHandle
// Access D3D12GpuDescriptorHandle
il.EmitReadMember(fieldInfo.FieldType.GetField(
nameof(Buffer<byte>.D3D12GpuDescriptorHandle),
BindingFlags.Instance | BindingFlags.NonPublic)!);
+8 -11
ファイルの表示
@@ -47,9 +47,9 @@ namespace ComputeSharp.Shaders.Translation
private readonly List<D3D12_DESCRIPTOR_RANGE1> d3D12DescriptorRanges1 = new();
/// <summary>
/// The <see cref="List{T}"/> with the <see cref="HlslBufferInfo"/> items for the shader fields.
/// The <see cref="List{T}"/> with the <see cref="Renderer.Models.HlslResourceInfo"/> items for the shader fields.
/// </summary>
private readonly List<HlslBufferInfo> hlslBuffersInfo = new();
private readonly List<HlslResourceInfo> hlslResourceInfo = new();
/// <summary>
/// The <see cref="List{T}"/> with the <see cref="CapturedFieldInfo"/> items for the shader fields.
@@ -85,7 +85,7 @@ namespace ComputeSharp.Shaders.Translation
public string EntryPoint { get; private set; }
/// <inheritdoc/>
public IReadOnlyList<HlslBufferInfo> HslsBuffersInfo => this.hlslBuffersInfo;
public IReadOnlyList<HlslResourceInfo> HlslResourceInfo => this.hlslResourceInfo;
/// <inheritdoc/>
public IReadOnlyList<CapturedFieldInfo> FieldsInfo => this.fieldsInfo;
@@ -156,32 +156,29 @@ namespace ComputeSharp.Shaders.Translation
Type fieldType = fieldInfo.FieldType;
(string hlslName, string hlslType) = Attribute.Fields[fieldInfo.Name];
// Constant buffer
if (HlslKnownTypes.IsConstantBufferType(fieldType))
{
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount);
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
this.capturedFields.Add(fieldInfo);
this.hlslBuffersInfo.Add(new HlslBufferInfo.Constant(hlslType, hlslName, (int)this.constantBuffersCount++));
this.hlslResourceInfo.Add(new HlslResourceInfo.Constant(hlslType, hlslName, (int)this.constantBuffersCount++));
}
else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType))
else if (HlslKnownTypes.IsReadOnlyResourceType(fieldType))
{
// Root parameter for a readonly buffer
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, this.readOnlyBuffersCount);
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
this.capturedFields.Add(fieldInfo);
this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadOnly(hlslType, hlslName, (int)this.readOnlyBuffersCount++));
this.hlslResourceInfo.Add(new HlslResourceInfo.ReadOnly(hlslType, hlslName, (int)this.readOnlyBuffersCount++));
}
else if (HlslKnownTypes.IsReadWriteBufferType(fieldType))
else if (HlslKnownTypes.IsReadWriteResourceType(fieldType))
{
// Root parameter for a read write buffer
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, this.readWriteBuffersCount);
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
this.capturedFields.Add(fieldInfo);
this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++));
this.hlslResourceInfo.Add(new HlslResourceInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++));
}
else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType))
{
+1 -1
ファイルの表示
@@ -120,7 +120,7 @@ namespace ComputeSharp.SourceGenerators
yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name, typeName };
// Track the type of items in the current buffer
if (HlslKnownTypes.IsStructuredBufferType(typeSymbol.GetFullMetadataName()))
if (HlslKnownTypes.IsTypedResourceType(typeSymbol.GetFullMetadataName()))
{
types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]);
}
+9 -5
ファイルの表示
@@ -57,15 +57,17 @@ namespace ComputeSharp.SourceGenerators.Mappings
});
/// <summary>
/// Checks whether or not a given type name matches a structured buffer type.
/// Checks whether or not a given type name matches a typed resource type.
/// </summary>
/// <param name="typeName">The input type name to check.</param>
/// <returns>Whether or not <paramref name="typeName"/> represents a structured buffer type.</returns>
public static bool IsStructuredBufferType(string typeName)
/// <returns>Whether or not <paramref name="typeName"/> represents a typed resource type.</returns>
public static bool IsTypedResourceType(string typeName)
{
return
typeName == "ComputeSharp.ReadOnlyBuffer`1" ||
typeName == "ComputeSharp.ReadWriteBuffer`1";
typeName == "ComputeSharp.ReadWriteBuffer`1" ||
typeName == "ComputeSharp.ReadOnlyTexture2D`1" ||
typeName == "ComputeSharp.ReadWriteTexture2D`1";
}
/// <summary>
@@ -83,7 +85,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
string typeName = typeSymbol.GetFullMetadataName();
// Special case for the structured buffer types
if (IsStructuredBufferType(typeName))
if (IsTypedResourceType(typeName))
{
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments[0]).GetFullMetadataName();
@@ -98,6 +100,8 @@ namespace ComputeSharp.SourceGenerators.Mappings
{
"ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mapped}>",
"ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mapped}>",
"ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mapped}>",
"ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mapped}>",
_ => throw new ArgumentException()
};
}
+184 -12
ファイルの表示
@@ -36,7 +36,7 @@ namespace ComputeSharp
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, T[] array)
where T : unmanaged
{
return device.AllocateConstantBuffer(array.AsSpan());
return device.AllocateConstantBuffer<T>(array.AsSpan());
}
/// <summary>
@@ -44,10 +44,10 @@ namespace ComputeSharp
/// </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>
/// <param name="span">The input <see cref="ReadOnlySpan{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="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, Span<T> span)
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, ReadOnlySpan<T> span)
where T : unmanaged
{
ConstantBuffer<T> buffer = new(device, span.Length);
@@ -100,7 +100,7 @@ namespace ComputeSharp
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, T[] array)
where T : unmanaged
{
return device.AllocateReadOnlyBuffer(array.AsSpan());
return device.AllocateReadOnlyBuffer<T>(array.AsSpan());
}
/// <summary>
@@ -108,10 +108,10 @@ namespace ComputeSharp
/// </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>
/// <param name="span">The input <see cref="ReadOnlySpan{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="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, Span<T> span)
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, ReadOnlySpan<T> span)
where T : unmanaged
{
ReadOnlyBuffer<T> buffer = new(device, span.Length);
@@ -164,7 +164,7 @@ namespace ComputeSharp
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, T[] array)
where T : unmanaged
{
return device.AllocateReadWriteBuffer(array.AsSpan());
return device.AllocateReadWriteBuffer<T>(array.AsSpan());
}
/// <summary>
@@ -172,10 +172,10 @@ namespace ComputeSharp
/// </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>
/// <param name="span">The input <see cref="ReadOnlySpan{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="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, Span<T> span)
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, ReadOnlySpan<T> span)
where T : unmanaged
{
ReadWriteBuffer<T> buffer = new(device, span.Length);
@@ -203,6 +203,178 @@ namespace ComputeSharp
return readWriteBuffer;
}
/// <summary>
/// Allocates a new readonly 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A zeroed <see cref="ReadOnlyTexture2D{T}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>].</returns>
[Pure]
public static ReadOnlyTexture2D<T> AllocateReadOnlyTexture2D<T>(this GraphicsDevice device, int width, int height)
where T : unmanaged
{
return new(device, width, height);
}
/// <summary>
/// Allocates a new readonly 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadOnlyTexture2D<T> AllocateReadOnlyTexture2D<T>(this GraphicsDevice device, T[] array, int width, int height)
where T : unmanaged
{
return device.AllocateReadOnlyTexture2D<T>(array.AsSpan(), width, height);
}
/// <summary>
/// Allocates a new readonly 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadOnlyTexture2D<T> AllocateReadOnlyTexture2D<T>(this GraphicsDevice device, T[] array, int offset, int width, int height)
where T : unmanaged
{
return device.AllocateReadOnlyTexture2D<T>(array.AsSpan(offset), width, height);
}
/// <summary>
/// Allocates a new readonly 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadOnlyTexture2D{T}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadOnlyTexture2D<T> AllocateReadOnlyTexture2D<T>(this GraphicsDevice device, T[,] array)
where T : unmanaged
{
ReadOnlyTexture2D<T> texture = new(device, array.GetLength(1), array.GetLength(0));
texture.SetData(array);
return texture;
}
/// <summary>
/// Allocates a new readonly 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="span">The input <see cref="ReadOnlySpan{T}"/> with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ReadOnlyTexture2D<T> AllocateReadOnlyTexture2D<T>(this GraphicsDevice device, ReadOnlySpan<T> span, int width, int height)
where T : unmanaged
{
ReadOnlyTexture2D<T> texture = new(device, width, height);
texture.SetData(span);
return texture;
}
/// <summary>
/// Allocates a new writeable 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A zeroed <see cref="ReadWriteTexture2D{T}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>].</returns>
[Pure]
public static ReadWriteTexture2D<T> AllocateReadWriteTexture2D<T>(this GraphicsDevice device, int width, int height)
where T : unmanaged
{
return new(device, width, height);
}
/// <summary>
/// Allocates a new writeable 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadWriteTexture2D<T> AllocateReadWriteTexture2D<T>(this GraphicsDevice device, T[] array, int width, int height)
where T : unmanaged
{
return device.AllocateReadWriteTexture2D<T>(array.AsSpan(), width, height);
}
/// <summary>
/// Allocates a new writeable 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadWriteTexture2D<T> AllocateReadWriteTexture2D<T>(this GraphicsDevice device, T[] array, int offset, int width, int height)
where T : unmanaged
{
return device.AllocateReadWriteTexture2D<T>(array.AsSpan(offset), width, height);
}
/// <summary>
/// Allocates a new writeable 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="array">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadWriteTexture2D{T}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadWriteTexture2D<T> AllocateReadWriteTexture2D<T>(this GraphicsDevice device, T[,] array)
where T : unmanaged
{
ReadWriteTexture2D<T> texture = new(device, array.GetLength(1), array.GetLength(0));
texture.SetData(array);
return texture;
}
/// <summary>
/// Allocates a new writeable 2D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="span">The input <see cref="ReadOnlySpan{T}"/> with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ReadWriteTexture2D<T> AllocateReadWriteTexture2D<T>(this GraphicsDevice device, ReadOnlySpan<T> span, int width, int height)
where T : unmanaged
{
ReadWriteTexture2D<T> texture = new(device, width, height);
texture.SetData(span);
return texture;
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>