From 3471d6233b39bf944e9dfbe0355791a4dac19d70 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Aug 2022 02:12:19 +0200 Subject: [PATCH 1/5] Make D2D1 resource texture types generic --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 2 +- ...exture1D.cs => D2D1ResourceTexture1D{T}.cs} | 10 ++++++---- ...exture2D.cs => D2D1ResourceTexture2D{T}.cs} | 16 +++++++++------- ...exture3D.cs => D2D1ResourceTexture3D{T}.cs} | 18 ++++++++++-------- .../D2D1PixelShaderTests.cs | 6 +++--- 5 files changed, 29 insertions(+), 23 deletions(-) rename src/ComputeSharp.D2D1/Resources/{D2D1ResourceTexture1D.cs => D2D1ResourceTexture1D{T}.cs} (69%) rename src/ComputeSharp.D2D1/Resources/{D2D1ResourceTexture2D.cs => D2D1ResourceTexture2D{T}.cs} (70%) rename src/ComputeSharp.D2D1/Resources/{D2D1ResourceTexture3D.cs => D2D1ResourceTexture3D{T}.cs} (71%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 37fac13b..ec2e545f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -82,7 +82,7 @@ - + diff --git a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D.cs b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D{T}.cs similarity index 69% rename from src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D.cs rename to src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D{T}.cs index 4796f6f4..725c4b6c 100644 --- a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D.cs +++ b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D{T}.cs @@ -6,26 +6,28 @@ namespace ComputeSharp.D2D1; /// A representing a readonly 1D texture stored on GPU memory. /// This can be used from D2D1 pixel shaders to be able to pass arbitrary inputs to them. /// +/// The type of items stored on the texture (only and are supported). [AutoConstructorBehavior(AutoConstructorBehavior.IgnoreAndSetToDefault)] -public readonly struct D2D1ResourceTexture1D +public readonly struct D2D1ResourceTexture1D + where T : unmanaged { /// /// Gets the width of the current texture. /// /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public int Width => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture1D)}.{nameof(Width)}"); + public int Width => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture1D)}.{nameof(Width)}"); /// /// Gets a single value from the current readonly texture. /// /// The horizontal 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 readonly Float4 this[int x] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}[{typeof(int)}]"); + public ref readonly T this[int x] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture1D)}[{typeof(int)}]"); /// /// Retrieves a single value from the current readonly texture with linear sampling. /// /// The horizontal normalized 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 readonly Float4 Sample(float u) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Sample)}({typeof(float)})"); + public ref readonly T Sample(float u) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture1D)}.{nameof(Sample)}({typeof(float)})"); } diff --git a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D.cs b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D{T}.cs similarity index 70% rename from src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D.cs rename to src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D{T}.cs index dabb4e5f..902579f3 100644 --- a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D.cs +++ b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D{T}.cs @@ -6,20 +6,22 @@ namespace ComputeSharp.D2D1; /// A representing a readonly 2D texture stored on GPU memory. /// This can be used from D2D1 pixel shaders to be able to pass arbitrary inputs to them. /// +/// The type of items stored on the texture (only and are supported). [AutoConstructorBehavior(AutoConstructorBehavior.IgnoreAndSetToDefault)] -public readonly struct D2D1ResourceTexture2D +public readonly struct D2D1ResourceTexture2D + where T : unmanaged { /// /// Gets the width of the current texture. /// /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public int Width => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Width)}"); + public int Width => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Width)}"); /// /// Gets the height of the current texture. /// /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public int Height => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Height)}"); + public int Height => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Height)}"); /// /// Gets a single value from the current readonly texture. @@ -27,14 +29,14 @@ public readonly struct D2D1ResourceTexture2D /// 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 readonly Float4 this[int x, int y] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}[{typeof(int)}, {typeof(int)}]"); + public ref readonly T this[int x, int y] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}[{typeof(int)}, {typeof(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 ref readonly Float4 this[Int2 xy] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}[{typeof(Int2)}]"); + public ref readonly T this[Int2 xy] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}[{typeof(Int2)}]"); /// /// Retrieves a single value from the current readonly texture with linear sampling. @@ -42,12 +44,12 @@ public readonly struct D2D1ResourceTexture2D /// The horizontal normalized offset of the value to get. /// The vertical normalized 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 readonly Float4 Sample(float u, float v) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Sample)}({typeof(float)}, {typeof(float)})"); + public ref readonly T Sample(float u, float v) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Sample)}({typeof(float)}, {typeof(float)})"); /// /// Retrieves a single value from the current readonly texture with linear sampling. /// /// The normalized 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 readonly Float4 Sample(Float2 uv) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Sample)}({typeof(Float2)})"); + public ref readonly T Sample(Float2 uv) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture2D)}.{nameof(Sample)}({typeof(Float2)})"); } diff --git a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D.cs b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D{T}.cs similarity index 71% rename from src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D.cs rename to src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D{T}.cs index 9d50238e..dfe6936a 100644 --- a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D.cs +++ b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D{T}.cs @@ -6,26 +6,28 @@ namespace ComputeSharp.D2D1; /// A representing a readonly 3D texture stored on GPU memory. /// This can be used from D2D1 pixel shaders to be able to pass arbitrary inputs to them. /// +/// The type of items stored on the texture (only and are supported). [AutoConstructorBehavior(AutoConstructorBehavior.IgnoreAndSetToDefault)] -public readonly struct D2D1ResourceTexture3D +public readonly struct D2D1ResourceTexture3D + where T : unmanaged { /// /// Gets the width of the current texture. /// /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public int Width => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Width)}"); + public int Width => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Width)}"); /// /// Gets the height of the current texture. /// /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public int Height => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Height)}"); + public int Height => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Height)}"); /// /// Gets the depth of the current texture. /// /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public int Depth => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Depth)}"); + public int Depth => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Depth)}"); /// /// Gets a single value from the current readonly texture. @@ -34,14 +36,14 @@ public readonly struct D2D1ResourceTexture3D /// 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 readonly Float4 this[int x, int y, int z] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}[{typeof(int)}, {typeof(int)}, {typeof(int)}]"); + public ref readonly T this[int x, int y, int z] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}[{typeof(int)}, {typeof(int)}, {typeof(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 ref readonly Float4 this[Int3 xyz] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}[{typeof(Int3)}]"); + public ref readonly T this[Int3 xyz] => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}[{typeof(Int3)}]"); /// /// Retrieves a single value from the current readonly texture with linear sampling. @@ -50,12 +52,12 @@ public readonly struct D2D1ResourceTexture3D /// The vertical normalized 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 readonly Float4 Sample(float u, float v, float w) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Sample)}({typeof(float)}, {typeof(float)}, {typeof(float)})"); + public ref readonly T Sample(float u, float v, float w) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Sample)}({typeof(float)}, {typeof(float)}, {typeof(float)})"); /// /// Retrieves a single value from the current readonly texture with linear sampling. /// /// The normalized 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 readonly Float4 Sample(Float3 uvw) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Sample)}({typeof(Float3)})"); + public ref readonly T Sample(Float3 uvw) => throw new InvalidExecutionContextException($"{typeof(D2D1ResourceTexture3D)}.{nameof(Sample)}({typeof(Float3)})"); } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index d89d18e4..c0c06e06 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -215,13 +215,13 @@ public partial class D2D1PixelShaderTests float number; [D2DResourceTextureIndex(5)] - D2D1ResourceTexture1D myTexture1; + D2D1ResourceTexture1D myTexture1; [D2DResourceTextureIndex(6)] - D2D1ResourceTexture2D myTexture2; + D2D1ResourceTexture2D myTexture2; [D2DResourceTextureIndex(7)] - D2D1ResourceTexture3D myTexture3; + D2D1ResourceTexture3D myTexture3; public Float4 Execute() { From 97a87500e28eb07633786f7b2d270701910b5945 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Aug 2022 02:22:21 +0200 Subject: [PATCH 2/5] Update D2D1 mappings for generic resource texture types --- ...eateLoadResourceTextureDescriptionsMethod.cs | 4 ++-- .../Mappings/HlslKnownMethods.cs | 10 +++++----- .../Mappings/HlslKnownProperties.cs | 16 ++++++++-------- .../Mappings/HlslKnownTypes.cs | 17 +++++++++++------ 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs index 0d9d6340..b55de47e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs @@ -46,8 +46,8 @@ partial class ID2D1ShaderGenerator // Check that the field is a resource texture type (if not, it will be processed by the HLSL rewriter too) if (HlslKnownTypes.IsResourceTextureType(metadataName)) { - // The type name will be ComputeSharp.D2D1.D2D1ResourceTexture1D or the 2D/3D versions - int rank = int.Parse(metadataName.Substring(metadataName.Length - 2, 1)); + // The type name will be ComputeSharp.D2D1.D2D1ResourceTexture1D`1 or the 2D/3D versions + int rank = int.Parse(metadataName.Substring(metadataName.Length - 4, 1)); int? index = null; // Get the index from the [D2DResourceTextureIndex] attribute over the field diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs index debb52e2..c99d7315 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs @@ -43,11 +43,11 @@ partial class HlslKnownMethods { return new Dictionary { - [$"ComputeSharp.D2D1.D2D1ResourceTexture1D.Sample({typeof(float).FullName})"] = null, - [$"ComputeSharp.D2D1.D2D1ResourceTexture2D.Sample({typeof(float).FullName}, {typeof(float).FullName})"] = "float2", - [$"ComputeSharp.D2D1.D2D1ResourceTexture2D.Sample({typeof(Float2).FullName})"] = null, - [$"ComputeSharp.D2D1.D2D1ResourceTexture3D.Sample({typeof(float).FullName}, {typeof(float).FullName}, {typeof(float).FullName})"] = "float3", - [$"ComputeSharp.D2D1.D2D1ResourceTexture3D.Sample({typeof(Float3).FullName})"] = null + [$"ComputeSharp.D2D1.D2D1ResourceTexture1D`1.Sample({typeof(float).FullName})"] = null, + [$"ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Sample({typeof(float).FullName}, {typeof(float).FullName})"] = "float2", + [$"ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Sample({typeof(Float2).FullName})"] = null, + [$"ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Sample({typeof(float).FullName}, {typeof(float).FullName}, {typeof(float).FullName})"] = "float3", + [$"ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Sample({typeof(Float3).FullName})"] = null }; } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownProperties.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownProperties.cs index 690d790c..578bf8a5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownProperties.cs @@ -10,8 +10,8 @@ partial class HlslKnownProperties { return new Dictionary { - [$"ComputeSharp.D2D1.D2D1ResourceTexture2D.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", - [$"ComputeSharp.D2D1.D2D1ResourceTexture3D.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" + [$"ComputeSharp.D2D1.D2D1ResourceTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.D2D1.D2D1ResourceTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" }; } @@ -20,12 +20,12 @@ partial class HlslKnownProperties { return new Dictionary { - ["ComputeSharp.D2D1.D2D1ResourceTexture1D.Width"] = (1, 0), - ["ComputeSharp.D2D1.D2D1ResourceTexture2D.Width"] = (2, 0), - ["ComputeSharp.D2D1.D2D1ResourceTexture2D.Height"] = (2, 1), - ["ComputeSharp.D2D1.D2D1ResourceTexture3D.Width"] = (3, 0), - ["ComputeSharp.D2D1.D2D1ResourceTexture3D.Height"] = (3, 1), - ["ComputeSharp.D2D1.D2D1ResourceTexture3D.Depth"] = (3, 2) + ["ComputeSharp.D2D1.D2D1ResourceTexture1D`1.Width"] = (1, 0), + ["ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Width"] = (2, 0), + ["ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Height"] = (2, 1), + ["ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Width"] = (3, 0), + ["ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Height"] = (3, 1), + ["ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Depth"] = (3, 2) }; } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownTypes.cs index b25b41dc..5569583a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -16,9 +16,9 @@ partial class HlslKnownTypes { return typeName switch { - "ComputeSharp.D2D1.D2D1ResourceTexture1D" or - "ComputeSharp.D2D1.D2D1ResourceTexture2D" or - "ComputeSharp.D2D1.D2D1ResourceTexture3D" => true, + "ComputeSharp.D2D1.D2D1ResourceTexture1D`1" or + "ComputeSharp.D2D1.D2D1ResourceTexture2D`1" or + "ComputeSharp.D2D1.D2D1ResourceTexture3D`1" => true, _ => false }; } @@ -31,11 +31,16 @@ partial class HlslKnownTypes // Special case for resource texture types if (IsResourceTextureType(typeName)) { + string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments[0]).GetFullMetadataName(); + + // Get the HLSL name for the type argument (it can only be either float or float4) + _ = KnownHlslTypes.TryGetValue(genericArgumentName, out string? mappedElementType); + return typeName switch { - "ComputeSharp.D2D1.D2D1ResourceTexture1D" => "Texture1D", - "ComputeSharp.D2D1.D2D1ResourceTexture2D" => "Texture2D", - "ComputeSharp.D2D1.D2D1ResourceTexture3D" => "Texture3D", + "ComputeSharp.D2D1.D2D1ResourceTexture1D`1" => $"Texture1D<{mappedElementType}>", + "ComputeSharp.D2D1.D2D1ResourceTexture2D`1" => $"Texture2D<{mappedElementType}>", + "ComputeSharp.D2D1.D2D1ResourceTexture3D`1" => $"Texture3D<{mappedElementType}>", _ => throw new ArgumentException() }; } From 3515c4402bd17adf5430111dcaae5751b7af3ad0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Aug 2022 02:35:00 +0200 Subject: [PATCH 3/5] Add diagnostic for invalid D2D1 resource texture element type --- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++++++++++++++ ...haderGenerator.CreateBuildHlslSourceMethod.cs | 14 ++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index e6f78fde..2678d236 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -57,3 +57,4 @@ CMPSD2D0047 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0048 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0049 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0050 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0051 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index a1d5d3a2..0a87ce13 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -728,4 +728,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "All fields of a D2D1 resource texture type must be annotated using the [D2DResourceTextureIndex] attribute.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a resource texture using an invalid element type. + /// + /// Format: "The field \"{0}\" (in type {1}) using a D2D1 resource texture of type {2} has an invalid element type (only float and float4 type arguments are supported)". + /// + /// + public static readonly DiagnosticDescriptor InvalidResourceTextureElementType = new DiagnosticDescriptor( + id: "CMPSD2D0051", + title: "Missing [D2DResourceTextureIndex] attribute", + messageFormat: "The field \"{0}\" (in type {1}) using a D2D1 resource texture of type {2} has an invalid element type (only float and float4 type arguments are supported)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The element type of D2D1 resource texture fields in a D2D1 shader must be either float or float4.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index 2e0ea8c3..a838ae1e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -132,6 +132,20 @@ partial class ID2D1ShaderGenerator // Handle resource textures as a special case if (HlslKnownTypes.IsResourceTextureType(metadataName)) { + ITypeSymbol resourceTextureTypeArgumentSymbol = typeSymbol.TypeArguments[0]; + + // Validate that the type argument is only either float or float4 + if (!resourceTextureTypeArgumentSymbol.HasFullyQualifiedName("System.Single") && + !resourceTextureTypeArgumentSymbol.HasFullyQualifiedName("ComputeSharp.Float4")) + { + diagnostics.Add( + InvalidResourceTextureElementType, + fieldSymbol, + fieldSymbol.Name, + structDeclarationSymbol, + fieldSymbol.Type); + } + int index = 0; // If [D2DResourceTextureIndex] is present, get the resource texture index. From 4720e4d65cba205edee72c0274c5816a6106f761 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Aug 2022 20:41:08 +0200 Subject: [PATCH 4/5] Add float texture to test shader, compile it too --- tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index c0c06e06..7b3d1e3b 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -190,7 +190,7 @@ public partial class D2D1PixelShaderTests { ReadOnlyMemory resourceTextureDescriptions = D2D1PixelShader.GetResourceTextureDescriptions(); - Assert.AreEqual(resourceTextureDescriptions.Length, 3); + Assert.AreEqual(resourceTextureDescriptions.Length, 4); ReadOnlySpan span = resourceTextureDescriptions.Span; @@ -202,6 +202,9 @@ public partial class D2D1PixelShaderTests Assert.AreEqual(span[2].Index, 7); Assert.AreEqual(span[2].Rank, 3); + + Assert.AreEqual(span[3].Index, 8); + Assert.AreEqual(span[3].Rank, 2); } [D2DInputCount(4)] @@ -209,6 +212,7 @@ public partial class D2D1PixelShaderTests [D2DInputSimple(2)] [D2DInputComplex(1)] [D2DInputComplex(3)] + [D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)] [AutoConstructor] partial struct ShaderWithResourceTextures : ID2D1PixelShader { @@ -223,6 +227,9 @@ public partial class D2D1PixelShaderTests [D2DResourceTextureIndex(7)] D2D1ResourceTexture3D myTexture3; + [D2DResourceTextureIndex(8)] + D2D1ResourceTexture2D myTexture4; + public Float4 Execute() { float4 pixel1 = myTexture1[0]; @@ -231,6 +238,8 @@ public partial class D2D1PixelShaderTests float4 pixel4 = myTexture2.Sample(0.4f, 0.3f); float4 pixel5 = myTexture3[0, 1, 2]; float4 pixel6 = myTexture3.Sample(0.4f, 0.5f, 0.6f); + float number1 = myTexture4[0, 2]; + float number2 = myTexture4.Sample(0.4f, 0.6f); return 0; } From 1f64e0e7fcfc382a93888ec061fb523195f337e3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Aug 2022 22:43:29 +0200 Subject: [PATCH 5/5] Rename Rank to Dimensions in D2D1ResourceTextureDescription --- .../Shaders/Interop/D2D1ResourceTextureDescription.cs | 8 ++++---- tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureDescription.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureDescription.cs index 8ed7c690..43e2d6c7 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureDescription.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureDescription.cs @@ -9,11 +9,11 @@ public readonly struct D2D1ResourceTextureDescription /// Creates a new instance with the specified parameters. /// /// The index of the resource texture the description belongs to. - /// The rank of the resource texture the description belongs to. - internal D2D1ResourceTextureDescription(int index, int rank) + /// The number of dimensions of the resource texture the description belongs to. + internal D2D1ResourceTextureDescription(int index, int dimensions) { Index = index; - Rank = rank; + Dimensions = dimensions; } /// @@ -24,5 +24,5 @@ public readonly struct D2D1ResourceTextureDescription /// /// Gets the rank of the resource texture the description belongs to. /// - public int Rank { get; } + public int Dimensions { get; } } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index 7b3d1e3b..3028574b 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -195,16 +195,16 @@ public partial class D2D1PixelShaderTests ReadOnlySpan span = resourceTextureDescriptions.Span; Assert.AreEqual(span[0].Index, 5); - Assert.AreEqual(span[0].Rank, 1); + Assert.AreEqual(span[0].Dimensions, 1); Assert.AreEqual(span[1].Index, 6); - Assert.AreEqual(span[1].Rank, 2); + Assert.AreEqual(span[1].Dimensions, 2); Assert.AreEqual(span[2].Index, 7); - Assert.AreEqual(span[2].Rank, 3); + Assert.AreEqual(span[2].Dimensions, 3); Assert.AreEqual(span[3].Index, 8); - Assert.AreEqual(span[3].Rank, 2); + Assert.AreEqual(span[3].Dimensions, 2); } [D2DInputCount(4)]