VkPriorityExtensionTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 2019 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkTypes.h"
  8. #if SK_SUPPORT_GPU && defined(SK_VULKAN)
  9. #include "include/gpu/vk/GrVkTypes.h"
  10. #include "src/core/SkAutoMalloc.h"
  11. #include "tests/Test.h"
  12. #include "tools/gpu/vk/VkTestUtils.h"
  13. #define ACQUIRE_VK_PROC_NOCHECK(name, instance, device) \
  14. PFN_vk##name grVk##name = reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device))
  15. #define ACQUIRE_VK_PROC(name, instance, device) \
  16. PFN_vk##name grVk##name = \
  17. reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device)); \
  18. do { \
  19. if (grVk##name == nullptr) { \
  20. if (device != VK_NULL_HANDLE) { \
  21. destroy_instance(getProc, inst); \
  22. } \
  23. return; \
  24. } \
  25. } while (0)
  26. #define ACQUIRE_VK_PROC_LOCAL(name, instance, device) \
  27. PFN_vk##name grVk##name = \
  28. reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device)); \
  29. do { \
  30. if (grVk##name == nullptr) { \
  31. return; \
  32. } \
  33. } while (0)
  34. #define GET_PROC_LOCAL(F, inst, device) PFN_vk ## F F = (PFN_vk ## F) getProc("vk" #F, inst, device)
  35. static void destroy_instance(GrVkGetProc getProc, VkInstance inst) {
  36. ACQUIRE_VK_PROC_LOCAL(DestroyInstance, inst, VK_NULL_HANDLE);
  37. grVkDestroyInstance(inst, nullptr);
  38. }
  39. // If the extension VK_EXT_GLOBAL_PRIORITY is supported, this test just tries to create a VkDevice
  40. // using the various global priorities. The test passes if no errors are reported or the test
  41. // doesn't crash.
  42. DEF_GPUTEST(VulkanPriorityExtension, reporter, options) {
  43. PFN_vkGetInstanceProcAddr instProc;
  44. PFN_vkGetDeviceProcAddr devProc;
  45. if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
  46. return;
  47. }
  48. auto getProc = [instProc, devProc](const char* proc_name,
  49. VkInstance instance, VkDevice device) {
  50. if (device != VK_NULL_HANDLE) {
  51. return devProc(device, proc_name);
  52. }
  53. return instProc(instance, proc_name);
  54. };
  55. VkResult err;
  56. ACQUIRE_VK_PROC_NOCHECK(EnumerateInstanceVersion, VK_NULL_HANDLE, VK_NULL_HANDLE);
  57. uint32_t instanceVersion = 0;
  58. if (!grVkEnumerateInstanceVersion) {
  59. instanceVersion = VK_MAKE_VERSION(1, 0, 0);
  60. } else {
  61. err = grVkEnumerateInstanceVersion(&instanceVersion);
  62. if (err) {
  63. ERRORF(reporter, "failed ot enumerate instance version. Err: %d", err);
  64. return;
  65. }
  66. }
  67. SkASSERT(instanceVersion >= VK_MAKE_VERSION(1, 0, 0));
  68. uint32_t apiVersion = VK_MAKE_VERSION(1, 0, 0);
  69. if (instanceVersion >= VK_MAKE_VERSION(1, 1, 0)) {
  70. // If the instance version is 1.0 we must have the apiVersion also be 1.0. However, if the
  71. // instance version is 1.1 or higher, we can set the apiVersion to be whatever the highest
  72. // api we may use in skia (technically it can be arbitrary). So for now we set it to 1.1
  73. // since that is the highest vulkan version.
  74. apiVersion = VK_MAKE_VERSION(1, 1, 0);
  75. }
  76. instanceVersion = SkTMin(instanceVersion, apiVersion);
  77. VkPhysicalDevice physDev;
  78. VkDevice device;
  79. VkInstance inst;
  80. const VkApplicationInfo app_info = {
  81. VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
  82. nullptr, // pNext
  83. "vktest", // pApplicationName
  84. 0, // applicationVersion
  85. "vktest", // pEngineName
  86. 0, // engineVersion
  87. apiVersion, // apiVersion
  88. };
  89. const VkInstanceCreateInfo instance_create = {
  90. VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
  91. nullptr, // pNext
  92. 0, // flags
  93. &app_info, // pApplicationInfo
  94. 0, // enabledLayerNameCount
  95. nullptr, // ppEnabledLayerNames
  96. 0, // enabledExtensionNameCount
  97. nullptr, // ppEnabledExtensionNames
  98. };
  99. ACQUIRE_VK_PROC(CreateInstance, VK_NULL_HANDLE, VK_NULL_HANDLE);
  100. err = grVkCreateInstance(&instance_create, nullptr, &inst);
  101. if (err < 0) {
  102. ERRORF(reporter, "Failed to create VkInstance");
  103. return;
  104. }
  105. ACQUIRE_VK_PROC(EnumeratePhysicalDevices, inst, VK_NULL_HANDLE);
  106. ACQUIRE_VK_PROC(GetPhysicalDeviceProperties, inst, VK_NULL_HANDLE);
  107. ACQUIRE_VK_PROC(GetPhysicalDeviceQueueFamilyProperties, inst, VK_NULL_HANDLE);
  108. ACQUIRE_VK_PROC(GetPhysicalDeviceFeatures, inst, VK_NULL_HANDLE);
  109. ACQUIRE_VK_PROC(CreateDevice, inst, VK_NULL_HANDLE);
  110. ACQUIRE_VK_PROC(GetDeviceQueue, inst, VK_NULL_HANDLE);
  111. ACQUIRE_VK_PROC(DeviceWaitIdle, inst, VK_NULL_HANDLE);
  112. ACQUIRE_VK_PROC(DestroyDevice, inst, VK_NULL_HANDLE);
  113. uint32_t gpuCount;
  114. err = grVkEnumeratePhysicalDevices(inst, &gpuCount, nullptr);
  115. if (err) {
  116. ERRORF(reporter, "vkEnumeratePhysicalDevices failed: %d", err);
  117. destroy_instance(getProc, inst);
  118. return;
  119. }
  120. if (!gpuCount) {
  121. ERRORF(reporter, "vkEnumeratePhysicalDevices returned no supported devices.");
  122. destroy_instance(getProc, inst);
  123. return;
  124. }
  125. // Just returning the first physical device instead of getting the whole array.
  126. // TODO: find best match for our needs
  127. gpuCount = 1;
  128. err = grVkEnumeratePhysicalDevices(inst, &gpuCount, &physDev);
  129. // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
  130. if (err && VK_INCOMPLETE != err) {
  131. ERRORF(reporter, "vkEnumeratePhysicalDevices failed: %d", err);
  132. destroy_instance(getProc, inst);
  133. return;
  134. }
  135. // query to get the initial queue props size
  136. uint32_t queueCount;
  137. grVkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, nullptr);
  138. if (!queueCount) {
  139. ERRORF(reporter, "vkGetPhysicalDeviceQueueFamilyProperties returned no queues.");
  140. destroy_instance(getProc, inst);
  141. return;
  142. }
  143. SkAutoMalloc queuePropsAlloc(queueCount * sizeof(VkQueueFamilyProperties));
  144. // now get the actual queue props
  145. VkQueueFamilyProperties* queueProps = (VkQueueFamilyProperties*)queuePropsAlloc.get();
  146. grVkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, queueProps);
  147. // iterate to find the graphics queue
  148. uint32_t graphicsQueueIndex = queueCount;
  149. for (uint32_t i = 0; i < queueCount; i++) {
  150. if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  151. graphicsQueueIndex = i;
  152. break;
  153. }
  154. }
  155. if (graphicsQueueIndex == queueCount) {
  156. ERRORF(reporter, "Could not find any supported graphics queues.");
  157. destroy_instance(getProc, inst);
  158. return;
  159. }
  160. GET_PROC_LOCAL(EnumerateDeviceExtensionProperties, inst, VK_NULL_HANDLE);
  161. GET_PROC_LOCAL(EnumerateDeviceLayerProperties, inst, VK_NULL_HANDLE);
  162. if (!EnumerateDeviceExtensionProperties ||
  163. !EnumerateDeviceLayerProperties) {
  164. destroy_instance(getProc, inst);
  165. return;
  166. }
  167. // device extensions
  168. // via Vulkan implementation and implicitly enabled layers
  169. uint32_t extensionCount = 0;
  170. err = EnumerateDeviceExtensionProperties(physDev, nullptr, &extensionCount, nullptr);
  171. if (VK_SUCCESS != err) {
  172. ERRORF(reporter, "Could not enumerate device extension properties.");
  173. destroy_instance(getProc, inst);
  174. return;
  175. }
  176. VkExtensionProperties* extensions = new VkExtensionProperties[extensionCount];
  177. err = EnumerateDeviceExtensionProperties(physDev, nullptr, &extensionCount, extensions);
  178. if (VK_SUCCESS != err) {
  179. delete[] extensions;
  180. ERRORF(reporter, "Could not enumerate device extension properties.");
  181. destroy_instance(getProc, inst);
  182. return;
  183. }
  184. bool hasPriorityExt = false;
  185. for (uint32_t i = 0; i < extensionCount; ++i) {
  186. if (!strcmp(extensions[i].extensionName, VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME)) {
  187. hasPriorityExt = true;
  188. }
  189. }
  190. delete[] extensions;
  191. if (!hasPriorityExt) {
  192. destroy_instance(getProc, inst);
  193. return;
  194. }
  195. const char* priorityExt = VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME;
  196. VkPhysicalDeviceFeatures deviceFeatures;
  197. grVkGetPhysicalDeviceFeatures(physDev, &deviceFeatures);
  198. // this looks like it would slow things down,
  199. // and we can't depend on it on all platforms
  200. deviceFeatures.robustBufferAccess = VK_FALSE;
  201. float queuePriorities[1] = { 0.0 };
  202. VkDeviceQueueGlobalPriorityCreateInfoEXT queuePriorityCreateInfo;
  203. queuePriorityCreateInfo.sType =
  204. VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT;
  205. queuePriorityCreateInfo.pNext = nullptr;
  206. VkDeviceQueueCreateInfo queueInfo = {
  207. VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
  208. &queuePriorityCreateInfo, // pNext
  209. 0, // VkDeviceQueueCreateFlags
  210. graphicsQueueIndex, // queueFamilyIndex
  211. 1, // queueCount
  212. queuePriorities, // pQueuePriorities
  213. };
  214. for (VkQueueGlobalPriorityEXT globalPriority : { VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT,
  215. VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT,
  216. VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT,
  217. VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT }) {
  218. queuePriorityCreateInfo.globalPriority = globalPriority;
  219. const VkDeviceCreateInfo deviceInfo = {
  220. VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
  221. nullptr, // pNext
  222. 0, // VkDeviceCreateFlags
  223. 1, // queueCreateInfoCount
  224. &queueInfo, // pQueueCreateInfos
  225. 0, // layerCount
  226. nullptr, // ppEnabledLayerNames
  227. 1, // extensionCount
  228. &priorityExt, // ppEnabledExtensionNames
  229. &deviceFeatures // ppEnabledFeatures
  230. };
  231. err = grVkCreateDevice(physDev, &deviceInfo, nullptr, &device);
  232. if (err != VK_SUCCESS && err != VK_ERROR_NOT_PERMITTED_EXT) {
  233. ERRORF(reporter, "CreateDevice failed: %d, priority %d", err, globalPriority);
  234. destroy_instance(getProc, inst);
  235. continue;
  236. }
  237. if (err != VK_ERROR_NOT_PERMITTED_EXT) {
  238. grVkDestroyDevice(device, nullptr);
  239. }
  240. }
  241. destroy_instance(getProc, inst);
  242. }
  243. #endif