summaryrefslogtreecommitdiff
path: root/registry/vulkan/scripts/cereal/dispatch.py
blob: 42cba8ca44ea0d0a928e51dd50e6836cb477ab3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# Copyright (c) 2018 The Android Open Source Project
# Copyright (c) 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .common.codegen import CodeGen
from .common.vulkantypes import \
        VulkanAPI, makeVulkanTypeSimple, iterateVulkanType

from .wrapperdefs import VulkanWrapperGenerator

# No real good way to automatically infer the most important Vulkan API
# functions as it relates to which getProcAddress function to use, plus
# we might want to control which function to use depending on our
# performance needs.

# This is based on the minimum set of functions needed to be directly
# queried with dlsym and not returning null.
getProcAddrFuncs = [
    "vkGetInstanceProcAddr",
    "vkDestroyInstance",
    "vkEnumeratePhysicalDevices",
    "vkGetPhysicalDeviceFeatures",
    "vkGetPhysicalDeviceFormatProperties",
    "vkGetPhysicalDeviceImageFormatProperties",
    "vkGetPhysicalDeviceProperties",
    "vkGetPhysicalDeviceQueueFamilyProperties",
    "vkGetPhysicalDeviceMemoryProperties",
    "vkCreateDevice",
    "vkDestroyDevice",
    "vkEnumerateDeviceExtensionProperties",
    "vkEnumerateDeviceLayerProperties",
]

# Some methods can only be found using dlsym() while we cannot get the function
# address using vkGetInstProcAddr() or vkGetDeviceProcAddr(). These function
# pointers should only be initialized when setting up the dispatch from system
# loader.
getProcAddrOnlyFuncs = [
    "vkGetMTLDeviceMVK",
    "vkSetMTLTextureMVK",
    "vkGetMTLTextureMVK",
    "vkGetMTLBufferMVK",
    "vkUseIOSurfaceMVK",
    "vkGetIOSurfaceMVK",
]

getInstanceProcAddrNoInstanceFuncs = [
    "vkCreateInstance",
    "vkEnumerateInstanceExtensionProperties",
    "vkEnumerateInstanceLayerProperties",
]

getInstanceProcAddrFuncs = [
    "vkGetDeviceProcAddr",
    "vkCreateSwapchainKHR",
    "vkDestroySwapchainKHR",
    "vkGetSwapchainImagesKHR",
    "vkAcquireNextImageKHR",
    "vkQueuePresentKHR",
    "vkCreateMacOSSurfaceMVK",
    "vkCreateWin32SurfaceKHR",
    "vkGetPhysicalDeviceWin32PresentationSupportKHR",
    "vkCreateXlibSurfaceKHR",
    "vkGetPhysicalDeviceXlibPresentationSupportKHR",
    "vkCreateXcbSurfaceKHR",
    "vkGetPhysicalDeviceXcbPresentationSupportKHR",
    "vkGetPhysicalDeviceSparseImageFormatProperties",
    "vkEnumerateInstanceVersion",
    "vkEnumeratePhysicalDeviceGroups",
    "vkGetPhysicalDeviceFeatures2",
    "vkGetPhysicalDeviceProperties2",
    "vkGetPhysicalDeviceFormatProperties2",
    "vkGetPhysicalDeviceImageFormatProperties2",
    "vkGetPhysicalDeviceQueueFamilyProperties2",
    "vkGetPhysicalDeviceMemoryProperties2",
    "vkGetPhysicalDeviceSparseImageFormatProperties2",
    "vkGetPhysicalDeviceExternalBufferProperties",
    "vkGetPhysicalDeviceExternalFenceProperties",
    "vkGetPhysicalDeviceExternalSemaphoreProperties",
]

# Implicitly, everything else is going to be obtained
# with vkGetDeviceProcAddr,
# unless it has instance in the arg.

def isGetProcAddressAPI(vulkanApi):
    return vulkanApi.name in getProcAddrFuncs

def isGetProcAddressOnlyAPI(vulkanApi):
    return vulkanApi.name in getProcAddrOnlyFuncs

