Skip to content

Commit 3b64f87

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: 246903715 @exported-using-ghexport Differential Revision: [D63298759](https://our.internmc.facebook.com/intern/diff/D63298759/)
1 parent b66c255 commit 3b64f87

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
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: 2 additions & 0 deletions
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,6 +128,7 @@ 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,

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: 31 additions & 4 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,6 +109,7 @@ 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,
@@ -116,7 +118,8 @@ VulkanImage::VulkanImage(
116118
const VkImageLayout layout,
117119
VkSampler sampler,
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,30 @@ 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+
: device_{device},
191+
image_properties_{image_props},
192+
view_properties_{},
193+
sampler_properties_{},
194+
allocator_(VK_NULL_HANDLE),
195+
memory_{},
196+
owns_memory_(false),
197+
is_copy_(false),
198+
handles_{
199+
image,
200+
image_view,
201+
sampler,
202+
},
203+
layout_{VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL} {}
204+
181205
VulkanImage::VulkanImage(const VulkanImage& other) noexcept
182-
: image_properties_(other.image_properties_),
206+
: device_(other.device_),
207+
image_properties_(other.image_properties_),
183208
view_properties_(other.view_properties_),
184209
sampler_properties_(other.sampler_properties_),
185210
allocator_(other.allocator_),
@@ -191,7 +216,8 @@ VulkanImage::VulkanImage(const VulkanImage& other) noexcept
191216
layout_(other.layout_) {}
192217

193218
VulkanImage::VulkanImage(VulkanImage&& other) noexcept
194-
: image_properties_(other.image_properties_),
219+
: device_(other.device_),
220+
image_properties_(other.image_properties_),
195221
view_properties_(other.view_properties_),
196222
sampler_properties_(other.sampler_properties_),
197223
allocator_(other.allocator_),
@@ -212,6 +238,7 @@ VulkanImage& VulkanImage::operator=(VulkanImage&& other) noexcept {
212238
VkImageView tmp_image_view = handles_.image_view;
213239
bool tmp_owns_memory = owns_memory_;
214240

241+
device_ = other.device_;
215242
image_properties_ = other.image_properties_;
216243
view_properties_ = other.view_properties_;
217244
sampler_properties_ = other.sampler_properties_;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class VulkanImage final {
9393
explicit VulkanImage();
9494

9595
explicit VulkanImage(
96+
VkDevice,
9697
const VmaAllocator,
9798
const VmaAllocationCreateInfo&,
9899
const ImageProperties&,
@@ -102,6 +103,13 @@ class VulkanImage final {
102103
VkSampler,
103104
const bool allocate_memory = true);
104105

106+
explicit VulkanImage(
107+
VkDevice,
108+
const ImageProperties&,
109+
VkImage,
110+
VkImageView,
111+
VkSampler);
112+
105113
protected:
106114
/*
107115
* The Copy constructor allows for creation of a class instance that are
@@ -136,6 +144,7 @@ class VulkanImage final {
136144
friend struct ImageMemoryBarrier;
137145

138146
private:
147+
VkDevice device_;
139148
ImageProperties image_properties_;
140149
ViewProperties view_properties_;
141150
SamplerProperties sampler_properties_;
@@ -159,9 +168,7 @@ class VulkanImage final {
159168
void create_image_view();
160169

161170
inline VkDevice device() const {
162-
VmaAllocatorInfo allocator_info{};
163-
vmaGetAllocatorInfo(allocator_, &allocator_info);
164-
return allocator_info.device;
171+
return device_;
165172
}
166173

167174
inline VmaAllocator vma_allocator() const {

0 commit comments

Comments
 (0)