1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/*
* Copyright (c) 2015-2025 The Khronos Group Inc.
* Copyright (c) 2015-2025 Valve Corporation
* Copyright (c) 2015-2025 LunarG, Inc.
* Copyright (c) 2015-2025 Google, Inc.
* Modifications Copyright (C) 2020-2021 Advanced Micro Devices, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <thread>
#include "../framework/layer_validation_tests.h"
#include "../framework/descriptor_helper.h"
#include "../framework/thread_helper.h"
#if GTEST_IS_THREADSAFE
class PositiveThreading : public VkLayerTest {};
TEST_F(PositiveThreading, DisplayObjects) {
TEST_DESCRIPTION("Create and use VkDisplayKHR objects with GetPhysicalDeviceDisplayPropertiesKHR in thread-safety.");
AddRequiredExtensions(VK_KHR_SURFACE_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_DISPLAY_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
uint32_t prop_count = 0;
vk::GetPhysicalDeviceDisplayPropertiesKHR(Gpu(), &prop_count, nullptr);
if (prop_count == 0) {
GTEST_SKIP() << "No VkDisplayKHR properties to query";
}
std::vector<VkDisplayPropertiesKHR> display_props{prop_count};
// Create a VkDisplayKHR object
vk::GetPhysicalDeviceDisplayPropertiesKHR(Gpu(), &prop_count, display_props.data());
ASSERT_NE(prop_count, 0U);
// Now use this new object in an API call that thread safety will track
prop_count = 0;
vk::GetDisplayModePropertiesKHR(Gpu(), display_props[0].display, &prop_count, nullptr);
}
TEST_F(PositiveThreading, DisplayPlaneObjects) {
TEST_DESCRIPTION("Create and use VkDisplayKHR objects with GetPhysicalDeviceDisplayPlanePropertiesKHR in thread-safety.");
AddRequiredExtensions(VK_KHR_SURFACE_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_DISPLAY_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
uint32_t prop_count = 0;
vk::GetPhysicalDeviceDisplayPlanePropertiesKHR(Gpu(), &prop_count, nullptr);
if (prop_count != 0) {
// only grab first plane property
prop_count = 1;
VkDisplayPlanePropertiesKHR display_plane_props = {};
// Create a VkDisplayKHR object
vk::GetPhysicalDeviceDisplayPlanePropertiesKHR(Gpu(), &prop_count, &display_plane_props);
// Now use this new object in an API call
prop_count = 0;
vk::GetDisplayModePropertiesKHR(Gpu(), display_plane_props.currentDisplay, &prop_count, nullptr);
}
}
TEST_F(PositiveThreading, UpdateDescriptorUpdateAfterBindNoCollision) {
TEST_DESCRIPTION("Two threads updating the same UAB descriptor set, expected not to generate a threading error");
AddRequiredExtensions(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_MAINTENANCE_3_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::descriptorBindingStorageBufferUpdateAfterBind);
RETURN_IF_SKIP(Init());
InitRenderTarget();
OneOffDescriptorIndexingSet descriptor_set(m_device, {
{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT,
nullptr, VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT},
{1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT,
nullptr, VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT},
});
vkt::Buffer buffer(*m_device, 256, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
ThreadTestData data;
data.device = device();
data.descriptorSet = descriptor_set.set_;
data.binding = 0;
data.buffer = buffer;
std::atomic<bool> bailout{false};
data.bailout = &bailout;
m_errorMonitor->SetBailout(data.bailout);
// Update descriptors from another thread.
std::thread thread(UpdateDescriptor, &data);
// Update descriptors from this thread at the same time.
ThreadTestData data2;
data2.device = device();
data2.descriptorSet = descriptor_set.set_;
data2.binding = 1;
data2.buffer = buffer;
data2.bailout = &bailout;
UpdateDescriptor(&data2);
thread.join();
m_errorMonitor->SetBailout(NULL);
}
TEST_F(PositiveThreading, UpdateDescriptorUnusedWhilePendingNoCollision) {
TEST_DESCRIPTION("https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8975");
AddRequiredExtensions(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_MAINTENANCE_3_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::descriptorBindingUpdateUnusedWhilePending);
RETURN_IF_SKIP(Init());
InitRenderTarget();
OneOffDescriptorIndexingSet descriptor_set(m_device, {
{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT,
nullptr, VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT},
{1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT,
nullptr, VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT},
});
vkt::Buffer buffer(*m_device, 256, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
ThreadTestData data;
data.device = device();
data.descriptorSet = descriptor_set.set_;
data.binding = 0;
data.buffer = buffer;
std::atomic<bool> bailout{false};
data.bailout = &bailout;
m_errorMonitor->SetBailout(data.bailout);
// Update descriptors from another thread.
std::thread thread(UpdateDescriptor, &data);
// Update descriptors from this thread at the same time.
ThreadTestData data2;
data2.device = device();
data2.descriptorSet = descriptor_set.set_;
data2.binding = 1;
data2.buffer = buffer;
data2.bailout = &bailout;
UpdateDescriptor(&data2);
thread.join();
m_errorMonitor->SetBailout(NULL);
}
TEST_F(PositiveThreading, NullFenceCollision) {
RETURN_IF_SKIP(Init());
ThreadTestData data;
data.device = device();
std::atomic<bool> bailout{false};
data.bailout = &bailout;
m_errorMonitor->SetBailout(data.bailout);
// Call vk::DestroyFence of VK_NULL_HANDLE repeatedly using multiple threads.
// There should be no validation error from collision of that non-object.
std::thread thread(ReleaseNullFence, &data);
for (int i = 0; i < 40000; i++) {
vk::DestroyFence(device(), VK_NULL_HANDLE, NULL);
}
thread.join();
m_errorMonitor->SetBailout(NULL);
}
TEST_F(PositiveThreading, DebugObjectNames) {
AddRequiredExtensions(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
RETURN_IF_SKIP(InitFramework(&kDisableMessageLimit));
RETURN_IF_SKIP(InitState());
constexpr uint32_t count = 10000u;
VkDescriptorPoolSize pool_size;
pool_size.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
pool_size.descriptorCount = count;
VkDescriptorPoolCreateInfo descriptor_pool_ci = vku::InitStructHelper();
descriptor_pool_ci.maxSets = count;
descriptor_pool_ci.poolSizeCount = 1u;
descriptor_pool_ci.pPoolSizes = &pool_size;
vkt::DescriptorPool descriptor_pool(*m_device, descriptor_pool_ci);
VkDescriptorSetLayoutBinding binding;
binding.binding = 0u;
binding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
binding.descriptorCount = 1u;
binding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
binding.pImmutableSamplers = nullptr;
VkDescriptorSetLayoutCreateInfo set_layout_ci = vku::InitStructHelper();
set_layout_ci.bindingCount = 1u;
set_layout_ci.pBindings = &binding;
vkt::DescriptorSetLayout set_layout(*m_device, set_layout_ci);
binding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
vkt::DescriptorSetLayout set_layout2(*m_device, set_layout_ci);
vkt::PipelineLayout pipeline_layout(*m_device, {&set_layout2});
VkDescriptorSetAllocateInfo allocate_info = vku::InitStructHelper();
allocate_info.descriptorPool = descriptor_pool;
allocate_info.descriptorSetCount = 1u;
allocate_info.pSetLayouts = &set_layout.handle();
VkDescriptorSet descriptor_sets[count];
for (uint32_t i = 0; i < count; ++i) {
vk::AllocateDescriptorSets(*m_device, &allocate_info, &descriptor_sets[i]);
}
vkt::Buffer buffer(*m_device, 256u, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
VkDescriptorBufferInfo buffer_info;
buffer_info.buffer = buffer;
buffer_info.offset = 0u;
buffer_info.range = 256u;
VkWriteDescriptorSet descriptor_write = vku::InitStructHelper();
descriptor_write.dstBinding = 0u;
descriptor_write.dstArrayElement = 0u;
descriptor_write.descriptorCount = 1u;
descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptor_write.pImageInfo = nullptr;
descriptor_write.pBufferInfo = &buffer_info;
descriptor_write.pTexelBufferView = nullptr;
for (uint32_t i = 0; i < count; ++i) {
descriptor_write.dstSet = descriptor_sets[i];
vk::UpdateDescriptorSets(*m_device, 1u, &descriptor_write, 0u, nullptr);
}
VkDebugUtilsObjectNameInfoEXT name_info = vku::InitStructHelper();
name_info.objectType = VK_OBJECT_TYPE_DESCRIPTOR_SET;
std::atomic<bool> bailout{false};
for (uint32_t i = 0; i < count; ++i) {
m_errorMonitor->SetDesiredError("VUID-vkCmdBindDescriptorSets-pDescriptorSets-00358");
}
m_errorMonitor->SetBailout(&bailout);
const auto set_name = [&]() {
for (uint32_t i = 0; i < count; ++i) {
std::string name = "handle" + std::to_string(i);
name_info.objectHandle = (uint64_t)descriptor_sets[i];
name_info.pObjectName = name.c_str();
vk::SetDebugUtilsObjectNameEXT(*m_device, &name_info);
if (i % 3 == 0) {
name_info.pObjectName = nullptr;
vk::SetDebugUtilsObjectNameEXT(*m_device, &name_info);
}
}
};
const auto bind_descriptor = [&]() {
m_command_buffer.Begin();
for (uint32_t i = 0; i < count; ++i) {
vk::CmdBindDescriptorSets(m_command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_layout, 0u, 1u,
&descriptor_sets[i], 0u, nullptr);
}
m_command_buffer.End();
};
std::thread thread2(bind_descriptor);
std::thread thread1(set_name);
thread1.join();
thread2.join();
m_errorMonitor->SetBailout(NULL);
m_errorMonitor->VerifyFound();
}
#endif // GTEST_IS_THREADSAFE
TEST_F(PositiveThreading, Queue) {
#if defined(VVL_ENABLE_TSAN)
GTEST_SKIP() << "https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5965";
#endif
TEST_DESCRIPTION("Test concurrent Queue access from vkGet and vkSubmit");
using namespace std::chrono;
SetTargetApiVersion(VK_API_VERSION_1_1);
RETURN_IF_SKIP(Init());
const auto queue_family = m_device->QueuesWithGraphicsCapability()[0]->family_index;
constexpr uint32_t queue_index = 0;
vkt::CommandPool command_pool(*m_device, queue_family);
const VkDevice device_h = device();
VkQueue queue_h;
vk::GetDeviceQueue(device(), queue_family, queue_index, &queue_h);
vkt::Queue queue_o(queue_h, queue_family);
const VkCommandBufferAllocateInfo cbai = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, command_pool,
VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1};
vkt::CommandBuffer mock_cmdbuff(*m_device, cbai);
const VkCommandBufferBeginInfo cbbi{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr,
VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, nullptr};
mock_cmdbuff.Begin(&cbbi);
mock_cmdbuff.End();
std::mutex queue_mutex;
constexpr auto test_duration = seconds{2};
const auto timer_begin = steady_clock::now();
const auto &testing_thread1 = [&]() {
for (auto timer_now = steady_clock::now(); timer_now - timer_begin < test_duration; timer_now = steady_clock::now()) {
VkQueue dummy_q;
vk::GetDeviceQueue(device_h, queue_family, queue_index, &dummy_q);
}
};
const auto &testing_thread2 = [&]() {
for (auto timer_now = steady_clock::now(); timer_now - timer_begin < test_duration; timer_now = steady_clock::now()) {
VkSubmitInfo si = vku::InitStructHelper();
si.commandBufferCount = 1;
si.pCommandBuffers = &mock_cmdbuff.handle();
queue_mutex.lock();
ASSERT_EQ(VK_SUCCESS, vk::QueueSubmit(queue_h, 1, &si, VK_NULL_HANDLE));
queue_mutex.unlock();
}
};
const auto &testing_thread3 = [&]() {
for (auto timer_now = steady_clock::now(); timer_now - timer_begin < test_duration; timer_now = steady_clock::now()) {
queue_mutex.lock();
ASSERT_EQ(VK_SUCCESS, vk::QueueWaitIdle(queue_h));
queue_mutex.unlock();
}
};
std::array<std::thread, 3> threads = {std::thread(testing_thread1), std::thread(testing_thread2), std::thread(testing_thread3)};
for (auto &t : threads) t.join();
vk::QueueWaitIdle(queue_h);
}
TEST_F(PositiveThreading, GetPhysicalDeviceFeatures) {
TEST_DESCRIPTION("https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/9931");
VkInstanceCreateInfo instance_ci = GetInstanceCreateInfo();
// mimics --gtest_repeat=100 which is needed to reproduce constantly
for (uint32_t repeat = 0; repeat < 100; repeat++) {
VkInstance instance;
vk::CreateInstance(&instance_ci, nullptr, &instance);
const uint32_t thread_count = 4u;
uint32_t physical_device_count;
vk::EnumeratePhysicalDevices(instance, &physical_device_count, nullptr);
std::vector<VkPhysicalDevice> physical_devices(physical_device_count);
vk::EnumeratePhysicalDevices(instance, &physical_device_count, physical_devices.data());
const auto &thread = [&](uint32_t id) {
VkPhysicalDeviceFeatures features;
vk::GetPhysicalDeviceFeatures(physical_devices[id % physical_device_count], &features);
};
std::array<std::thread, thread_count> threads;
for (uint32_t i = 0u; i < thread_count; ++i) {
threads[i] = std::thread(thread, i);
}
for (auto &t : threads) {
t.join();
}
vk::DestroyInstance(instance, nullptr);
}
}
|