Added support for normalized 2D textures

このコミットが含まれているのは:
Sergio Pedri committed 2021-01-04 23:39:11 +01:00
1 親 050182367f
コミット aaf8b5cac4
7ファイル変更
+92 -3

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

+56
ファイルの表示
@@ -0,0 +1,56 @@
using System.Diagnostics;
using ComputeSharp.__Internals;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Buffers.Views;
#pragma warning disable CS0618
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>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
[DebuggerTypeProxy(typeof(Texture2DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadWriteTexture2D<T, TPixel> : Texture2D<T>
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadWriteTexture2D{T,TPixel}"/> 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 a single <typeparamref name="T"/> value from the current writeable texture.
/// </summary>
/// <param name="x">The horizontal offset of the value to get.</param>
/// <param name="y">The vertical offset 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 TPixel this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D<T, TPixel>)}<T>[int,int]");
/// <summary>
/// Gets or sets a single <typeparamref name="T"/> value from the current writeable 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 TPixel this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D<T, TPixel>)}<T>[Int2]");
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadWriteTexture2D<{typeof(T)},{typeof(TPixel)}>[{Width}, {Height}]";
}
}
}
+2 -1
ファイルの表示
@@ -2,8 +2,8 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.Diagnostics;
using static TerraFX.Interop.DXGI_FORMAT;
using TerraFX.Interop;
using static TerraFX.Interop.DXGI_FORMAT;
namespace ComputeSharp.Graphics.Helpers
{
@@ -38,6 +38,7 @@ namespace ComputeSharp.Graphics.Helpers
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 if (typeof(T) == typeof(Bgra32)) return DXGI_FORMAT_B8G8R8A8_UNORM;
else return ThrowHelper.ThrowArgumentException<DXGI_FORMAT>("Invalid texture type");
}
}
+1
ファイルの表示
@@ -95,6 +95,7 @@ namespace ComputeSharp.Shaders.Mappings
return
genericType == typeof(ReadWriteBuffer<>) ||
genericType == typeof(ReadWriteTexture2D<>) ||
genericType == typeof(ReadWriteTexture2D<,>) ||
genericType == typeof(ReadWriteTexture3D<>);
}
}
+2 -1
ファイルの表示
@@ -122,7 +122,8 @@ namespace ComputeSharp.SourceGenerators
yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name, typeName };
// Track the type of items in the current buffer
if (HlslKnownTypes.IsTypedResourceType(typeSymbol.GetFullMetadataName()))
if (HlslKnownTypes.IsTypedResourceType(typeSymbol.GetFullMetadataName()) &&
typeSymbol.TypeArguments.Length == 1)
{
types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]);
}
+1
ファイルの表示
@@ -19,6 +19,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
{
[$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
[$"ComputeSharp.ReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
[$"ComputeSharp.ReadWriteTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
[$"ComputeSharp.ReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
[$"ComputeSharp.ReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3"
};
+5 -1
ファイルの表示
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Numerics;
using ComputeSharp.SourceGenerators.Extensions;
using Microsoft.CodeAnalysis;
@@ -71,6 +73,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
case "ComputeSharp.ReadWriteBuffer`1":
case "ComputeSharp.ReadOnlyTexture2D`1":
case "ComputeSharp.ReadWriteTexture2D`1":
case "ComputeSharp.ReadWriteTexture2D`2":
case "ComputeSharp.ReadOnlyTexture3D`1":
case "ComputeSharp.ReadWriteTexture3D`1":
return true;
@@ -107,7 +110,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
// Special case for the structured buffer types
if (IsTypedResourceType(typeName))
{
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments[0]).GetFullMetadataName();
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.Last()).GetFullMetadataName();
// If the current type is a custom type, format it as needed
if (!KnownTypes.TryGetValue(genericArgumentName, out string? mapped))
@@ -123,6 +126,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
"ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mapped}>",
"ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mapped}>",
"ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mapped}>",
"ComputeSharp.ReadWriteTexture2D`2" => $"RWTexture2D<unorm {mapped}>",
"ComputeSharp.ReadOnlyTexture3D`1" => $"Texture3D<{mapped}>",
"ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mapped}>",
_ => throw new ArgumentException()
+25
ファイルの表示
@@ -1,9 +1,12 @@
using System;
using System.Diagnostics.Contracts;
using ComputeSharp.__Internals;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Shaders;
#pragma warning disable CS0618
namespace ComputeSharp
{
/// <summary>
@@ -469,6 +472,28 @@ namespace ComputeSharp
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>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</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, TPixel> AllocateReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<T> span, int width, int height)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
ReadWriteTexture2D<T, TPixel> texture = new(device, width, height);
texture.SetData(span);
return texture;
}
/// <summary>
/// Allocates a new writeable 3D texture with the specified parameters.
/// </summary>