blob: c3391e1c969bb53abd2aaa923a7be0f8f388e8c9 [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) {
123 if (strcmp(name, "vkGetDeviceProcAddr") == 0)
124 return reinterpret_cast<PFN_vkVoidFunction>(vkGetDeviceProcAddr);
125 const NameProcEntry* entry = FindProcEntry(kInstanceProcTbl, name);
126 return entry ? entry->proc : nullptr;
127}
128
129PFN_vkVoidFunction GetGlobalDeviceProcAddr(const char* name) {
130 const NameProcEntry* entry = FindProcEntry(kDeviceProcTbl, name);
131 return entry ? entry->proc : nullptr;
132}
133
134PFN_vkVoidFunction GetSpecificInstanceProcAddr(const InstanceVtbl* vtbl,
135 const char* name) {
136 const NameOffsetEntry* entry = FindProcEntry(kInstanceOffsetTbl, name);
137 if (!entry)
138 return nullptr;
139 const unsigned char* base = reinterpret_cast<const unsigned char*>(vtbl);
140 return reinterpret_cast<PFN_vkVoidFunction>(
141 const_cast<unsigned char*>(base) + entry->offset);
142}
143
144PFN_vkVoidFunction GetSpecificDeviceProcAddr(const DeviceVtbl* vtbl,
145 const char* name) {
146 const NameOffsetEntry* entry = FindProcEntry(kDeviceOffsetTbl, name);
147 if (!entry)
148 return nullptr;
149 const unsigned char* base = reinterpret_cast<const unsigned char*>(vtbl);
150 return reinterpret_cast<PFN_vkVoidFunction>(
151 const_cast<unsigned char*>(base) + entry->offset);
152}
153
154bool LoadInstanceVtbl(VkInstance instance,
155 PFN_vkGetInstanceProcAddr get_proc_addr,
156 InstanceVtbl& vtbl)
157 bool success = true;
158 // clang-format off
159 {{range $f := AllCommands $}}
160 {{if eq (Macro "Vtbl" $f) "Instance"}}
161 vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}} = §
162 reinterpret_cast<{{Macro "FunctionPtrName" $f}}>(§
163 get_proc_addr(instance, "{{Macro "FunctionName" $f}}"));
164 if (UNLIKELY(!vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}})) {
165 ALOGE("missing instance proc: %s", "{{Macro "FunctionName" $f}}");
166 success = false;
167 }
168 {{end}}
169 {{end}}
170 // clang-format on
171 return success;
172»}
173
174bool LoadDeviceVtbl(VkDevice device,
175 PFN_vkGetDeviceProcAddr get_proc_addr,
176 DeviceVtbl& vtbl)
177 bool success = true;
178 // clang-format off
179 {{range $f := AllCommands $}}
180 {{if eq (Macro "Vtbl" $f) "Device"}}
181 vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}} = §
182 reinterpret_cast<{{Macro "FunctionPtrName" $f}}>(§
183 get_proc_addr(device, "{{Macro "FunctionName" $f}}"));
184 if (UNLIKELY(!vtbl.{{TrimPrefix "vk" (Macro "FunctionName" $f)}})) {
185 ALOGE("missing device proc: %s", "{{Macro "FunctionName" $f}}");
186 success = false;
187 }
188 {{end}}
189 {{end}}
190 // clang-format on
191 return success;
192»}
193
194} // namespace vulkan
195
196{{end}}