blob: 693a3dcfd3c4bb7a35eb955e320151fc22d79b08 [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"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060054#define LAYERS_PATH_ENV "LIBVK_LAYERS_PATH"
55#define LAYER_NAMES_ENV "LIBVK_LAYER_NAMES"
Courtney Goeltzenleuchter9cc421e2015-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060060#ifndef DEFAULT_VK_LAYERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -070061// TODO: Are these good default locations?
Courtney Goeltzenleuchter9cc421e2015-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 Elliott665c5632015-02-04 11:22:39 -070063#endif
64
Ian Elliottecb60ec2015-02-12 16:44:56 -070065// C99:
Tobin Ehlisbf0146e2015-02-11 14:24:02 -070066#define PRINTF_SIZE_T_SPECIFIER "%zu"
67
Jon Ashburnffd5d672015-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 Elliott81ac44c2015-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 Wu7397c432015-02-18 14:39:54 -070081 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott81ac44c2015-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 Ashburnfce93d92015-05-12 17:26:48 -0600107#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \
108 pthread_once_t var;
Ian Elliott81ac44c2015-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 Stroyan354ed672015-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 Elliott81ac44c2015-01-13 17:52:38 -0700154
155
156#elif defined(_WIN32) // defined(__linux__)
157/* Windows-specific common code: */
158
159// Headers:
Piers Danielle2bca482015-02-24 13:58:47 -0700160#include <WinSock2.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -0700161#include <windows.h>
162#include <assert.h>
Tony Barbour69698512015-06-18 16:29:32 -0600163#include <stdio.h>
Jon Ashburnee33ae72015-06-30 14:46:22 -0700164#include <io.h>
165#include <stdbool.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -0700166#ifdef __cplusplus
167#include <iostream>
168#include <string>
169using namespace std;
170#endif // __cplusplus
171
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600172// VK Library Filenames, Paths, etc.:
Ian Elliott665c5632015-02-04 11:22:39 -0700173#define PATH_SEPERATOR ';'
Jon Ashburnffd5d672015-06-29 11:25:34 -0600174#define DIRECTORY_SYMBOL '\\'
Jon Ashburnee33ae72015-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 Ashburnffd5d672015-06-29 11:25:34 -0600177// TODO: Are these the correct paths
scygan8420b4c2015-06-01 19:47:08 +0200178#define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64"
Ian Elliott665c5632015-02-04 11:22:39 -0700179// TODO/TBD: Is this an appropriate prefix for Windows?
Jon Ashburnffd5d672015-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 Elliott665c5632015-02-04 11:22:39 -0700184// TODO/TBD: Is this an appropriate suffix for Windows?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600185#define VK_LAYER_LIBRARY_PREFIX "VKLayer"
Jon Ashburnb67859d2015-04-24 14:10:50 -0700186#define VK_LAYER_LIBRARY_PREFIX_LEN 7
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600187#define VK_LIBRARY_SUFFIX ".dll"
188#define VK_LIBRARY_SUFFIX_LEN 4
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600189#ifndef DEFAULT_VK_LAYERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -0700190// TODO: Is this a good default location?
Jon Ashburnffd5d672015-06-29 11:25:34 -0600191#define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600192#endif // DEFAULT_VK_LAYERS_PATH
Ian Elliott665c5632015-02-04 11:22:39 -0700193
Ian Elliott81ac44c2015-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 Elliott64f74a82015-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 Elliott81ac44c2015-01-13 17:52:38 -0700199#define snprintf _snprintf
Tobin Ehlisbf0146e2015-02-11 14:24:02 -0700200#define PRINTF_SIZE_T_SPECIFIER "%Iu"
David Pinedo3053c672015-07-02 09:04:37 -0600201
202// Microsoft doesn't implement alloca, instead we have _alloca
203#define alloca _alloca
204
Ian Elliott81ac44c2015-01-13 17:52:38 -0700205// Microsoft also doesn't have basename(). Paths are different on Windows, and
206// so this is just a temporary solution in order to get us compiling, so that we
207// can test some scenarios, and develop the correct solution for Windows.
208 // TODO: Develop a better, permanent solution for Windows, to replace this
209 // temporary code:
210static char *basename(char *pathname)
211{
212 char *current, *next;
213
Ian Elliott81ac44c2015-01-13 17:52:38 -0700214// TODO/TBD: Do we need to deal with the Windows's ":" character?
215
Ian Elliott665c5632015-02-04 11:22:39 -0700216#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700217 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott665c5632015-02-04 11:22:39 -0700218 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700219 if (next == NULL) {
Ian Elliott665c5632015-02-04 11:22:39 -0700220 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700221 return current;
222 } else {
Ian Elliott665c5632015-02-04 11:22:39 -0700223 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700224 next++;
225 }
226 }
Ian Elliott64f74a82015-02-04 12:06:46 -0700227 // We shouldn't get to here, but this makes the compiler happy:
228 return current;
Ian Elliott81ac44c2015-01-13 17:52:38 -0700229}
230
Jon Ashburnffd5d672015-06-29 11:25:34 -0600231// File IO
Jon Ashburnee33ae72015-06-30 14:46:22 -0700232static bool loader_platform_file_exists(const char *path)
Jon Ashburnffd5d672015-06-29 11:25:34 -0600233{
Jon Ashburnee33ae72015-06-30 14:46:22 -0700234 if ((_access(path, 0)) == -1)
Jon Ashburnffd5d672015-06-29 11:25:34 -0600235 return false;
236 else
237 return true;
238}
239
Ian Elliott81ac44c2015-01-13 17:52:38 -0700240// Dynamic Loading:
241typedef HMODULE loader_platform_dl_handle;
242static loader_platform_dl_handle loader_platform_open_library(const char* libPath)
243{
244 return LoadLibrary(libPath);
245}
246static char * loader_platform_open_library_error(const char* libPath)
247{
248 static char errorMsg[120];
249 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
250 return errorMsg;
251}
252static void loader_platform_close_library(loader_platform_dl_handle library)
253{
254 FreeLibrary(library);
255}
256static void * loader_platform_get_proc_address(loader_platform_dl_handle library,
257 const char *name)
258{
259 assert(library);
260 assert(name);
261 return GetProcAddress(library, name);
262}
263static char * loader_platform_get_proc_address_error(const char *name)
264{
265 static char errorMsg[120];
266 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
267 return errorMsg;
268}
269
270// Threads:
271typedef HANDLE loader_platform_thread;
272#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
273 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Jon Ashburnfce93d92015-05-12 17:26:48 -0600274#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \
275 INIT_ONCE var;
Piers Daniell886be472015-02-23 16:23:13 -0700276static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
277{
278 void (*func)(void) = (void (*)(void))Parameter;
279 func();
280 return TRUE;
281}
282
Ian Elliott81ac44c2015-01-13 17:52:38 -0700283static void loader_platform_thread_once(void *ctl, void (* func) (void))
284{
285 assert(func != NULL);
286 assert(ctl != NULL);
Piers Daniell886be472015-02-23 16:23:13 -0700287 InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700288}
289
290// Thread IDs:
291typedef DWORD loader_platform_thread_id;
292static loader_platform_thread_id loader_platform_get_thread_id()
293{
294 return GetCurrentThreadId();
295}
296
297// Thread mutex:
298typedef CRITICAL_SECTION loader_platform_thread_mutex;
299static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
300{
301 InitializeCriticalSection(pMutex);
302}
303static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
304{
305 EnterCriticalSection(pMutex);
306}
307static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
308{
309 LeaveCriticalSection(pMutex);
310}
311static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
312{
313 DeleteCriticalSection(pMutex);
314}
Mike Stroyan33053d02015-05-15 17:34:51 -0600315typedef CONDITION_VARIABLE loader_platform_thread_cond;
316static void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond)
317{
318 InitializeConditionVariable(pCond);
319}
320static void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex)
321{
322 SleepConditionVariableCS(pCond, pMutex, INFINITE);
323}
324static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond)
325{
326 WakeAllConditionVariable(pCond);
327}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700328
Ian Elliott76005132015-03-31 15:32:41 -0600329// Windows Registry:
330char *loader_get_registry_string(const HKEY hive,
331 const LPCTSTR sub_key,
332 const char *value);
333
Ian Elliott81ac44c2015-01-13 17:52:38 -0700334#else // defined(_WIN32)
335
336#error The "loader_platform.h" file must be modified for this OS.
337
338// NOTE: In order to support another OS, an #elif needs to be added (above the
339// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
340// contents of this file must be created.
341
342// NOTE: Other OS-specific changes are also needed for this OS. Search for
343// files with "WIN32" in it, as a quick way to find files that must be changed.
344
345#endif // defined(_WIN32)
346
Ian Elliott20f06872015-02-12 17:08:34 -0700347#else /* LOADER_PLATFORM_H */
348#ifndef LOADER_PLATFORM_H_TEMP
349#define LOADER_PLATFORM_H_TEMP
350
351// NOTE: The following are hopefully-temporary macros to ensure that people
352// don't forget to use the loader_platform_*() functions above:
353
354#if defined(__linux__)
355/* Linux-specific common code: */
356
357// Dynamic Loading:
358#define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION
359#define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY
360#define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION
361#define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION
362
363// Threads:
364#define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION
365#define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION
366
367// Thread mutex:
368#define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
369#define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
370#define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
371#define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
372
373
374#elif defined(_WIN32) // defined(__linux__)
375/* Windows-specific common code: */
376
377// Dynamic Loading:
378//#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION
379#define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION
380#define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION
381
382// Threads:
383#define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION
384#define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION
385
386// Thread mutex:
387#define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
388#define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
389#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
390#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
391
392
393#endif // defined(_WIN32)
394#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott81ac44c2015-01-13 17:52:38 -0700395#endif /* LOADER_PLATFORM_H */