using System; using System.Runtime.InteropServices; using RefreshCS; namespace MoonWorks.Graphics { /// /// Buffers are generic data containers that can be used by the GPU. /// public class Buffer : GraphicsResource { protected override Action QueueDestroyFunction => Refresh.Refresh_QueueDestroyBuffer; /// /// Creates a buffer of appropriate size given a type and element count. /// /// The type that the buffer will contain. /// The GraphicsDevice. /// Specifies how the buffer will be used. /// How many elements of type T the buffer will contain. /// public static Buffer Create( GraphicsDevice device, BufferUsageFlags usageFlags, uint elementCount ) { return new Buffer( device, usageFlags, (uint) Marshal.SizeOf() * elementCount ); } /// /// Creates a buffer. /// /// An initialized GraphicsDevice. /// Specifies how the buffer will be used. /// The length of the array. Cannot be resized. public Buffer( GraphicsDevice device, BufferUsageFlags usageFlags, uint sizeInBytes ) : base(device) { Handle = Refresh.Refresh_CreateBuffer( device.Handle, (Refresh.BufferUsageFlags) usageFlags, sizeInBytes ); } /// /// Reads data out of a buffer and into an array. /// This operation is only guaranteed to read up-to-date data if GraphicsDevice.Wait is called first. /// /// The array that data will be copied to. /// The length of the data to read. public unsafe void GetData( T[] data, uint dataLengthInBytes ) where T : unmanaged { fixed (T* ptr = &data[0]) { Refresh.Refresh_GetBufferData( Device.Handle, Handle, (IntPtr) ptr, dataLengthInBytes ); } } } }