public static unsafe Buffer SetDataFromStream(this Buffer buffer, CommandList commandList, Stream stream) { var pool = ArrayPool <byte> .Shared; var chunk = pool.Rent(Math.Min(buffer.SizeInBytes, 0x10000)); try { fixed(byte *chunkPtr = chunk) { var offset = 0; while (stream.CanRead) { var bytesRead = stream.Read(chunk, 0, chunk.Length); if (bytesRead > 0) { var dp = new DataPointer(chunkPtr, bytesRead); buffer.SetData(commandList, dp, offset); offset += bytesRead; } } } } finally { pool.Return(chunk); } return(buffer); }
/// <summary> /// Copies the <paramref name="fromData"/> to the given <paramref name="buffer"/> on GPU memory. /// </summary> /// <typeparam name="TData">The type of the T data.</typeparam> /// <param name="buffer">The <see cref="Buffer"/>.</param> /// <param name="commandList">The <see cref="CommandList"/>.</param> /// <param name="fromData">The data to copy from.</param> /// <param name="offsetInBytes">The offset in bytes to write to.</param> /// <exception cref="ArgumentException"></exception> /// <remarks> /// See the unmanaged documentation about Map/UnMap for usage and restrictions. /// </remarks> /// <returns>The GPU buffer.</returns> public static unsafe Buffer SetData <TData>(this Buffer buffer, CommandList commandList, Spread <TData> fromData, int offsetInBytes = 0) where TData : struct { var immutableArray = fromData._array; var array = Unsafe.As <ImmutableArray <TData>, TData[]>(ref immutableArray); buffer.SetData(commandList, array, offsetInBytes); return(buffer); }