MoonWorks/src/Graphics/Resources/GpuBuffer.cs

89 lines
2.0 KiB
C#
Raw Normal View History

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