Skip to content

Commit 8f8f574

Browse files
committed
[ET-VK] Only use LINEAR tiling if it's available
## Context Recently, we switched to using `VK_IMAGE_TILING_LINEAR` to minimize memory footprint. However, according to the [Vulkan Spec](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkImageCreateInfo.html) the `VK_IMAGE_TILING_LINEAR` tiling may only be available for 2D textures. ## Changes * When constructing the `Adapter` class, check if it's possible to create a 3D texture with LINEAR tiling * Add a way to query preferred tiling from `Context` * Construct VkImage with the preferred tiling. Differential Revision: [D66029137](https://our.internmc.facebook.com/intern/diff/D66029137/) [ghstack-poisoned]
1 parent faab497 commit 8f8f574

File tree

10 files changed

+58
-3
lines changed

10 files changed

+58
-3
lines changed

backends/vulkan/runtime/api/Context.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ Context::Context(size_t adapter_i, const ContextConfig& config)
3939
buffer_clearlist_mutex_{},
4040
buffers_to_clear_{},
4141
image_clearlist_mutex_{},
42-
images_to_clear_{} {}
42+
images_to_clear_{},
43+
preferred_image_tiling_{VK_IMAGE_TILING_OPTIMAL} {
44+
if (adapter_p_->linear_tiling_3d_enabled()) {
45+
preferred_image_tiling_ = VK_IMAGE_TILING_LINEAR;
46+
}
47+
}
4348

4449
Context::~Context() {
4550
try {

backends/vulkan/runtime/api/Context.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class Context final {
7373
std::vector<vkapi::VulkanBuffer> buffers_to_clear_;
7474
std::mutex image_clearlist_mutex_;
7575
std::vector<vkapi::VulkanImage> images_to_clear_;
76+
// Misc
77+
VkImageTiling preferred_image_tiling_;
7678

7779
public:
7880
// Adapter access
@@ -123,6 +125,10 @@ class Context final {
123125
return querypool_;
124126
}
125127

128+
inline VkImageTiling preferred_image_tiling() {
129+
return preferred_image_tiling_;
130+
}
131+
126132
/*
127133
* By default, the querypool attached to a Context instance is uninitialized.
128134
* This function triggers the querypool to be created via vkCreateQueryPool.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ vkapi::VulkanImage allocate_image(
245245
vkapi::create_extent3d(image_extents),
246246
image_format,
247247
image_type,
248+
context_ptr->preferred_image_tiling(),
248249
image_view_type,
249250
sampler_props,
250251
sampler,

backends/vulkan/runtime/vk_api/Adapter.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,39 @@ Adapter::Adapter(
156156
pipeline_layout_cache_(device_.handle),
157157
compute_pipeline_cache_(device_.handle, cache_data_path),
158158
sampler_cache_(device_.handle),
159-
vma_(instance_, physical_device_.handle, device_.handle) {}
159+
vma_(instance_, physical_device_.handle, device_.handle),
160+
linear_tiling_3d_enabled_{true} {
161+
// Test creating a 3D image with linear tiling to see if it is supported.
162+
// According to the Vulkan spec, linear tiling may not be supported for 3D
163+
// images.
164+
VkExtent3D image_extents{1u, 1u, 1u};
165+
const VkImageCreateInfo image_create_info{
166+
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
167+
nullptr, // pNext
168+
0u, // flags
169+
VK_IMAGE_TYPE_3D, // imageType
170+
VK_FORMAT_R32G32B32A32_SFLOAT, // format
171+
image_extents, // extents
172+
1u, // mipLevels
173+
1u, // arrayLayers
174+
VK_SAMPLE_COUNT_1_BIT, // samples
175+
VK_IMAGE_TILING_LINEAR, // tiling
176+
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT, // usage
177+
VK_SHARING_MODE_EXCLUSIVE, // sharingMode
178+
0u, // queueFamilyIndexCount
179+
nullptr, // pQueueFamilyIndices
180+
VK_IMAGE_LAYOUT_UNDEFINED, // initialLayout
181+
};
182+
VkImage image = VK_NULL_HANDLE;
183+
VkResult res =
184+
vkCreateImage(device_.handle, &image_create_info, nullptr, &image);
185+
if (res == VK_ERROR_FEATURE_NOT_PRESENT) {
186+
linear_tiling_3d_enabled_ = false;
187+
} else if (res == VK_SUCCESS) {
188+
vkDestroyImage(device_.handle, image, nullptr);
189+
}
190+
return;
191+
}
160192

161193
Adapter::Queue Adapter::request_queue() {
162194
// Lock the mutex as multiple threads can request a queue at the same time

backends/vulkan/runtime/vk_api/Adapter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class Adapter final {
9292
// Memory Management
9393
SamplerCache sampler_cache_;
9494
Allocator vma_;
95+
// Miscellaneous
96+
bool linear_tiling_3d_enabled_;
9597

9698
public:
9799
// Physical Device metadata
@@ -153,6 +155,10 @@ class Adapter final {
153155
return vma_;
154156
}
155157

158+
inline bool linear_tiling_3d_enabled() const {
159+
return linear_tiling_3d_enabled_;
160+
}
161+
156162
// Physical Device Features
157163

158164
inline bool supports_16bit_storage_buffers() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ VulkanImage Allocator::create_image(
9999
const VkExtent3D& extents,
100100
const VkFormat image_format,
101101
const VkImageType image_type,
102+
const VkImageTiling image_tiling,
102103
const VkImageViewType image_view_type,
103104
const VulkanImage::SamplerProperties& sampler_props,
104105
VkSampler sampler,
@@ -117,6 +118,7 @@ VulkanImage Allocator::create_image(
117118
image_type,
118119
image_format,
119120
extents,
121+
image_tiling,
120122
usage,
121123
};
122124

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Allocator final {
5959
const VkExtent3D&,
6060
const VkFormat,
6161
const VkImageType,
62+
const VkImageTiling,
6263
const VkImageViewType,
6364
const VulkanImage::SamplerProperties&,
6465
VkSampler,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ VulkanImage::VulkanImage(
156156
1u, // mipLevels
157157
1u, // arrayLayers
158158
VK_SAMPLE_COUNT_1_BIT, // samples
159-
VK_IMAGE_TILING_LINEAR, // tiling
159+
image_properties_.image_tiling, // tiling
160160
image_properties_.image_usage, // usage
161161
VK_SHARING_MODE_EXCLUSIVE, // sharingMode
162162
0u, // queueFamilyIndexCount

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class VulkanImage final {
7474
VkImageType image_type;
7575
VkFormat image_format;
7676
VkExtent3D image_extents;
77+
VkImageTiling image_tiling;
7778
VkImageUsageFlags image_usage;
7879
};
7980

backends/vulkan/test/vulkan_compute_api_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,7 @@ TEST_F(VulkanComputeAPITest, test_tensor_creation_from_vulkan_image) {
11231123
vkapi::create_extent3d(image_extents),
11241124
image_format,
11251125
image_type,
1126+
context()->preferred_image_tiling(),
11261127
image_view_type,
11271128
sampler_props,
11281129
sampler,

0 commit comments

Comments
 (0)