blob: 66b24cb3ba072df460ffdebb9286dbafd70adfdd [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;
Jon Ashburnfce93d92015-05-12 17:26:48 -0600100#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \
101 pthread_once_t var;
Ian Elliott81ac44c2015-01-13 17:52:38 -0700102static inline void loader_platform_thread_once(void *ctl, void (* func) (void))
103{
104 assert(func != NULL);
105 assert(ctl != NULL);
106 pthread_once((pthread_once_t *) ctl, func);
107}
108
109// Thread IDs:
110typedef pthread_t loader_platform_thread_id;
111static inline loader_platform_thread_id loader_platform_get_thread_id()
112{
113 return pthread_self();
114}
115
116// Thread mutex:
117typedef pthread_mutex_t loader_platform_thread_mutex;
118static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
119{
120 pthread_mutex_init(pMutex, NULL);
121}
122static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
123{
124 pthread_mutex_lock(pMutex);
125}
126static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
127{
128 pthread_mutex_unlock(pMutex);
129}
130static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
131{
132 pthread_mutex_destroy(pMutex);
133}
Mike Stroyan354ed672015-05-15 08:50:57 -0600134typedef pthread_cond_t loader_platform_thread_cond;
135static inline void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond)
136{
137 pthread_cond_init(pCond, NULL);
138}
139static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex)
140{
141 pthread_cond_wait(pCond, pMutex);
142}
143static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond)
144{
145 pthread_cond_broadcast(pCond);
146}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700147
148
149#elif defined(_WIN32) // defined(__linux__)
150/* Windows-specific common code: */
151
152// Headers:
Piers Danielle2bca482015-02-24 13:58:47 -0700153#include <WinSock2.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -0700154#include <windows.h>
155#include <assert.h>
156#ifdef __cplusplus
157#include <iostream>
158#include <string>
159using namespace std;
160#endif // __cplusplus
161
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600162// VK Library Filenames, Paths, etc.:
Ian Elliott665c5632015-02-04 11:22:39 -0700163#define PATH_SEPERATOR ';'
164#define DIRECTORY_SYMBOL "\\"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600165#define DRIVER_PATH_REGISTRY_VALUE "VK_DRIVERS_PATH"
166#define LAYERS_PATH_REGISTRY_VALUE "VK_LAYERS_PATH"
167#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
168#define DRIVER_PATH_ENV "VK_DRIVERS_PATH"
169#define LAYERS_PATH_ENV "VK_LAYERS_PATH"
170#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
171#ifndef DEFAULT_VK_DRIVERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -0700172// TODO: Is this a good default location?
173// Need to search for both 32bit and 64bit ICDs
scygan8420b4c2015-06-01 19:47:08 +0200174#define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64"
Ian Elliott665c5632015-02-04 11:22:39 -0700175// TODO/TBD: Is this an appropriate prefix for Windows?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600176#define VK_DRIVER_LIBRARY_PREFIX "VK_"
Ian Elliott10ec9622015-04-09 18:07:15 -0600177#define VK_DRIVER_LIBRARY_PREFIX_LEN 3
Ian Elliott665c5632015-02-04 11:22:39 -0700178// TODO/TBD: Is this an appropriate suffix for Windows?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600179#define VK_LAYER_LIBRARY_PREFIX "VKLayer"
Jon Ashburnb67859d2015-04-24 14:10:50 -0700180#define VK_LAYER_LIBRARY_PREFIX_LEN 7
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600181#define VK_LIBRARY_SUFFIX ".dll"
182#define VK_LIBRARY_SUFFIX_LEN 4
183#endif // DEFAULT_VK_DRIVERS_PATH
184#ifndef DEFAULT_VK_LAYERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -0700185// TODO: Is this a good default location?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600186#define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32"
187#endif // DEFAULT_VK_LAYERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -0700188
Ian Elliott81ac44c2015-01-13 17:52:38 -0700189// C99:
190// Microsoft didn't implement C99 in Visual Studio; but started adding it with
191// VS2013. However, VS2013 still didn't have snprintf(). The following is a
Ian Elliott64f74a82015-02-04 12:06:46 -0700192// work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the
193// "CMakeLists.txt" file).
Ian Elliott81ac44c2015-01-13 17:52:38 -0700194#define snprintf _snprintf
Tobin Ehlisbf0146e2015-02-11 14:24:02 -0700195#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Ian Elliott81ac44c2015-01-13 17:52:38 -0700196// Microsoft also doesn't have basename(). Paths are different on Windows, and
197// so this is just a temporary solution in order to get us compiling, so that we
198// can test some scenarios, and develop the correct solution for Windows.
199 // TODO: Develop a better, permanent solution for Windows, to replace this
200 // temporary code:
201static char *basename(char *pathname)
202{
203 char *current, *next;
204
Ian Elliott81ac44c2015-01-13 17:52:38 -0700205// TODO/TBD: Do we need to deal with the Windows's ":" character?
206
Ian Elliott665c5632015-02-04 11:22:39 -0700207#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700208 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott665c5632015-02-04 11:22:39 -0700209 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700210 if (next == NULL) {
Ian Elliott665c5632015-02-04 11:22:39 -0700211 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700212 return current;
213 } else {
Ian Elliott665c5632015-02-04 11:22:39 -0700214 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700215 next++;
216 }
217 }
Ian Elliott64f74a82015-02-04 12:06:46 -0700218 // We shouldn't get to here, but this makes the compiler happy:
219 return current;
Ian Elliott81ac44c2015-01-13 17:52:38 -0700220}
221
222// Dynamic Loading:
223typedef HMODULE loader_platform_dl_handle;
224static loader_platform_dl_handle loader_platform_open_library(const char* libPath)
225{
226 return LoadLibrary(libPath);
227}
228static char * loader_platform_open_library_error(const char* libPath)
229{
230 static char errorMsg[120];
231 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
232 return errorMsg;
233}
234static void loader_platform_close_library(loader_platform_dl_handle library)
235{
236 FreeLibrary(library);
237}
238static void * loader_platform_get_proc_address(loader_platform_dl_handle library,
239 const char *name)
240{
241 assert(library);
242 assert(name);
243 return GetProcAddress(library, name);
244}
245static char * loader_platform_get_proc_address_error(const char *name)
246{
247 static char errorMsg[120];
248 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
249 return errorMsg;
250}
251
252// Threads:
253typedef HANDLE loader_platform_thread;
254#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
255 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Jon Ashburnfce93d92015-05-12 17:26:48 -0600256#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \
257 INIT_ONCE var;
Piers Daniell886be472015-02-23 16:23:13 -0700258static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
259{
260 void (*func)(void) = (void (*)(void))Parameter;
261 func();
262 return TRUE;
263}
264
Ian Elliott81ac44c2015-01-13 17:52:38 -0700265static void loader_platform_thread_once(void *ctl, void (* func) (void))
266{
267 assert(func != NULL);
268 assert(ctl != NULL);
Piers Daniell886be472015-02-23 16:23:13 -0700269 InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700270}
271
272// Thread IDs:
273typedef DWORD loader_platform_thread_id;
274static loader_platform_thread_id loader_platform_get_thread_id()
275{
276 return GetCurrentThreadId();
277}
278
279// Thread mutex:
280typedef CRITICAL_SECTION loader_platform_thread_mutex;
281static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
282{
283 InitializeCriticalSection(pMutex);
284}
285static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
286{
287 EnterCriticalSection(pMutex);
288}
289static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
290{
291 LeaveCriticalSection(pMutex);
292}
293static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
294{
295 DeleteCriticalSection(pMutex);
296}
Mike Stroyan33053d02015-05-15 17:34:51 -0600297typedef CONDITION_VARIABLE loader_platform_thread_cond;
298static void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond)
299{
300 InitializeConditionVariable(pCond);
301}
302static void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex)
303{
304 SleepConditionVariableCS(pCond, pMutex, INFINITE);
305}
306static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond)
307{
308 WakeAllConditionVariable(pCond);
309}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700310
Ian Elliott76005132015-03-31 15:32:41 -0600311// Windows Registry:
312char *loader_get_registry_string(const HKEY hive,
313 const LPCTSTR sub_key,
314 const char *value);
315
Ian Elliott81ac44c2015-01-13 17:52:38 -0700316#else // defined(_WIN32)
317
318#error The "loader_platform.h" file must be modified for this OS.
319
320// NOTE: In order to support another OS, an #elif needs to be added (above the
321// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
322// contents of this file must be created.
323
324// NOTE: Other OS-specific changes are also needed for this OS. Search for
325// files with "WIN32" in it, as a quick way to find files that must be changed.
326
327#endif // defined(_WIN32)
328
Ian Elliott20f06872015-02-12 17:08:34 -0700329#else /* LOADER_PLATFORM_H */
330#ifndef LOADER_PLATFORM_H_TEMP
331#define LOADER_PLATFORM_H_TEMP
332
333// NOTE: The following are hopefully-temporary macros to ensure that people
334// don't forget to use the loader_platform_*() functions above:
335
336#if defined(__linux__)
337/* Linux-specific common code: */
338
339// Dynamic Loading:
340#define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION
341#define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY
342#define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION
343#define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION
344
345// Threads:
346#define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION
347#define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION
348
349// Thread mutex:
350#define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
351#define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
352#define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
353#define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
354
355
356#elif defined(_WIN32) // defined(__linux__)
357/* Windows-specific common code: */
358
359// Dynamic Loading:
360//#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION
361#define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION
362#define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION
363
364// Threads:
365#define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION
366#define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION
367
368// Thread mutex:
369#define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
370#define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
371#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
372#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
373
374
375#endif // defined(_WIN32)
376#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott81ac44c2015-01-13 17:52:38 -0700377#endif /* LOADER_PLATFORM_H */