blob: 9568f7fc61cb904dee8a85bd17abe0c854b95887 [file] [log] [blame]
Chia-I Wu0c203242016-03-15 13:44:51 +08001{{define "Copyright"}}
2/*
3•* Copyright 2016 The Android Open Source Project
4•*
5•* Licensed under the Apache License, Version 2.0 (the "License");
6•* you may not use this file except in compliance with the License.
7•* You may obtain a copy of the License at
8•*
9•* http://www.apache.org/licenses/LICENSE-2.0
10•*
11•* Unless required by applicable law or agreed to in writing, software
12•* distributed under the License is distributed on an "AS IS" BASIS,
13•* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14•* See the License for the specific language governing permissions and
15•* limitations under the License.
16•*/
17¶{{end}}
18
19{{Include "../api/templates/vulkan_common.tmpl"}}
20{{Global "clang-format" (Strings "clang-format" "-style=file")}}
21{{Macro "DefineGlobals" $}}
22{{$ | Macro "api_gen.h" | Format (Global "clang-format") | Write "api_gen.h" }}
23{{$ | Macro "api_gen.cpp" | Format (Global "clang-format") | Write "api_gen.cpp"}}
Chia-I Wueb7db122016-03-24 09:11:06 +080024{{$ | Macro "driver_gen.h" | Format (Global "clang-format") | Write "driver_gen.h"}}
25{{$ | Macro "driver_gen.cpp" | Format (Global "clang-format") | Write "driver_gen.cpp"}}
Chia-I Wu0c203242016-03-15 13:44:51 +080026
27{{/*
28-------------------------------------------------------------------------------
29 api_gen.h
30-------------------------------------------------------------------------------
31*/}}
32{{define "api_gen.h"}}
33{{Macro "Copyright"}}
34
35// WARNING: This file is generated. See ../README.md for instructions.
36
37#ifndef LIBVULKAN_API_GEN_H
38#define LIBVULKAN_API_GEN_H
39
Chia-I Wu8925efd2016-04-13 15:13:21 +080040#include <bitset>
Chia-I Wu0c203242016-03-15 13:44:51 +080041#include <vulkan/vulkan.h>
Chia-I Wu8925efd2016-04-13 15:13:21 +080042#include "driver_gen.h"
Chia-I Wu0c203242016-03-15 13:44:51 +080043
44namespace vulkan
45namespace api
46
47struct InstanceDispatchTable {
48 // clang-format off
49 {{range $f := AllCommands $}}
50 {{if (Macro "api.IsInstanceDispatchTableEntry" $f)}}
Chia-I Wucc5e2762016-03-24 13:01:16 +080051 {{Macro "C++.DeclareTableEntry" $f}};
Chia-I Wu0c203242016-03-15 13:44:51 +080052 {{end}}
53 {{end}}
54 // clang-format on
55};
56
57struct DeviceDispatchTable {
58 // clang-format off
59 {{range $f := AllCommands $}}
60 {{if (Macro "api.IsDeviceDispatchTableEntry" $f)}}
Chia-I Wucc5e2762016-03-24 13:01:16 +080061 {{Macro "C++.DeclareTableEntry" $f}};
Chia-I Wu0c203242016-03-15 13:44:51 +080062 {{end}}
63 {{end}}
64 // clang-format on
65};
66
Chia-I Wu8925efd2016-04-13 15:13:21 +080067bool InitDispatchTable(
68 VkInstance instance,
69 PFN_vkGetInstanceProcAddr get_proc,
70 const std::bitset<driver::ProcHook::EXTENSION_COUNT> &extensions);
71bool InitDispatchTable(
72 VkDevice dev,
73 PFN_vkGetDeviceProcAddr get_proc,
74 const std::bitset<driver::ProcHook::EXTENSION_COUNT> &extensions);
Chia-I Wu0c203242016-03-15 13:44:51 +080075
76»} // namespace api
77»} // namespace vulkan
78
79#endif // LIBVULKAN_API_GEN_H
80¶{{end}}
81
82
83{{/*
84-------------------------------------------------------------------------------
85 api_gen.cpp
86-------------------------------------------------------------------------------
87*/}}
88{{define "api_gen.cpp"}}
89{{Macro "Copyright"}}
90
91// WARNING: This file is generated. See ../README.md for instructions.
92
93#include <string.h>
94#include <algorithm>
95#include <log/log.h>
96
97#include "api.h"
98
99namespace vulkan
100namespace api
101
Chia-I Wucc5e2762016-03-24 13:01:16 +0800102{{Macro "C++.DefineInitProcMacro" "dispatch"}}
103
104{{Macro "api.C++.DefineInitProcExtMacro"}}
Chia-I Wu0c203242016-03-15 13:44:51 +0800105
Chia-I Wu8925efd2016-04-13 15:13:21 +0800106namespace
107
108// clang-format off
109
110{{range $f := AllCommands $}}
111 {{Macro "api.C++.DefineExtensionStub" $f}}
112{{end}}
113// clang-format on
114
115»} // anonymous
116
117bool InitDispatchTable(
118 VkInstance instance,
119 PFN_vkGetInstanceProcAddr get_proc,
120 const std::bitset<driver::ProcHook::EXTENSION_COUNT> &extensions) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800121 auto& data = GetData(instance);
122 bool success = true;
123
124 // clang-format off
125 {{range $f := AllCommands $}}
126 {{if (Macro "api.IsInstanceDispatchTableEntry" $f)}}
127 {{Macro "C++.InitProc" $f}}
128 {{end}}
129 {{end}}
130 // clang-format on
131
132 return success;
133}
134
Chia-I Wu8925efd2016-04-13 15:13:21 +0800135bool InitDispatchTable(
136 VkDevice dev,
137 PFN_vkGetDeviceProcAddr get_proc,
138 const std::bitset<driver::ProcHook::EXTENSION_COUNT> &extensions) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800139 auto& data = GetData(dev);
140 bool success = true;
141
142 // clang-format off
143 {{range $f := AllCommands $}}
144 {{if (Macro "api.IsDeviceDispatchTableEntry" $f)}}
145 {{Macro "C++.InitProc" $f}}
146 {{end}}
147 {{end}}
148 // clang-format on
149
150 return success;
151}
152
153»} // namespace api
154»} // namespace vulkan
155
156// clang-format off
157
158{{range $f := AllCommands $}}
159 {{if (Macro "IsFunctionExported" $f)}}
160 __attribute__((visibility("default")))
161 VKAPI_ATTR {{Node "Type" $f.Return}} {{$f.Name}}({{Macro "Parameters" $f}}) {
162 {{ if eq $f.Name "vkGetInstanceProcAddr"}}
163 {{Macro "api.C++.InterceptInstanceProcAddr" $}}
164 {{else if eq $f.Name "vkGetDeviceProcAddr"}}
165 {{Macro "api.C++.InterceptDeviceProcAddr" $}}
166 {{end}}
167
168 {{Macro "api.C++.Dispatch" $f}}
169 }
170
171 {{end}}
172{{end}}
173
174// clang-format on
175¶{{end}}
176
177
178{{/*
Chia-I Wueb7db122016-03-24 09:11:06 +0800179-------------------------------------------------------------------------------
180 driver_gen.h
181-------------------------------------------------------------------------------
182*/}}
183{{define "driver_gen.h"}}
184{{Macro "Copyright"}}
185
186// WARNING: This file is generated. See ../README.md for instructions.
187
188#ifndef LIBVULKAN_DRIVER_GEN_H
189#define LIBVULKAN_DRIVER_GEN_H
190
Chia-I Wucbe07ef2016-04-13 15:01:00 +0800191#include <bitset>
Chia-I Wueb7db122016-03-24 09:11:06 +0800192#include <vulkan/vulkan.h>
193#include <vulkan/vk_android_native_buffer.h>
194
195namespace vulkan
196namespace driver
197
198{{Macro "driver.C++.DefineProcHookType"}}
199
Chia-I Wucc5e2762016-03-24 13:01:16 +0800200struct InstanceDriverTable {
201 // clang-format off
202 {{range $f := AllCommands $}}
203 {{if (Macro "driver.IsInstanceDriverTableEntry" $f)}}
204 {{Macro "C++.DeclareTableEntry" $f}};
205 {{end}}
206 {{end}}
207 // clang-format on
208};
209
210struct DeviceDriverTable {
211 // clang-format off
212 {{range $f := AllCommands $}}
213 {{if (Macro "driver.IsDeviceDriverTableEntry" $f)}}
214 {{Macro "C++.DeclareTableEntry" $f}};
215 {{end}}
216 {{end}}
217 // clang-format on
218};
219
Chia-I Wueb7db122016-03-24 09:11:06 +0800220const ProcHook* GetProcHook(const char* name);
221ProcHook::Extension GetProcHookExtension(const char* name);
222
Chia-I Wucbe07ef2016-04-13 15:01:00 +0800223bool InitDriverTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc,
224 const std::bitset<ProcHook::EXTENSION_COUNT> &extensions);
225bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
226 const std::bitset<ProcHook::EXTENSION_COUNT> &extensions);
Chia-I Wucc5e2762016-03-24 13:01:16 +0800227
Chia-I Wueb7db122016-03-24 09:11:06 +0800228»} // namespace driver
229»} // namespace vulkan
230
231#endif // LIBVULKAN_DRIVER_TABLE_H
232¶{{end}}
233
234
235{{/*
236-------------------------------------------------------------------------------
237 driver_gen.cpp
238-------------------------------------------------------------------------------
239*/}}
240{{define "driver_gen.cpp"}}
241{{Macro "Copyright"}}
242
243// WARNING: This file is generated. See ../README.md for instructions.
244
245#include <string.h>
246#include <algorithm>
247#include <log/log.h>
248
249#include "driver.h"
Chia-I Wueb7db122016-03-24 09:11:06 +0800250
251namespace vulkan
252namespace driver
253
254namespace
255
256// clang-format off
257
258{{range $f := AllCommands $}}
259 {{Macro "driver.C++.DefineProcHookStubs" $f}}
260{{end}}
261// clang-format on
262
263const ProcHook g_proc_hooks[] = {
264 // clang-format off
265 {{range $f := SortBy (AllCommands $) "FunctionName"}}
266 {{if (Macro "driver.IsIntercepted" $f)}}
267 {{ if (Macro "IsGloballyDispatched" $f)}}
268 {{Macro "driver.C++.DefineGlobalProcHook" $f}}
269 {{else if (Macro "IsInstanceDispatched" $f)}}
270 {{Macro "driver.C++.DefineInstanceProcHook" $f}}
271 {{else if (Macro "IsDeviceDispatched" $f)}}
272 {{Macro "driver.C++.DefineDeviceProcHook" $f}}
273 {{end}}
274 {{end}}
275 {{end}}
276 // clang-format on
277};
278
279»} // anonymous
280
281const ProcHook* GetProcHook(const char* name) {
282 const auto& begin = g_proc_hooks;
283 const auto& end = g_proc_hooks +
284 sizeof(g_proc_hooks) / sizeof(g_proc_hooks[0]);
285 const auto hook = std::lower_bound(begin, end, name,
286 [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
287 return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
288}
289
290ProcHook::Extension GetProcHookExtension(const char* name) {
291 {{$exts := Strings (Macro "driver.InterceptedExtensions") | SplitOn "\n"}}
292 // clang-format off
293 {{range $e := $exts}}
294 if (strcmp(name, "{{$e}}") == 0) return ProcHook::{{TrimPrefix "VK_" $e}};
295 {{end}}
296 // clang-format on
297 return ProcHook::EXTENSION_UNKNOWN;
298}
299
Chia-I Wucc5e2762016-03-24 13:01:16 +0800300{{Macro "C++.DefineInitProcMacro" "driver"}}
301
302{{Macro "driver.C++.DefineInitProcExtMacro"}}
303
Chia-I Wucbe07ef2016-04-13 15:01:00 +0800304bool InitDriverTable(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc,
305 const std::bitset<ProcHook::EXTENSION_COUNT> &extensions)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800306{
307 auto& data = GetData(instance);
308 bool success = true;
309
310 // clang-format off
311 {{range $f := AllCommands $}}
312 {{if (Macro "driver.IsInstanceDriverTableEntry" $f)}}
313 {{Macro "C++.InitProc" $f}}
314 {{end}}
315 {{end}}
316 // clang-format on
317
318 return success;
319}
320
Chia-I Wucbe07ef2016-04-13 15:01:00 +0800321bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
322 const std::bitset<ProcHook::EXTENSION_COUNT> &extensions)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800323{
324 auto& data = GetData(dev);
325 bool success = true;
326
327 // clang-format off
328 {{range $f := AllCommands $}}
329 {{if (Macro "driver.IsDeviceDriverTableEntry" $f)}}
330 {{Macro "C++.InitProc" $f}}
331 {{end}}
332 {{end}}
333 // clang-format on
334
335 return success;
336}
337
Chia-I Wueb7db122016-03-24 09:11:06 +0800338»} // namespace driver
339»} // namespace vulkan
340
341// clang-format on
342¶{{end}}
343
344
345{{/*
Chia-I Wu0c203242016-03-15 13:44:51 +0800346------------------------------------------------------------------------------
Chia-I Wucc5e2762016-03-24 13:01:16 +0800347 Emits a declaration of a dispatch/driver table entry.
Chia-I Wu0c203242016-03-15 13:44:51 +0800348------------------------------------------------------------------------------
349*/}}
Chia-I Wucc5e2762016-03-24 13:01:16 +0800350{{define "C++.DeclareTableEntry"}}
Chia-I Wu0c203242016-03-15 13:44:51 +0800351 {{AssertType $ "Function"}}
352
353 {{Macro "FunctionPtrName" $}} {{Macro "BaseName" $}}
354{{end}}
355
356
357{{/*
358-------------------------------------------------------------------------------
Chia-I Wucc5e2762016-03-24 13:01:16 +0800359 Emits INIT_PROC macro.
Chia-I Wu0c203242016-03-15 13:44:51 +0800360-------------------------------------------------------------------------------
361*/}}
Chia-I Wucc5e2762016-03-24 13:01:16 +0800362{{define "C++.DefineInitProcMacro"}}
Chia-I Wu0c203242016-03-15 13:44:51 +0800363 #define UNLIKELY(expr) __builtin_expect((expr), 0)
364
365 #define INIT_PROC(obj, proc) do { \
366 data.{{$}}.proc = reinterpret_cast<PFN_vk ## proc>( \
367 get_proc(obj, "vk" # proc)); \
368 if (UNLIKELY(!data.{{$}}.proc)) { \
369 ALOGE("missing " # obj " proc: vk" # proc); \
370 success = false; \
371 } \
372 } while(0)
Chia-I Wu0c203242016-03-15 13:44:51 +0800373{{end}}
374
375
376{{/*
377-------------------------------------------------------------------------------
378 Emits code to invoke INIT_PROC or INIT_PROC_EXT.
379-------------------------------------------------------------------------------
380*/}}
381{{define "C++.InitProc"}}
382 {{AssertType $ "Function"}}
383
384 {{$ext := GetAnnotation $ "extension"}}
385 {{if $ext}}
386 INIT_PROC_EXT({{Macro "BaseName" $ext}}, §
387 {{else}}
388 INIT_PROC
389 {{end}}
390
391 {{if (Macro "IsInstanceDispatched" $)}}
392 instance, §
393 {{else}}
394 dev, §
395 {{end}}
396
397 {{Macro "BaseName" $}});
398{{end}}
399
400
401{{/*
402------------------------------------------------------------------------------
403 Emits true if a function is exported and instance-dispatched.
404------------------------------------------------------------------------------
405*/}}
406{{define "api.IsInstanceDispatchTableEntry"}}
407 {{AssertType $ "Function"}}
408
409 {{if and (Macro "IsFunctionExported" $) (Macro "IsInstanceDispatched" $)}}
410 true
411 {{end}}
412{{end}}
413
414
415{{/*
416------------------------------------------------------------------------------
417 Emits true if a function is exported and device-dispatched.
418------------------------------------------------------------------------------
419*/}}
420{{define "api.IsDeviceDispatchTableEntry"}}
421 {{AssertType $ "Function"}}
422
423 {{if and (Macro "IsFunctionExported" $) (Macro "IsDeviceDispatched" $)}}
424 true
425 {{end}}
426{{end}}
427
428
429{{/*
430------------------------------------------------------------------------------
431 Emits true if a function is intercepted by vulkan::api.
432------------------------------------------------------------------------------
433*/}}
434{{define "api.IsIntercepted"}}
435 {{AssertType $ "Function"}}
436
437 {{if (Macro "IsFunctionSupported" $)}}
438 {{/* Global functions cannot be dispatched at all */}}
439 {{ if (Macro "IsGloballyDispatched" $)}}true
440
441 {{/* VkPhysicalDevice functions that manage device layers */}}
442 {{else if eq $.Name "vkCreateDevice"}}true
443 {{else if eq $.Name "vkEnumerateDeviceLayerProperties"}}true
444 {{else if eq $.Name "vkEnumerateDeviceExtensionProperties"}}true
445
446 {{/* Destroy functions of dispatchable objects */}}
447 {{else if eq $.Name "vkDestroyInstance"}}true
448 {{else if eq $.Name "vkDestroyDevice"}}true
449
450 {{end}}
451 {{end}}
452{{end}}
453
454
455{{/*
Chia-I Wucc5e2762016-03-24 13:01:16 +0800456-------------------------------------------------------------------------------
457 Emits INIT_PROC_EXT macro for vulkan::api.
458-------------------------------------------------------------------------------
459*/}}
460{{define "api.C++.DefineInitProcExtMacro"}}
Chia-I Wu8925efd2016-04-13 15:13:21 +0800461 // Exported extension functions may be invoked even when their extensions
462 // are disabled. Dispatch to stubs when that happens.
Chia-I Wucc5e2762016-03-24 13:01:16 +0800463 #define INIT_PROC_EXT(ext, obj, proc) do { \
Chia-I Wu8925efd2016-04-13 15:13:21 +0800464 if (extensions[driver::ProcHook::ext]) \
465 INIT_PROC(obj, proc); \
466 else \
467 data.dispatch.proc = disabled ## proc; \
Chia-I Wucc5e2762016-03-24 13:01:16 +0800468 } while(0)
469{{end}}
470
471
472{{/*
Chia-I Wu8925efd2016-04-13 15:13:21 +0800473-------------------------------------------------------------------------------
474 Emits a stub for an exported extension function.
475-------------------------------------------------------------------------------
476*/}}
477{{define "api.C++.DefineExtensionStub"}}
478 {{AssertType $ "Function"}}
479
480 {{$ext := GetAnnotation $ "extension"}}
481 {{if and $ext (Macro "IsFunctionExported" $)}}
482 {{$ext_name := index $ext.Arguments 0}}
483
484 {{$base := (Macro "BaseName" $)}}
485 {{$unnamed_params := (ForEach $.CallParameters "ParameterType" | JoinWith ", ")}}
486
487 VKAPI_ATTR {{Node "Type" $.Return}} disabled{{$base}}({{$unnamed_params}}) {
488 ALOGE("{{$ext_name}} not enabled. {{$.Name}} not executed.");
489 {{if not (IsVoid $.Return.Type)}}return VK_SUCCESS;{{end}}
490 }
491
492 {{end}}
493{{end}}
494
495
496{{/*
Chia-I Wu0c203242016-03-15 13:44:51 +0800497------------------------------------------------------------------------------
498 Emits code for vkGetInstanceProcAddr for function interception.
499------------------------------------------------------------------------------
500*/}}
501{{define "api.C++.InterceptInstanceProcAddr"}}
502 {{AssertType $ "API"}}
503
504 // global functions
505 if (!instance) {
506 {{range $f := AllCommands $}}
507 {{if (Macro "IsGloballyDispatched" $f)}}
508 if (strcmp(pName, "{{$f.Name}}") == 0) return §
509 reinterpret_cast<PFN_vkVoidFunction>(§
510 vulkan::api::{{Macro "BaseName" $f}});
511 {{end}}
512 {{end}}
513
514 ALOGE("vkGetInstanceProcAddr called with %s without instance", pName);
515 return nullptr;
516 }
517
518 static const struct Hook {
519 const char* name;
520 PFN_vkVoidFunction proc;
521 } hooks[] = {
522 {{range $f := SortBy (AllCommands $) "FunctionName"}}
523 {{if (Macro "IsFunctionExported" $f)}}
524 {{/* hide global functions */}}
525 {{if (Macro "IsGloballyDispatched" $f)}}
526 { "{{$f.Name}}", nullptr },
527
528 {{/* redirect intercepted functions */}}
529 {{else if (Macro "api.IsIntercepted" $f)}}
530 { "{{$f.Name}}", reinterpret_cast<PFN_vkVoidFunction>(§
531 vulkan::api::{{Macro "BaseName" $f}}) },
532
533 {{/* redirect vkGetInstanceProcAddr to itself */}}
534 {{else if eq $f.Name "vkGetInstanceProcAddr"}}
535 { "{{$f.Name}}", reinterpret_cast<PFN_vkVoidFunction>({{$f.Name}}) },
536
537 {{/* redirect device functions to themselves as a workaround for
538 layers that do not intercept in their vkGetInstanceProcAddr */}}
539 {{else if (Macro "IsDeviceDispatched" $f)}}
540 { "{{$f.Name}}", reinterpret_cast<PFN_vkVoidFunction>({{$f.Name}}) },
541
542 {{end}}
543 {{end}}
544 {{end}}
545 };
546 // clang-format on
547 constexpr size_t count = sizeof(hooks) / sizeof(hooks[0]);
548 auto hook = std::lower_bound(
549 hooks, hooks + count, pName,
550 [](const Hook& h, const char* n) { return strcmp(h.name, n) < 0; });
551 if (hook < hooks + count && strcmp(hook->name, pName) == 0) {
552 if (!hook->proc)
553 ALOGE("vkGetInstanceProcAddr called with %s with instance", pName);
554 return hook->proc;
555 }
556 // clang-format off
557
558{{end}}
559
560
561{{/*
562------------------------------------------------------------------------------
563 Emits code for vkGetDeviceProcAddr for function interception.
564------------------------------------------------------------------------------
565*/}}
566{{define "api.C++.InterceptDeviceProcAddr"}}
567 {{AssertType $ "API"}}
568
569 if (device == VK_NULL_HANDLE) {
570 ALOGE("vkGetDeviceProcAddr called with invalid device");
571 return nullptr;
572 }
573
574 static const char* const known_non_device_names[] = {
575 {{range $f := SortBy (AllCommands $) "FunctionName"}}
576 {{if (Macro "IsFunctionSupported" $f)}}
577 {{if not (Macro "IsDeviceDispatched" $f)}}
578 "{{$f.Name}}",
579 {{end}}
580 {{end}}
581 {{end}}
582 };
583 // clang-format on
584 constexpr size_t count = sizeof(known_non_device_names) /
585 sizeof(known_non_device_names[0]);
586 if (!pName ||
587 std::binary_search(
588 known_non_device_names, known_non_device_names + count, pName,
589 [](const char* a, const char* b) { return (strcmp(a, b) < 0); })) {
Chia-I Wuc56603e2016-04-12 11:16:17 +0800590 ALOGE("vkGetDeviceProcAddr called with %s", (pName) ? pName : "(null)");
Chia-I Wu0c203242016-03-15 13:44:51 +0800591 return nullptr;
592 }
593 // clang-format off
594
Chia-I Wuc56603e2016-04-12 11:16:17 +0800595 {{range $f := AllCommands $}}
596 {{if (Macro "IsDeviceDispatched" $f)}}
597 {{ if (Macro "api.IsIntercepted" $f)}}
598 if (strcmp(pName, "{{$f.Name}}") == 0) return §
599 reinterpret_cast<PFN_vkVoidFunction>(§
600 vulkan::api::{{Macro "BaseName" $f}});
601 {{else if eq $f.Name "vkGetDeviceProcAddr"}}
602 if (strcmp(pName, "{{$f.Name}}") == 0) return §
603 reinterpret_cast<PFN_vkVoidFunction>(§
604 {{$f.Name}});
605 {{end}}
606 {{end}}
607 {{end}}
608
Chia-I Wu0c203242016-03-15 13:44:51 +0800609{{end}}
610
611
612{{/*
613------------------------------------------------------------------------------
614 Emits code to dispatch a function.
615------------------------------------------------------------------------------
616*/}}
617{{define "api.C++.Dispatch"}}
618 {{AssertType $ "Function"}}
619
620 {{if (Macro "api.IsIntercepted" $)}}// call into api.cpp{{end}}
621 {{if not (IsVoid $.Return.Type)}}return §{{end}}
622
623 {{if (Macro "api.IsIntercepted" $)}}
624 vulkan::api::§
625 {{else}}
626 {{$p0 := index $.CallParameters 0}}
627 vulkan::api::GetData({{$p0.Name}}).dispatch
628 {{end}}
629
630 {{Macro "BaseName" $}}({{Macro "Arguments" $}});
631{{end}}
632
633
634{{/*
Chia-I Wueb7db122016-03-24 09:11:06 +0800635------------------------------------------------------------------------------
636 Emits a list of extensions intercepted by vulkan::driver.
637------------------------------------------------------------------------------
638*/}}
639{{define "driver.InterceptedExtensions"}}
640VK_ANDROID_native_buffer
641VK_EXT_debug_report
642VK_KHR_android_surface
643VK_KHR_surface
644VK_KHR_swapchain
645{{end}}
646
647
648{{/*
649------------------------------------------------------------------------------
650 Emits true if an extension is intercepted by vulkan::driver.
651------------------------------------------------------------------------------
652*/}}
653{{define "driver.IsExtensionIntercepted"}}
654 {{$ext_name := index $.Arguments 0}}
655 {{$filters := Strings (Macro "driver.InterceptedExtensions") | SplitOn "\n"}}
656
657 {{range $f := $filters}}
658 {{if eq $ext_name $f}}true{{end}}
659 {{end}}
660{{end}}
661
662
663{{/*
664------------------------------------------------------------------------------
665 Emits true if a function is intercepted by vulkan::driver.
666------------------------------------------------------------------------------
667*/}}
668{{define "driver.IsIntercepted"}}
669 {{AssertType $ "Function"}}
670
671 {{if (Macro "IsFunctionSupported" $)}}
672 {{/* Create functions of dispatchable objects */}}
673 {{ if eq $.Name "vkCreateInstance"}}true
674 {{else if eq $.Name "vkCreateDevice"}}true
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800675 {{else if eq $.Name "vkEnumeratePhysicalDevices"}}true
Chia-I Wueb7db122016-03-24 09:11:06 +0800676 {{else if eq $.Name "vkGetDeviceQueue"}}true
677 {{else if eq $.Name "vkAllocateCommandBuffers"}}true
678
679 {{/* Destroy functions of dispatchable objects */}}
680 {{else if eq $.Name "vkDestroyInstance"}}true
681 {{else if eq $.Name "vkDestroyDevice"}}true
682
683 {{/* Enumeration of extensions */}}
684 {{else if eq $.Name "vkEnumerateInstanceExtensionProperties"}}true
685 {{else if eq $.Name "vkEnumerateDeviceExtensionProperties"}}true
686
Chia-I Wueb7db122016-03-24 09:11:06 +0800687 {{else if eq $.Name "vkGetInstanceProcAddr"}}true
688 {{else if eq $.Name "vkGetDeviceProcAddr"}}true
689
690 {{end}}
691
692 {{$ext := GetAnnotation $ "extension"}}
693 {{if $ext}}
694 {{Macro "driver.IsExtensionIntercepted" $ext}}
695 {{end}}
696
697 {{end}}
698{{end}}
699
700
701{{/*
702------------------------------------------------------------------------------
703 Emits true if a function needs ProcHook stubs.
704------------------------------------------------------------------------------
705*/}}
706{{define "driver.NeedProcHookStubs"}}
707 {{AssertType $ "Function"}}
708
709 {{if (Macro "driver.IsIntercepted" $)}}
710 {{$ext := GetAnnotation $ "extension"}}
711 {{if $ext}}
712 {{if not (Macro "IsExtensionInternal" $ext)}}true{{end}}
713 {{end}}
714 {{end}}
715{{end}}
716
717
718{{/*
719-------------------------------------------------------------------------------
720 Emits definition of struct ProcHook.
721-------------------------------------------------------------------------------
722*/}}
723{{define "driver.C++.DefineProcHookType"}}
724 struct ProcHook {
725 enum Type {
726 GLOBAL,
727 INSTANCE,
728 DEVICE,
729 };
730
731 enum Extension {
732 {{$exts := Strings (Macro "driver.InterceptedExtensions") | SplitOn "\n"}}
733 {{range $e := $exts}}
734 {{TrimPrefix "VK_" $e}},
735 {{end}}
736
737 EXTENSION_CORE, // valid bit
738 EXTENSION_COUNT,
739 EXTENSION_UNKNOWN,
740 };
741
742 const char* name;
743 Type type;
744 Extension extension;
745
746 PFN_vkVoidFunction proc;
747 PFN_vkVoidFunction disabled_proc; // nullptr for global hooks
748 PFN_vkVoidFunction checked_proc; // nullptr for global/instance hooks
749 };
750{{end}}
751
752
753{{/*
754-------------------------------------------------------------------------------
Chia-I Wucc5e2762016-03-24 13:01:16 +0800755 Emits INIT_PROC_EXT macro for vulkan::driver.
756-------------------------------------------------------------------------------
757*/}}
758{{define "driver.C++.DefineInitProcExtMacro"}}
759 #define INIT_PROC_EXT(ext, obj, proc) do { \
Chia-I Wucbe07ef2016-04-13 15:01:00 +0800760 if (extensions[ProcHook::ext]) \
Chia-I Wucc5e2762016-03-24 13:01:16 +0800761 INIT_PROC(obj, proc); \
762 } while(0)
763{{end}}
764
765
766{{/*
767-------------------------------------------------------------------------------
Chia-I Wueb7db122016-03-24 09:11:06 +0800768 Emits definitions of stub functions for ProcHook.
769-------------------------------------------------------------------------------
770*/}}
771{{define "driver.C++.DefineProcHookStubs"}}
772 {{AssertType $ "Function"}}
773
774 {{if (Macro "driver.NeedProcHookStubs" $)}}
775 {{$ext := GetAnnotation $ "extension"}}
776 {{$ext_name := index $ext.Arguments 0}}
777
778 {{$base := (Macro "BaseName" $)}}
779 {{$unnamed_params := (ForEach $.CallParameters "ParameterType" | JoinWith ", ")}}
780
781 VKAPI_ATTR {{Node "Type" $.Return}} disabled{{$base}}({{$unnamed_params}}) {
782 ALOGE("{{$ext_name}} not enabled. {{$.Name}} not executed.");
783 {{if not (IsVoid $.Return.Type)}}return VK_SUCCESS;{{end}}
784 }
785 {{if (Macro "IsDeviceDispatched" $)}}
786
787 VKAPI_ATTR {{Node "Type" $.Return}} checked{{$base}}({{Macro "Parameters" $}}) {
788 {{if not (IsVoid $.Return.Type)}}return §{{end}}
789
790 {{$p0 := index $.CallParameters 0}}
791 {{$ext_hook := Strings ("ProcHook::") (Macro "BaseName" $ext)}}
792 (GetData({{$p0.Name}}).hook_extensions[{{$ext_hook}}]) ? §
Chia-I Wu62262232016-03-26 07:06:44 +0800793 {{$base}}({{Macro "Arguments" $}}) : §
Chia-I Wueb7db122016-03-24 09:11:06 +0800794 disabled{{$base}}({{Macro "Arguments" $}});
795 }
796 {{end}}
797
798 {{end}}
799{{end}}
800
801
802{{/*
803-------------------------------------------------------------------------------
804 Emits definition of a global ProcHook.
805-------------------------------------------------------------------------------
806*/}}
807{{define "driver.C++.DefineGlobalProcHook"}}
808 {{AssertType $ "Function"}}
809
810 {{$base := (Macro "BaseName" $)}}
811
812 {{$ext := GetAnnotation $ "extension"}}
813 {{if $ext}}
814 {{Error "invalid global extension"}}
815 {{end}}
816
817 {
818 "{{$.Name}}",
819 ProcHook::GLOBAL,
820 ProcHook::EXTENSION_CORE,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800821 reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
Chia-I Wueb7db122016-03-24 09:11:06 +0800822 nullptr,
823 nullptr,
824 },
825{{end}}
826
827
828{{/*
829-------------------------------------------------------------------------------
830 Emits definition of an instance ProcHook.
831-------------------------------------------------------------------------------
832*/}}
833{{define "driver.C++.DefineInstanceProcHook"}}
834 {{AssertType $ "Function"}}
835
836 {{$base := (Macro "BaseName" $)}}
837
838 {
839 "{{$.Name}}",
840 ProcHook::INSTANCE,
841
842 {{$ext := GetAnnotation $ "extension"}}
843 {{if $ext}}
844 ProcHook::{{Macro "BaseName" $ext}},
845
846 {{if (Macro "IsExtensionInternal" $ext)}}
847 nullptr,
848 nullptr,
849 nullptr,
850 {{else}}
Chia-I Wu62262232016-03-26 07:06:44 +0800851 reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
Chia-I Wueb7db122016-03-24 09:11:06 +0800852 reinterpret_cast<PFN_vkVoidFunction>(disabled{{$base}}),
853 nullptr,
854 {{end}}
855 {{else}}
856 ProcHook::EXTENSION_CORE,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800857 reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
Chia-I Wueb7db122016-03-24 09:11:06 +0800858 nullptr,
859 nullptr,
860 {{end}}
861 },
862{{end}}
863
864
865{{/*
866-------------------------------------------------------------------------------
867 Emits definition of a device ProcHook.
868-------------------------------------------------------------------------------
869*/}}
870{{define "driver.C++.DefineDeviceProcHook"}}
871 {{AssertType $ "Function"}}
872
873 {{$base := (Macro "BaseName" $)}}
874
875 {
876 "{{$.Name}}",
877 ProcHook::DEVICE,
878
879 {{$ext := GetAnnotation $ "extension"}}
880 {{if $ext}}
881 ProcHook::{{Macro "BaseName" $ext}},
882
883 {{if (Macro "IsExtensionInternal" $ext)}}
884 nullptr,
885 nullptr,
886 nullptr,
887 {{else}}
Chia-I Wu62262232016-03-26 07:06:44 +0800888 reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
Chia-I Wueb7db122016-03-24 09:11:06 +0800889 reinterpret_cast<PFN_vkVoidFunction>(disabled{{$base}}),
890 reinterpret_cast<PFN_vkVoidFunction>(checked{{$base}}),
891 {{end}}
892 {{else}}
Chia-I Wu4901db72016-03-24 16:38:58 +0800893 ProcHook::EXTENSION_CORE,
894 reinterpret_cast<PFN_vkVoidFunction>({{$base}}),
895 nullptr,
896 nullptr,
Chia-I Wueb7db122016-03-24 09:11:06 +0800897 {{end}}
898 },
899{{end}}
900
901
902{{/*
Chia-I Wu0c203242016-03-15 13:44:51 +0800903-------------------------------------------------------------------------------
Chia-I Wucc5e2762016-03-24 13:01:16 +0800904 Emits true if a function is needed by vulkan::driver.
905-------------------------------------------------------------------------------
906*/}}
907{{define "driver.IsDriverTableEntry"}}
908 {{AssertType $ "Function"}}
909
910 {{if (Macro "IsFunctionSupported" $)}}
911 {{/* Create functions of dispatchable objects */}}
912 {{ if eq $.Name "vkCreateDevice"}}true
913 {{else if eq $.Name "vkGetDeviceQueue"}}true
914 {{else if eq $.Name "vkAllocateCommandBuffers"}}true
915
916 {{/* Destroy functions of dispatchable objects */}}
917 {{else if eq $.Name "vkDestroyInstance"}}true
918 {{else if eq $.Name "vkDestroyDevice"}}true
919
Chia-I Wu4901db72016-03-24 16:38:58 +0800920 {{else if eq $.Name "vkEnumerateDeviceLayerProperties"}}true
921
Chia-I Wucc5e2762016-03-24 13:01:16 +0800922 {{/* Enumeration of extensions */}}
923 {{else if eq $.Name "vkEnumerateDeviceExtensionProperties"}}true
924
925 {{/* We cache physical devices in loader.cpp */}}
926 {{else if eq $.Name "vkEnumeratePhysicalDevices"}}true
927
928 {{else if eq $.Name "vkGetInstanceProcAddr"}}true
929 {{else if eq $.Name "vkGetDeviceProcAddr"}}true
930
931 {{/* VK_KHR_swapchain->VK_ANDROID_native_buffer translation */}}
932 {{else if eq $.Name "vkCreateImage"}}true
933 {{else if eq $.Name "vkDestroyImage"}}true
934
935 {{end}}
936
937 {{$ext := GetAnnotation $ "extension"}}
938 {{if $ext}}
939 {{$ext_name := index $ext.Arguments 0}}
940 {{ if eq $ext_name "VK_ANDROID_native_buffer"}}true
941 {{else if eq $ext_name "VK_EXT_debug_report"}}true
942 {{end}}
943 {{end}}
944 {{end}}
945{{end}}
946
947
948{{/*
949------------------------------------------------------------------------------
950 Emits true if an instance-dispatched function is needed by vulkan::driver.
951------------------------------------------------------------------------------
952*/}}
953{{define "driver.IsInstanceDriverTableEntry"}}
954 {{AssertType $ "Function"}}
955
956 {{if and (Macro "driver.IsDriverTableEntry" $) (Macro "IsInstanceDispatched" $)}}
957 true
958 {{end}}
959{{end}}
960
961
962{{/*
963------------------------------------------------------------------------------
964 Emits true if a device-dispatched function is needed by vulkan::driver.
965------------------------------------------------------------------------------
966*/}}
967{{define "driver.IsDeviceDriverTableEntry"}}
968 {{AssertType $ "Function"}}
969
970 {{if and (Macro "driver.IsDriverTableEntry" $) (Macro "IsDeviceDispatched" $)}}
971 true
972 {{end}}
973{{end}}
974
975
976{{/*
977-------------------------------------------------------------------------------
Chia-I Wu0c203242016-03-15 13:44:51 +0800978 Emits a function/extension name without the "vk"/"VK_" prefix.
979-------------------------------------------------------------------------------
980*/}}
981{{define "BaseName"}}
982 {{ if IsFunction $}}{{TrimPrefix "vk" $.Name}}
983 {{else if eq $.Name "extension"}}{{TrimPrefix "VK_" (index $.Arguments 0)}}
984 {{else}}{{Error "invalid use of BaseName"}}
985 {{end}}
986{{end}}
987
988
989{{/*
990-------------------------------------------------------------------------------
991 Emits a comma-separated list of C parameter names for the given command.
992-------------------------------------------------------------------------------
993*/}}
994{{define "Arguments"}}
995 {{AssertType $ "Function"}}
996
997 {{ForEach $.CallParameters "ParameterName" | JoinWith ", "}}
998{{end}}
999
1000
1001{{/*
1002------------------------------------------------------------------------------
1003------------------------------------------------------------------------------
1004*/}}
1005{{define "IsGloballyDispatched"}}
1006 {{AssertType $ "Function"}}
1007 {{if and (Macro "IsFunctionSupported" $) (eq (Macro "Vtbl" $) "Global")}}
1008 true
1009 {{end}}
1010{{end}}
1011
1012
1013{{/*
1014------------------------------------------------------------------------------
1015 Emit "true" for supported functions that undergo table dispatch. Only global
1016 functions and functions handled in the loader top without calling into
1017 lower layers are not dispatched.
1018------------------------------------------------------------------------------
1019*/}}
1020{{define "IsInstanceDispatched"}}
1021 {{AssertType $ "Function"}}
1022 {{if and (Macro "IsFunctionSupported" $) (eq (Macro "Vtbl" $) "Instance")}}
1023 true
1024 {{end}}
1025{{end}}
1026
1027
1028{{/*
1029------------------------------------------------------------------------------
1030 Emit "true" for supported functions that can have device-specific dispatch.
1031------------------------------------------------------------------------------
1032*/}}
1033{{define "IsDeviceDispatched"}}
1034 {{AssertType $ "Function"}}
1035 {{if and (Macro "IsFunctionSupported" $) (eq (Macro "Vtbl" $) "Device")}}
1036 true
1037 {{end}}
1038{{end}}
1039
1040
1041{{/*
1042------------------------------------------------------------------------------
1043 Emit "true" if a function is core or from a supportable extension.
1044------------------------------------------------------------------------------
1045*/}}
1046{{define "IsFunctionSupported"}}
1047 {{AssertType $ "Function"}}
1048 {{if not (GetAnnotation $ "pfn")}}
1049 {{$ext := GetAnnotation $ "extension"}}
1050 {{if not $ext}}true
1051 {{else if not (Macro "IsExtensionBlacklisted" $ext)}}true
1052 {{end}}
1053 {{end}}
1054{{end}}
1055
1056
1057{{/*
1058------------------------------------------------------------------------------
1059 Decides whether a function should be exported from the Android Vulkan
1060 library. Functions in the core API and in loader extensions are exported.
1061------------------------------------------------------------------------------
1062*/}}
1063{{define "IsFunctionExported"}}
1064 {{AssertType $ "Function"}}
1065
1066 {{if (Macro "IsFunctionSupported" $)}}
1067 {{$ext := GetAnnotation $ "extension"}}
1068 {{if $ext}}
1069 {{Macro "IsExtensionExported" $ext}}
1070 {{else}}
1071 true
1072 {{end}}
1073 {{end}}
1074{{end}}
1075
1076
1077{{/*
1078------------------------------------------------------------------------------
1079 Emit "true" if an extension is unsupportable on Android.
1080------------------------------------------------------------------------------
1081*/}}
1082{{define "IsExtensionBlacklisted"}}
1083 {{$ext := index $.Arguments 0}}
1084 {{ if eq $ext "VK_KHR_display"}}true
1085 {{else if eq $ext "VK_KHR_display_swapchain"}}true
1086 {{else if eq $ext "VK_KHR_xlib_surface"}}true
1087 {{else if eq $ext "VK_KHR_xcb_surface"}}true
1088 {{else if eq $ext "VK_KHR_wayland_surface"}}true
1089 {{else if eq $ext "VK_KHR_mir_surface"}}true
1090 {{else if eq $ext "VK_KHR_win32_surface"}}true
1091 {{end}}
1092{{end}}
1093
1094
1095{{/*
1096------------------------------------------------------------------------------
1097 Reports whether an extension is implemented entirely by the loader,
1098 so drivers should not enumerate it.
1099------------------------------------------------------------------------------
1100*/}}
1101{{define "IsExtensionExported"}}
1102 {{$ext := index $.Arguments 0}}
1103 {{ if eq $ext "VK_KHR_surface"}}true
1104 {{else if eq $ext "VK_KHR_swapchain"}}true
1105 {{else if eq $ext "VK_KHR_android_surface"}}true
1106 {{end}}
1107{{end}}
Chia-I Wueb7db122016-03-24 09:11:06 +08001108
1109
1110{{/*
1111------------------------------------------------------------------------------
1112 Reports whether an extension is internal to the loader and drivers,
1113 so the loader should not enumerate it.
1114------------------------------------------------------------------------------
1115*/}}
1116{{define "IsExtensionInternal"}}
1117 {{$ext := index $.Arguments 0}}
1118 {{ if eq $ext "VK_ANDROID_native_buffer"}}true
1119 {{end}}
1120{{end}}