blob: 0da826f3d7aa2ed5cb3cb49adf5e8d8a4f86ff40 [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:
Jon Ashburnffd5d672015-06-29 11:25:34 -060027 * Jon Ashburn <jon@luanrg.com>
Ian Elliott81ac44c2015-01-13 17:52:38 -070028 * Ian Elliott <ian@lunarg.com>
29 */
30
31#ifndef LOADER_PLATFORM_H
32#define LOADER_PLATFORM_H
33
34#if defined(__linux__)
35/* Linux-specific common code: */
36
37// Headers:
38//#define _GNU_SOURCE 1
39// TBD: Are the contents of the following file used?
40#include <unistd.h>
41// Note: The following file is for dynamic loading:
42#include <dlfcn.h>
43#include <pthread.h>
44#include <assert.h>
Jon Ashburnffd5d672015-06-29 11:25:34 -060045#include <stdbool.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -070046
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060047// VK Library Filenames, Paths, etc.:
Ian Elliott665c5632015-02-04 11:22:39 -070048#define PATH_SEPERATOR ':'
Jon Ashburnffd5d672015-06-29 11:25:34 -060049#define DIRECTORY_SYMBOL '/'
50
51// TODO: Need to handle different Linux distros
52#define DEFAULT_VK_DRIVERS_INFO "/usr/share/vulkan/icd.d:/etc/vulkan/icd.d"
53#define DEFAULT_VK_DRIVERS_PATH "/usr/lib/i386-linux-gnu/vulkan/icd:/usr/lib/x86_64-linux-gnu/vulkan/icd"
Jon Ashburn1b111de2015-07-06 15:40:35 -060054#define DEFAULT_VK_LAYERS_INFO "/usr/share/vulkan/explicit_layer.d:/usr/share/vulkan/implicit_layer.d:/etc/vulkan/explicit_layer.d:/etc/vulkan/implicit_layer.d"
55#define LAYERS_PATH_ENV "VK_LAYER_DIRS"
Ian Elliott665c5632015-02-04 11:22:39 -070056
Ian Elliottecb60ec2015-02-12 16:44:56 -070057// C99:
Tobin Ehlisbf0146e2015-02-11 14:24:02 -070058#define PRINTF_SIZE_T_SPECIFIER "%zu"
59
Jon Ashburnffd5d672015-06-29 11:25:34 -060060// File IO
61static inline bool loader_platform_file_exists(const char *path)
62{
63 if (access(path, F_OK))
64 return false;
65 else
66 return true;
67}
68
69// Dynamic Loading of libraries:
Ian Elliott81ac44c2015-01-13 17:52:38 -070070typedef void * loader_platform_dl_handle;
71static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath)
72{
Chia-I Wu7397c432015-02-18 14:39:54 -070073 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott81ac44c2015-01-13 17:52:38 -070074}
75static inline char * loader_platform_open_library_error(const char* libPath)
76{
77 return dlerror();
78}
79static inline void loader_platform_close_library(loader_platform_dl_handle library)
80{
81 dlclose(library);
82}
83static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library,
84 const char *name)
85{
86 assert(library);
87 assert(name);
88 return dlsym(library, name);
89}
90static inline char * loader_platform_get_proc_address_error(const char *name)
91{
92 return dlerror();
93}
94
95// Threads:
96typedef pthread_t loader_platform_thread;
97#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
98 pthread_once_t var = PTHREAD_ONCE_INIT;
Jon Ashburnfce93d92015-05-12 17:26:48 -060099#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \
100 pthread_once_t var;
Ian Elliott81ac44c2015-01-13 17:52:38 -0700101static inline void loader_platform_thread_once(void *ctl, void (* func) (void))
102{
103 assert(func != NULL);
104 assert(ctl != NULL);
105 pthread_once((pthread_once_t *) ctl, func);
106}
107
108// Thread IDs:
109typedef pthread_t loader_platform_thread_id;
110static inline loader_platform_thread_id loader_platform_get_thread_id()
111{
112 return pthread_self();
113}
114
115// Thread mutex:
116typedef pthread_mutex_t loader_platform_thread_mutex;
117static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
118{
119 pthread_mutex_init(pMutex, NULL);
120}
121static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
122{
123 pthread_mutex_lock(pMutex);
124}
125static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
126{
127 pthread_mutex_unlock(pMutex);
128}
129static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
130{
131 pthread_mutex_destroy(pMutex);
132}
Mike Stroyan354ed672015-05-15 08:50:57 -0600133typedef pthread_cond_t loader_platform_thread_cond;
134static inline void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond)
135{
136 pthread_cond_init(pCond, NULL);
137}
138static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex)
139{
140 pthread_cond_wait(pCond, pMutex);
141}
142static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond)
143{
144 pthread_cond_broadcast(pCond);
145}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700146
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -0600147#define loader_stack_alloc(size) alloca(size)
148static inline void *loader_aligned_alloc(size_t alignment, size_t size) { return aligned_alloc(alignment, size); }
Ian Elliott81ac44c2015-01-13 17:52:38 -0700149
150#elif defined(_WIN32) // defined(__linux__)
151/* Windows-specific common code: */
152
153// Headers:
Piers Danielle2bca482015-02-24 13:58:47 -0700154#include <WinSock2.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -0700155#include <windows.h>
156#include <assert.h>
Tony Barbour69698512015-06-18 16:29:32 -0600157#include <stdio.h>
Jon Ashburnee33ae72015-06-30 14:46:22 -0700158#include <io.h>
159#include <stdbool.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -0700160#ifdef __cplusplus
161#include <iostream>
162#include <string>
163using namespace std;
164#endif // __cplusplus
165
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600166// VK Library Filenames, Paths, etc.:
Ian Elliott665c5632015-02-04 11:22:39 -0700167#define PATH_SEPERATOR ';'
Jon Ashburnffd5d672015-06-29 11:25:34 -0600168#define DIRECTORY_SYMBOL '\\'
Jon Ashburnee33ae72015-06-30 14:46:22 -0700169#define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE
170#define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers"
Jon Ashburnffd5d672015-06-29 11:25:34 -0600171// TODO: Are these the correct paths
scygan8420b4c2015-06-01 19:47:08 +0200172#define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64"
Jon Ashburn1b111de2015-07-06 15:40:35 -0600173#define DEFAULT_VK_LAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers;SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers"
174#define LAYERS_PATH_ENV "VK_LAYERS_FOLDERS"
Ian Elliott665c5632015-02-04 11:22:39 -0700175
Ian Elliott81ac44c2015-01-13 17:52:38 -0700176// C99:
177// Microsoft didn't implement C99 in Visual Studio; but started adding it with
178// VS2013. However, VS2013 still didn't have snprintf(). The following is a
Ian Elliott64f74a82015-02-04 12:06:46 -0700179// work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the
180// "CMakeLists.txt" file).
Ian Elliott81ac44c2015-01-13 17:52:38 -0700181#define snprintf _snprintf
Tobin Ehlisbf0146e2015-02-11 14:24:02 -0700182#define PRINTF_SIZE_T_SPECIFIER "%Iu"
David Pinedo3053c672015-07-02 09:04:37 -0600183
184// Microsoft doesn't implement alloca, instead we have _alloca
185#define alloca _alloca
186
Ian Elliott81ac44c2015-01-13 17:52:38 -0700187// Microsoft also doesn't have basename(). Paths are different on Windows, and
188// so this is just a temporary solution in order to get us compiling, so that we
189// can test some scenarios, and develop the correct solution for Windows.
190 // TODO: Develop a better, permanent solution for Windows, to replace this
191 // temporary code:
192static char *basename(char *pathname)
193{
194 char *current, *next;
195
Ian Elliott81ac44c2015-01-13 17:52:38 -0700196// TODO/TBD: Do we need to deal with the Windows's ":" character?
197
Ian Elliott665c5632015-02-04 11:22:39 -0700198#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700199 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott665c5632015-02-04 11:22:39 -0700200 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700201 if (next == NULL) {
Ian Elliott665c5632015-02-04 11:22:39 -0700202 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700203 return current;
204 } else {
Ian Elliott665c5632015-02-04 11:22:39 -0700205 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700206 next++;
207 }
208 }
Ian Elliott64f74a82015-02-04 12:06:46 -0700209 // We shouldn't get to here, but this makes the compiler happy:
210 return current;
Ian Elliott81ac44c2015-01-13 17:52:38 -0700211}
212
Jon Ashburnffd5d672015-06-29 11:25:34 -0600213// File IO
Jon Ashburnee33ae72015-06-30 14:46:22 -0700214static bool loader_platform_file_exists(const char *path)
Jon Ashburnffd5d672015-06-29 11:25:34 -0600215{
Jon Ashburnee33ae72015-06-30 14:46:22 -0700216 if ((_access(path, 0)) == -1)
Jon Ashburnffd5d672015-06-29 11:25:34 -0600217 return false;
218 else
219 return true;
220}
221
Ian Elliott81ac44c2015-01-13 17:52:38 -0700222// 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
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -0600374#define loader_stack_alloc(size) _alloca(size)
375static inline void *loader_aligned_alloc(size_t alignment, size_t size) { return _aligned_alloc(alignment, size); }
Ian Elliott20f06872015-02-12 17:08:34 -0700376
377#endif // defined(_WIN32)
378#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott81ac44c2015-01-13 17:52:38 -0700379#endif /* LOADER_PLATFORM_H */