Skip to content

Commit 80c89b5

Browse files
SS-JIAfacebook-github-bot
authored andcommitted
Only use LINEAR tiling if it's available
Summary: ## 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. Reviewed By: yipjustin Differential Revision: D66029137
1 parent 0877926 commit 80c89b5

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
@@ -150,7 +150,39 @@ Adapter::Adapter(
150150
pipeline_layout_cache_(device_.handle),
151151
compute_pipeline_cache_(device_.handle, cache_data_path),
152152
sampler_cache_(device_.handle),
153-
vma_(instance_, physical_device_.handle, device_.handle) {}
153+
vma_(instance_, physical_device_.handle, device_.handle),
154+
linear_tiling_3d_enabled_{true} {
155+
// Test creating a 3D image with linear tiling to see if it is supported.
156+
// According to the Vulkan spec, linear tiling may not be supported for 3D
157+
// images.
158+
VkExtent3D image_extents{1u, 1u, 1u};
159+
const VkImageCreateInfo image_create_info{
160+
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
161+
nullptr, // pNext
162+
0u, // flags
163+
VK_IMAGE_TYPE_3D, // imageType
164+
VK_FORMAT_R32G32B32A32_SFLOAT, // format
165+
image_extents, // extents
166+
1u, // mipLevels
167+
1u, // arrayLayers
168+
VK_SAMPLE_COUNT_1_BIT, // samples
169+
VK_IMAGE_TILING_LINEAR, // tiling
170+
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT, // usage
171+
VK_SHARING_MODE_EXCLUSIVE, // sharingMode
172+
0u, // queueFamilyIndexCount
173+
nullptr, // pQueueFamilyIndices
174+
VK_IMAGE_LAYOUT_UNDEFINED, // initialLayout
175+
};
176+
VkImage image = VK_NULL_HANDLE;
177+
VkResult res =
178+
vkCreateImage(device_.handle, &image_create_info, nullptr, &image);
179+
if (res == VK_ERROR_FEATURE_NOT_PRESENT) {
180+
linear_tiling_3d_enabled_ = false;
181+
} else if (res == VK_SUCCESS) {
182+
vkDestroyImage(device_.handle, image, nullptr);
183+
}
184+
return;
185+
}
154186

155187
Adapter::Queue Adapter::request_queue() {
156188
// 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)