using System;
using System.Runtime.CompilerServices;
namespace ComputeSharp.Graphics.Helpers
{
///
/// A with some helper methods to copy data between memory areas.
///
internal static class MemoryHelper
{
///
/// Copies the content of a source memory area to a target one.
///
/// The source memory area to read from.
/// The pointer for the destination memory area.
/// The destination offset to start writing data to.
/// The number of items to copy.
/// The size in bytes of each item to copy.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy(
void* source,
void* destination,
uint offset,
uint length,
uint elementSizeInBytes)
{
Buffer.MemoryCopy(
source,
(byte*)destination + offset * elementSizeInBytes,
ulong.MaxValue,
elementSizeInBytes * length);
}
///
/// Copies the content of a source memory area to a target one, accounting for padding.
/// The destination memory area has padding for each element, while the source does not.
///
/// The source memory area to read from.
/// The pointer for the destination memory area.
/// The destination offset to start writing data to.
/// The number of items to copy.
/// The padded size of each element.
public static unsafe void Copy(
void* source,
void* destination,
uint offset,
uint length,
uint elementPitchInBytes)
where T : unmanaged
{
destination = (byte*)destination + offset * elementPitchInBytes;
for (int i = 0; i < length; i++)
{
*(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
}
}
///
/// Copies the content of a source memory area to a target one.
///
/// The source memory area to read from.
/// The source offset to start reading data from.
/// The number of items to copy.
/// The size in bytes of each item to copy.
/// The pointer for the destination memory area.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy(
void* source,
uint offset,
uint length,
uint elementSizeInBytes,
void* destination)
{
Buffer.MemoryCopy(
(byte*)source + offset * elementSizeInBytes,
destination,
ulong.MaxValue,
elementSizeInBytes * length);
}
///
/// Copies the content of a source memory area to a target one, accounting for padding.
/// The source memory area has padding for each element, while the destination does not.
///
/// The source memory area to read from.
/// The source offset to start reading data from.
/// The number of items to copy.
/// The padded size of each element.
/// The pointer for the destination memory area.
public static unsafe void Copy(
void* source,
uint offset,
uint length,
uint elementPitchInBytes,
void* destination)
where T : unmanaged
{
source = (byte*)source + offset * elementPitchInBytes;
for (int i = 0; i < length; i++)
{
((T*)destination)[i] = *(T*)&((byte*)source)[i * elementPitchInBytes];
}
}
///
/// Copies the content of a source memory area to the 2D area pointed by an input pointer.
/// The destination memory area has padding in each row, while the source does not.
///
/// The source pointer to read from.
/// The pointer that indicates the 2D memory area to write to.
/// The height of the 2D memory area to write to.
/// The width of the memory area in bytes.
/// The pitch (padded width) of the memory area in bytes.
public static unsafe void Copy(
void* source,
void* destination,
uint height,
ulong widthInBytes,
ulong pitchInBytes)
{
if (widthInBytes == pitchInBytes)
{
Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height);
}
else
{
for (int i = 0; i < height; i++)
{
Buffer.MemoryCopy(
(byte*)source + (uint)i * widthInBytes,
(byte*)destination + (uint)i * pitchInBytes,
ulong.MaxValue,
widthInBytes);
}
}
}
///
/// Copies a 2D memory area pointed by a pointer value to a target memory area.
/// The source memory area has padding in each row, while the target does not.
///
/// The pointer that indicates the memory area to read from.
/// The height of the 2D memory area to read.
/// The width of the memory area in bytes.
/// The pitch (padded width) of the memory area in bytes.
/// The destination pointer to write to.
public static unsafe void Copy(
void* source,
uint height,
ulong widthInBytes,
ulong pitchInBytes,
void* destination)
{
if (widthInBytes == pitchInBytes)
{
Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height);
}
else
{
for (int i = 0; i < height; i++)
{
Buffer.MemoryCopy(
(byte*)source + (uint)i * pitchInBytes,
(byte*)destination + (uint)i * widthInBytes,
ulong.MaxValue,
widthInBytes);
}
}
}
///
/// Copies the content of a source memory area to the 3D area pointed by an input pointer.
/// The destination memory area has padding in each row, while the source does not.
///
/// The source memory area to read from.
/// The pointer that indicates the 3D memory area to write to.
/// The height of the 3D memory area to write to.
/// The depth of the 3D memory area to write to.
/// The width of the memory area in bytes.
/// The pitch (padded width) of the memory area in bytes.
/// The size in bytes of each 2D slice within the target 3D memory area.
public static unsafe void Copy(
void* source,
void* destination,
uint height,
uint depth,
ulong widthInBytes,
ulong pitchInBytes,
ulong sliceInBytes)
{
if (widthInBytes == pitchInBytes)
{
Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height * depth);
}
else
{
for (int z = 0; z < depth; z++)
{
ulong
sourceZOffset = (uint)z * widthInBytes * height,
destinationZOffset = (uint)z * sliceInBytes;
for (int i = 0; i < height; i++)
{
Buffer.MemoryCopy(
(byte*)source + sourceZOffset + (uint)i * widthInBytes,
(byte*)destination + destinationZOffset + (uint)i * pitchInBytes,
ulong.MaxValue,
widthInBytes);
}
}
}
}
///
/// Copies a 3D memory area pointed by a pointer value to a target memory area.
/// The source memory area has padding in each row, while the target does not.
///
/// The pointer that indicates the memory area to read from.
/// The height of the 3D memory area to read.
/// The depth of the 3D memory area to read.
/// The width of the memory area in bytes.
/// The pitch (padded width) of the memory area in bytes.
/// The size in bytes of each 2D slice within the source 3D memory area.
/// The destination pointer to write.
public static unsafe void Copy(
void* source,
uint height,
uint depth,
ulong widthInBytes,
ulong pitchInBytes,
ulong sliceInBytes,
void* destination)
{
if (widthInBytes == pitchInBytes)
{
Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height * depth);
}
else
{
for (int z = 0; z < depth; z++)
{
ulong
sourceZOffset = (uint)z * sliceInBytes,
destinationZOffset = (uint)z * widthInBytes * height;
for (int i = 0; i < height; i++)
{
Buffer.MemoryCopy(
(byte*)source + sourceZOffset + (uint)i * pitchInBytes,
(byte*)destination + destinationZOffset + (uint)i * widthInBytes,
ulong.MaxValue,
widthInBytes);
}
}
}
}
}
}