blob: 423dea4d73f99704d55d7468f0fee919772d83c5 [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>
Michael Lentine88594d72015-11-12 12:49:45 -080069const TEntry* FindProcEntry(const TEntry (&table)[N], const char* name) {
Jesse Hall04f4f472015-08-16 19:51:04 -070070 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"}}
Jesse Hall1356b0d2015-11-23 17:24:58 -080081 {{if and (Macro "IsDispatched" $f) (eq (Macro "Vtbl" $f) "Instance")}}
Jesse Hall04f4f472015-08-16 19:51:04 -070082 {"{{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"}}
Jesse Hall1356b0d2015-11-23 17:24:58 -080091 {{if and (Macro "IsDispatched" $f) (eq (Macro "Vtbl" $f) "Device")}}
Jesse Hall04f4f472015-08-16 19:51:04 -070092 {"{{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"}}
Jesse Hall1356b0d2015-11-23 17:24:58 -0800101 {{if and (Macro "IsDispatched" $f) (eq (Macro "Vtbl" $f) "Instance")}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700102 {"{{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"}}
Jesse Hall1356b0d2015-11-23 17:24:58 -0800111 {{if and (Macro "IsDispatched" $f) (eq (Macro "Vtbl" $f) "Device")}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700112 {"{{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 return nullptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700130}
131
132PFN_vkVoidFunction GetGlobalDeviceProcAddr(const char* name) {
133 const NameProcEntry* entry = FindProcEntry(kDeviceProcTbl, name);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700134 if (entry)
135 return entry->proc;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700136 return nullptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700137}
138
139PFN_vkVoidFunction GetSpecificInstanceProcAddr(const InstanceVtbl* vtbl,
140 const char* name) {
Jesse Hallb1352bc2015-09-04 16:12:33 -0700141 size_t offset;
Jesse Hall04f4f472015-08-16 19:51:04 -0700142 const NameOffsetEntry* entry = FindProcEntry(kInstanceOffsetTbl, name);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700143 if (entry)
144 offset = entry->offset;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700145 else
Jesse Hall04f4f472015-08-16 19:51:04 -0700146 return nullptr;
147 const unsigned char* base = reinterpret_cast<const unsigned char*>(vtbl);
Michael Lentine62270162015-09-15 13:25:36 -0500148 return *reinterpret_cast<PFN_vkVoidFunction*>(
Jesse Hall6614a572015-10-23 11:33:39 -0500149 const_cast<unsigned char*>(base) + offset);
Jesse Hall04f4f472015-08-16 19:51:04 -0700150}
151
152PFN_vkVoidFunction GetSpecificDeviceProcAddr(const DeviceVtbl* vtbl,
153 const char* name) {
Jesse Hallb1352bc2015-09-04 16:12:33 -0700154 size_t offset;
Jesse Hall04f4f472015-08-16 19:51:04 -0700155 const NameOffsetEntry* entry = FindProcEntry(kDeviceOffsetTbl, name);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700156 if (entry)
157 offset = entry->offset;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700158 else
Jesse Hall04f4f472015-08-16 19:51:04 -0700159 return nullptr;
160 const unsigned char* base = reinterpret_cast<const unsigned char*>(vtbl);
Michael Lentine62270162015-09-15 13:25:36 -0500161 return *reinterpret_cast<PFN_vkVoidFunction*>(
Jesse Hall6614a572015-10-23 11:33:39 -0500162 const_cast<unsigned char*>(base) + offset);
Jesse Hall04f4f472015-08-16 19:51:04 -0700163}
164
165bool LoadInstanceVtbl(VkInstance instance,
Michael Lentine03c64b02015-08-26 18:27:26 -0500166 VkInstance instance_next,
Jesse Hall04f4f472015-08-16 19:51:04 -0700167 PFN_vkGetInstanceProcAddr get_proc_addr,
168 InstanceVtbl& vtbl)
169 bool success = true;
170 // clang-format off
Michael Lentine03c64b02015-08-26 18:27:26 -0500171 vtbl.GetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(get_proc_addr(instance_next, "vkGetInstanceProcAddr"));
172 if (UNLIKELY(!vtbl.GetInstanceProcAddr)) {
173 ALOGE("missing instance proc: %s", "vkGetInstanceProcAddr");
174 success = false;
175 }
176 vtbl.CreateInstance = reinterpret_cast<PFN_vkCreateInstance>(get_proc_addr(instance, "vkCreateInstance"));
177 if (UNLIKELY(!vtbl.CreateInstance)) {
178 // This is allowed to fail as the driver doesn't have to return vkCreateInstance when given an instance
179 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700180 {{range $f := AllCommands $}}
181 {{if eq (Macro "Vtbl" $f) "Instance"}}
Michael Lentine03c64b02015-08-26 18:27:26 -0500182 {{if not (eq (Macro "FunctionName" $f) "vkGetInstanceProcAddr")}}
Michael Lentine88594d72015-11-12 12:49:45 -0800183 {{if not (GetAnnotation $f "extension")}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700184 vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}} = §
185 reinterpret_cast<{{Macro "FunctionPtrName" $f}}>(§
186 get_proc_addr(instance, "{{Macro "FunctionName" $f}}"));
187 if (UNLIKELY(!vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}})) {
188 ALOGE("missing instance proc: %s", "{{Macro "FunctionName" $f}}");
189 success = false;
190 }
Michael Lentine88594d72015-11-12 12:49:45 -0800191 {{end}}
192 {{end}}
193 {{end}}
194 {{end}}
195 {{range $f := AllCommands $}}
196 {{if eq (Macro "Vtbl" $f) "Instance"}}
Jesse Hall1356b0d2015-11-23 17:24:58 -0800197 {{if and (GetAnnotation $f "extension") (Macro "IsDispatched" $f)}}
Michael Lentine88594d72015-11-12 12:49:45 -0800198 vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}} = §
199 reinterpret_cast<{{Macro "FunctionPtrName" $f}}>(§
200 get_proc_addr(instance, "{{Macro "FunctionName" $f}}"));
Michael Lentine03c64b02015-08-26 18:27:26 -0500201 {{end}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700202 {{end}}
203 {{end}}
204 // clang-format on
205 return success;
206»}
207
208bool LoadDeviceVtbl(VkDevice device,
Michael Lentine03c64b02015-08-26 18:27:26 -0500209 VkDevice device_next,
Jesse Hall04f4f472015-08-16 19:51:04 -0700210 PFN_vkGetDeviceProcAddr get_proc_addr,
211 DeviceVtbl& vtbl)
212 bool success = true;
213 // clang-format off
Michael Lentine03c64b02015-08-26 18:27:26 -0500214 vtbl.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(get_proc_addr(device_next, "vkGetDeviceProcAddr"));
215 if (UNLIKELY(!vtbl.GetDeviceProcAddr)) {
216 ALOGE("missing device proc: %s", "vkGetDeviceProcAddr");
217 success = false;
218 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700219 {{range $f := AllCommands $}}
Jesse Hall9ba8bc82015-11-30 16:22:16 -0800220 {{if and (eq (Macro "Vtbl" $f) "Device") (not (eq (Macro "FunctionName" $f) "vkGetDeviceProcAddr"))}}
221 {{if not (GetAnnotation $f "extension")}}
222 vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}} = §
223 reinterpret_cast<{{Macro "FunctionPtrName" $f}}>(§
224 get_proc_addr(device, "{{Macro "FunctionName" $f}}"));
225 if (UNLIKELY(!vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}})) {
226 ALOGE("missing device proc: %s", "{{Macro "FunctionName" $f}}");
227 success = false;
228 }
229 {{end}}
230 {{end}}
231 {{end}}
232 {{range $f := AllCommands $}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700233 {{if eq (Macro "Vtbl" $f) "Device"}}
Jesse Hall9ba8bc82015-11-30 16:22:16 -0800234 {{if and (GetAnnotation $f "extension") (Macro "IsDispatched" $f)}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700235 vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}} = §
236 reinterpret_cast<{{Macro "FunctionPtrName" $f}}>(§
237 get_proc_addr(device, "{{Macro "FunctionName" $f}}"));
238 if (UNLIKELY(!vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}})) {
239 ALOGE("missing device proc: %s", "{{Macro "FunctionName" $f}}");
240 success = false;
241 }
Michael Lentine03c64b02015-08-26 18:27:26 -0500242 {{end}}
Jesse Hall04f4f472015-08-16 19:51:04 -0700243 {{end}}
244 {{end}}
Jesse Hall70f93352015-11-04 09:41:31 -0800245 vtbl.GetSwapchainGrallocUsageANDROID = reinterpret_cast<PFN_vkGetSwapchainGrallocUsageANDROID>(get_proc_addr(device, "vkGetSwapchainGrallocUsageANDROID"));
246 if (UNLIKELY(!vtbl.GetSwapchainGrallocUsageANDROID)) {
247 // TODO(jessehall): temporarily make this optional, until drivers have been updated
248 // ALOGE("missing device proc: %s", "vkGetSwapchainGrallocUsageANDROID");
249 ALOGW("missing device proc: %s", "vkGetSwapchainGrallocUsageANDROID");
250 // success = false;
251 }
Jesse Hallab9aeef2015-11-04 10:56:20 -0800252 vtbl.AcquireImageANDROID = reinterpret_cast<PFN_vkAcquireImageANDROID>(get_proc_addr(device, "vkAcquireImageANDROID"));
253 if (UNLIKELY(!vtbl.AcquireImageANDROID)) {
254 // TODO(jessehall): temporarily make this optional, until drivers have been updated
255 // ALOGE("missing device proc: %s", "vkImportNativeFenceANDROID");
256 ALOGW("missing device proc: %s", "vkAcquireImageANDROID");
257 // success = false;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700258 }
Jesse Hallab9aeef2015-11-04 10:56:20 -0800259 vtbl.QueueSignalReleaseImageANDROID = reinterpret_cast<PFN_vkQueueSignalReleaseImageANDROID>(get_proc_addr(device, "vkQueueSignalReleaseImageANDROID"));
260 if (UNLIKELY(!vtbl.QueueSignalReleaseImageANDROID)) {
261 // TODO(jessehall): temporarily make this optional, until drivers have been updated
262 // ALOGE("missing device proc: %s", "vkQueueSignalReleaseImageANDROID");
263 ALOGW("missing device proc: %s", "vkQueueSignalReleaseImageANDROID");
264 // success = false;
265 }
266 // TODO(jessehall): these are deprecated; remove when drivers have been updated
267 vtbl.ImportNativeFenceANDROID = reinterpret_cast<PFN_vkImportNativeFenceANDROID>(get_proc_addr(device, "vkImportNativeFenceANDROID"));
Jesse Hallb1352bc2015-09-04 16:12:33 -0700268 vtbl.QueueSignalNativeFenceANDROID = reinterpret_cast<PFN_vkQueueSignalNativeFenceANDROID>(get_proc_addr(device, "vkQueueSignalNativeFenceANDROID"));
Jesse Hallab9aeef2015-11-04 10:56:20 -0800269 if (!((!vtbl.AcquireImageANDROID && !vtbl.QueueSignalReleaseImageANDROID && vtbl.ImportNativeFenceANDROID && vtbl.QueueSignalNativeFenceANDROID) ||
270 (vtbl.AcquireImageANDROID && vtbl.QueueSignalReleaseImageANDROID && !vtbl.ImportNativeFenceANDROID && !vtbl.QueueSignalNativeFenceANDROID))) {
Jesse Hall3e0dc8f2015-11-30 00:42:57 -0800271 ALOGE("driver doesn't support exactly one of old- or new-style VK_ANDROID_native_buffer commands");
Jesse Hallb1352bc2015-09-04 16:12:33 -0700272 success = false;
273 }
Jesse Hall04f4f472015-08-16 19:51:04 -0700274 // clang-format on
275 return success;
276»}
277
278} // namespace vulkan
279
280{{end}}
Jesse Hall1356b0d2015-11-23 17:24:58 -0800281
282
283{{/*
284-------------------------------------------------------------------------------
285 This function emits nil for extension entrypoints that should not be
286 included in dispatch tables, "true" otherwise. Extensions that are ony used
287 directly between the loader and driver, or that aren't supported on Android
288 at all, should be excluded from dispatch.
289-------------------------------------------------------------------------------
290*/}}
291{{define "IsUnsupportedExtension"}}
292 {{$ext := index $.Arguments 0}}
Jesse Hall3e0dc8f2015-11-30 00:42:57 -0800293 {{ if eq $ext "VK_KHR_display"}}true
294 {{else if eq $ext "VK_KHR_display_swapchain"}}true
295 {{else if eq $ext "VK_KHR_xlib_surface"}}true
296 {{else if eq $ext "VK_KHR_xcb_surface"}}true
297 {{else if eq $ext "VK_KHR_wayland_surface"}}true
298 {{else if eq $ext "VK_KHR_mir_surface"}}true
299 {{else if eq $ext "VK_KHR_win32_surface"}}true
Jesse Hall1356b0d2015-11-23 17:24:58 -0800300 {{end}}
301{{end}}
302
303{{define "IsDispatched"}}
304 {{AssertType $ "Function"}}
305 {{$ext := GetAnnotation $ "extension"}}
306 {{if not $ext}}true
307 {{else if not (Macro "IsUnsupportedExtension" $ext)}}true
308 {{end}}
309{{end}}