1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker
15*03ce13f7SAndroid Build Coastguard Worker #include <vulkan/vulkan_core.h>
16*03ce13f7SAndroid Build Coastguard Worker
17*03ce13f7SAndroid Build Coastguard Worker // Driver is used to load a Vulkan graphics driver and expose its functions.
18*03ce13f7SAndroid Build Coastguard Worker // It is used by the unit tests to test the SwiftShader driver and optionally
19*03ce13f7SAndroid Build Coastguard Worker // load a system vulkan driver to test against.
20*03ce13f7SAndroid Build Coastguard Worker class Driver
21*03ce13f7SAndroid Build Coastguard Worker {
22*03ce13f7SAndroid Build Coastguard Worker public:
23*03ce13f7SAndroid Build Coastguard Worker Driver();
24*03ce13f7SAndroid Build Coastguard Worker ~Driver();
25*03ce13f7SAndroid Build Coastguard Worker
26*03ce13f7SAndroid Build Coastguard Worker // loadSwiftShader attempts to load the SwiftShader vulkan driver.
27*03ce13f7SAndroid Build Coastguard Worker // returns true on success, false on failure.
28*03ce13f7SAndroid Build Coastguard Worker bool loadSwiftShader();
29*03ce13f7SAndroid Build Coastguard Worker
30*03ce13f7SAndroid Build Coastguard Worker // loadSystem attempts to load the system's vulkan driver.
31*03ce13f7SAndroid Build Coastguard Worker // returns true on success, false on failure.
32*03ce13f7SAndroid Build Coastguard Worker bool loadSystem();
33*03ce13f7SAndroid Build Coastguard Worker
34*03ce13f7SAndroid Build Coastguard Worker // load attempts to load the vulkan driver from the given path.
35*03ce13f7SAndroid Build Coastguard Worker // returns true on success, false on failure.
36*03ce13f7SAndroid Build Coastguard Worker bool load(const char *path);
37*03ce13f7SAndroid Build Coastguard Worker
38*03ce13f7SAndroid Build Coastguard Worker // unloads the currently loaded driver.
39*03ce13f7SAndroid Build Coastguard Worker // No-op if no driver is currently loaded.
40*03ce13f7SAndroid Build Coastguard Worker void unload();
41*03ce13f7SAndroid Build Coastguard Worker
42*03ce13f7SAndroid Build Coastguard Worker // isLoaded returns true if the driver is currently loaded.
43*03ce13f7SAndroid Build Coastguard Worker bool isLoaded() const;
44*03ce13f7SAndroid Build Coastguard Worker
45*03ce13f7SAndroid Build Coastguard Worker // resolve all the functions for the given VkInstance.
46*03ce13f7SAndroid Build Coastguard Worker bool resolve(VkInstance);
47*03ce13f7SAndroid Build Coastguard Worker
48*03ce13f7SAndroid Build Coastguard Worker VKAPI_ATTR PFN_vkVoidFunction(VKAPI_CALL *vk_icdGetInstanceProcAddr)(VkInstance instance, const char *pName);
49*03ce13f7SAndroid Build Coastguard Worker
50*03ce13f7SAndroid Build Coastguard Worker // Global vulkan function pointers.
51*03ce13f7SAndroid Build Coastguard Worker #define VK_GLOBAL(N, R, ...) VKAPI_ATTR R(VKAPI_CALL *N)(__VA_ARGS__)
52*03ce13f7SAndroid Build Coastguard Worker #include "VkGlobalFuncs.hpp"
53*03ce13f7SAndroid Build Coastguard Worker #undef VK_GLOBAL
54*03ce13f7SAndroid Build Coastguard Worker
55*03ce13f7SAndroid Build Coastguard Worker // Per-instance vulkan function pointers.
56*03ce13f7SAndroid Build Coastguard Worker #define VK_INSTANCE(N, R, ...) VKAPI_ATTR R(VKAPI_CALL *N)(__VA_ARGS__)
57*03ce13f7SAndroid Build Coastguard Worker #include "VkInstanceFuncs.hpp"
58*03ce13f7SAndroid Build Coastguard Worker #undef VK_INSTANCE
59*03ce13f7SAndroid Build Coastguard Worker
60*03ce13f7SAndroid Build Coastguard Worker private:
61*03ce13f7SAndroid Build Coastguard Worker Driver(const Driver &) = delete;
62*03ce13f7SAndroid Build Coastguard Worker Driver(Driver &&) = delete;
63*03ce13f7SAndroid Build Coastguard Worker Driver &operator=(const Driver &) = delete;
64*03ce13f7SAndroid Build Coastguard Worker
65*03ce13f7SAndroid Build Coastguard Worker // lookup searches the loaded driver for a symbol with the given name,
66*03ce13f7SAndroid Build Coastguard Worker // returning the address of this symbol if found, otherwise nullptr.
67*03ce13f7SAndroid Build Coastguard Worker void *lookup(const char *name);
68*03ce13f7SAndroid Build Coastguard Worker
69*03ce13f7SAndroid Build Coastguard Worker // Helper function to lookup a symbol and cast it to the appropriate type.
70*03ce13f7SAndroid Build Coastguard Worker // Returns true if the symbol was found and assigned to ptr, otherwise
71*03ce13f7SAndroid Build Coastguard Worker // returns false.
72*03ce13f7SAndroid Build Coastguard Worker template<typename T>
73*03ce13f7SAndroid Build Coastguard Worker inline bool lookup(T *ptr, const char *name);
74*03ce13f7SAndroid Build Coastguard Worker
75*03ce13f7SAndroid Build Coastguard Worker void *dll;
76*03ce13f7SAndroid Build Coastguard Worker };
77*03ce13f7SAndroid Build Coastguard Worker
78*03ce13f7SAndroid Build Coastguard Worker template<typename T>
lookup(T * ptr,const char * name)79*03ce13f7SAndroid Build Coastguard Worker bool Driver::lookup(T *ptr, const char *name)
80*03ce13f7SAndroid Build Coastguard Worker {
81*03ce13f7SAndroid Build Coastguard Worker void *sym = lookup(name);
82*03ce13f7SAndroid Build Coastguard Worker if(sym == nullptr)
83*03ce13f7SAndroid Build Coastguard Worker {
84*03ce13f7SAndroid Build Coastguard Worker return false;
85*03ce13f7SAndroid Build Coastguard Worker }
86*03ce13f7SAndroid Build Coastguard Worker *ptr = reinterpret_cast<T>(sym);
87*03ce13f7SAndroid Build Coastguard Worker return true;
88*03ce13f7SAndroid Build Coastguard Worker }