blob: 4bbd8b305be8f780abd735db4d0b507b9ca9d4c0 [file] [log] [blame]
Ian Elliott81ac44c2015-01-13 17:52:38 -07001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Ian Elliott81ac44c2015-01-13 17:52:38 -07003 *
4 * Copyright (C) 2015 LunarG, Inc.
5 * Copyright 2014 Valve Software
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Ian Elliott <ian@lunarg.com>
28 */
29
30#ifndef LOADER_PLATFORM_H
31#define LOADER_PLATFORM_H
32
33#if defined(__linux__)
34/* Linux-specific common code: */
35
36// Headers:
37//#define _GNU_SOURCE 1
38// TBD: Are the contents of the following file used?
39#include <unistd.h>
40// Note: The following file is for dynamic loading:
41#include <dlfcn.h>
42#include <pthread.h>
43#include <assert.h>
44
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060045// VK Library Filenames, Paths, etc.:
Ian Elliott665c5632015-02-04 11:22:39 -070046#define PATH_SEPERATOR ':'
47#define DIRECTORY_SYMBOL "/"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060048#define DRIVER_PATH_ENV "LIBVK_DRIVERS_PATH"
49#define LAYERS_PATH_ENV "LIBVK_LAYERS_PATH"
50#define LAYER_NAMES_ENV "LIBVK_LAYER_NAMES"
51#ifndef DEFAULT_VK_DRIVERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -070052// TODO: Is this a good default location?
53// Need to search for both 32bit and 64bit ICDs
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060054#define DEFAULT_VK_DRIVERS_PATH "/usr/lib/i386-linux-gnu/vk:/usr/lib/x86_64-linux-gnu/vk"
55#define VK_DRIVER_LIBRARY_PREFIX "libVK_"
56#define VK_DRIVER_LIBRARY_PREFIX_LEN 6
57#define VK_LAYER_LIBRARY_PREFIX "libVKLayer"
58#define VK_LAYER_LIBRARY_PREFIX_LEN 10
59#define VK_LIBRARY_SUFFIX ".so"
60#define VK_LIBRARY_SUFFIX_LEN 3
61#endif // DEFAULT_VK_DRIVERS_PATH
62#ifndef DEFAULT_VK_LAYERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -070063// TODO: Are these good default locations?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060064#define DEFAULT_VK_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/vk:/usr/lib/x86_64-linux-gnu/vk"
Ian Elliott665c5632015-02-04 11:22:39 -070065#endif
66
Ian Elliottecb60ec2015-02-12 16:44:56 -070067// C99:
Tobin Ehlisbf0146e2015-02-11 14:24:02 -070068#define PRINTF_SIZE_T_SPECIFIER "%zu"
69
Ian Elliott81ac44c2015-01-13 17:52:38 -070070// Dynamic Loading:
71typedef void * loader_platform_dl_handle;
72static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath)
73{
Chia-I Wu7397c432015-02-18 14:39:54 -070074 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott81ac44c2015-01-13 17:52:38 -070075}
76static inline char * loader_platform_open_library_error(const char* libPath)
77{
78 return dlerror();
79}
80static inline void loader_platform_close_library(loader_platform_dl_handle library)
81{
82 dlclose(library);
83}
84static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library,
85 const char *name)
86{
87 assert(library);
88 assert(name);
89 return dlsym(library, name);
90}
91static inline char * loader_platform_get_proc_address_error(const char *name)
92{
93 return dlerror();
94}
95
96// Threads:
97typedef pthread_t loader_platform_thread;
98#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
99 pthread_once_t var = PTHREAD_ONCE_INIT;
100static inline void loader_platform_thread_once(void *ctl, void (* func) (void))
101{
102 assert(func != NULL);
103 assert(ctl != NULL);
104 pthread_once((pthread_once_t *) ctl, func);
105}
106
107// Thread IDs:
108typedef pthread_t loader_platform_thread_id;
109static inline loader_platform_thread_id loader_platform_get_thread_id()
110{
111 return pthread_self();
112}
113
114// Thread mutex:
115typedef pthread_mutex_t loader_platform_thread_mutex;
116static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
117{
118 pthread_mutex_init(pMutex, NULL);
119}
120static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
121{
122 pthread_mutex_lock(pMutex);
123}
124static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
125{
126 pthread_mutex_unlock(pMutex);
127}
128static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
129{
130 pthread_mutex_destroy(pMutex);
131}
Mike Stroyan354ed672015-05-15 08:50:57 -0600132typedef pthread_cond_t loader_platform_thread_cond;
133static inline void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond)
134{
135 pthread_cond_init(pCond, NULL);
136}
137static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex)
138{
139 pthread_cond_wait(pCond, pMutex);
140}
141static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond)
142{
143 pthread_cond_broadcast(pCond);
144}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700145
146
147#elif defined(_WIN32) // defined(__linux__)
148/* Windows-specific common code: */
149
150// Headers:
Piers Danielle2bca482015-02-24 13:58:47 -0700151#include <WinSock2.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -0700152#include <windows.h>
153#include <assert.h>
154#ifdef __cplusplus
155#include <iostream>
156#include <string>
157using namespace std;
158#endif // __cplusplus
159
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600160// VK Library Filenames, Paths, etc.:
Ian Elliott665c5632015-02-04 11:22:39 -0700161#define PATH_SEPERATOR ';'
162#define DIRECTORY_SYMBOL "\\"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600163#define DRIVER_PATH_REGISTRY_VALUE "VK_DRIVERS_PATH"
164#define LAYERS_PATH_REGISTRY_VALUE "VK_LAYERS_PATH"
165#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
166#define DRIVER_PATH_ENV "VK_DRIVERS_PATH"
167#define LAYERS_PATH_ENV "VK_LAYERS_PATH"
168#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
169#ifndef DEFAULT_VK_DRIVERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -0700170// TODO: Is this a good default location?
171// Need to search for both 32bit and 64bit ICDs
scygan8420b4c2015-06-01 19:47:08 +0200172#define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64"
Ian Elliott665c5632015-02-04 11:22:39 -0700173// TODO/TBD: Is this an appropriate prefix for Windows?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600174#define VK_DRIVER_LIBRARY_PREFIX "VK_"
Ian Elliott10ec9622015-04-09 18:07:15 -0600175#define VK_DRIVER_LIBRARY_PREFIX_LEN 3
Ian Elliott665c5632015-02-04 11:22:39 -0700176// TODO/TBD: Is this an appropriate suffix for Windows?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600177#define VK_LAYER_LIBRARY_PREFIX "VKLayer"
Jon Ashburnb67859d2015-04-24 14:10:50 -0700178#define VK_LAYER_LIBRARY_PREFIX_LEN 7
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600179#define VK_LIBRARY_SUFFIX ".dll"
180#define VK_LIBRARY_SUFFIX_LEN 4
181#endif // DEFAULT_VK_DRIVERS_PATH
182#ifndef DEFAULT_VK_LAYERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -0700183// TODO: Is this a good default location?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600184#define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32"
185#endif // DEFAULT_VK_LAYERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -0700186
Ian Elliott81ac44c2015-01-13 17:52:38 -0700187// C99:
188// Microsoft didn't implement C99 in Visual Studio; but started adding it with
189// VS2013. However, VS2013 still didn't have snprintf(). The following is a
Ian Elliott64f74a82015-02-04 12:06:46 -0700190// work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the
191// "CMakeLists.txt" file).
Ian Elliott81ac44c2015-01-13 17:52:38 -0700192#define snprintf _snprintf
Tobin Ehlisbf0146e2015-02-11 14:24:02 -0700193#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Ian Elliott81ac44c2015-01-13 17:52:38 -0700194// Microsoft also doesn't have basename(). Paths are different on Windows, and
195// so this is just a temporary solution in order to get us compiling, so that we
196// can test some scenarios, and develop the correct solution for Windows.
197 // TODO: Develop a better, permanent solution for Windows, to replace this
198 // temporary code:
199static char *basename(char *pathname)
200{
201 char *current, *next;
202
Ian Elliott81ac44c2015-01-13 17:52:38 -0700203// TODO/TBD: Do we need to deal with the Windows's ":" character?
204
Ian Elliott665c5632015-02-04 11:22:39 -0700205#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700206 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott665c5632015-02-04 11:22:39 -0700207 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700208 if (next == NULL) {
Ian Elliott665c5632015-02-04 11:22:39 -0700209 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700210 return current;
211 } else {
Ian Elliott665c5632015-02-04 11:22:39 -0700212 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700213 next++;
214 }
215 }
Ian Elliott64f74a82015-02-04 12:06:46 -0700216 // We shouldn't get to here, but this makes the compiler happy:
217 return current;
Ian Elliott81ac44c2015-01-13 17:52:38 -0700218}
219
220// Dynamic Loading:
221typedef HMODULE loader_platform_dl_handle;
222static loader_platform_dl_handle loader_platform_open_library(const char* libPath)
223{
224 return LoadLibrary(libPath);
225}
226static char * loader_platform_open_library_error(const char* libPath)
227{
228 static char errorMsg[120];
229 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
230 return errorMsg;
231}
232static void loader_platform_close_library(loader_platform_dl_handle library)
233{
234 FreeLibrary(library);
235}
236static void * loader_platform_get_proc_address(loader_platform_dl_handle library,
237 const char *name)
238{
239 assert(library);
240 assert(name);
241 return GetProcAddress(library, name);
242}
243static char * loader_platform_get_proc_address_error(const char *name)
244{
245 static char errorMsg[120];
246 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
247 return errorMsg;
248}
249
250// Threads:
251typedef HANDLE loader_platform_thread;
252#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
253 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Piers Daniell886be472015-02-23 16:23:13 -0700254static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
255{
256 void (*func)(void) = (void (*)(void))Parameter;
257 func();
258 return TRUE;
259}
260
Ian Elliott81ac44c2015-01-13 17:52:38 -0700261static void loader_platform_thread_once(void *ctl, void (* func) (void))
262{
263 assert(func != NULL);
264 assert(ctl != NULL);
Piers Daniell886be472015-02-23 16:23:13 -0700265 InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700266}
267
268// Thread IDs:
269typedef DWORD loader_platform_thread_id;
270static loader_platform_thread_id loader_platform_get_thread_id()
271{
272 return GetCurrentThreadId();
273}
274
275// Thread mutex:
276typedef CRITICAL_SECTION loader_platform_thread_mutex;
277static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
278{
279 InitializeCriticalSection(pMutex);
280}
281static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
282{
283 EnterCriticalSection(pMutex);
284}
285static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
286{
287 LeaveCriticalSection(pMutex);
288}
289static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
290{
291 DeleteCriticalSection(pMutex);
292}
Mike Stroyan33053d02015-05-15 17:34:51 -0600293typedef CONDITION_VARIABLE loader_platform_thread_cond;
294static void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond)
295{
296 InitializeConditionVariable(pCond);
297}
298static void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex)
299{
300 SleepConditionVariableCS(pCond, pMutex, INFINITE);
301}
302static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond)
303{
304 WakeAllConditionVariable(pCond);
305}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700306
Ian Elliott76005132015-03-31 15:32:41 -0600307// Windows Registry:
308char *loader_get_registry_string(const HKEY hive,
309 const LPCTSTR sub_key,
310 const char *value);
311
Ian Elliott81ac44c2015-01-13 17:52:38 -0700312#else // defined(_WIN32)
313
314#error The "loader_platform.h" file must be modified for this OS.
315
316// NOTE: In order to support another OS, an #elif needs to be added (above the
317// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
318// contents of this file must be created.
319
320// NOTE: Other OS-specific changes are also needed for this OS. Search for
321// files with "WIN32" in it, as a quick way to find files that must be changed.
322
323#endif // defined(_WIN32)
324
Ian Elliott20f06872015-02-12 17:08:34 -0700325#else /* LOADER_PLATFORM_H */
326#ifndef LOADER_PLATFORM_H_TEMP
327#define LOADER_PLATFORM_H_TEMP
328
329// NOTE: The following are hopefully-temporary macros to ensure that people
330// don't forget to use the loader_platform_*() functions above:
331
332#if defined(__linux__)
333/* Linux-specific common code: */
334
335// Dynamic Loading:
336#define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION
337#define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY
338#define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION
339#define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION
340
341// Threads:
342#define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION
343#define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION
344
345// Thread mutex:
346#define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
347#define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
348#define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
349#define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
350
351
352#elif defined(_WIN32) // defined(__linux__)
353/* Windows-specific common code: */
354
355// Dynamic Loading:
356//#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION
357#define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION
358#define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION
359
360// Threads:
361#define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION
362#define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION
363
364// Thread mutex:
365#define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
366#define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
367#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
368#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
369
370
371#endif // defined(_WIN32)
372#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott81ac44c2015-01-13 17:52:38 -0700373#endif /* LOADER_PLATFORM_H */