namespace Brutal.VulkanApi.Abstractions; /// /// Lightweight handle to an image allocation /// public readonly struct ImageEx : IDisposable, INullable { public ref struct CreateInfo { /// The name to apply to the image and allocation public required string Name; /// public VkImageCreateFlags ImageFlags; /// public VkImageType ImageType; /// public VkFormat ImageFormat; /// public VkExtent3D ImageExtent; /// public int ImageMipLevels; /// public int ImageArrayLayers; /// public VkSampleCountFlags ImageSamples; /// public VkImageTiling ImageTiling; /// public VkImageUsageFlags ImageUsage; /// public VkSharingMode ImageSharingMode; /// public ReadOnlySpan ImageQueueFamilyIndices; /// public VkImageLayout ImageInitialLayout; /// public Ptr ImageNext { get; set; } /// public MemoryPreference AllocPreference; /// public VkMemoryPropertyFlags AllocRequiredProperties; /// public VkMemoryPropertyFlags AllocPreferredProperties; /// public float AllocPriority; /// public Ptr AllocNext { get; set; } /// public VkMemoryAllocateFlags AllocFlags; /// /// Extract or populate this struct with an ImageCreateInfo /// public unsafe VkImageCreateInfo ImageCreateInfo { get => new() { ImageType = ImageType, ArrayLayers = ImageArrayLayers, Samples = ImageSamples, MipLevels = ImageMipLevels, Flags = ImageFlags, InitialLayout = ImageInitialLayout, Format = ImageFormat, Extent = ImageExtent, Tiling = ImageTiling, Usage = ImageUsage, SharingMode = ImageSharingMode, QueueFamilyIndices = ImageQueueFamilyIndices.AsPointer(), QueueFamilyIndexCount = ImageQueueFamilyIndices.Length, Next = ImageNext, }; set { ImageType = value.ImageType; ImageArrayLayers = value.ArrayLayers; ImageSamples = value.Samples; ImageMipLevels = value.MipLevels; ImageFlags = value.Flags; ImageInitialLayout = value.InitialLayout; ImageFormat = value.Format; ImageExtent = value.Extent; ImageTiling = value.Tiling; ImageUsage = value.Usage; ImageSharingMode = value.SharingMode; ImageQueueFamilyIndices = new ReadOnlySpan(value.QueueFamilyIndices, value.QueueFamilyIndexCount); ImageNext = value.Next; } } /// /// Extract or populate this struct with an AllocationInfo.CreateInfo /// public AllocationInfo.CreateInfo AllocationInfo { get => new() { Preference = AllocPreference, RequiredProperties = AllocRequiredProperties, PreferredProperties = AllocPreferredProperties, Priority = AllocPriority, Next = AllocNext, AllocateFlags = AllocFlags, }; set { AllocPreference = value.Preference; AllocRequiredProperties = value.RequiredProperties; AllocPreferredProperties = value.PreferredProperties; AllocPriority = value.Priority; AllocNext = value.Next; AllocFlags = value.AllocateFlags; } } } /// /// Source of the image allocation /// public readonly IImageAllocator Allocator; /// /// Handles to allocated objects and metadata /// public readonly ImageAllocationInfo AllocationInfo; /// /// Image handle /// public VkImage VkImage => AllocationInfo.Image; /// /// Image memory /// public VkDeviceMemory VkDeviceMemory => AllocationInfo.MemAllocationInfo.Memory; /// /// Size of image /// public VkExtent3D Extent => AllocationInfo.Extent; /// /// Format of allocated image /// public VkFormat Format => AllocationInfo.Format; /// /// Number of mip levels allocated /// public int MipLevels => AllocationInfo.MipLevels; /// /// Number of array layers allocated /// public int ArrayLayers => AllocationInfo.ArrayLayers; /// /// Allocator-specific handle /// public nint UserHandle => AllocationInfo.AllocHandle; private readonly VkImageSubresourceRange _fullRange; /// /// Construct an ImageEx object /// /// Allocator for image memory /// Creation struct internal unsafe ImageEx(IImageAllocator allocator, CreateInfo createInfo) { Allocator = allocator; AllocationInfo = allocator.AllocateImage( createInfo.Name, createInfo.ImageCreateInfo, createInfo.AllocationInfo ); _fullRange = FullRangeOf(AllocationInfo); } /// /// Constructor for an image that has already been preallocated /// /// Allocated image sourced from an IImageAllocator public ImageEx(ImageAllocationInfo allocationInfo) { AllocationInfo = allocationInfo; Allocator = (IImageAllocator)allocationInfo.MemAllocationInfo.Allocator; _fullRange = FullRangeOf(allocationInfo); } // The full range is derived state; both constructors must agree on it private static VkImageSubresourceRange FullRangeOf(in ImageAllocationInfo allocationInfo) => new() { AspectMask = VkImageAspectFlags.None, BaseMipLevel = 0, LevelCount = allocationInfo.MipLevels, BaseArrayLayer = 0, LayerCount = allocationInfo.ArrayLayers, }; /// /// Convenience method to create an image view from the image /// /// Type of imageview needed /// Range imageview should encompass /// VkImageView handle. It is up to user to dispose of this public ImageViewEx CreateImageView( VkImageViewType imageViewType, VkImageSubresourceRange subresourceRange, VkAllocator? allocator ) { var device = Allocator.GetDevice(); var handle = device.CreateImageView( new VkImageViewCreateInfo { Image = VkImage, ViewType = imageViewType, Format = Format, SubresourceRange = subresourceRange, }, allocator ); return new ImageViewEx(device, this, handle, imageViewType, subresourceRange, allocator); } public VkImageSubresourceRange GetFullRange(VkImageAspectFlags aspectMask) => _fullRange with { AspectMask = aspectMask, }; /// /// The maximum number of mip levels a full chain for this image's extent can hold /// public int MaxMipLevels => VulkanHelpers.MaxMipLevels(Extent); /// public bool IsNull() { return VkImage.IsNull(); } /// public void Dispose() { Allocator.FreeAllocation(in AllocationInfo); } } public static class ImageExExtensions { public static ImageEx CreateImage(this IImageAllocator allocator, ImageEx.CreateInfo createInfo) { return new ImageEx(allocator, createInfo); } }