From bbb42e5367b4d25aa71e0c1f39182765b5f88ee6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 21 Jul 2021 13:57:18 +0200 Subject: [PATCH] Switched generated methods to explicit interface implementations --- ...aderGenerator.CreateBuildHlslStringMethod.cs | 5 +++-- ...ShaderGenerator.CreateGetDispatchIdMethod.cs | 8 ++++++-- ...derGenerator.CreateLoadDispatchDataMethod.cs | 14 ++++++-------- ...enerator.CreateLoadDispatchMetadataMethod.cs | 14 ++++++-------- .../ShaderDataLoaderTests.cs | 17 +++++++++-------- .../ShaderHashCodeTests.cs | 13 +++++++------ 6 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs index 934d9b31..3ae64350 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs @@ -79,7 +79,7 @@ namespace ComputeSharp.SourceGenerators // This code produces a method declaration as follows: // - // public readonly void BuildHlslString(ref ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ) + // readonly void IShader.BuildHlslString(ref ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ) // { // // } @@ -87,7 +87,8 @@ namespace ComputeSharp.SourceGenerators MethodDeclaration( PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("BuildHlslString")) - .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.ReadOnlyKeyword)) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName(nameof(IShader)))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddParameterListParameters( Parameter(Identifier("builder")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(IdentifierName("ArrayPoolStringBuilder")), Parameter(Identifier("threadsX")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetDispatchIdMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetDispatchIdMethod.cs index 9f3e0235..aa056abb 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetDispatchIdMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetDispatchIdMethod.cs @@ -2,11 +2,14 @@ using System.Collections.Immutable; using System.Diagnostics.Contracts; using System.Linq; +using ComputeSharp.__Internals; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; +#pragma warning disable CS0618 + namespace ComputeSharp.SourceGenerators { /// @@ -17,7 +20,7 @@ namespace ComputeSharp.SourceGenerators { // This code produces a method declaration as follows: // - // public readonly int GetDispatchId() + // readonly int IShader.GetDispatchId() // { // // } @@ -25,7 +28,8 @@ namespace ComputeSharp.SourceGenerators MethodDeclaration( PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("GetDispatchId")) - .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.ReadOnlyKeyword)) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName(nameof(IShader)))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .WithBody(GetShaderHashCodeBody(structDeclarationSymbol)); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs index 50e40a94..84473701 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using ComputeSharp.__Internals; using ComputeSharp.Core.Helpers; using ComputeSharp.SourceGenerators.Diagnostics; using ComputeSharp.SourceGenerators.Extensions; @@ -11,6 +12,8 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; +#pragma warning disable CS0618 + namespace ComputeSharp.SourceGenerators { /// @@ -25,8 +28,7 @@ namespace ComputeSharp.SourceGenerators { // This code produces a method declaration as follows: // - // public readonly void LoadDispatchData(ref TDataLoader loader, GraphicsDevice device, int x, int y, int z) - // where TDataLoader : struct, IDispatchDataLoader + // readonly void IShader.LoadDispatchData(ref TDataLoader loader, GraphicsDevice device, int x, int y, int z) // { // // } @@ -34,7 +36,8 @@ namespace ComputeSharp.SourceGenerators MethodDeclaration( PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("LoadDispatchData")) - .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.ReadOnlyKeyword)) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName(nameof(IShader)))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddTypeParameterListParameters(TypeParameter(Identifier("TDataLoader"))) .AddParameterListParameters( Parameter(Identifier("loader")) @@ -44,11 +47,6 @@ namespace ComputeSharp.SourceGenerators Parameter(Identifier("x")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), Parameter(Identifier("y")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), Parameter(Identifier("z")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword)))) - .AddConstraintClauses( - TypeParameterConstraintClause(IdentifierName("TDataLoader")) - .AddConstraints( - ClassOrStructConstraint(SyntaxKind.StructConstraint), - TypeConstraint(IdentifierName("IDispatchDataLoader")))) .WithBody(Block(GetDispatchDataLoadingStatements(context, structDeclarationSymbol, out discoveredResources, out root32BitConstantsCount))); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs index c67767fe..3e72c856 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; +using ComputeSharp.__Internals; using ComputeSharp.SourceGenerators.Mappings; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; +#pragma warning disable CS0618 + namespace ComputeSharp.SourceGenerators { /// @@ -19,8 +22,7 @@ namespace ComputeSharp.SourceGenerators { // This code produces a method declaration as follows: // - // public readonly void LoadDispatchMetadata(ref TMetadataLoader loader, out IntPtr result) - // where TMetadataLoader : struct, IDispatchMetadataLoader + // readonly void IShader.LoadDispatchMetadata(ref TMetadataLoader loader, out IntPtr result) // { // // } @@ -28,7 +30,8 @@ namespace ComputeSharp.SourceGenerators MethodDeclaration( PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("LoadDispatchMetadata")) - .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.ReadOnlyKeyword)) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName(nameof(IShader)))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddTypeParameterListParameters(TypeParameter(Identifier("TMetadataLoader"))) .AddParameterListParameters( Parameter(Identifier("loader")) @@ -37,11 +40,6 @@ namespace ComputeSharp.SourceGenerators Parameter(Identifier("result")) .AddModifiers(Token(SyntaxKind.OutKeyword)) .WithType(IdentifierName(typeof(IntPtr).Name))) - .AddConstraintClauses( - TypeParameterConstraintClause(IdentifierName("TMetadataLoader")) - .AddConstraints( - ClassOrStructConstraint(SyntaxKind.StructConstraint), - TypeConstraint(IdentifierName("IDispatchMetadataLoader")))) .WithBody(Block(GetDispatchMetadataLoadingStatements(implicitTextureType, discoveredResources, root32BitConstantsCount, isSamplerUsed))); } diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs index 6626ef2e..4a14c642 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs @@ -1,4 +1,5 @@ -using ComputeSharp.Tests.Internals.Helpers; +using ComputeSharp.__Internals; +using ComputeSharp.Tests.Internals.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; #pragma warning disable CS0618 @@ -26,7 +27,7 @@ namespace ComputeSharp.Tests.Internals DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); - new CapturedResourceShader(buffer).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); + ((IShader)new CapturedResourceShader(buffer)).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); Assert.AreEqual(3, dataLoader.Values.Length); Assert.AreEqual(1, dataLoader.Resources.Length); @@ -59,7 +60,7 @@ namespace ComputeSharp.Tests.Internals DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); - new MultipleResourcesAndPrimitivesShader(buffer0, buffer1, 1, 22, 77).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); + ((IShader)new MultipleResourcesAndPrimitivesShader(buffer0, buffer1, 1, 22, 77)).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); Assert.AreEqual(6, dataLoader.Values.Length); Assert.AreEqual(2, dataLoader.Resources.Length); @@ -98,7 +99,7 @@ namespace ComputeSharp.Tests.Internals DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); ScalarAndVectorTypesShader shader = new(buffer, new(55, 44, 888), 22, 77, new(3.14, 6.28), 42, 9999); - shader.LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); + ((IShader)shader).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); Assert.AreEqual(18, dataLoader.Values.Length); Assert.AreEqual(1, dataLoader.Resources.Length); @@ -160,7 +161,7 @@ namespace ComputeSharp.Tests.Internals i2x2: new(11, 22, 33, 44), d: 9999); - shader.LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); + ((IShader)shader).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); Assert.AreEqual(31, dataLoader.Values.Length); Assert.AreEqual(1, dataLoader.Resources.Length); @@ -240,7 +241,7 @@ namespace ComputeSharp.Tests.Internals i2x2: new(11, 22, 33, 44), d: 9999)); - shader.LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); + ((IShader)shader).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); Assert.AreEqual(31, dataLoader.Values.Length); Assert.AreEqual(1, dataLoader.Resources.Length); @@ -344,7 +345,7 @@ namespace ComputeSharp.Tests.Internals a: 888888, b: new(333.3f, 444.4f)))); - shader.LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); + ((IShader)shader).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); Assert.AreEqual(47, dataLoader.Values.Length); Assert.AreEqual(1, dataLoader.Resources.Length); @@ -425,7 +426,7 @@ namespace ComputeSharp.Tests.Internals DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); - new AmbiguousNamesShader(a, b, c, x, y, z, 7777, 8888, 9999).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); + ((IShader)new AmbiguousNamesShader(a, b, c, x, y, z, 7777, 8888, 9999)).LoadDispatchData(ref dataLoader, Gpu.Default, 111, 222, 333); Assert.AreEqual(6, dataLoader.Values.Length); Assert.AreEqual(6, dataLoader.Resources.Length); diff --git a/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs index 5d8b7d59..4ac713a2 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs @@ -1,4 +1,5 @@ using System; +using ComputeSharp.__Internals; using Microsoft.VisualStudio.TestTools.UnitTesting; #pragma warning disable CS0618 @@ -29,14 +30,14 @@ namespace ComputeSharp.Tests.Internals Shader1 shader1 = new() { A = value, B = buffer }; int - hash1 = shader1.GetDispatchId(), - hash2 = shader1.GetDispatchId(); + hash1 = ((IShader)shader1).GetDispatchId(), + hash2 = ((IShader)shader1).GetDispatchId(); Assert.IsTrue(hash1 == hash2); Shader1 shader2 = new() { A = value, B = buffer }; - int hash3 = shader2.GetDispatchId(); + int hash3 = ((IShader)shader2).GetDispatchId(); Assert.IsTrue(hash1 == hash3); } @@ -63,8 +64,8 @@ namespace ComputeSharp.Tests.Internals Shader2 shader1 = new() { A = 1, B = buffer, F = f }; int - hash1 = shader1.GetDispatchId(), - hash2 = shader1.GetDispatchId(); + hash1 = ((IShader)shader1).GetDispatchId(), + hash2 = ((IShader)shader1).GetDispatchId(); Assert.IsTrue(hash1 == hash2); @@ -72,7 +73,7 @@ namespace ComputeSharp.Tests.Internals Shader2 shader2 = new() { A = 1, B = buffer, F = f }; - int hash3 = shader2.GetDispatchId(); + int hash3 = ((IShader)shader2).GetDispatchId(); Assert.IsFalse(hash1 == hash3); }