using System;
using System.Runtime.InteropServices;
using RefreshCS;
namespace MoonWorks.Graphics;
///
/// GpuBuffers are generic data containers that can be used by the GPU.
///
public class GpuBuffer : RefreshResource
{
protected override Action ReleaseFunction => Refresh.Refresh_ReleaseBuffer;
public BufferUsageFlags UsageFlags { get; }
///
/// Size in bytes.
///
public uint Size { get; }
private string name;
public string Name
{
get => name;
set
{
if (Device.DebugMode)
{
Refresh.Refresh_SetBufferName(
Device.Handle,
Handle,
value
);
}
name = value;
}
}
///
/// 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 unsafe static GpuBuffer Create(
GraphicsDevice device,
BufferUsageFlags usageFlags,
uint elementCount
) where T : unmanaged
{
return new GpuBuffer(
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 GpuBuffer(
GraphicsDevice device,
BufferUsageFlags usageFlags,
uint sizeInBytes
) : base(device)
{
Handle = Refresh.Refresh_CreateBuffer(
device.Handle,
(Refresh.BufferUsageFlags) usageFlags,
sizeInBytes
);
UsageFlags = usageFlags;
Size = sizeInBytes;
name = "";
}
public static implicit operator BufferBinding(GpuBuffer b)
{
return new BufferBinding(b, 0);
}
}