using System;
using System.Runtime.InteropServices;
using SDL2_gpuCS;
namespace MoonWorks.Graphics;
///
/// GpuBuffers are generic data containers that can be used by the GPU.
///
public class GpuBuffer : SDL_GpuResource
{
protected override Action ReleaseFunction => SDL_Gpu.SDL_GpuReleaseBuffer;
public BufferUsageFlags UsageFlags { get; }
///
/// Size in bytes.
///
public uint Size { get; }
private string name;
public string Name
{
get => name;
set
{
if (Device.DebugMode)
{
SDL_Gpu.SDL_GpuSetBufferName(
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 = SDL_Gpu.SDL_GpuCreateBuffer(
device.Handle,
(SDL_Gpu.BufferUsageFlags) usageFlags,
sizeInBytes
);
UsageFlags = usageFlags;
Size = sizeInBytes;
name = "";
}
public static implicit operator BufferBinding(GpuBuffer b)
{
return new BufferBinding(b, 0);
}
}