From b54fb3c7dfaa6112064f8a9640f4bacf452e4d71 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 4 Jan 2021 16:37:51 +0100 Subject: [PATCH] Added missing normalized texture types --- .../Buffers/ReadOnlyTexture2D{T,TPixel}.cs | 56 ++++++++++++++++++ .../Buffers/ReadOnlyTexture3D{T,TPixel}.cs | 58 +++++++++++++++++++ .../Buffers/ReadWriteTexture3D{T,TPixel}.cs | 58 +++++++++++++++++++ .../Translation/Mappings/HlslKnownTypes.cs | 7 ++- .../Mappings/HlslKnownMembers.cs | 5 +- .../Mappings/HlslKnownTypes.cs | 7 ++- 6 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T,TPixel}.cs create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T,TPixel}.cs create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T,TPixel}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T,TPixel}.cs new file mode 100644 index 00000000..771fb853 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{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 readonly 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 ReadOnlyTexture2D : 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 ReadOnlyTexture2D(GraphicsDevice device, int width, int height) + : base(device, width, height, ResourceType.ReadOnly) + { + } + + /// + /// Gets a single value from the current readonly 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 T this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[int,int]"); + + /// + /// Gets a single value from the current readonly 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 T this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[Int2]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadOnlyTexture2D<{typeof(T)},{typeof(TPixel)}>[{Width}, {Height}]"; + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T,TPixel}.cs new file mode 100644 index 00000000..e0eaedb0 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T,TPixel}.cs @@ -0,0 +1,58 @@ +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 readonly 3D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadOnlyTexture3D : Texture3D + 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. + /// The depth of the texture. + internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth) + : base(device, width, height, depth, ResourceType.ReadOnly) + { + } + + /// + /// Gets a single value from the current readonly texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// The depthwise 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 T this[int x, int y, int z] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[int,int,int]"); + + /// + /// Gets a single value from the current readonly 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 T this[Int3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[Int3]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadOnlyTexture3D<{typeof(T)},{nameof(TPixel)}>[{Width}, {Height}, {Depth}]"; + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T,TPixel}.cs new file mode 100644 index 00000000..2d44f219 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T,TPixel}.cs @@ -0,0 +1,58 @@ +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 readonly 3D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadWriteTexture3D : Texture3D + 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. + /// The depth of the texture. + internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth) + : base(device, width, height, depth, 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. + /// The depthwise 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 T this[int x, int y, int z] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[int,int,int]"); + + /// + /// Gets 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 T this[Int3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[Int3]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadWriteTexture3D<{typeof(T)},{nameof(TPixel)}>[{Width}, {Height}, {Depth}]"; + } + } +} diff --git a/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs index 0e5c821b..b2c84d86 100644 --- a/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs @@ -77,7 +77,9 @@ namespace ComputeSharp.Shaders.Mappings return genericType == typeof(ReadOnlyBuffer<>) || genericType == typeof(ReadOnlyTexture2D<>) || - genericType == typeof(ReadOnlyTexture3D<>); + genericType == typeof(ReadOnlyTexture2D<,>) || + genericType == typeof(ReadOnlyTexture3D<>) || + genericType == typeof(ReadOnlyTexture3D<,>); } /// @@ -96,7 +98,8 @@ namespace ComputeSharp.Shaders.Mappings genericType == typeof(ReadWriteBuffer<>) || genericType == typeof(ReadWriteTexture2D<>) || genericType == typeof(ReadWriteTexture2D<,>) || - genericType == typeof(ReadWriteTexture3D<>); + genericType == typeof(ReadWriteTexture3D<>) || + genericType == typeof(ReadWriteTexture3D<,>); } } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index a1212d69..e3751ef4 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -18,10 +18,13 @@ namespace ComputeSharp.SourceGenerators.Mappings private static readonly IReadOnlyDictionary KnownIndexers = new Dictionary { [$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.ReadOnlyTexture2D`2.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" + [$"ComputeSharp.ReadOnlyTexture3D`2.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", + [$"ComputeSharp.ReadWriteTexture3D`2.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 68433561..2bc27d99 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.Diagnostics.Contracts; using System.Linq; using System.Numerics; @@ -72,10 +71,13 @@ namespace ComputeSharp.SourceGenerators.Mappings case "ComputeSharp.ReadOnlyBuffer`1": case "ComputeSharp.ReadWriteBuffer`1": case "ComputeSharp.ReadOnlyTexture2D`1": + case "ComputeSharp.ReadOnlyTexture2D`2": case "ComputeSharp.ReadWriteTexture2D`1": case "ComputeSharp.ReadWriteTexture2D`2": case "ComputeSharp.ReadOnlyTexture3D`1": + case "ComputeSharp.ReadOnlyTexture3D`2": case "ComputeSharp.ReadWriteTexture3D`1": + case "ComputeSharp.ReadWriteTexture3D`2": return true; default: return false; }; @@ -125,10 +127,13 @@ namespace ComputeSharp.SourceGenerators.Mappings "ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mapped}>", "ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mapped}>", "ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mapped}>", + "ComputeSharp.ReadOnlyTexture2D`2" => $"Texture2D", "ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mapped}>", "ComputeSharp.ReadWriteTexture2D`2" => $"RWTexture2D", "ComputeSharp.ReadOnlyTexture3D`1" => $"Texture3D<{mapped}>", + "ComputeSharp.ReadOnlyTexture3D`2" => $"Texture3D", "ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mapped}>", + "ComputeSharp.ReadWriteTexture3D`2" => $"RWTexture3D", _ => throw new ArgumentException() }; }