Added AlignmentHelper type

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-08 15:13:36 +01:00
1 親 5ccd88d05a
コミット 624c1a35d0
4ファイル変更
+100 -8

検索条件に一致するファイルはありません

+7 -1
ファイルの表示
@@ -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<T> : Buffer<T>
where T : unmanaged
{
/// <summary>
/// The alignment boundary for elements in a constant buffer.
/// </summary>
private const int ElementSizeAlignment = FX.D3D12_COMMONSHADER_CONSTANT_BUFFER_PARTIAL_UPDATE_EXTENTS_BYTE_ALIGNMENT;
/// <summary>
/// Creates a new <see cref="ConstantBuffer{T}"/> instance with the specified parameters.
/// </summary>
@@ -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);
}
/// <inheritdoc/>
+43
ファイルの表示
@@ -0,0 +1,43 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// A <see langword="class"/> with helper methods to work with alignment and padding.
/// </summary>
internal static class AlignmentHelper
{
/// <summary>
/// Pads the input size so that its pitch is aligned to a multiple of a specified length.
/// </summary>
/// <param name="size">The input size to pad, if needed.</param>
/// <param name="alignment">The target alignment.</param>
/// <returns>The padded value relative to the given inputs.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Pad(int size, int alignment)
{
return (size + alignment - 1) & ~(alignment - 1);
}
/// <summary>
/// Aligns a given offset to a specified boundary, if a given size increment would have crossed it.
/// </summary>
/// <param name="offset">The starting offset to align, if needed.</param>
/// <param name="size">The size increment to apply to <paramref name="offset"/>.</param>
/// <param name="alignment">The alignment boundary to calculate the final offset.</param>
/// <returns>A valid starting offset for the specified alignment.</returns>
[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);
}
}
}