def isGetInstanceProcAddressNoInstanceAPI(vulkanApi):
    return vulkanApi.name in getInstanceProcAddrNoInstanceFuncs

def isGetInstanceProcAddressAPI(vulkanApi):
    if vulkanApi.name in getInstanceProcAddrFuncs:
        return True

    if vulkanApi.parameters[0].typeName == "VkInstance":
        return True

    return False

def isGetDeviceProcAddressAPI(vulkanApi):
    if isGetProcAddressAPI(vulkanApi):
        return False

    if isGetProcAddressOnlyAPI(vulkanApi):
        return False

    if isGetInstanceProcAddressAPI(vulkanApi):
        return False

    return True

def inferProcAddressFuncType(vulkanApi):
    if isGetProcAddressAPI(vulkanApi):
        return "global"
    if isGetProcAddressOnlyAPI(vulkanApi):
        return "global-only"
    if isGetInstanceProcAddressNoInstanceAPI(vulkanApi):
        return "global-instance"
    if isGetInstanceProcAddressAPI(vulkanApi):
        return "instance"
    return "device"

# VulkanDispatch defines a struct, VulkanDispatch,
# that is populated by function pointers from the Vulkan
# loader. No attempt is made to do something different
# for instance vs device functions.
class VulkanDispatch(VulkanWrapperGenerator):
    def __init__(self, module, typeInfo):
        VulkanWrapperGenerator.__init__(self, module, typeInfo)

        self.apisToGet = {}

        self.cgenHeader = CodeGen()
        self.cgenImpl = CodeGen()
        self.typeInfo = typeInfo

        self.currentFeature = ""
        self.featureForCodegen = ""

    def onBegin(self):

        # The first way is to use just the loader to get symbols. This doesn't
        # necessarily work with extensions because at that point the dispatch
        # table needs to be specific to a particular Vulkan instance or device.

        self.cgenHeader.line("""
void init_vulkan_dispatch_from_system_loader(
    DlOpenFunc dlOpenFunc,
    DlSymFunc dlSymFunc,
    VulkanDispatch* dispatch_out);
""")

        # The second way is to initialize the table from a given Vulkan
        # instance or device. Provided the instance or device was created with
        # the right extensions, we can obtain function pointers to extension
        # functions this way.

        self.cgenHeader.line("""
void init_vulkan_dispatch_from_instance(
    VulkanDispatch* vk,
    VkInstance instance,
    VulkanDispatch* dispatch_out);
""")
        self.cgenHeader.line("""
void init_vulkan_dispatch_from_device(
    VulkanDispatch* vk,
    VkDevice device,
    VulkanDispatch* dispatch_out);
""")

        # After populating a VulkanDispatch with the above methods,
        # it can be useful to check whether the Vulkan 1.0 or 1.1 methods
        # are all there.
        def emit_feature_check_decl(cgen, tag, featureToCheck):
            cgen.line("""
bool vulkan_dispatch_check_%s_%s(
    const VulkanDispatch* vk);
""" % (tag, featureToCheck))

        emit_feature_check_decl(self.cgenHeader, "instance", "VK_VERSION_1_0")
        emit_feature_check_decl(self.cgenHeader, "instance", "VK_VERSION_1_1")
        emit_feature_check_decl(self.cgenHeader, "device", "VK_VERSION_1_0")
        emit_feature_check_decl(self.cgenHeader, "device", "VK_VERSION_1_1")

        self.cgenHeader.line("struct VulkanDispatch {")
        self.module.appendHeader(self.cgenHeader.swapCode())

    def syncFeatureQuiet(self, cgen, feature):
        if self.featureForCodegen != feature:
            if feature == "":
                self.featureForCodegen = feature
                return

            self.featureForCodegen = feature

    def syncFeature(self, cgen, feature):
        if self.featureForCodegen != feature:
            if feature == "":
                cgen.leftline("#endif")
                self.featureForCodegen = feature
                return

            if self.featureForCodegen != "":
                cgen.leftline("#endif")

            cgen.leftline("#ifdef %s" % feature)
            self.featureForCodegen = feature

    def makeDlsymCall(self, cgen, apiname, typedecl):
        cgen.stmt( \
            "out->%s = (%s)dlSymFunc(lib, \"%s\")" % \
            (apiname, typedecl, apiname))

    def makeGetInstanceProcAddrCall(self, cgen, dispatch, instance, apiname, typedecl):
        cgen.stmt( \
            "out->%s = (%s)%s->vkGetInstanceProcAddr(%s, \"%s\")" % \
            (apiname, typedecl, dispatch, instance, apiname))

    def makeGetDeviceProcAddrCall(self, cgen, dispatch, device, apiname, typedecl):
        cgen.stmt( \
            "out->%s = (%s)%s->vkGetDeviceProcAddr(%s, \"%s\")" % \
            (apiname, typedecl, dispatch, device, apiname))

    def onEnd(self):
        self.cgenHeader.line("};")
        self.module.appendHeader(self.cgenHeader.swapCode())

        # Getting dispatch tables from the loader
        self.cgenImpl.line("""
void init_vulkan_dispatch_from_system_loader(
    DlOpenFunc dlOpenFunc,
    DlSymFunc dlSymFunc,
    VulkanDispatch* out)""")

        self.cgenImpl.beginBlock()

        self.cgenImpl.stmt("memset(out, 0x0, sizeof(VulkanDispatch))")

        self.cgenImpl.stmt("void* lib = dlOpenFunc()")
        self.cgenImpl.stmt("if (!lib) return")

        apis = \
            self.apisToGet["global"] + \
            self.apisToGet["global-instance"] + \
            self.apisToGet["instance"] + \
            self.apisToGet["device"] + \
            self.apisToGet["global-only"]

        for vulkanApi, typeDecl, feature in apis:
            self.syncFeature(self.cgenImpl, feature)
            self.makeDlsymCall(self.cgenImpl, vulkanApi.name, typeDecl)

        self.syncFeature(self.cgenImpl, "")
        self.cgenImpl.endBlock()

        # Getting instance dispatch tables
        self.cgenImpl.line("""
void init_vulkan_dispatch_from_instance(
    VulkanDispatch* vk,
    VkInstance instance,
    VulkanDispatch* out)""")

        self.cgenImpl.beginBlock()

        self.cgenImpl.stmt("memset(out, 0x0, sizeof(VulkanDispatch))")

        apis = \
            self.apisToGet["global"] + \
            self.apisToGet["global-instance"] + \
            self.apisToGet["instance"] + \
            self.apisToGet["device"]

        for vulkanApi, typeDecl, feature in apis:
            self.syncFeature(self.cgenImpl, feature)
            self.makeGetInstanceProcAddrCall(
                self.cgenImpl, "vk", "instance", vulkanApi.name, typeDecl)

        self.syncFeature(self.cgenImpl, "")
        self.cgenImpl.endBlock()

        # Getting device dispatch tables
        self.cgenImpl.line("""
void init_vulkan_dispatch_from_device(
    VulkanDispatch* vk,
    VkDevice device,
    VulkanDispatch* out)""")

        self.cgenImpl.beginBlock()

        self.cgenImpl.stmt("memset(out, 0x0, sizeof(VulkanDispatch))")

        apis = \
            self.apisToGet["global"] + \
            self.apisToGet["global-instance"] + \
            self.apisToGet["instance"] + \
            self.apisToGet["device"]

        for vulkanApi, typeDecl, feature in apis:
            self.syncFeature(self.cgenImpl, feature)
            self.makeGetDeviceProcAddrCall(
                self.cgenImpl, "vk", "device", vulkanApi.name, typeDecl)

        self.syncFeature(self.cgenImpl, "")
        self.cgenImpl.endBlock()

        # Check Vulkan 1.0 / 1.1 functions

        def emit_check_impl(cgen, dispatchVar, feature, featureToCheck, apiName):
            if feature == featureToCheck:
                cgen.beginIf("!%s->%s" % (dispatchVar, apiName))
                cgen.stmt("fprintf(stderr, \"%s check failed: %s not found\\n\")" % (featureToCheck, apiName))
                cgen.stmt("good = false")
                cgen.endIf()

        def emit_feature_check_impl(context, cgen, tag, featureToCheck, apis):
            cgen.line("""
bool vulkan_dispatch_check_%s_%s(
    const VulkanDispatch* vk)
""" % (tag, featureToCheck))

            cgen.beginBlock()

            cgen.stmt("bool good = true")

            for vulkanApi, typeDecl, feature in apis:
                context.syncFeatureQuiet(self.cgenImpl, feature)
                emit_check_impl(cgen, "vk", feature, featureToCheck, vulkanApi.name)

            context.syncFeatureQuiet(self.cgenImpl, "")

            cgen.stmt("return good")
            cgen.endBlock()

        instanceApis = self.apisToGet["global-instance"] + self.apisToGet["instance"]

        emit_feature_check_impl(self, self.cgenImpl, "instance", "VK_VERSION_1_0", instanceApis)
        emit_feature_check_impl(self, self.cgenImpl, "instance", "VK_VERSION_1_1", instanceApis)
        emit_feature_check_impl(self, self.cgenImpl, "device", "VK_VERSION_1_0", self.apisToGet["device"])
        emit_feature_check_impl(self, self.cgenImpl, "device", "VK_VERSION_1_1", self.apisToGet["device"])

        self.module.appendImpl(self.cgenImpl.swapCode())

    def onBeginFeature(self, featureName, featureType):
        self.currentFeature = featureName

    def onGenType(self, typeXml, name, alias):
        VulkanWrapperGenerator.onGenType(self, typeXml, name, alias)

    def onGenCmd(self, cmdinfo, name, alias):
        VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias)

        vulkanApi = self.typeInfo.apis[name]

        typeDecl = "PFN_%s" % name

        procAddressType = inferProcAddressFuncType(vulkanApi)

        self.cgenHeader.stmt("%s %s" % (typeDecl, name));
        self.module.appendHeader(self.cgenHeader.swapCode())

        current = self.apisToGet.get(procAddressType, [])
        if current == []:
            self.apisToGet[procAddressType] = current
        current.append((vulkanApi, typeDecl, self.currentFeature))

