Added initial Texture2D<T> base type

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-02 20:17:10 +01:00
1 親 5fdf6d4c5c
コミット 4c8fadac6f
5ファイル変更
+121 -16

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

+2 -2
ファイルの表示
@@ -40,7 +40,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <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)
private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, BufferType bufferType)
{
device.ThrowIfDisposed();
@@ -69,7 +69,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
/// <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; }
+1 -1
ファイルの表示
@@ -24,7 +24,7 @@ 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="bufferType">The buffer type for the current buffer.</param>
internal StructuredBuffer(GraphicsDevice device, int length, BufferType bufferType)
private protected StructuredBuffer(GraphicsDevice device, int length, BufferType bufferType)
: base(device, length, (uint)Unsafe.SizeOf<T>(), bufferType)
{
}
+88
ファイルの表示
@@ -0,0 +1,88 @@
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Interop;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
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>
internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
/// <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="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>
private protected Texture2D(GraphicsDevice device, int height, int width, TextureType textureType)
{
device.ThrowIfDisposed();
Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height));
Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width));
GraphicsDevice = device;
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(textureType, DXGIFormatHelper.GetForType<T>(), (uint)height, (uint)width);
device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (textureType)
{
case TextureType.ReadOnly:
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), d3D12CpuDescriptorHandle);
break;
case TextureType.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 <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <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);
}
}
}
}
+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);
}
}
}
+5 -5
ファイルの表示
@@ -1,9 +1,9 @@
using static TerraFX.Interop.DXGI_FORMAT;
using TerraFX.Interop;
using ABI.System.Numerics;
using Microsoft.Toolkit.Diagnostics;
using System.Diagnostics.Contracts;
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
{