1 // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
20 #elif defined(__APPLE__)
23 #elif defined(__ANDROID__)
26 #elif defined(__linux__)
30 # error Unimplemented platform
33 Driver::Driver() : vk_icdGetInstanceProcAddr(nullptr), dll(nullptr)
35 #define VK_GLOBAL(N, R, ...) N = nullptr;
36 #include "VkGlobalFuncs.hpp"
39 #define VK_INSTANCE(N, R, ...) N = nullptr;
40 #include "VkInstanceFuncs.hpp"
49 bool Driver::loadSwiftShader()
53 return load("../../build/Release_x64/vk_swiftshader.dll");
55 return load("../../build/Debug_x64/vk_swiftshader.dll");
58 return load("./build/Darwin/libvk_swiftshader.dylib");
60 return load("./build/Linux/libvk_swiftshader.so");
62 return load("libvk_swiftshader.so");
64 # error Unimplemented platform
68 bool Driver::loadSystem()
71 return load("libvulkan.so.1");
77 bool Driver::load(const char* path)
80 dll = LoadLibraryA(path);
81 #elif(OS_MAC || OS_LINUX || OS_ANDROID)
82 dll = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
91 // Is the driver an ICD?
92 if(!lookup(&vk_icdGetInstanceProcAddr, "vk_icdGetInstanceProcAddr"))
94 // Nope, attempt to use the loader version.
95 if(!lookup(&vk_icdGetInstanceProcAddr, "vkGetInstanceProcAddr"))
101 #define VK_GLOBAL(N, R, ...) \
102 if(auto pfn = vk_icdGetInstanceProcAddr(nullptr, #N)) \
104 N = reinterpret_cast<decltype(N)>(pfn); \
106 #include "VkGlobalFuncs.hpp"
112 void Driver::unload()
120 FreeLibrary((HMODULE)dll);
125 #define VK_GLOBAL(N, R, ...) N = nullptr;
126 #include "VkGlobalFuncs.hpp"
129 #define VK_INSTANCE(N, R, ...) N = nullptr;
130 #include "VkInstanceFuncs.hpp"
134 bool Driver::isLoaded() const
136 return dll != nullptr;
139 bool Driver::resolve(VkInstance instance)
146 #define VK_INSTANCE(N, R, ...) \
147 if(auto pfn = vk_icdGetInstanceProcAddr(instance, #N)) \
149 N = reinterpret_cast<decltype(N)>(pfn); \
155 #include "VkInstanceFuncs.hpp"
161 void* Driver::lookup(const char* name)
164 return GetProcAddress((HMODULE)dll, name);
165 #elif(OS_MAC || OS_LINUX || OS_ANDROID)
166 return dlsym(dll, name);