GrVkExtensions.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2016 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/gpu/vk/GrVkExtensions.h"
  8. // Can remove this once we get rid of the extension flags.
  9. #include "include/gpu/vk/GrVkBackendContext.h"
  10. #include "src/core/SkTSearch.h"
  11. #include "src/core/SkTSort.h"
  12. // finds the index of ext in infos or a negative result if ext is not found.
  13. static int find_info(const SkTArray<GrVkExtensions::Info>& infos, const char ext[]) {
  14. if (infos.empty()) {
  15. return -1;
  16. }
  17. SkString extensionStr(ext);
  18. GrVkExtensions::Info::Less less;
  19. int idx = SkTSearch<GrVkExtensions::Info, SkString, GrVkExtensions::Info::Less>(
  20. &infos.front(), infos.count(), extensionStr, sizeof(GrVkExtensions::Info),
  21. less);
  22. return idx;
  23. }
  24. namespace { // This cannot be static because it is used as a template parameter.
  25. inline bool extension_compare(const GrVkExtensions::Info& a, const GrVkExtensions::Info& b) {
  26. return strcmp(a.fName.c_str(), b.fName.c_str()) < 0;
  27. }
  28. }
  29. void GrVkExtensions::init(GrVkGetProc getProc,
  30. VkInstance instance,
  31. VkPhysicalDevice physDev,
  32. uint32_t instanceExtensionCount,
  33. const char* const* instanceExtensions,
  34. uint32_t deviceExtensionCount,
  35. const char* const* deviceExtensions) {
  36. SkTLessFunctionToFunctorAdaptor<GrVkExtensions::Info, extension_compare> cmp;
  37. for (uint32_t i = 0; i < instanceExtensionCount; ++i) {
  38. const char* extension = instanceExtensions[i];
  39. // if not already in the list, add it
  40. if (find_info(fExtensions, extension) < 0) {
  41. fExtensions.push_back() = Info(extension);
  42. SkTQSort(&fExtensions.front(), &fExtensions.back(), cmp);
  43. }
  44. }
  45. for (uint32_t i = 0; i < deviceExtensionCount; ++i) {
  46. const char* extension = deviceExtensions[i];
  47. // if not already in the list, add it
  48. if (find_info(fExtensions, extension) < 0) {
  49. fExtensions.push_back() = Info(extension);
  50. SkTQSort(&fExtensions.front(), &fExtensions.back(), cmp);
  51. }
  52. }
  53. this->getSpecVersions(getProc, instance, physDev);
  54. }
  55. #define GET_PROC(F, inst) \
  56. PFN_vk##F grVk##F = (PFN_vk ## F) getProc("vk" #F, inst, VK_NULL_HANDLE)
  57. void GrVkExtensions::getSpecVersions(GrVkGetProc getProc, VkInstance instance,
  58. VkPhysicalDevice physDevice) {
  59. // We grab all the extensions for the VkInstance and VkDevice so we can look up what spec
  60. // version each of the supported extensions are. We do not grab the extensions for layers
  61. // because we don't know what layers the client has enabled and in general we don't do anything
  62. // special for those extensions.
  63. if (instance == VK_NULL_HANDLE) {
  64. return;
  65. }
  66. GET_PROC(EnumerateInstanceExtensionProperties, VK_NULL_HANDLE);
  67. SkASSERT(grVkEnumerateInstanceExtensionProperties);
  68. VkResult res;
  69. // instance extensions
  70. uint32_t extensionCount = 0;
  71. res = grVkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
  72. if (VK_SUCCESS != res) {
  73. return;
  74. }
  75. VkExtensionProperties* extensions = new VkExtensionProperties[extensionCount];
  76. res = grVkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions);
  77. if (VK_SUCCESS != res) {
  78. delete[] extensions;
  79. return;
  80. }
  81. for (uint32_t i = 0; i < extensionCount; ++i) {
  82. int idx = find_info(fExtensions, extensions[i].extensionName);
  83. if (idx >= 0) {
  84. fExtensions[idx].fSpecVersion = extensions[i].specVersion;
  85. }
  86. }
  87. delete[] extensions;
  88. if (physDevice == VK_NULL_HANDLE) {
  89. return;
  90. }
  91. GET_PROC(EnumerateDeviceExtensionProperties, instance);
  92. SkASSERT(grVkEnumerateDeviceExtensionProperties);
  93. // device extensions
  94. extensionCount = 0;
  95. res = grVkEnumerateDeviceExtensionProperties(physDevice, nullptr, &extensionCount, nullptr);
  96. if (VK_SUCCESS != res) {
  97. return;
  98. }
  99. extensions = new VkExtensionProperties[extensionCount];
  100. res = grVkEnumerateDeviceExtensionProperties(physDevice, nullptr, &extensionCount, extensions);
  101. if (VK_SUCCESS != res) {
  102. delete[] extensions;
  103. return;
  104. }
  105. for (uint32_t i = 0; i < extensionCount; ++i) {
  106. int idx = find_info(fExtensions, extensions[i].extensionName);
  107. if (idx >= 0) {
  108. fExtensions[idx].fSpecVersion = extensions[i].specVersion;
  109. }
  110. }
  111. delete[] extensions;
  112. }
  113. bool GrVkExtensions::hasExtension(const char ext[], uint32_t minVersion) const {
  114. int idx = find_info(fExtensions, ext);
  115. return idx >= 0 && fExtensions[idx].fSpecVersion >= minVersion;
  116. }