From b44504e1e7ae3e4976a7902d313e3bfad6e4d44d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Nov 2023 13:34:48 +0100 Subject: [PATCH] Move AlignmentHelper type to Graphics folder --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 2 +- .../ConstantBufferSyntaxProcessor.cs | 2 +- ...eShaderDescriptorGenerator.ConstantBuffer.cs | 2 +- .../ComputeSharp.SourceGenerators.csproj | 2 +- .../Helpers/AlignmentHelper.cs | 6 +++++- .../Graphics/Resources/Abstract/Buffer{T}.cs | 1 - .../Graphics/Resources/ConstantBuffer{T}.cs | 1 - .../AlignmentHelperTests.cs | 17 +++++++++++++++-- 8 files changed, 24 insertions(+), 9 deletions(-) rename src/ComputeSharp/{Core => Graphics}/Helpers/AlignmentHelper.cs (88%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 86a86264..1644d727 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -65,7 +65,7 @@ - + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.cs index 3d24fa41..d346273d 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.cs @@ -1,7 +1,7 @@ using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Text.RegularExpressions; -using ComputeSharp.Core.Helpers; +using ComputeSharp.Graphics.Helpers; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs index 8ec40690..13c7dd4e 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs @@ -1,5 +1,5 @@ using System.Collections.Immutable; -using ComputeSharp.Core.Helpers; +using ComputeSharp.Graphics.Helpers; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 541a27ee..2e821ef4 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -67,9 +67,9 @@ - + diff --git a/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs b/src/ComputeSharp/Graphics/Helpers/AlignmentHelper.cs similarity index 88% rename from src/ComputeSharp/Core/Helpers/AlignmentHelper.cs rename to src/ComputeSharp/Graphics/Helpers/AlignmentHelper.cs index 6751bde4..ef5eb411 100644 --- a/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/AlignmentHelper.cs @@ -1,6 +1,6 @@ using System.Runtime.CompilerServices; -namespace ComputeSharp.Core.Helpers; +namespace ComputeSharp.Graphics.Helpers; /// /// A with helper methods to work with alignment and padding. @@ -31,6 +31,7 @@ internal static class AlignmentHelper return (size + alignment - 1) & ~(alignment - 1); } +#if SOURCE_GENERATOR /// /// Aligns a given offset to a specified boundary, if a given size increment would have crossed it. /// @@ -41,6 +42,8 @@ internal static class AlignmentHelper [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int AlignToBoundary(int offset, int size, int alignment) { + // This method is mirrored in AlignmentHelperTests, so it can be tested individually + // as well. Any changes done here should be applied to that standalone copy as well. if ((uint)offset / (uint)alignment == (uint)(offset + size - 1) / (uint)alignment) { return offset; @@ -48,4 +51,5 @@ internal static class AlignmentHelper return (offset + alignment - 1) & ~(alignment - 1); } +#endif } \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index d3b30619..72d6f599 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -3,7 +3,6 @@ using System.Buffers; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Core.Helpers; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index 75f4ae58..f0f05414 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -2,7 +2,6 @@ using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using ComputeSharp.Core.Helpers; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; diff --git a/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs b/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs index 91b6604c..2968a15f 100644 --- a/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs +++ b/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs @@ -1,4 +1,4 @@ -using ComputeSharp.Core.Helpers; +using ComputeSharp.Graphics.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests.Internals; @@ -33,7 +33,20 @@ public class AlignmentHelperTests [DataRow(24, 8, 16, 24)] public void AlignToBoundary(int offset, int size, int alignment, int expected) { - int result = AlignmentHelper.AlignToBoundary(offset, size, alignment); + // Note: this method is defined in AlignmentHelper, but it's only compiled when referenced + // by the source generator, so it's not available here. Just for the sake of the additional + // testing, and because the method is very small, it's just mirrored here for validation. + static int AlignToBoundary(int offset, int size, int alignment) + { + if ((uint)offset / (uint)alignment == (uint)(offset + size - 1) / (uint)alignment) + { + return offset; + } + + return (offset + alignment - 1) & ~(alignment - 1); + } + + int result = AlignToBoundary(offset, size, alignment); Assert.AreEqual(result, expected); }