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