blob: 429455faf60d2eb0a7954947dcaf676c96b49285 [file] [log] [blame]
Jesse Halld02edcb2015-09-08 07:44:48 -07001{{/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */}}
16
Jesse Hall04f4f472015-08-16 19:51:04 -070017{{Include "../api/templates/vulkan_common.tmpl"}}
18{{Global "clang-format" (Strings "clang-format" "-style=file")}}
19{{Macro "DefineGlobals" $}}
20{{$ | Macro "get_proc_addr.cpp" | Format (Global "clang-format") | Write "get_proc_addr.cpp"}}
21
22
23{{/*
24-------------------------------------------------------------------------------
25 Entry point
26-------------------------------------------------------------------------------
27*/}}
28{{define "get_proc_addr.cpp"}}
29/*
30 * Copyright 2015 The Android Open Source Project
31 *
32 * Licensed under the Apache License, Version 2.0 (the "License");
33 * you may not use this file except in compliance with the License.
34 * You may obtain a copy of the License at
35 *
36 * http://www.apache.org/licenses/LICENSE-2.0
37 *
38 * Unless required by applicable law or agreed to in writing, software
39 * distributed under the License is distributed on an "AS IS" BASIS,
40 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41 * See the License for the specific language governing permissions and
42 * limitations under the License.
43 */
44
45// This file is generated. Do not edit manually!
46// To regenerate: $ apic template ../api/vulkan.api get_proc_addr.cpp.tmpl
47// Requires apic from https://android.googlesource.com/platform/tools/gpu/.
48
49#include <algorithm>
50#include <log/log.h>
51#include "loader.h"
52using namespace vulkan;
53
54#define UNLIKELY(expr) __builtin_expect((expr), 0)
55
56namespace {
57
58struct NameProcEntry {
59 const char* name;
60 PFN_vkVoidFunction proc;
61};
62
63struct NameOffsetEntry {
64 const char* name;
65 size_t offset;
66};
67
68template <typename TEntry, size_t N>
69const TEntry* FindProcEntry(const TEntry(&table)[N], const char* name) {
70 auto entry = std::lower_bound(
71 table, table + N, name,
72 [](const TEntry& e, const char* n) { return strcmp(e.name, n) < 0; });
73 if (entry != (table + N) && strcmp(entry->name, name) == 0)
74 return entry;
75 return nullptr;
76}
77
78const NameProcEntry kInstanceProcTbl[] =
79 // clang-format off
80 {{range $f := SortBy (AllCommands $) "FunctionName"}}
81 {{if eq (Macro "Vtbl" $f) "Instance"}}
82 {"{{Macro "FunctionName" $f}}", reinterpret_cast<PFN_vkVoidFunction>({{Macro "FunctionName" $f}})},
83 {{end}}
84 {{end}}
85 // clang-format on
86»};
87
88const NameProcEntry kDeviceProcTbl[] =
89 // clang-format off
90 {{range $f := SortBy (AllCommands $) "FunctionName"}}
91 {{if eq (Macro "Vtbl" $f) "Device"}}
92 {"{{Macro "FunctionName" $f}}", reinterpret_cast<PFN_vkVoidFunction>({{Macro "FunctionName" $f}})},
93 {{end}}
94 {{end}}
95 // clang-format on
96»};
97
98const NameOffsetEntry kInstanceOffsetTbl[] =
99 // clang-format off
100 {{range $f := SortBy (AllCommands $) "FunctionName"}}
101 {{if eq (Macro "Vtbl" $f) "Instance"}}
102 {"{{Macro "FunctionName" $f}}", offsetof(InstanceVtbl, {{TrimPrefix "vk" (Macro "FunctionName" $f)}})},
103 {{end}}
104 {{end}}
105 // clang-format on
106»};
107
108const NameOffsetEntry kDeviceOffsetTbl[] =
109 // clang-format off
110 {{range $f := SortBy (AllCommands $) "FunctionName"}}
111 {{if eq (Macro "Vtbl" $f) "Device"}}
112 {"{{Macro "FunctionName" $f}}", offsetof(DeviceVtbl, {{TrimPrefix "vk" (Macro "FunctionName" $f)}})},
113 {{end}}
114 {{end}}
115 // clang-format on
116»};
117
118} // namespace
119
120namespace vulkan {
121
122PFN_vkVoidFunction GetGlobalInstanceProcAddr(const char* name) {
Jesse Hallb1352bc2015-09-04 16:12:33 -0700123 const NameProcEntry* entry = FindProcEntry(kInstanceProcTbl, name);
124 if (entry)
125 return entry->proc;
126 // vkGetDeviceProcAddr must be available at the global/instance level for bootstrapping
Jesse Hall04f4f472015-08-16 19:51:04 -0700127 if (strcmp(name, "vkGetDeviceProcAddr") == 0)
128 return reinterpret_cast<PFN_vkVoidFunction>(vkGetDeviceProcAddr);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700129 // special-case extension functions until they can be auto-generated
130 if (strcmp(name, "vkGetPhysicalDeviceSurfaceSupportKHR") == 0)
131 return reinterpret_cast<PFN_vkVoidFunction>(vkGetPhysicalDeviceSurfaceSupportKHR);
132 return nullptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700133}
134
135PFN_vkVoidFunction GetGlobalDeviceProcAddr(const char* name) {
136 const NameProcEntry* entry = FindProcEntry(kDeviceProcTbl, name);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700137 if (entry)
138 return entry->proc;
139 // special-case extension functions until they can be auto-generated
140 if (strcmp(name, "vkGetSurfacePropertiesKHR") == 0)
141 return reinterpret_cast<PFN_vkVoidFunction>(vkGetSurfacePropertiesKHR);
142 if (strcmp(name, "vkGetSurfaceFormatsKHR") == 0)
143 return reinterpret_cast<PFN_vkVoidFunction>(vkGetSurfaceFormatsKHR);
144 if (strcmp(name, "vkGetSurfacePresentModesKHR") == 0)
145 return reinterpret_cast<PFN_vkVoidFunction>(vkGetSurfacePresentModesKHR);
146 if (strcmp(name, "vkCreateSwapchainKHR") == 0)
147 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateSwapchainKHR);
148 if (strcmp(name, "vkDestroySwapchainKHR") == 0)
149 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroySwapchainKHR);
150 if (strcmp(name, "vkGetSwapchainImagesKHR") == 0)
151 return reinterpret_cast<PFN_vkVoidFunction>(vkGetSwapchainImagesKHR);
152 if (strcmp(name, "vkAcquireNextImageKHR") == 0)
153 return reinterpret_cast<PFN_vkVoidFunction>(vkAcquireNextImageKHR);
154 if (strcmp(name, "vkQueuePresentKHR") == 0)
155 return reinterpret_cast<PFN_vkVoidFunction>(vkQueuePresentKHR);
156 return nullptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700157}
158
159PFN_vkVoidFunction GetSpecificInstanceProcAddr(const InstanceVtbl* vtbl,
160 const char* name) {
Jesse Hallb1352bc2015-09-04 16:12:33 -0700161 size_t offset;
Jesse Hall04f4f472015-08-16 19:51:04 -0700162 const NameOffsetEntry* entry = FindProcEntry(kInstanceOffsetTbl, name);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700163 if (entry)
164 offset = entry->offset;
165 else if (strcmp(name, "vkGetPhysicalDeviceSurfaceSupportKHR") == 0)
166 offset = offsetof(InstanceVtbl, GetPhysicalDeviceSurfaceSupportKHR);
167 else
Jesse Hall04f4f472015-08-16 19:51:04 -0700168 return nullptr;
169 const unsigned char* base = reinterpret_cast<const unsigned char*>(vtbl);
Michael Lentine62270162015-09-15 13:25:36 -0500170 return *reinterpret_cast<PFN_vkVoidFunction*>(
Jesse Hall6614a572015-10-23 11:33:39 -0500171 const_cast<unsigned char*>(base) + offset);
Jesse Hall04f4f472015-08-16 19:51:04 -0700172}
173
174PFN_vkVoidFunction GetSpecificDeviceProcAddr(const DeviceVtbl* vtbl,
175 const char* name) {
Jesse Hallb1352bc2015-09-04 16:12:33 -0700176 size_t offset;
Jesse Hall04f4f472015-08-16 19:51:04 -0700177 const NameOffsetEntry* entry = FindProcEntry(kDeviceOffsetTbl, name);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700178 if (entry)
179 offset = entry->offset;
180 else if (strcmp(name, "vkGetSurfacePropertiesKHR") == 0)
181 offset = offsetof(DeviceVtbl, GetSurfacePropertiesKHR);
182 else if (strcmp(name, "vkGetSurfaceFormatsKHR") == 0)
183 offset = offsetof(DeviceVtbl, GetSurfaceFormatsKHR);
184 else if (strcmp(name, "vkGetSurfacePresentModesKHR") == 0)
185 offset = offsetof(DeviceVtbl, GetSurfacePresentModesKHR);
186 else if (strcmp(name, "vkCreateSwapchainKHR") == 0)
187 offset = offsetof(DeviceVtbl, CreateSwapchainKHR);
188 else if (strcmp(name, "vkDestroySwapchainKHR") == 0)
189 offset = offsetof(DeviceVtbl, DestroySwapchainKHR);
190 else if (strcmp(name, "vkGetSwapchainImagesKHR") == 0)
191 offset = offsetof(DeviceVtbl, GetSwapchainImagesKHR);
192 else if (strcmp(name, "vkAcquireNextImageKHR") == 0)
193 offset = offsetof(DeviceVtbl, AcquireNextImageKHR);
194 else if (strcmp(name, "vkQueuePresentKHR") == 0)
195 offset = offsetof(DeviceVtbl, QueuePresentKHR);
196 else
Jesse Hall04f4f472015-08-16 19:51:04 -0700197 return nullptr;
198 const unsigned char* base = reinterpret_cast<const unsigned char*>(vtbl);
Michael Lentine62270162015-09-15 13:25:36 -0500199 return *reinterpret_cast<PFN_vkVoidFunction*>(
Jesse Hall6614a572015-10-23 11:33:39 -0500200 const_cast<unsigned char*>(base) + offset);
Jesse Hall04f4f472015-08-16 19:51:04 -0700201}
202
203bool LoadInstanceVtbl(VkInstance instance,
Michael Lentine03c64b02015-08-26 18:27:26 -0500204 VkInstance instance_next,
Jesse Hall04f4f472015-08-16 19:51:04 -0700205 PFN_vkGetInstanceProcAddr get_proc_addr,
206 InstanceVtbl& vtbl)
207 bool success = true;
208 // clang-format off
Michael Lentine03c64b02015-08-26 18:27:26 -0500209 vtbl.GetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(get_proc_addr(instance_next, "vkGetInstanceProcAddr"));
210 if (UNLIKELY(!vtbl.GetInstanceProcAddr)) {
211 ALOGE("missing instance proc: %s", "vkGetInstanceProcAddr");
212 success = false;
213 }
214 vtbl.CreateInstance = reinterpret_cast<PFN_vkCreateInstance>(get_proc_addr(instance, "vkCreateInstance"));
215 if (UNLIKELY(!vtbl.CreateInstance)) {
216 // This is allowed to fail as the driver doesn't have to return vkCreateInstance when given an instance
217 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700218 {{range $f := AllCommands $}}
219 {{if eq (Macro "Vtbl" $f) "Instance"}}
Michael Lentine03c64b02015-08-26 18:27:26 -0500220 {{if not (eq (Macro "FunctionName" $f) "vkGetInstanceProcAddr")}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700221 vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}} = §
222 reinterpret_cast<{{Macro "FunctionPtrName" $f}}>(§
223 get_proc_addr(instance, "{{Macro "FunctionName" $f}}"));
224 if (UNLIKELY(!vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}})) {
225 ALOGE("missing instance proc: %s", "{{Macro "FunctionName" $f}}");
226 success = false;
227 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500228 {{end}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700229 {{end}}
230 {{end}}
231 // clang-format on
232 return success;
233»}
234
235bool LoadDeviceVtbl(VkDevice device,
Michael Lentine03c64b02015-08-26 18:27:26 -0500236 VkDevice device_next,
Jesse Hall04f4f472015-08-16 19:51:04 -0700237 PFN_vkGetDeviceProcAddr get_proc_addr,
238 DeviceVtbl& vtbl)
239 bool success = true;
240 // clang-format off
Michael Lentine03c64b02015-08-26 18:27:26 -0500241 vtbl.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(get_proc_addr(device_next, "vkGetDeviceProcAddr"));
242 if (UNLIKELY(!vtbl.GetDeviceProcAddr)) {
243 ALOGE("missing device proc: %s", "vkGetDeviceProcAddr");
244 success = false;
245 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700246 {{range $f := AllCommands $}}
247 {{if eq (Macro "Vtbl" $f) "Device"}}
Michael Lentine03c64b02015-08-26 18:27:26 -0500248 {{if not (eq (Macro "FunctionName" $f) "vkGetDeviceProcAddr")}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700249 vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}} = §
250 reinterpret_cast<{{Macro "FunctionPtrName" $f}}>(§
251 get_proc_addr(device, "{{Macro "FunctionName" $f}}"));
252 if (UNLIKELY(!vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}})) {
253 ALOGE("missing device proc: %s", "{{Macro "FunctionName" $f}}");
254 success = false;
255 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500256 {{end}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700257 {{end}}
258 {{end}}
Jesse Hall70f93352015-11-04 09:41:31 -0800259 vtbl.GetSwapchainGrallocUsageANDROID = reinterpret_cast<PFN_vkGetSwapchainGrallocUsageANDROID>(get_proc_addr(device, "vkGetSwapchainGrallocUsageANDROID"));
260 if (UNLIKELY(!vtbl.GetSwapchainGrallocUsageANDROID)) {
261 // TODO(jessehall): temporarily make this optional, until drivers have been updated
262 // ALOGE("missing device proc: %s", "vkGetSwapchainGrallocUsageANDROID");
263 ALOGW("missing device proc: %s", "vkGetSwapchainGrallocUsageANDROID");
264 // success = false;
265 }
Jesse Hallab9aeef2015-11-04 10:56:20 -0800266 vtbl.AcquireImageANDROID = reinterpret_cast<PFN_vkAcquireImageANDROID>(get_proc_addr(device, "vkAcquireImageANDROID"));
267 if (UNLIKELY(!vtbl.AcquireImageANDROID)) {
268 // TODO(jessehall): temporarily make this optional, until drivers have been updated
269 // ALOGE("missing device proc: %s", "vkImportNativeFenceANDROID");
270 ALOGW("missing device proc: %s", "vkAcquireImageANDROID");
271 // success = false;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700272 }
Jesse Hallab9aeef2015-11-04 10:56:20 -0800273 vtbl.QueueSignalReleaseImageANDROID = reinterpret_cast<PFN_vkQueueSignalReleaseImageANDROID>(get_proc_addr(device, "vkQueueSignalReleaseImageANDROID"));
274 if (UNLIKELY(!vtbl.QueueSignalReleaseImageANDROID)) {
275 // TODO(jessehall): temporarily make this optional, until drivers have been updated
276 // ALOGE("missing device proc: %s", "vkQueueSignalReleaseImageANDROID");
277 ALOGW("missing device proc: %s", "vkQueueSignalReleaseImageANDROID");
278 // success = false;
279 }
280 // TODO(jessehall): these are deprecated; remove when drivers have been updated
281 vtbl.ImportNativeFenceANDROID = reinterpret_cast<PFN_vkImportNativeFenceANDROID>(get_proc_addr(device, "vkImportNativeFenceANDROID"));
Jesse Hallb1352bc2015-09-04 16:12:33 -0700282 vtbl.QueueSignalNativeFenceANDROID = reinterpret_cast<PFN_vkQueueSignalNativeFenceANDROID>(get_proc_addr(device, "vkQueueSignalNativeFenceANDROID"));
Jesse Hallab9aeef2015-11-04 10:56:20 -0800283 if (!((!vtbl.AcquireImageANDROID && !vtbl.QueueSignalReleaseImageANDROID && vtbl.ImportNativeFenceANDROID && vtbl.QueueSignalNativeFenceANDROID) ||
284 (vtbl.AcquireImageANDROID && vtbl.QueueSignalReleaseImageANDROID && !vtbl.ImportNativeFenceANDROID && !vtbl.QueueSignalNativeFenceANDROID))) {
285 ALOGE("driver doesn't support exactly one of old- or new-style VK_EXT_ANDROID_native_buffer commands");
Jesse Hallb1352bc2015-09-04 16:12:33 -0700286 success = false;
287 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700288 // clang-format on
289 return success;
290»}
291
292} // namespace vulkan
293
294{{end}}