mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 21:18:24 +01:00
SOURCE DUMP v1
This commit is contained in:
parent
bd62d51ec9
commit
fb20299e9d
5 changed files with 768360 additions and 0 deletions
|
|
@ -0,0 +1,152 @@
|
|||
using System.Numerics;
|
||||
|
||||
namespace Brutal.VulkanApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Atlas allocator that allocates texture space in a 2D texture atlas
|
||||
/// This variant divides the atlas into rows of different sizes and packs similar sized textures into appropriate rows.
|
||||
/// </summary>
|
||||
public class Atlas2DPartitioner : IRecycleImageViewPartitioner, ISize2D
|
||||
{
|
||||
public ImageViewPartitionInfo BackingPartition { get; set; }
|
||||
|
||||
protected class Row(int yPos, int texSize, int atlasSize)
|
||||
{
|
||||
public readonly int YPos = yPos;
|
||||
public readonly int TotalSlots = atlasSize / texSize;
|
||||
public readonly int TexSize = texSize;
|
||||
public readonly FreeListIndexPool FreeListIndexPool = new(atlasSize / texSize, false);
|
||||
}
|
||||
|
||||
protected readonly List<Row> Rows = [];
|
||||
protected int RemainingSpace;
|
||||
|
||||
/// <summary>
|
||||
/// Width/height of atlas
|
||||
/// </summary>
|
||||
public int AtlasSize { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Width => AtlasSize;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Height => AtlasSize;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public Atlas2DPartitioner(ImageViewPartitionInfo backingPartition)
|
||||
{
|
||||
BackingPartition = backingPartition;
|
||||
AtlasSize = backingPartition.Image.Extent.Width;
|
||||
RemainingSpace = AtlasSize;
|
||||
}
|
||||
|
||||
private static bool AllocateRow(Row row, out int2 offset)
|
||||
{
|
||||
var slot = row.FreeListIndexPool.Add();
|
||||
if (slot >= row.TotalSlots)
|
||||
{
|
||||
offset = new int2();
|
||||
return false;
|
||||
}
|
||||
|
||||
offset = new int2(row.TexSize * slot, row.YPos);
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryAllocate(int width, int height, out int x, out int y)
|
||||
{
|
||||
// Round up to the nearest power of 2, clamping to 1 so a zero-sized request doesn't divide by zero
|
||||
var size = (int)BitOperations.RoundUpToPowerOf2(Math.Max(1u, (uint)Math.Max(width, height)));
|
||||
// Find a row with the right size or create a new one, avoiding a LINQ enumerator allocation on this hot path
|
||||
for (var i = 0; i < Rows.Count; i++)
|
||||
{
|
||||
var row = Rows[i];
|
||||
if (row.TexSize != size)
|
||||
continue;
|
||||
|
||||
if (!AllocateRow(row, out var offset))
|
||||
continue;
|
||||
|
||||
x = offset.X;
|
||||
y = offset.Y;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (size > RemainingSpace)
|
||||
{
|
||||
x = -1;
|
||||
y = -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
Rows.Add(new Row(AtlasSize - RemainingSpace, size, AtlasSize));
|
||||
RemainingSpace -= size;
|
||||
|
||||
if (!AllocateRow(Rows[^1], out var offset2))
|
||||
throw new Exception("AtlasAllocator: Internal error");
|
||||
x = offset2.X;
|
||||
y = offset2.Y;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Free(int x, int y)
|
||||
{
|
||||
foreach (var row in Rows)
|
||||
{
|
||||
if (y != row.YPos)
|
||||
continue;
|
||||
var slot = x / row.TexSize;
|
||||
row.FreeListIndexPool.Remove(slot);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception("AtlasAllocator: Invalid offset");
|
||||
}
|
||||
|
||||
public void Reset(ImageViewPartitionInfo info)
|
||||
{
|
||||
BackingPartition = info;
|
||||
AtlasSize = info.Image.Extent.Width;
|
||||
RemainingSpace = AtlasSize;
|
||||
}
|
||||
|
||||
public bool TryCreatePartition(
|
||||
in ImageViewPartitionInfo.CreateInfo createInfo,
|
||||
out ImageViewPartitionInfo partition
|
||||
)
|
||||
{
|
||||
if (createInfo.Extent.Depth != 1)
|
||||
throw new ArgumentException("Depth must be 1");
|
||||
if (createInfo.LayerCount != 1)
|
||||
throw new ArgumentException("LayerCount must be 1");
|
||||
|
||||
if (!TryAllocate(createInfo.Extent.Width, createInfo.Extent.Height, out var x, out var y))
|
||||
{
|
||||
partition = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
partition = BackingPartition with
|
||||
{
|
||||
Partitioner = this,
|
||||
Extent = createInfo.Extent,
|
||||
Offset = new VkOffset3D
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
Z = 0,
|
||||
},
|
||||
ArrayCount = 1,
|
||||
ArrayOffset = BackingPartition.ArrayOffset,
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void FreePartition(in ImageViewPartitionInfo inAllocation)
|
||||
{
|
||||
Free(inAllocation.Offset.X, inAllocation.Offset.Y);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
namespace Brutal.VulkanApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for a sub-image allocator object capable of allocating portions of an image (e.g.: texture atlas, or texture array)
|
||||
/// </summary>
|
||||
public interface IImageViewPartitioner
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the handle to the backing ImageView this partitioner is based on
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public ImageViewPartitionInfo BackingPartition { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Reset the partitioner to its initial state with the given partition info. Invalidates all current allocations.
|
||||
/// </summary>
|
||||
/// <param name="info"></param>
|
||||
public void Reset(ImageViewPartitionInfo info);
|
||||
|
||||
/// <summary>
|
||||
/// Get a partition of an imageview with the given creation info.
|
||||
/// </summary>
|
||||
/// <param name="createInfo">Partition creation info</param>
|
||||
/// <param name="partition">The resulting partition</param>
|
||||
/// <returns>True if the allocation was successful</returns>
|
||||
public bool TryCreatePartition(
|
||||
in ImageViewPartitionInfo.CreateInfo createInfo,
|
||||
out ImageViewPartitionInfo partition
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface for a sub-image allocator object capable of allocating portions of an image (e.g.: texture atlas, or texture array)
|
||||
/// </summary>
|
||||
public interface IRecycleImageViewPartitioner : IImageViewPartitioner
|
||||
{
|
||||
/// <summary>
|
||||
/// Free an imageview partition sourced from this partitioner
|
||||
/// </summary>
|
||||
/// <param name="inAllocation">Struct containing all relevant information about the allocation/partition</param>
|
||||
public void FreePartition(in ImageViewPartitionInfo inAllocation);
|
||||
}
|
||||
|
||||
public static class ImageViewPartitionerExtensions
|
||||
{
|
||||
public static ImageViewPartitionInfo CreatePartition<T>(
|
||||
this T partitioner,
|
||||
in ImageViewPartitionInfo.CreateInfo createInfo
|
||||
)
|
||||
where T : IImageViewPartitioner
|
||||
{
|
||||
if (!partitioner.TryCreatePartition(createInfo, out var partition))
|
||||
throw new OutOfMemoryException("Failed to create partition");
|
||||
|
||||
return partition;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
namespace Brutal.VulkanApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Struct representing a sub-image resource, containing the image handle, subresource, offset and extent
|
||||
/// Used to help with staging resources
|
||||
/// </summary>
|
||||
public struct ImageStagingRange
|
||||
{
|
||||
/// <summary>
|
||||
/// Image handle
|
||||
/// </summary>
|
||||
public VkImage Image;
|
||||
|
||||
/// <summary>
|
||||
/// Subresource range of the image
|
||||
/// </summary>
|
||||
public VkImageSubresourceRange SubresourceRange;
|
||||
|
||||
/// <summary>
|
||||
/// Number of mip levels in the subresource
|
||||
/// </summary>
|
||||
public int MipLevels => SubresourceRange.LevelCount;
|
||||
|
||||
/// <summary>
|
||||
/// Offset of the sub-image within the original image
|
||||
/// </summary>
|
||||
public VkOffset3D Offset;
|
||||
|
||||
/// <summary>
|
||||
/// Extent of the sub-image
|
||||
/// </summary>
|
||||
public VkExtent3D Extent;
|
||||
|
||||
/// <summary>
|
||||
/// Create a staging range from an image, extent and subresource range
|
||||
/// </summary>
|
||||
/// <param name="image"></param>
|
||||
/// <param name="extent3D"></param>
|
||||
/// <param name="subresourceRange"></param>
|
||||
/// <returns></returns>
|
||||
public static ImageStagingRange FromImage(
|
||||
VkImage image,
|
||||
VkExtent3D extent3D,
|
||||
VkImageSubresourceRange subresourceRange
|
||||
)
|
||||
{
|
||||
return new ImageStagingRange
|
||||
{
|
||||
Image = image,
|
||||
SubresourceRange = subresourceRange,
|
||||
Offset = new VkOffset3D(),
|
||||
Extent = extent3D,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convenience method to get a staging subresource struct for this sub-image
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static ImageStagingRange FromPartition(ImageViewPartitionInfo subImage)
|
||||
{
|
||||
return new ImageStagingRange
|
||||
{
|
||||
Image = subImage.VkImage,
|
||||
SubresourceRange = subImage.LocalSubresourceRange,
|
||||
Offset = subImage.Offset,
|
||||
Extent = subImage.Extent,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
namespace Brutal.VulkanApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Allocation information for a partition of an image allocation (portion of a larger image)
|
||||
/// </summary>
|
||||
/// <param name="ImageView">Handle to the source image view</param>
|
||||
/// <param name="ArrayCount">Number of array layers allocated to this sub-image, within ImageViewSubresourceRange</param>
|
||||
/// <param name="ArrayOffset">Array offset of the sub-image, within ImageViewSubresourceRange</param>
|
||||
/// <param name="Extent">Size of the sub-image</param>
|
||||
/// <param name="Offset">Offset of the sub-image within the original image</param>
|
||||
/// <param name="Partitioner">Source object that created this partition, if any. If null, assume this is a full image allocation</param>
|
||||
public readonly record struct ImageViewPartitionInfo(
|
||||
ImageViewEx ImageView,
|
||||
int ArrayCount,
|
||||
int ArrayOffset,
|
||||
VkExtent3D Extent,
|
||||
VkOffset3D Offset,
|
||||
IImageViewPartitioner? Partitioner
|
||||
)
|
||||
{
|
||||
/// <summary>
|
||||
/// Creation information for a sub-image allocation
|
||||
/// </summary>
|
||||
/// <param name="Extent">Size of the sub-image</param>
|
||||
/// <param name="LayerCount">Number of array layers allocated to this sub-image, within ImageViewSubresourceRange</param>
|
||||
public record struct CreateInfo(VkExtent3D Extent, int LayerCount);
|
||||
|
||||
/// <summary>
|
||||
/// If true, this partition is a full image allocation
|
||||
/// </summary>
|
||||
public bool IsFullPartition => Partitioner is null;
|
||||
|
||||
public ImageEx Image => ImageView.Image;
|
||||
|
||||
/// <summary>
|
||||
/// Image handle
|
||||
/// </summary>
|
||||
public VkImage VkImage => Image.VkImage;
|
||||
|
||||
/// <summary>
|
||||
/// Subresource range of the actual sub-image, within the original image
|
||||
/// </summary>
|
||||
public VkImageSubresourceRange LocalSubresourceRange =>
|
||||
ImageView.SubresourceRange with
|
||||
{
|
||||
BaseArrayLayer = ImageView.SubresourceRange.BaseArrayLayer + ArrayOffset,
|
||||
LayerCount = ArrayCount,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Get the normalized offset of the sub-image within the original image.
|
||||
/// EG: for UV mapping
|
||||
/// </summary>
|
||||
/// <returns>Normalized offset as float3</returns>
|
||||
public readonly float3 GetNormalizedOffset()
|
||||
{
|
||||
var totalExtent = Image.Extent;
|
||||
return new float3(
|
||||
Offset.X / (float)totalExtent.Width,
|
||||
Offset.Y / (float)totalExtent.Height,
|
||||
Offset.Z / (float)totalExtent.Depth
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the normalized extent of the sub-image within the original image
|
||||
/// EG: for UV mapping
|
||||
/// </summary>
|
||||
/// <returns>Normalized extent as float3</returns>
|
||||
public readonly float3 GetNormalizedExtent()
|
||||
{
|
||||
var totalExtent = Image.Extent;
|
||||
return new float3(
|
||||
Extent.Width / (float)totalExtent.Width,
|
||||
Extent.Height / (float)totalExtent.Height,
|
||||
Extent.Depth / (float)totalExtent.Depth
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
namespace Brutal.VulkanApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Texture array slot partitioner
|
||||
/// Creates and manages an image array, and this allocator will distribute slots from the array for users to stage/bind to
|
||||
/// </summary>
|
||||
public class TextureArrayPartitioner : IRecycleImageViewPartitioner
|
||||
{
|
||||
public ImageViewPartitionInfo BackingPartition { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Free list of texture array slots
|
||||
/// </summary>
|
||||
private readonly FreeListIndexPool _textureArraySlots;
|
||||
|
||||
/// <summary>
|
||||
/// Total number of slots in the texture array
|
||||
/// </summary>
|
||||
public int TotalSlots => BackingPartition.LocalSubresourceRange.LayerCount;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a texture array partitioner from an existing image allocation
|
||||
/// </summary>
|
||||
/// <param name="backingPartition">Backing image allocation</param>
|
||||
public TextureArrayPartitioner(ImageViewPartitionInfo backingPartition)
|
||||
{
|
||||
BackingPartition = backingPartition;
|
||||
_textureArraySlots = new FreeListIndexPool(backingPartition.LocalSubresourceRange.LayerCount, false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryCreatePartition(
|
||||
in ImageViewPartitionInfo.CreateInfo createInfo,
|
||||
out ImageViewPartitionInfo partition
|
||||
)
|
||||
{
|
||||
if (
|
||||
createInfo.Extent.Width > BackingPartition.Extent.Width
|
||||
|| createInfo.Extent.Height > BackingPartition.Extent.Height
|
||||
|| createInfo.Extent.Depth > BackingPartition.Extent.Depth
|
||||
)
|
||||
{
|
||||
throw new ArgumentException("Extent mismatch");
|
||||
}
|
||||
if (createInfo.LayerCount > 1)
|
||||
{
|
||||
throw new ArgumentException("ArraySlots must be 1");
|
||||
}
|
||||
|
||||
if (!TryAllocateSlot(out var slot))
|
||||
{
|
||||
partition = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
partition = BackingPartition with
|
||||
{
|
||||
Partitioner = this,
|
||||
Extent = createInfo.Extent,
|
||||
ArrayCount = 1,
|
||||
ArrayOffset = BackingPartition.ArrayOffset + slot,
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a slot in the texture array and return the array index (slot)
|
||||
/// Can call this directly if you don't need the full ImageSubAllocationInfo
|
||||
/// </summary>
|
||||
/// <returns>Slot/array index allocated</returns>
|
||||
public bool TryAllocateSlot(out int slot)
|
||||
{
|
||||
return _textureArraySlots.TryAdd(out slot);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void FreePartition(in ImageViewPartitionInfo inAllocation)
|
||||
{
|
||||
FreeSlot(inAllocation.ArrayOffset - BackingPartition.LocalSubresourceRange.LayerCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Free up a slot in texture array
|
||||
/// Can call this directly if you didn't need the full ImageSubAllocationInfo and just used the slot handle
|
||||
/// </summary>
|
||||
/// <param name="slot">Array index (aka slot handle)</param>
|
||||
public void FreeSlot(int slot)
|
||||
{
|
||||
_textureArraySlots.Remove(slot);
|
||||
}
|
||||
|
||||
public void Reset(ImageViewPartitionInfo info)
|
||||
{
|
||||
BackingPartition = info;
|
||||
_textureArraySlots.Clear();
|
||||
_textureArraySlots.Capacity = BackingPartition.LocalSubresourceRange.LayerCount;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue