blob: a7bd0c64dc45ef58b1c9847aebe5912c4ac3d4f1 [file] [log] [blame]
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan
Ian Elliott2d4ab1e2015-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 Ashburn2077e382015-06-29 11:25:34 -060027 * Jon Ashburn <jon@luanrg.com>
Ian Elliott2d4ab1e2015-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 Ashburn2077e382015-06-29 11:25:34 -060045#include <stdbool.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070046
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060047// VK Library Filenames, Paths, etc.:
Ian Elliott457810e2015-02-04 11:22:39 -070048#define PATH_SEPERATOR ':'
Jon Ashburn2077e382015-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"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060054#define LAYERS_PATH_ENV "LIBVK_LAYERS_PATH"
55#define LAYER_NAMES_ENV "LIBVK_LAYER_NAMES"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060056#define VK_LAYER_LIBRARY_PREFIX "libVKLayer"
57#define VK_LAYER_LIBRARY_PREFIX_LEN 10
58#define VK_LIBRARY_SUFFIX ".so"
59#define VK_LIBRARY_SUFFIX_LEN 3
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060060#ifndef DEFAULT_VK_LAYERS_PATH
Ian Elliott457810e2015-02-04 11:22:39 -070061// TODO: Are these good default locations?
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060062#define DEFAULT_VK_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/vk:/usr/lib/x86_64-linux-gnu/vk"
Ian Elliott457810e2015-02-04 11:22:39 -070063#endif
64
Ian Elliotte5369462015-02-12 16:44:56 -070065// C99:
Tobin Ehlisb0497852015-02-11 14:24:02 -070066#define PRINTF_SIZE_T_SPECIFIER "%zu"
67
Jon Ashburn2077e382015-06-29 11:25:34 -060068// File IO
69static inline bool loader_platform_file_exists(const char *path)
70{
71 if (access(path, F_OK))
72 return false;
73 else
74 return true;
75}
76
77// Dynamic Loading of libraries:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070078typedef void * loader_platform_dl_handle;
79static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath)
80{
Chia-I Wu6a218342015-02-18 14:39:54 -070081 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070082}
83static inline char * loader_platform_open_library_error(const char* libPath)
84{
85 return dlerror();
86}
87static inline void loader_platform_close_library(loader_platform_dl_handle library)
88{
89 dlclose(library);
90}
91static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library,
92 const char *name)
93{
94 assert(library);
95 assert(name);
96 return dlsym(library, name);
97}
98static inline char * loader_platform_get_proc_address_error(const char *name)
99{
100 return dlerror();
101}
102
103// Threads:
104typedef pthread_t loader_platform_thread;
105#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
106 pthread_once_t var = PTHREAD_ONCE_INIT;
Jon Ashburn27cd5842015-05-12 17:26:48 -0600107#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \
108 pthread_once_t var;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700109static inline void loader_platform_thread_once(void *ctl, void (* func) (void))
110{
111 assert(func != NULL);
112 assert(ctl != NULL);
113 pthread_once((pthread_once_t *) ctl, func);
114}
115
116// Thread IDs:
117typedef pthread_t loader_platform_thread_id;
118static inline loader_platform_thread_id loader_platform_get_thread_id()
119{
120 return pthread_self();
121}
122
123// Thread mutex:
124typedef pthread_mutex_t loader_platform_thread_mutex;
125static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
126{
127 pthread_mutex_init(pMutex, NULL);
128}
129static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
130{
131 pthread_mutex_lock(pMutex);
132}
133static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
134{
135 pthread_mutex_unlock(pMutex);
136}
137static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
138{
139 pthread_mutex_destroy(pMutex);
140}
Mike Stroyaned238bb2015-05-15 08:50:57 -0600141typedef pthread_cond_t loader_platform_thread_cond;
142static inline void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond)
143{
144 pthread_cond_init(pCond, NULL);
145}
146static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex)
147{
148 pthread_cond_wait(pCond, pMutex);
149}
150static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond)
151{
152 pthread_cond_broadcast(pCond);
153}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700154
155
156#elif defined(_WIN32) // defined(__linux__)
157/* Windows-specific common code: */
158
159// Headers:
Piers Daniell188f8202015-02-24 13:58:47 -0700160#include <WinSock2.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700161#include <windows.h>
162#include <assert.h>
Tony Barbour1d825c72015-06-18 16:29:32 -0600163#include <stdio.h>
Jon Ashburnffad94d2015-06-30 14:46:22 -0700164#include <io.h>
165#include <stdbool.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700166#ifdef __cplusplus
167#include <iostream>
168#include <string>
169using namespace std;
170#endif // __cplusplus
171
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600172// VK Library Filenames, Paths, etc.:
Ian Elliott457810e2015-02-04 11:22:39 -0700173#define PATH_SEPERATOR ';'
Jon Ashburn2077e382015-06-29 11:25:34 -0600174#define DIRECTORY_SYMBOL '\\'
Jon Ashburnffad94d2015-06-30 14:46:22 -0700175#define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE
176#define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers"
Jon Ashburn2077e382015-06-29 11:25:34 -0600177// TODO: Are these the correct paths
scygan713b1b02015-06-01 19:47:08 +0200178#define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64"
Ian Elliott457810e2015-02-04 11:22:39 -0700179// TODO/TBD: Is this an appropriate prefix for Windows?
Jon Ashburn2077e382015-06-29 11:25:34 -0600180#define LAYERS_PATH_REGISTRY_VALUE "VK_LAYERS_PATH"
181#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
182#define LAYERS_PATH_ENV "VK_LAYERS_PATH"
183#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
Ian Elliott457810e2015-02-04 11:22:39 -0700184// TODO/TBD: Is this an appropriate suffix for Windows?
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600185#define VK_LAYER_LIBRARY_PREFIX "VKLayer"
Jon Ashburnae053e92015-04-24 14:10:50 -0700186#define VK_LAYER_LIBRARY_PREFIX_LEN 7
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600187#define VK_LIBRARY_SUFFIX ".dll"
188#define VK_LIBRARY_SUFFIX_LEN 4
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600189#ifndef DEFAULT_VK_LAYERS_PATH
Ian Elliott457810e2015-02-04 11:22:39 -0700190// TODO: Is this a good default location?
Jon Ashburn2077e382015-06-29 11:25:34 -0600191#define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600192#endif // DEFAULT_VK_LAYERS_PATH
Ian Elliott457810e2015-02-04 11:22:39 -0700193
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700194// C99:
195// Microsoft didn't implement C99 in Visual Studio; but started adding it with
196// VS2013. However, VS2013 still didn't have snprintf(). The following is a
Ian Elliott19628802015-02-04 12:06:46 -0700197// work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the
198// "CMakeLists.txt" file).
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700199#define snprintf _snprintf
Tobin Ehlisb0497852015-02-11 14:24:02 -0700200#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700201// Microsoft also doesn't have basename(). Paths are different on Windows, and
202// so this is just a temporary solution in order to get us compiling, so that we
203// can test some scenarios, and develop the correct solution for Windows.
204 // TODO: Develop a better, permanent solution for Windows, to replace this
205 // temporary code:
206static char *basename(char *pathname)
207{
208 char *current, *next;
209
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700210// TODO/TBD: Do we need to deal with the Windows's ":" character?
211
Ian Elliott457810e2015-02-04 11:22:39 -0700212#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700213 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott457810e2015-02-04 11:22:39 -0700214 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700215 if (next == NULL) {
Ian Elliott457810e2015-02-04 11:22:39 -0700216 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700217 return current;
218 } else {
Ian Elliott457810e2015-02-04 11:22:39 -0700219 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700220 next++;
221 }
222 }
Ian Elliott19628802015-02-04 12:06:46 -0700223 // We shouldn't get to here, but this makes the compiler happy:
224 return current;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700225}
226
Jon Ashburn2077e382015-06-29 11:25:34 -0600227// File IO
Jon Ashburnffad94d2015-06-30 14:46:22 -0700228static bool loader_platform_file_exists(const char *path)
Jon Ashburn2077e382015-06-29 11:25:34 -0600229{
Jon Ashburnffad94d2015-06-30 14:46:22 -0700230 if ((_access(path, 0)) == -1)
Jon Ashburn2077e382015-06-29 11:25:34 -0600231 return false;
232 else
233 return true;
234}
235
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700236// Dynamic Loading:
237typedef HMODULE loader_platform_dl_handle;
238static loader_platform_dl_handle loader_platform_open_library(const char* libPath)
239{
240 return LoadLibrary(libPath);
241}
242static char * loader_platform_open_library_error(const char* libPath)
243{
244 static char errorMsg[120];
245 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
246 return errorMsg;
247}
248static void loader_platform_close_library(loader_platform_dl_handle library)
249{
250 FreeLibrary(library);
251}
252static void * loader_platform_get_proc_address(loader_platform_dl_handle library,
253 const char *name)
254{
255 assert(library);
256 assert(name);
257 return GetProcAddress(library, name);
258}
259static char * loader_platform_get_proc_address_error(const char *name)
260{
261 static char errorMsg[120];
262 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
263 return errorMsg;
264}
265
266// Threads:
267typedef HANDLE loader_platform_thread;
268#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
269 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Jon Ashburn27cd5842015-05-12 17:26:48 -0600270#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \
271 INIT_ONCE var;
Piers Daniell4da523a2015-02-23 16:23:13 -0700272static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
273{
274 void (*func)(void) = (void (*)(void))Parameter;
275 func();
276 return TRUE;
277}
278
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700279static void loader_platform_thread_once(void *ctl, void (* func) (void))
280{
281 assert(func != NULL);
282 assert(ctl != NULL);
Piers Daniell4da523a2015-02-23 16:23:13 -0700283 InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700284}
285
286// Thread IDs:
287typedef DWORD loader_platform_thread_id;
288static loader_platform_thread_id loader_platform_get_thread_id()
289{
290 return GetCurrentThreadId();
291}
292
293// Thread mutex:
294typedef CRITICAL_SECTION loader_platform_thread_mutex;
295static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
296{
297 InitializeCriticalSection(pMutex);
298}
299static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
300{
301 EnterCriticalSection(pMutex);
302}
303static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
304{
305 LeaveCriticalSection(pMutex);
306}
307static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
308{
309 DeleteCriticalSection(pMutex);
310}
Mike Stroyanc3d98332015-05-15 17:34:51 -0600311typedef CONDITION_VARIABLE loader_platform_thread_cond;
312static void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond)
313{
314 InitializeConditionVariable(pCond);
315}
316static void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex)
317{
318 SleepConditionVariableCS(pCond, pMutex, INFINITE);
319}
320static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond)
321{
322 WakeAllConditionVariable(pCond);
323}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700324
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600325// Windows Registry:
326char *loader_get_registry_string(const HKEY hive,
327 const LPCTSTR sub_key,
328 const char *value);
329
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700330#else // defined(_WIN32)
331
332#error The "loader_platform.h" file must be modified for this OS.
333
334// NOTE: In order to support another OS, an #elif needs to be added (above the
335// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
336// contents of this file must be created.
337
338// NOTE: Other OS-specific changes are also needed for this OS. Search for
339// files with "WIN32" in it, as a quick way to find files that must be changed.
340
341#endif // defined(_WIN32)
342
Ian Elliott655cad72015-02-12 17:08:34 -0700343#else /* LOADER_PLATFORM_H */
344#ifndef LOADER_PLATFORM_H_TEMP
345#define LOADER_PLATFORM_H_TEMP
346
347// NOTE: The following are hopefully-temporary macros to ensure that people
348// don't forget to use the loader_platform_*() functions above:
349
350#if defined(__linux__)
351/* Linux-specific common code: */
352
353// Dynamic Loading:
354#define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION
355#define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY
356#define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION
357#define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION
358
359// Threads:
360#define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION
361#define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION
362
363// Thread mutex:
364#define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
365#define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
366#define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
367#define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
368
369
370#elif defined(_WIN32) // defined(__linux__)
371/* Windows-specific common code: */
372
373// Dynamic Loading:
374//#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION
375#define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION
376#define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION
377
378// Threads:
379#define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION
380#define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION
381
382// Thread mutex:
383#define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
384#define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
385#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
386#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
387
388
389#endif // defined(_WIN32)
390#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700391#endif /* LOADER_PLATFORM_H */