mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
SOURCE DUMP v1
This commit is contained in:
parent
bd62d51ec9
commit
fb20299e9d
1 changed files with 768360 additions and 0 deletions
85
brutal.vulkan/Brutal.Vulkan.Abstractions/IMappable.cs
Normal file
85
brutal.vulkan/Brutal.Vulkan.Abstractions/IMappable.cs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
namespace Brutal.VulkanApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Struct containing information about an intention to map a memory allocation.
|
||||
/// </summary>
|
||||
public readonly record struct AllocationMapInfo(AllocationInfo AllocationInfo)
|
||||
{
|
||||
public MapRegion Region { get; init; } = new();
|
||||
public VkMemoryMapFlags Flags { get; init; } = VkMemoryMapFlags.None;
|
||||
|
||||
public record struct MapKey(Ptr MapPtr, ByteSize MapSize, ByteSize MapOffset);
|
||||
|
||||
/// <summary>
|
||||
/// Check if this MappedInfo is a subset of another MappedInfo, in terms of size and offset.
|
||||
/// </summary>
|
||||
public bool IsSubsetOf(MapKey other)
|
||||
{
|
||||
return other.MapSize >= Region.Size
|
||||
&& other.MapOffset <= Region.Offset
|
||||
&& other.MapOffset + other.MapSize >= Region.Offset + Region.Size;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Describes an object that can be mapped to a pointer.
|
||||
/// </summary>
|
||||
public interface IMappable
|
||||
{
|
||||
AllocationInfo AllocationInfo { get; }
|
||||
ByteSize MapSize { get; }
|
||||
ByteSize MapOffset { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Information required to map a memory region.
|
||||
/// </summary>
|
||||
public readonly struct MapInfo()
|
||||
{
|
||||
public MapRegion Region { get; init; } = new();
|
||||
public VkMemoryMapFlags Flags { get; init; } = VkMemoryMapFlags.None;
|
||||
}
|
||||
}
|
||||
|
||||
public static class MappableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Map the given object to a pointer.
|
||||
/// </summary>
|
||||
/// <param name="mappable"> The object to map</param>
|
||||
public static MappedMemory Map<T>(this T mappable)
|
||||
where T : IMappable
|
||||
{
|
||||
return mappable.Map(new IMappable.MapInfo());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Map the given object to a pointer.
|
||||
/// </summary>
|
||||
/// <param name="mappable"> The object to map</param>
|
||||
public static MappedMemory Map<T>(this T mappable, IMappable.MapInfo mapInfo)
|
||||
where T : IMappable
|
||||
{
|
||||
var region = mapInfo.Region;
|
||||
|
||||
// The byte offset must be within the buffer size
|
||||
if (region.Offset > mappable.MapSize)
|
||||
throw new ArgumentException(@"Byte offset exceeds buffer size", nameof(region.Offset));
|
||||
|
||||
// A negative byte size is invalid
|
||||
if (region.Size.Bytes == VK.WHOLE_SIZE)
|
||||
region = region with { Size = mappable.MapSize - region.Offset };
|
||||
|
||||
// The byte size must be within the buffer size
|
||||
if (region.Offset + region.Size > mappable.MapSize)
|
||||
throw new ArgumentException(@"Data exceeds buffer size", nameof(region.Size));
|
||||
|
||||
return new MappedMemory(
|
||||
new AllocationMapInfo(mappable.AllocationInfo)
|
||||
{
|
||||
Region = region with { Offset = region.Offset + mappable.MapOffset },
|
||||
Flags = mapInfo.Flags,
|
||||
},
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue