From f9a73d9b095895db76df6e700e0d9697a2384aaa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Sep 2023 03:30:18 +0200 Subject: [PATCH 1/4] Add ID2D1Shader.ConstantBufferSize property --- ...nerator.CreateInputCountProperty.Syntax.cs | 27 ------------- ...enerator.CreateNumericProperties.Syntax.cs | 39 +++++++++++++++++++ .../ID2D1ShaderGenerator.cs | 3 +- .../Interfaces/__Internals/ID2D1Shader.cs | 7 ++++ 4 files changed, 48 insertions(+), 28 deletions(-) delete mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateNumericProperties.Syntax.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs deleted file mode 100644 index bbde29e1..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs +++ /dev/null @@ -1,27 +0,0 @@ -using ComputeSharp.D2D1.SourceGenerators.Models; -using ComputeSharp.SourceGeneration.Extensions; -using ComputeSharp.SourceGeneration.Helpers; - -namespace ComputeSharp.D2D1.SourceGenerators; - -/// -partial class ID2D1ShaderGenerator -{ - /// - /// A helper with all logic to generate the InputCount property. - /// - private static partial class InputCount - { - /// - /// Writes the InputCount property. - /// - /// The input instance with gathered shader info. - /// The instance to write into. - public static void WriteSyntax(D2D1ShaderInfo info, IndentedTextWriter writer) - { - writer.WriteLine("/// "); - writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator)); - writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputCount => {info.InputTypes.Length};"); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateNumericProperties.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateNumericProperties.Syntax.cs new file mode 100644 index 00000000..1a45c069 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateNumericProperties.Syntax.cs @@ -0,0 +1,39 @@ +using ComputeSharp.D2D1.SourceGenerators.Models; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + /// A helper with all logic to generate the available numeric properties. + /// + private static partial class NumericProperties + { + /// + /// Writes the ConstantBufferSize property. + /// + /// The input instance with gathered shader info. + /// The instance to write into. + public static void WriteConstantBufferSizeSyntax(D2D1ShaderInfo info, IndentedTextWriter writer) + { + writer.WriteLine("/// "); + writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator)); + writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.ConstantBufferSize => {info.ConstantBufferSizeInBytes};"); + } + + /// + /// Writes the InputCount property. + /// + /// The input instance with gathered shader info. + /// The instance to write into. + public static void WriteInputCountSyntax(D2D1ShaderInfo info, IndentedTextWriter writer) + { + writer.WriteLine("/// "); + writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator)); + writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputCount => {info.InputTypes.Length};"); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index c1a0eb76..5561deae 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -201,7 +201,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator declaredMembers.Add(EffectMetadata.WriteEffectDescriptionSyntax); declaredMembers.Add(EffectMetadata.WriteEffectCategorySyntax); declaredMembers.Add(EffectMetadata.WriteEffectAuthorSyntax); - declaredMembers.Add(InputCount.WriteSyntax); + declaredMembers.Add(NumericProperties.WriteConstantBufferSizeSyntax); + declaredMembers.Add(NumericProperties.WriteInputCountSyntax); declaredMembers.Add(InputTypes.WriteSyntax); declaredMembers.Add(InputDescriptions.WriteSyntax); declaredMembers.Add(ResourceTextureDescriptions.WriteSyntax); diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 3da044d0..0cff2231 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -67,6 +67,13 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be used directly by user code")] D2D1PixelOptions PixelOptions { get; } + /// + /// Gets the size in bytes of the constant buffer for the current shader. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be used directly by user code")] + int ConstantBufferSize { get; } + /// /// Gets the number of inputs for the current shader. /// From 9d3188cec5dc6578693d38809d6857787939d0c5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Sep 2023 03:31:42 +0200 Subject: [PATCH 2/4] Remove unnecessary D2D1BufferSizeDispatchDataLoader type --- .../Shaders/Interop/D2D1PixelShader.cs | 4 +- .../D2D1BufferSizeDispatchDataLoader.cs | 63 ------------------- 2 files changed, 3 insertions(+), 64 deletions(-) delete mode 100644 src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 473af757..6d523ad1 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -387,7 +387,9 @@ public static class D2D1PixelShader public static int GetConstantBufferSize() where T : unmanaged, ID2D1PixelShader { - return D2D1BufferSizeDispatchDataLoader.For.ConstantBufferSize; + Unsafe.SkipInit(out T shader); + + return shader.ConstantBufferSize; } /// diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs deleted file mode 100644 index 24d38972..00000000 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.__Internals; -using TerraFX.Interop.DirectX; - -#pragma warning disable CS0618 - -namespace ComputeSharp.D2D1.Shaders.Loaders; - -/// -/// A data loader for D2D1 pixel shaders dispatched via , to just retrieve the constant buffer size. -/// -internal unsafe struct D2D1BufferSizeDispatchDataLoader : ID2D1DispatchDataLoader -{ - /// - /// The size of the constant buffer. - /// - private int size; - - /// - /// Gets the size of the constant buffer. - /// - /// The size of the constant buffer. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private readonly int GetConstantBufferSize() - { - return this.size; - } - - /// - void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data) - { - this.size = data.Length; - } - - /// - /// A helper class caching computed constant buffer sizes for generic shaders. - /// - /// The type of D2D1 pixel shader to retrieve info for. - public static class For - where T : unmanaged, ID2D1PixelShader - { - /// - /// Gets the constant buffer size for a shader of type . - /// - public static int ConstantBufferSize { get; } = GetConstantBufferSize(); - - /// - /// Calculates the constant buffer size for a shader of type . - /// - /// The constant buffer size for a shader of type . - private static int GetConstantBufferSize() - { - D2D1BufferSizeDispatchDataLoader dataLoader = default; - - Unsafe.SkipInit(out T shader); - - shader.LoadDispatchData(ref dataLoader); - - return dataLoader.GetConstantBufferSize(); - } - } -} \ No newline at end of file From ba732ac96a0590918f8c6ec25c1219b3dd9cf43c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Sep 2023 03:35:52 +0200 Subject: [PATCH 3/4] Add ID2D1Shader.ResourceTextureManagerCount property --- ...ShaderGenerator.CreateNumericProperties.Syntax.cs | 12 ++++++++++++ .../ID2D1ShaderGenerator.cs | 1 + .../Interfaces/__Internals/ID2D1Shader.cs | 7 +++++++ 3 files changed, 20 insertions(+) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateNumericProperties.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateNumericProperties.Syntax.cs index 1a45c069..1bb81881 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateNumericProperties.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateNumericProperties.Syntax.cs @@ -35,5 +35,17 @@ partial class ID2D1ShaderGenerator writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator)); writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputCount => {info.InputTypes.Length};"); } + + /// + /// Writes the ResourceTextureCount property. + /// + /// The input instance with gathered shader info. + /// The instance to write into. + public static void WriteResourceTextureCountSyntax(D2D1ShaderInfo info, IndentedTextWriter writer) + { + writer.WriteLine("/// "); + writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator)); + writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.ResourceTextureCount => {info.ResourceTextureDescriptions.Length};"); + } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 5561deae..ec56e55b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -203,6 +203,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator declaredMembers.Add(EffectMetadata.WriteEffectAuthorSyntax); declaredMembers.Add(NumericProperties.WriteConstantBufferSizeSyntax); declaredMembers.Add(NumericProperties.WriteInputCountSyntax); + declaredMembers.Add(NumericProperties.WriteResourceTextureCountSyntax); declaredMembers.Add(InputTypes.WriteSyntax); declaredMembers.Add(InputDescriptions.WriteSyntax); declaredMembers.Add(ResourceTextureDescriptions.WriteSyntax); diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 0cff2231..4fb00ab4 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -81,6 +81,13 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be used directly by user code")] int InputCount { get; } + /// + /// Gets the number of resource textures for the current shader. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be used directly by user code")] + int ResourceTextureCount { get; } + /// /// Gets the input types for the current shader. /// From a873fafdd4d3d1e000a6bd02c9e941f5eea9c9fe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Sep 2023 13:55:41 +0200 Subject: [PATCH 4/4] Add D2D1PixelShader.GetResourceTextureCount() and tests --- .../Shaders/Interop/D2D1PixelShader.cs | 13 +++++++ .../D2D1PixelShaderTests.cs | 37 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 6d523ad1..20cbbcce 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -295,6 +295,19 @@ public static class D2D1PixelShader return shader.InputCount; } + /// + /// Gets the number of resource textures for a D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to get the resource texture count for. + /// The number of resource texture for the D2D1 pixel shader of type . + public static int GetResourceTextureCount() + where T : unmanaged, ID2D1PixelShader + { + Unsafe.SkipInit(out T shader); + + return shader.ResourceTextureCount; + } + /// /// Gets the input types for a D2D1 pixel shader. /// diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index e8405d0a..5dfd2639 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -235,6 +235,43 @@ public partial class D2D1PixelShaderTests Assert.AreEqual(span[3].Dimensions, 2); } + [TestMethod] + public unsafe void GetResourceTextureCount_IsCorrect() + { + Assert.AreEqual(0, D2D1PixelShader.GetResourceTextureCount()); + Assert.AreEqual(1, D2D1PixelShader.GetResourceTextureCount()); + Assert.AreEqual(4, D2D1PixelShader.GetResourceTextureCount()); + } + + [D2DInputCount(4)] + [D2DInputSimple(0)] + [D2DInputSimple(2)] + [D2DInputComplex(1)] + [D2DInputComplex(3)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [AutoConstructor] + partial struct ShaderWithoutResourceTextures : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [AutoConstructor] + partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader + { + [D2DResourceTextureIndex(0)] + D2D1ResourceTexture1D myTexture; + + public Float4 Execute() + { + return this.myTexture.Sample(0); + } + } + [D2DInputCount(4)] [D2DInputSimple(0)] [D2DInputSimple(2)]