2022-02-23 05:14:32 +00:00
|
|
|
|
using System;
|
2021-01-22 01:27:25 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
using RefreshCS;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Graphics
|
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Buffers are generic data containers that can be used by the GPU.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Buffer : GraphicsResource
|
|
|
|
|
{
|
2022-03-04 01:16:39 +00:00
|
|
|
|
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyBuffer;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-03-04 01:18:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Size in bytes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint Size { get; }
|
|
|
|
|
|
2022-03-02 22:29:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a buffer of appropriate size given a type and element count.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type that the buffer will contain.</typeparam>
|
|
|
|
|
/// <param name="device">The GraphicsDevice.</param>
|
|
|
|
|
/// <param name="usageFlags">Specifies how the buffer will be used.</param>
|
|
|
|
|
/// <param name="elementCount">How many elements of type T the buffer will contain.</param>
|
|
|
|
|
/// <returns></returns>
|
2022-04-27 21:14:15 +00:00
|
|
|
|
public unsafe static Buffer Create<T>(
|
2022-03-02 22:29:43 +00:00
|
|
|
|
GraphicsDevice device,
|
|
|
|
|
BufferUsageFlags usageFlags,
|
|
|
|
|
uint elementCount
|
2022-04-27 21:14:15 +00:00
|
|
|
|
) where T : unmanaged
|
2022-03-02 22:29:43 +00:00
|
|
|
|
{
|
|
|
|
|
return new Buffer(
|
|
|
|
|
device,
|
|
|
|
|
usageFlags,
|
2022-04-27 21:14:15 +00:00
|
|
|
|
(uint) sizeof(T) * elementCount
|
2022-03-02 22:29:43 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a buffer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="device">An initialized GraphicsDevice.</param>
|
|
|
|
|
/// <param name="usageFlags">Specifies how the buffer will be used.</param>
|
|
|
|
|
/// <param name="sizeInBytes">The length of the array. Cannot be resized.</param>
|
|
|
|
|
public Buffer(
|
|
|
|
|
GraphicsDevice device,
|
|
|
|
|
BufferUsageFlags usageFlags,
|
|
|
|
|
uint sizeInBytes
|
|
|
|
|
) : base(device)
|
|
|
|
|
{
|
|
|
|
|
Handle = Refresh.Refresh_CreateBuffer(
|
|
|
|
|
device.Handle,
|
|
|
|
|
(Refresh.BufferUsageFlags) usageFlags,
|
|
|
|
|
sizeInBytes
|
|
|
|
|
);
|
2022-03-04 01:18:38 +00:00
|
|
|
|
Size = sizeInBytes;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">The array that data will be copied to.</param>
|
|
|
|
|
/// <param name="dataLengthInBytes">The length of the data to read.</param>
|
|
|
|
|
public unsafe void GetData<T>(
|
|
|
|
|
T[] data,
|
|
|
|
|
uint dataLengthInBytes
|
|
|
|
|
) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
fixed (T* ptr = &data[0])
|
|
|
|
|
{
|
|
|
|
|
Refresh.Refresh_GetBufferData(
|
|
|
|
|
Device.Handle,
|
|
|
|
|
Handle,
|
|
|
|
|
(IntPtr) ptr,
|
|
|
|
|
dataLengthInBytes
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-20 03:33:27 +00:00
|
|
|
|
}
|