diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 3e432010..e3774a34 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -12,6 +12,7 @@ using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using Microsoft.Toolkit.Diagnostics; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; +using FX = TerraFX.Interop.Windows; namespace ComputeSharp { @@ -24,6 +25,11 @@ namespace ComputeSharp public sealed class ConstantBuffer : Buffer where T : unmanaged { + /// + /// The alignment boundary for elements in a constant buffer. + /// + private const int ElementSizeAlignment = FX.D3D12_COMMONSHADER_CONSTANT_BUFFER_PARTIAL_UPDATE_EXTENTS_BYTE_ALIGNMENT; + /// /// Creates a new instance with the specified parameters. /// @@ -48,7 +54,7 @@ namespace ComputeSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe int GetPaddedSize() { - return (sizeof(T) + 15) & ~15; + return AlignmentHelper.Pad(sizeof(T), ElementSizeAlignment); } /// diff --git a/src/ComputeSharp.Graphics/Helpers/AlignmentHelper.cs b/src/ComputeSharp.Graphics/Helpers/AlignmentHelper.cs new file mode 100644 index 00000000..6191c0c3 --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/AlignmentHelper.cs @@ -0,0 +1,43 @@ +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + /// A with helper methods to work with alignment and padding. + /// + internal static class AlignmentHelper + { + /// + /// Pads the input size so that its pitch is aligned to a multiple of a specified length. + /// + /// The input size to pad, if needed. + /// The target alignment. + /// The padded value relative to the given inputs. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int Pad(int size, int alignment) + { + return (size + alignment - 1) & ~(alignment - 1); + } + + /// + /// Aligns a given offset to a specified boundary, if a given size increment would have crossed it. + /// + /// The starting offset to align, if needed. + /// The size increment to apply to . + /// The alignment boundary to calculate the final offset. + /// A valid starting offset for the specified alignment. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public 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); + } + } +} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs index 89ce2f73..9012fb0f 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs @@ -7,12 +7,14 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Interop; using ComputeSharp.Shaders.Extensions; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Translation.Models; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Shaders.Translation { @@ -75,7 +77,7 @@ namespace ComputeSharp.Shaders.Translation // Invoke the dynamic method to extract the captured data this.dispatchDataLoader!(device, in shader, ref r0, ref r1); - return new DispatchData(resources, this.totalResourceCount, variables, this.totalVariablesByteSize); + return new(resources, this.totalResourceCount, variables, this.totalVariablesByteSize); } /// @@ -133,12 +135,12 @@ namespace ComputeSharp.Shaders.Translation HlslKnownTypes.IsKnownVectorType(fieldInfo.FieldType)) { // Calculate the right offset with 16-bytes padding (HLSL constant buffer) - int size = Marshal.SizeOf(fieldInfo.FieldType); + int elementSizeInBytes = Marshal.SizeOf(fieldInfo.FieldType); - if (this.totalVariablesByteSize % 16 > 16 - size) - { - this.totalVariablesByteSize += 16 - this.totalVariablesByteSize % 16; - } + this.totalVariablesByteSize = AlignmentHelper.AlignToBoundary( + this.totalVariablesByteSize, + elementSizeInBytes, + FX.D3D12_COMMONSHADER_CONSTANT_BUFFER_PARTIAL_UPDATE_EXTENTS_BYTE_ALIGNMENT); // Load the target address into the variables buffer il.Emit(OpCodes.Ldarg_S, (byte)4); @@ -148,7 +150,7 @@ namespace ComputeSharp.Shaders.Translation il.EmitReadMember(fieldInfo); il.EmitStoreToAddress(fieldInfo.FieldType); - this.totalVariablesByteSize += size; + this.totalVariablesByteSize += elementSizeInBytes; } else ThrowHelper.ThrowArgumentException("Invalid captured member type"); } diff --git a/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs b/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs new file mode 100644 index 00000000..90868229 --- /dev/null +++ b/tests/ComputeSharp.Tests.Internals/AlignmentHelperTests.cs @@ -0,0 +1,41 @@ +using ComputeSharp.Graphics.Helpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Internals +{ + [TestClass] + [TestCategory("AlignmentHelper")] + public class AlignmentHelperTests + { + [TestMethod] + [DataRow(1, 16, 16)] + [DataRow(2, 16, 16)] + [DataRow(4, 16, 16)] + [DataRow(8, 16, 16)] + [DataRow(16, 16, 16)] + [DataRow(20, 16, 32)] + [DataRow(40, 16, 48)] + public void Pad(int size, int alignment, int expected) + { + int result = AlignmentHelper.Pad(size, alignment); + + Assert.AreEqual(result, expected); + } + + [TestMethod] + [DataRow(0, 4, 16, 0)] + [DataRow(0, 8, 16, 0)] + [DataRow(0, 12, 16, 0)] + [DataRow(0, 16, 16, 0)] + [DataRow(0, 20, 16, 0)] + [DataRow(20, 4, 16, 20)] + [DataRow(20, 16, 16, 32)] + [DataRow(24, 8, 16, 24)] + public void AlignToBoundary(int offset, int size, int alignment, int expected) + { + int result = AlignmentHelper.AlignToBoundary(offset, size, alignment); + + Assert.AreEqual(result, expected); + } + } +}