diff --git a/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs b/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs index 7ea5a097..846d2a6b 100644 --- a/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs +++ b/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs @@ -21,6 +21,19 @@ internal static class AlignmentHelper return (size + alignment - 1) & ~(alignment - 1); } + /// + /// 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 nint Pad(nint size, nint alignment) + { + return (size + alignment - 1) & ~(alignment - 1); + } + /// /// Aligns a given offset to a specified boundary, if a given size increment would have crossed it. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index ce442d70..034f0c6e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -1,6 +1,7 @@ using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ComputeSharp.Core.Helpers; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -72,11 +73,14 @@ public unsafe abstract class Buffer : NativeObject UnsupportedDoubleOperationsException.Throw(); } - SizeInBytes = checked((nint)(length * elementSizeInBytes)); + nint usableSizeInBytes = checked((nint)(length * elementSizeInBytes)); + nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, Windows.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes; + + SizeInBytes = usableSizeInBytes; GraphicsDevice = device; Length = length; - this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)SizeInBytes); + this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); device.RegisterAllocatedResource(); @@ -85,7 +89,7 @@ public unsafe abstract class Buffer : NativeObject switch (resourceType) { case ResourceType.Constant: - device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), SizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, D3D12CpuDescriptorHandle); break; case ResourceType.ReadOnly: device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 2eb9a06e..a4833d8f 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -18,12 +18,16 @@ public partial class BufferTests [Resource(typeof(ReadWriteBuffer<>))] [Data(AllocationMode.Default)] [Data(AllocationMode.Clear)] - public void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode) + [AdditionalData(128)] + [AdditionalData(768)] + [AdditionalData(1024)] + [AdditionalData(443)] + public void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode, int size) { - using Buffer buffer = device.Get().AllocateBuffer(bufferType, 128, allocationMode); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, size, allocationMode); Assert.IsNotNull(buffer); - Assert.AreEqual(buffer.Length, 128); + Assert.AreEqual(buffer.Length, size); Assert.AreSame(buffer.GraphicsDevice, device.Get()); if (allocationMode == AllocationMode.Clear) @@ -53,14 +57,18 @@ public partial class BufferTests [Resource(typeof(ConstantBuffer<>))] [Resource(typeof(ReadOnlyBuffer<>))] [Resource(typeof(ReadWriteBuffer<>))] - public void Allocate_FromArray(Device device, Type bufferType) + [Data(128)] + [Data(768)] + [Data(1024)] + [Data(443)] + public void Allocate_FromArray(Device device, Type bufferType, int size) { - float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); + float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray(); using Buffer buffer = device.Get().AllocateBuffer(bufferType, data); Assert.IsNotNull(buffer); - Assert.AreEqual(buffer.Length, 128); + Assert.AreEqual(buffer.Length, size); Assert.AreSame(buffer.GraphicsDevice, device.Get()); float[] result = buffer.ToArray(); @@ -75,18 +83,22 @@ public partial class BufferTests [Resource(typeof(ConstantBuffer<>))] [Resource(typeof(ReadOnlyBuffer<>))] [Resource(typeof(ReadWriteBuffer<>))] - [Data(typeof(ConstantBuffer<>))] - [Data(typeof(ReadOnlyBuffer<>))] - [Data(typeof(ReadWriteBuffer<>))] - public void Allocate_FromBuffer(Device device, Type sourceType, Type destinationType) + [AdditionalResource(typeof(ConstantBuffer<>))] + [AdditionalResource(typeof(ReadOnlyBuffer<>))] + [AdditionalResource(typeof(ReadWriteBuffer<>))] + [Data(128)] + [Data(768)] + [Data(1024)] + [Data(443)] + public void Allocate_FromBuffer(Device device, Type sourceType, Type destinationType, int size) { - float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); + float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray(); using Buffer source = device.Get().AllocateBuffer(sourceType, data); using Buffer destination = device.Get().AllocateBuffer(destinationType, source); Assert.IsNotNull(destination); - Assert.AreEqual(destination.Length, 128); + Assert.AreEqual(destination.Length, size); Assert.AreSame(destination.GraphicsDevice, device.Get()); float[] result = destination.ToArray();