diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T,TPixel}.cs new file mode 100644 index 00000000..a49e97ab --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T,TPixel}.cs @@ -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 +{ + /// + /// A representing a typed read write 2D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadWriteTexture2D : Texture2D + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The width of the texture. + /// The height of the texture. + internal ReadWriteTexture2D(GraphicsDevice device, int width, int height) + : base(device, width, height, ResourceType.ReadWrite) + { + } + + /// + /// Gets a single value from the current writeable texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D)}[int,int]"); + + /// + /// Gets or sets a single value from the current writeable texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D)}[Int2]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadWriteTexture2D<{typeof(T)},{typeof(TPixel)}>[{Width}, {Height}]"; + } + } +} diff --git a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs index be8d5d21..6260d27e 100644 --- a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs @@ -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("Invalid texture type"); } } diff --git a/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs index b3fe7b73..0e5c821b 100644 --- a/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs @@ -95,6 +95,7 @@ namespace ComputeSharp.Shaders.Mappings return genericType == typeof(ReadWriteBuffer<>) || genericType == typeof(ReadWriteTexture2D<>) || + genericType == typeof(ReadWriteTexture2D<,>) || genericType == typeof(ReadWriteTexture3D<>); } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index a90ba55c..fa51f8ba 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -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]); } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 7a7df32b..a1212d69 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -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" }; diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 43ba20b4..68433561 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -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", "ComputeSharp.ReadOnlyTexture3D`1" => $"Texture3D<{mapped}>", "ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mapped}>", _ => throw new ArgumentException() diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index e22ed529..4da8df60 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -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 { /// @@ -469,6 +472,28 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadWriteTexture2D texture = new(device, width, height); + + texture.SetData(span); + + return texture; + } + /// /// Allocates a new writeable 3D texture with the specified parameters. ///