namespace Brutal.VulkanApi.Abstractions; /// /// Lightweight handle to an image allocation /// public readonly struct ImageEx : IDisposable { 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 MemoryPreference AllocPreference; /// public VkMemoryPropertyFlags AllocRequiredProperties; /// public VkMemoryPropertyFlags AllocPreferredProperties; /// public float AllocPriority; /// /// 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, }; set { ImageType = value.ImageType; ImageArrayLayers = value.ArrayLayers; ImageSamples = value.Samples; ImageMipLevels = value.MipLevels; ImageFlags = value.Flags; ImageType = value.ImageType; ImageFormat = value.Format; ImageExtent = value.Extent; ImageTiling = value.Tiling; ImageUsage = value.Usage; ImageSharingMode = value.SharingMode; ImageQueueFamilyIndices = new ReadOnlySpan(value.QueueFamilyIndices, value.QueueFamilyIndexCount); } } /// /// Extract or populate this struct with an AllocationInfo.CreateInfo /// public AllocationInfo.CreateInfo AllocationInfo { get => new() { Preference = AllocPreference, RequiredProperties = AllocRequiredProperties, PreferredProperties = AllocPreferredProperties, Priority = AllocPriority, }; set { AllocPreference = value.Preference; AllocRequiredProperties = value.RequiredProperties; AllocPreferredProperties = value.PreferredProperties; AllocPriority = value.Priority; } } } private 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 = new VkImageSubresourceRange { AspectMask = VkImageAspectFlags.None, BaseMipLevel = 0, LevelCount = AllocationInfo.MipLevels, BaseArrayLayer = 0, LayerCount = AllocationInfo.ArrayLayers, }; } /// /// 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; } /// /// 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, }; /// 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); } }