Skip to content
1 change: 1 addition & 0 deletions backends/vulkan/runtime/api/containers/Tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ vkapi::VulkanImage allocate_image(
VkSampler sampler = adapter_ptr->sampler_cache().retrieve(sampler_props);

return adapter_ptr->vma().create_image(
context_ptr->device(),
vkapi::create_extent3d(image_extents),
image_format,
image_type,
Expand Down
6 changes: 5 additions & 1 deletion backends/vulkan/runtime/vk_api/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ DescriptorSet& DescriptorSet::bind(
DescriptorSet& DescriptorSet::bind(
const uint32_t idx,
const VulkanImage& image) {
// If the image does not have an allocator attached, then it is externally
// allocated; assume it is already bound to memory. Otherwise, it must be
// bound to a VmaAllocation to be used.
VK_CHECK_COND(
image.has_memory(), "Image must be bound to memory for it to be usable");
image.vma_allocator() == VK_NULL_HANDLE || image.has_memory(),
"Image must be bound to memory for it to be usable");

VkImageLayout binding_layout = image.layout();
if (shader_layout_signature_[idx] == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
Expand Down
4 changes: 3 additions & 1 deletion backends/vulkan/runtime/vk_api/memory/Allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Allocation Allocator::create_allocation(
}

VulkanImage Allocator::create_image(
const VkDevice device,
const VkExtent3D& extents,
const VkFormat image_format,
const VkImageType image_type,
Expand Down Expand Up @@ -127,13 +128,14 @@ VulkanImage Allocator::create_image(
const VkImageLayout initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;

return VulkanImage(
device,
allocator_,
alloc_create_info,
image_props,
view_props,
sampler_props,
initial_layout,
sampler,
initial_layout,
allocate_memory);
}

Expand Down
1 change: 1 addition & 0 deletions backends/vulkan/runtime/vk_api/memory/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Allocator final {
const VmaAllocationCreateInfo& create_info);

VulkanImage create_image(
const VkDevice,
const VkExtent3D&,
const VkFormat,
const VkImageType,
Expand Down
38 changes: 33 additions & 5 deletions backends/vulkan/runtime/vk_api/memory/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ void swap(ImageSampler& lhs, ImageSampler& rhs) noexcept {
//

VulkanImage::VulkanImage()
: image_properties_{},
: device_{VK_NULL_HANDLE},
image_properties_{},
view_properties_{},
sampler_properties_{},
allocator_(VK_NULL_HANDLE),
Expand All @@ -108,15 +109,17 @@ VulkanImage::VulkanImage()
layout_{} {}

VulkanImage::VulkanImage(
VkDevice device,
VmaAllocator vma_allocator,
const VmaAllocationCreateInfo& allocation_create_info,
const ImageProperties& image_props,
const ViewProperties& view_props,
const SamplerProperties& sampler_props,
const VkImageLayout layout,
VkSampler sampler,
const VkImageLayout layout,
const bool allocate_memory)
: image_properties_(image_props),
: device_{device},
image_properties_(image_props),
view_properties_(view_props),
sampler_properties_(sampler_props),
allocator_(vma_allocator),
Expand Down Expand Up @@ -178,8 +181,31 @@ VulkanImage::VulkanImage(
}
}

VulkanImage::VulkanImage(
VkDevice device,
const ImageProperties& image_props,
VkImage image,
VkImageView image_view,
VkSampler sampler,
const VkImageLayout layout)
: device_{device},
image_properties_{image_props},
view_properties_{},
sampler_properties_{},
allocator_(VK_NULL_HANDLE),
memory_{},
owns_memory_(false),
is_copy_(false),
handles_{
image,
image_view,
sampler,
},
layout_{layout} {}

VulkanImage::VulkanImage(const VulkanImage& other) noexcept
: image_properties_(other.image_properties_),
: device_(other.device_),
image_properties_(other.image_properties_),
view_properties_(other.view_properties_),
sampler_properties_(other.sampler_properties_),
allocator_(other.allocator_),
Expand All @@ -191,7 +217,8 @@ VulkanImage::VulkanImage(const VulkanImage& other) noexcept
layout_(other.layout_) {}

VulkanImage::VulkanImage(VulkanImage&& other) noexcept
: image_properties_(other.image_properties_),
: device_(other.device_),
image_properties_(other.image_properties_),
view_properties_(other.view_properties_),
sampler_properties_(other.sampler_properties_),
allocator_(other.allocator_),
Expand All @@ -212,6 +239,7 @@ VulkanImage& VulkanImage::operator=(VulkanImage&& other) noexcept {
VkImageView tmp_image_view = handles_.image_view;
bool tmp_owns_memory = owns_memory_;

device_ = other.device_;
image_properties_ = other.image_properties_;
view_properties_ = other.view_properties_;
sampler_properties_ = other.sampler_properties_;
Expand Down
16 changes: 12 additions & 4 deletions backends/vulkan/runtime/vk_api/memory/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,24 @@ class VulkanImage final {
explicit VulkanImage();

explicit VulkanImage(
VkDevice,
const VmaAllocator,
const VmaAllocationCreateInfo&,
const ImageProperties&,
const ViewProperties&,
const SamplerProperties&,
const VkImageLayout layout,
VkSampler,
const VkImageLayout,
const bool allocate_memory = true);

explicit VulkanImage(
VkDevice,
const ImageProperties&,
VkImage,
VkImageView,
VkSampler,
const VkImageLayout);

protected:
/*
* The Copy constructor allows for creation of a class instance that are
Expand Down Expand Up @@ -136,6 +145,7 @@ class VulkanImage final {
friend struct ImageMemoryBarrier;

private:
VkDevice device_;
ImageProperties image_properties_;
ViewProperties view_properties_;
SamplerProperties sampler_properties_;
Expand All @@ -159,9 +169,7 @@ class VulkanImage final {
void create_image_view();

inline VkDevice device() const {
VmaAllocatorInfo allocator_info{};
vmaGetAllocatorInfo(allocator_, &allocator_info);
return allocator_info.device;
return device_;
}

inline VmaAllocator vma_allocator() const {
Expand Down
Loading