Skip to content

Commit 63ff05c

Browse files
committed
[10/n][ET-VK] Enable externally allocated VulkanImages
Pull Request resolved: #5992 We now include handle to `VkDevice` in `VulkanImage` since our allocations don't go through VMA. **Internal:** Following image creation of `myvulkanahb`. https://www.internalfb.com/code/aosp-vendor-meta-tools-camera-tools/[wrist-sw5100-14.0%3Af8460689765eee9332f461491a1edcfc6ad8fb92]/myvulkanahb/myvulkanahb.cpp?lines=22%2C94-96 https://www.internalfb.com/code/aosp-vendor-meta-tools-camera-tools/[wrist-sw5100-14.0%3Af8460689765eee9332f461491a1edcfc6ad8fb92]/myvulkanahb/vulkan/ImageProcessorAhbRgbaInRgbaOut.cpp?lines=26-36%2C40-47 ghstack-source-id: 247129375 @exported-using-ghexport Differential Revision: [D63298759](https://our.internmc.facebook.com/intern/diff/D63298759/)
1 parent e08d3e1 commit 63ff05c

File tree

6 files changed

+55
-11
lines changed

6 files changed

+55
-11
lines changed

backends/vulkan/runtime/api/containers/Tensor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ vkapi::VulkanImage allocate_image(
221221
VkSampler sampler = adapter_ptr->sampler_cache().retrieve(sampler_props);
222222

223223
return adapter_ptr->vma().create_image(
224+
context_ptr->device(),
224225
vkapi::create_extent3d(image_extents),
225226
image_format,
226227
image_type,

backends/vulkan/runtime/vk_api/Descriptor.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,12 @@ DescriptorSet& DescriptorSet::bind(
116116
DescriptorSet& DescriptorSet::bind(
117117
const uint32_t idx,
118118
const VulkanImage& image) {
119+
// If the image does not have an allocator attached, then it is externally
120+
// allocated; assume it is already bound to memory. Otherwise, it must be
121+
// bound to a VmaAllocation to be used.
119122
VK_CHECK_COND(
120-
image.has_memory(), "Image must be bound to memory for it to be usable");
123+
image.vma_allocator() == VK_NULL_HANDLE || image.has_memory(),
124+
"Image must be bound to memory for it to be usable");
121125

122126
VkImageLayout binding_layout = image.layout();
123127
if (shader_layout_signature_[idx] == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {

backends/vulkan/runtime/vk_api/memory/Allocator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Allocation Allocator::create_allocation(
9595
}
9696

9797
VulkanImage Allocator::create_image(
98+
const VkDevice device,
9899
const VkExtent3D& extents,
99100
const VkFormat image_format,
100101
const VkImageType image_type,
@@ -127,13 +128,14 @@ VulkanImage Allocator::create_image(
127128
const VkImageLayout initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
128129

129130
return VulkanImage(
131+
device,
130132
allocator_,
131133
alloc_create_info,
132134
image_props,
133135
view_props,
134136
sampler_props,
135-
initial_layout,
136137
sampler,
138+
initial_layout,
137139
allocate_memory);
138140
}
139141

backends/vulkan/runtime/vk_api/memory/Allocator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Allocator final {
5555
const VmaAllocationCreateInfo& create_info);
5656

5757
VulkanImage create_image(
58+
const VkDevice,
5859
const VkExtent3D&,
5960
const VkFormat,
6061
const VkImageType,

backends/vulkan/runtime/vk_api/memory/Image.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ void swap(ImageSampler& lhs, ImageSampler& rhs) noexcept {
9292
//
9393

9494
VulkanImage::VulkanImage()
95-
: image_properties_{},
95+
: device_{VK_NULL_HANDLE},
96+
image_properties_{},
9697
view_properties_{},
9798
sampler_properties_{},
9899
allocator_(VK_NULL_HANDLE),
@@ -108,15 +109,17 @@ VulkanImage::VulkanImage()
108109
layout_{} {}
109110

110111
VulkanImage::VulkanImage(
112+
VkDevice device,
111113
VmaAllocator vma_allocator,
112114
const VmaAllocationCreateInfo& allocation_create_info,
113115
const ImageProperties& image_props,
114116
const ViewProperties& view_props,
115117
const SamplerProperties& sampler_props,
116-
const VkImageLayout layout,
117118
VkSampler sampler,
119+
const VkImageLayout layout,
118120
const bool allocate_memory)
119-
: image_properties_(image_props),
121+
: device_{device},
122+
image_properties_(image_props),
120123
view_properties_(view_props),
121124
sampler_properties_(sampler_props),
122125
allocator_(vma_allocator),
@@ -178,8 +181,31 @@ VulkanImage::VulkanImage(
178181
}
179182
}
180183

184+
VulkanImage::VulkanImage(
185+
VkDevice device,
186+
const ImageProperties& image_props,
187+
VkImage image,
188+
VkImageView image_view,
189+
VkSampler sampler,
190+
const VkImageLayout layout)
191+
: device_{device},
192+
image_properties_{image_props},
193+
view_properties_{},
194+
sampler_properties_{},
195+
allocator_(VK_NULL_HANDLE),
196+
memory_{},
197+
owns_memory_(false),
198+
is_copy_(false),
199+
handles_{
200+
image,
201+
image_view,
202+
sampler,
203+
},
204+
layout_{layout} {}
205+
181206
VulkanImage::VulkanImage(const VulkanImage& other) noexcept
182-
: image_properties_(other.image_properties_),
207+
: device_(other.device_),
208+
image_properties_(other.image_properties_),
183209
view_properties_(other.view_properties_),
184210
sampler_properties_(other.sampler_properties_),
185211
allocator_(other.allocator_),
@@ -191,7 +217,8 @@ VulkanImage::VulkanImage(const VulkanImage& other) noexcept
191217
layout_(other.layout_) {}
192218

193219
VulkanImage::VulkanImage(VulkanImage&& other) noexcept
194-
: image_properties_(other.image_properties_),
220+
: device_(other.device_),
221+
image_properties_(other.image_properties_),
195222
view_properties_(other.view_properties_),
196223
sampler_properties_(other.sampler_properties_),
197224
allocator_(other.allocator_),
@@ -212,6 +239,7 @@ VulkanImage& VulkanImage::operator=(VulkanImage&& other) noexcept {
212239
VkImageView tmp_image_view = handles_.image_view;
213240
bool tmp_owns_memory = owns_memory_;
214241

242+
device_ = other.device_;
215243
image_properties_ = other.image_properties_;
216244
view_properties_ = other.view_properties_;
217245
sampler_properties_ = other.sampler_properties_;

backends/vulkan/runtime/vk_api/memory/Image.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,24 @@ class VulkanImage final {
9393
explicit VulkanImage();
9494

9595
explicit VulkanImage(
96+
VkDevice,
9697
const VmaAllocator,
9798
const VmaAllocationCreateInfo&,
9899
const ImageProperties&,
99100
const ViewProperties&,
100101
const SamplerProperties&,
101-
const VkImageLayout layout,
102102
VkSampler,
103+
const VkImageLayout,
103104
const bool allocate_memory = true);
104105

106+
explicit VulkanImage(
107+
VkDevice,
108+
const ImageProperties&,
109+
VkImage,
110+
VkImageView,
111+
VkSampler,
112+
const VkImageLayout);
113+
105114
protected:
106115
/*
107116
* The Copy constructor allows for creation of a class instance that are
@@ -136,6 +145,7 @@ class VulkanImage final {
136145
friend struct ImageMemoryBarrier;
137146

138147
private:
148+
VkDevice device_;
139149
ImageProperties image_properties_;
140150
ViewProperties view_properties_;
141151
SamplerProperties sampler_properties_;
@@ -159,9 +169,7 @@ class VulkanImage final {
159169
void create_image_view();
160170

161171
inline VkDevice device() const {
162-
VmaAllocatorInfo allocator_info{};
163-
vmaGetAllocatorInfo(allocator_, &allocator_info);
164-
return allocator_info.device;
172+
return device_;
165173
}
166174

167175
inline VmaAllocator vma_allocator() const {

0 commit comments

Comments
 (0)