示例#1
0
文件: Stride.cs 项目: rpfeuti/ILGPU
        /// <summary>
        /// Determines a pitched leading dimension.
        /// </summary>
        /// <typeparam name="T">The element type.</typeparam>
        /// <param name="leadingDimension">The size of the leading dimension.</param>
        /// <param name="alignmentInBytes">
        /// The alignment in bytes of the leading dimension.
        /// </param>
        /// <returns>The pitched leading dimension.</returns>
        public static long GetPitchedLeadingDimension <T>(
            long leadingDimension,
            int alignmentInBytes)
            where T : unmanaged
        {
            // Validate the byte pitch and the element size
            if (alignmentInBytes < 1 || !Utilities.IsPowerOf2(alignmentInBytes))
            {
                throw new ArgumentOutOfRangeException(
                          nameof(alignmentInBytes));
            }
            int elementSize = ArrayView <T> .ElementSize;

            if (elementSize > alignmentInBytes || alignmentInBytes % elementSize != 0)
            {
                throw new ArgumentException(
                          string.Format(
                              RuntimeErrorMessages.NotSupportedPitchedAllocation,
                              nameof(T),
                              alignmentInBytes));
            }

            // Ensure a proper alignment of the leading dimension
            long unpichtedBytes = leadingDimension * elementSize;
            long pitchedBytes   = TypeNode.Align(unpichtedBytes, alignmentInBytes);

            // Return the pitched dimension
            return(pitchedBytes / elementSize);
        }