# VulkanDispatchFast allows one to get the optimal function pointers
# for a given Vulkan API call, in order to improve performance.
#
# We can optionally query VkDevices to get function pointers that are
# closer to the ICD and have fewer levels of indirection from the loader
# to get there.
# See
# https://github.com/KhronosGroup/Vulkan-Loader/blob/master/loader/LoaderAndLayerInterface.md
# for more info.
#
# This requires the calling C++ code to provide functions to
# generate the desired instances and devices, otherwise we won't know
# which instance or device to pass to vkGet(Instance|Device)ProcAddr,
# so it does push more complexity to the user.
class VulkanDispatchFast(VulkanDispatch):

    def __init__(self, module, typeInfo):
        VulkanDispatch.__init__(self, module, typeInfo)

    def onBegin(self):
        self.cgenHeader.line("""
void init_vulkan_dispatch_from_system_loader(
    DlOpenFunc dlOpenFunc,
    DlSymFunc dlSymFunc,
    InstanceGetter instanceGetter,
    DeviceGetter deviceGetter,
    VulkanDispatch* dispatch_out);
""")

        self.cgenHeader.line("struct VulkanDispatch {")
        self.cgenHeader.line("VkInstance instance;")
        self.cgenHeader.line("VkPhysicalDevice physicalDevice;")
        self.cgenHeader.line("uint32_t physicalDeviceQueueFamilyInfoCount;")
        self.cgenHeader.line("VkQueueFamilyProperties* physicalDeviceQueueFamilyInfos;")
        self.cgenHeader.line("VkDevice device;")
        self.cgenHeader.line("bool presentCapable;")
        self.module.appendHeader(self.cgenHeader.swapCode())

    def makeGetProcAddr(self, cgen, dispatchLevel, dispatch, apiname, typedecl):
        if dispatchLevel == "instance":
            funcname = "vkGetInstanceProcAddr"
        elif dispatchLevel == "device":
            funcname = "vkGetDeviceProcAddr"
        else:
            raise

        cgen.stmt( \
            "out->%s = (%s)out->%s(%s, \"%s\")" % \
            (apiname, typedecl, funcname, dispatch, apiname))

    def onEnd(self):
        self.cgenHeader.line("};")
        self.module.appendHeader(self.cgenHeader.swapCode())

        self.cgenImpl.line("""
void init_vulkan_dispatch_from_system_loader(
    DlOpenFunc dlOpenFunc,
    DlSymFunc dlSymFunc,
    InstanceGetter instanceGetter,
    DeviceGetter deviceGetter,
    VulkanDispatch* out)""")

        self.cgenImpl.beginBlock()

        self.cgenImpl.stmt("out->instance = nullptr")
        self.cgenImpl.stmt("out->physicalDevice = nullptr")
        self.cgenImpl.stmt("out->physicalDeviceQueueFamilyInfoCount = 0")
        self.cgenImpl.stmt("out->physicalDeviceQueueFamilyInfos = nullptr")
        self.cgenImpl.stmt("out->device = nullptr")
        self.cgenImpl.stmt("out->presentCapable = false")

        self.cgenImpl.stmt("void* lib = dlOpenFunc()")
        self.cgenImpl.stmt("if (!lib) return")

        for vulkanApi, typeDecl, feature in self.apisToGet["global"]:
            self.syncFeature(self.cgenImpl, feature)
            self.makeDlsymCall(self.cgenImpl, vulkanApi.name, typeDecl)

        self.syncFeature(self.cgenImpl, "")
        self.cgenImpl.stmt("if (!out->vkGetInstanceProcAddr) return")

        for vulkanApi, typeDecl, feature in self.apisToGet["global-instance"]:
            self.syncFeature(self.cgenImpl, feature)
            self.makeGetProcAddr( \
                self.cgenImpl, "instance", "nullptr", vulkanApi.name, typeDecl);

        self.syncFeature(self.cgenImpl, "")
        self.cgenImpl.stmt("if (!instanceGetter(out, &out->instance)) return")

        for vulkanApi, typeDecl, feature in self.apisToGet["instance"]:
            self.syncFeature(self.cgenImpl, feature)
            self.makeGetProcAddr( \
                self.cgenImpl, "instance", "out->instance", vulkanApi.name, typeDecl);

        self.syncFeature(self.cgenImpl, "")

        self.cgenImpl.stmt("if (!deviceGetter(out, out->instance, &out->physicalDevice, &out->physicalDeviceQueueFamilyInfoCount, nullptr, &out->device, &out->presentCapable)) return")
        self.cgenImpl.stmt("out->physicalDeviceQueueFamilyInfos = (VkQueueFamilyProperties*)malloc(out->physicalDeviceQueueFamilyInfoCount * sizeof(VkQueueFamilyProperties))");
        self.cgenImpl.stmt("if (!deviceGetter(out, out->instance, &out->physicalDevice, &out->physicalDeviceQueueFamilyInfoCount, out->physicalDeviceQueueFamilyInfos, &out->device, &out->presentCapable)) return")

        for vulkanApi, typeDecl, feature in self.apisToGet["device"]:
            self.syncFeature(self.cgenImpl, feature)
            self.makeGetProcAddr( \
                self.cgenImpl, "device", "out->device", vulkanApi.name, typeDecl);

        self.syncFeature(self.cgenImpl, "")

        self.cgenImpl.endBlock()

        self.module.appendImpl(self.cgenImpl.swapCode())

    def onBeginFeature(self, featureName, featureType):
        VulkanDispatch.onBeginFeature(self, featureName, featureType);

    def onGenType(self, typeXml, name, alias):
        VulkanDispatch.onGenType(self, typeXml, name, alias);

    def onGenCmd(self, cmdinfo, name, alias):
        VulkanDispatch.onGenCmd(self, cmdinfo, name, alias);