Skip to content

Only use LINEAR tiling if it's available #6902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion backends/vulkan/runtime/api/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ Context::Context(size_t adapter_i, const ContextConfig& config)
buffer_clearlist_mutex_{},
buffers_to_clear_{},
image_clearlist_mutex_{},
images_to_clear_{} {}
images_to_clear_{},
preferred_image_tiling_{VK_IMAGE_TILING_OPTIMAL} {
if (adapter_p_->linear_tiling_3d_enabled()) {
preferred_image_tiling_ = VK_IMAGE_TILING_LINEAR;
}
}

Context::~Context() {
try {
Expand Down
6 changes: 6 additions & 0 deletions backends/vulkan/runtime/api/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Context final {
std::vector<vkapi::VulkanBuffer> buffers_to_clear_;
std::mutex image_clearlist_mutex_;
std::vector<vkapi::VulkanImage> images_to_clear_;
// Misc
VkImageTiling preferred_image_tiling_;

public:
// Adapter access
Expand Down Expand Up @@ -123,6 +125,10 @@ class Context final {
return querypool_;
}

inline VkImageTiling preferred_image_tiling() {
return preferred_image_tiling_;
}

/*
* By default, the querypool attached to a Context instance is uninitialized.
* This function triggers the querypool to be created via vkCreateQueryPool.
Expand Down
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 @@ -245,6 +245,7 @@ vkapi::VulkanImage allocate_image(
vkapi::create_extent3d(image_extents),
image_format,
image_type,
context_ptr->preferred_image_tiling(),
image_view_type,
sampler_props,
sampler,
Expand Down
34 changes: 33 additions & 1 deletion backends/vulkan/runtime/vk_api/Adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,39 @@ Adapter::Adapter(
pipeline_layout_cache_(device_.handle),
compute_pipeline_cache_(device_.handle, cache_data_path),
sampler_cache_(device_.handle),
vma_(instance_, physical_device_.handle, device_.handle) {}
vma_(instance_, physical_device_.handle, device_.handle),
linear_tiling_3d_enabled_{true} {
// Test creating a 3D image with linear tiling to see if it is supported.
// According to the Vulkan spec, linear tiling may not be supported for 3D
// images.
VkExtent3D image_extents{1u, 1u, 1u};
const VkImageCreateInfo image_create_info{
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
nullptr, // pNext
0u, // flags
VK_IMAGE_TYPE_3D, // imageType
VK_FORMAT_R32G32B32A32_SFLOAT, // format
image_extents, // extents
1u, // mipLevels
1u, // arrayLayers
VK_SAMPLE_COUNT_1_BIT, // samples
VK_IMAGE_TILING_LINEAR, // tiling
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT, // usage
VK_SHARING_MODE_EXCLUSIVE, // sharingMode
0u, // queueFamilyIndexCount
nullptr, // pQueueFamilyIndices
VK_IMAGE_LAYOUT_UNDEFINED, // initialLayout
};
VkImage image = VK_NULL_HANDLE;
VkResult res =
vkCreateImage(device_.handle, &image_create_info, nullptr, &image);
if (res == VK_ERROR_FEATURE_NOT_PRESENT) {
linear_tiling_3d_enabled_ = false;
} else if (res == VK_SUCCESS) {
vkDestroyImage(device_.handle, image, nullptr);
}
return;
}

Adapter::Queue Adapter::request_queue() {
// Lock the mutex as multiple threads can request a queue at the same time
Expand Down
6 changes: 6 additions & 0 deletions backends/vulkan/runtime/vk_api/Adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class Adapter final {
// Memory Management
SamplerCache sampler_cache_;
Allocator vma_;
// Miscellaneous
bool linear_tiling_3d_enabled_;

public:
// Physical Device metadata
Expand Down Expand Up @@ -153,6 +155,10 @@ class Adapter final {
return vma_;
}

inline bool linear_tiling_3d_enabled() const {
return linear_tiling_3d_enabled_;
}

// Physical Device Features

inline bool supports_16bit_storage_buffers() {
Expand Down
2 changes: 2 additions & 0 deletions backends/vulkan/runtime/vk_api/memory/Allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ VulkanImage Allocator::create_image(
const VkExtent3D& extents,
const VkFormat image_format,
const VkImageType image_type,
const VkImageTiling image_tiling,
const VkImageViewType image_view_type,
const VulkanImage::SamplerProperties& sampler_props,
VkSampler sampler,
Expand All @@ -117,6 +118,7 @@ VulkanImage Allocator::create_image(
image_type,
image_format,
extents,
image_tiling,
usage,
};

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 @@ -59,6 +59,7 @@ class Allocator final {
const VkExtent3D&,
const VkFormat,
const VkImageType,
const VkImageTiling,
const VkImageViewType,
const VulkanImage::SamplerProperties&,
VkSampler,
Expand Down
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/vk_api/memory/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ VulkanImage::VulkanImage(
1u, // mipLevels
1u, // arrayLayers
VK_SAMPLE_COUNT_1_BIT, // samples
VK_IMAGE_TILING_LINEAR, // tiling
image_properties_.image_tiling, // tiling
image_properties_.image_usage, // usage
VK_SHARING_MODE_EXCLUSIVE, // sharingMode
0u, // queueFamilyIndexCount
Expand Down
1 change: 1 addition & 0 deletions backends/vulkan/runtime/vk_api/memory/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class VulkanImage final {
VkImageType image_type;
VkFormat image_format;
VkExtent3D image_extents;
VkImageTiling image_tiling;
VkImageUsageFlags image_usage;
};

Expand Down
1 change: 1 addition & 0 deletions backends/vulkan/test/vulkan_compute_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ TEST_F(VulkanComputeAPITest, test_tensor_creation_from_vulkan_image) {
vkapi::create_extent3d(image_extents),
image_format,
image_type,
context()->preferred_image_tiling(),
image_view_type,
sampler_props,
sampler,
Expand Down
Loading