blob: 230ee5d091f2ab723815616fa5e89d6295f8aeea [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:
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 Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060045// VK Library Filenames, Paths, etc.:
Ian Elliott457810e2015-02-04 11:22:39 -070046#define PATH_SEPERATOR ':'
47#define DIRECTORY_SYMBOL "/"
Courtney Goeltzenleuchterd8e229c2015-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 Elliott457810e2015-02-04 11:22:39 -070052// TODO: Is this a good default location?
53// Need to search for both 32bit and 64bit ICDs
Courtney Goeltzenleuchterd8e229c2015-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 Elliott457810e2015-02-04 11:22:39 -070063// TODO: Are these good default locations?
Courtney Goeltzenleuchterd8e229c2015-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 Elliott457810e2015-02-04 11:22:39 -070065#endif
66
Ian Elliotte5369462015-02-12 16:44:56 -070067// C99:
Tobin Ehlisb0497852015-02-11 14:24:02 -070068#define PRINTF_SIZE_T_SPECIFIER "%zu"
69
Ian Elliott2d4ab1e2015-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 Wu6a218342015-02-18 14:39:54 -070074 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott2d4ab1e2015-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}
132
133
134#elif defined(_WIN32) // defined(__linux__)
135/* Windows-specific common code: */
136
137// Headers:
Piers Daniell188f8202015-02-24 13:58:47 -0700138#include <WinSock2.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700139#include <windows.h>
140#include <assert.h>
141#ifdef __cplusplus
142#include <iostream>
143#include <string>
144using namespace std;
145#endif // __cplusplus
146
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600147// VK Library Filenames, Paths, etc.:
Ian Elliott457810e2015-02-04 11:22:39 -0700148#define PATH_SEPERATOR ';'
149#define DIRECTORY_SYMBOL "\\"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600150#define DRIVER_PATH_REGISTRY_VALUE "VK_DRIVERS_PATH"
151#define LAYERS_PATH_REGISTRY_VALUE "VK_LAYERS_PATH"
152#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
153#define DRIVER_PATH_ENV "VK_DRIVERS_PATH"
154#define LAYERS_PATH_ENV "VK_LAYERS_PATH"
155#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
156#ifndef DEFAULT_VK_DRIVERS_PATH
Ian Elliott457810e2015-02-04 11:22:39 -0700157// TODO: Is this a good default location?
158// Need to search for both 32bit and 64bit ICDs
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600159#define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32"
Ian Elliott457810e2015-02-04 11:22:39 -0700160// TODO/TBD: Is this an appropriate prefix for Windows?
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600161#define VK_DRIVER_LIBRARY_PREFIX "VK_"
162#define VK_DRIVER_LIBRARY_PREFIX_LEN 4
Ian Elliott457810e2015-02-04 11:22:39 -0700163// TODO/TBD: Is this an appropriate suffix for Windows?
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600164#define VK_LAYER_LIBRARY_PREFIX "VKLayer"
165#define VK_LAYER_LIBRARY_PREFIX_LEN 8
166#define VK_LIBRARY_SUFFIX ".dll"
167#define VK_LIBRARY_SUFFIX_LEN 4
168#endif // DEFAULT_VK_DRIVERS_PATH
169#ifndef DEFAULT_VK_LAYERS_PATH
Ian Elliott457810e2015-02-04 11:22:39 -0700170// TODO: Is this a good default location?
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600171#define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32"
172#endif // DEFAULT_VK_LAYERS_PATH
Ian Elliott457810e2015-02-04 11:22:39 -0700173
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700174// C99:
175// Microsoft didn't implement C99 in Visual Studio; but started adding it with
176// VS2013. However, VS2013 still didn't have snprintf(). The following is a
Ian Elliott19628802015-02-04 12:06:46 -0700177// work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the
178// "CMakeLists.txt" file).
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700179#define snprintf _snprintf
Tobin Ehlisb0497852015-02-11 14:24:02 -0700180#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700181// Microsoft also doesn't have basename(). Paths are different on Windows, and
182// so this is just a temporary solution in order to get us compiling, so that we
183// can test some scenarios, and develop the correct solution for Windows.
184 // TODO: Develop a better, permanent solution for Windows, to replace this
185 // temporary code:
186static char *basename(char *pathname)
187{
188 char *current, *next;
189
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700190// TODO/TBD: Do we need to deal with the Windows's ":" character?
191
Ian Elliott457810e2015-02-04 11:22:39 -0700192#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700193 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott457810e2015-02-04 11:22:39 -0700194 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700195 if (next == NULL) {
Ian Elliott457810e2015-02-04 11:22:39 -0700196 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700197 return current;
198 } else {
Ian Elliott457810e2015-02-04 11:22:39 -0700199 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700200 next++;
201 }
202 }
Ian Elliott19628802015-02-04 12:06:46 -0700203 // We shouldn't get to here, but this makes the compiler happy:
204 return current;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700205}
206
207// Dynamic Loading:
208typedef HMODULE loader_platform_dl_handle;
209static loader_platform_dl_handle loader_platform_open_library(const char* libPath)
210{
211 return LoadLibrary(libPath);
212}
213static char * loader_platform_open_library_error(const char* libPath)
214{
215 static char errorMsg[120];
216 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
217 return errorMsg;
218}
219static void loader_platform_close_library(loader_platform_dl_handle library)
220{
221 FreeLibrary(library);
222}
223static void * loader_platform_get_proc_address(loader_platform_dl_handle library,
224 const char *name)
225{
226 assert(library);
227 assert(name);
228 return GetProcAddress(library, name);
229}
230static char * loader_platform_get_proc_address_error(const char *name)
231{
232 static char errorMsg[120];
233 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
234 return errorMsg;
235}
236
237// Threads:
238typedef HANDLE loader_platform_thread;
239#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
240 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Piers Daniell4da523a2015-02-23 16:23:13 -0700241static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
242{
243 void (*func)(void) = (void (*)(void))Parameter;
244 func();
245 return TRUE;
246}
247
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700248static void loader_platform_thread_once(void *ctl, void (* func) (void))
249{
250 assert(func != NULL);
251 assert(ctl != NULL);
Piers Daniell4da523a2015-02-23 16:23:13 -0700252 InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700253}
254
255// Thread IDs:
256typedef DWORD loader_platform_thread_id;
257static loader_platform_thread_id loader_platform_get_thread_id()
258{
259 return GetCurrentThreadId();
260}
261
262// Thread mutex:
263typedef CRITICAL_SECTION loader_platform_thread_mutex;
264static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
265{
266 InitializeCriticalSection(pMutex);
267}
268static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
269{
270 EnterCriticalSection(pMutex);
271}
272static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
273{
274 LeaveCriticalSection(pMutex);
275}
276static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
277{
278 DeleteCriticalSection(pMutex);
279}
280
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600281// Windows Registry:
282char *loader_get_registry_string(const HKEY hive,
283 const LPCTSTR sub_key,
284 const char *value);
285
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700286#else // defined(_WIN32)
287
288#error The "loader_platform.h" file must be modified for this OS.
289
290// NOTE: In order to support another OS, an #elif needs to be added (above the
291// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
292// contents of this file must be created.
293
294// NOTE: Other OS-specific changes are also needed for this OS. Search for
295// files with "WIN32" in it, as a quick way to find files that must be changed.
296
297#endif // defined(_WIN32)
298
Ian Elliott655cad72015-02-12 17:08:34 -0700299#else /* LOADER_PLATFORM_H */
300#ifndef LOADER_PLATFORM_H_TEMP
301#define LOADER_PLATFORM_H_TEMP
302
303// NOTE: The following are hopefully-temporary macros to ensure that people
304// don't forget to use the loader_platform_*() functions above:
305
306#if defined(__linux__)
307/* Linux-specific common code: */
308
309// Dynamic Loading:
310#define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION
311#define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY
312#define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION
313#define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION
314
315// Threads:
316#define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION
317#define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION
318
319// Thread mutex:
320#define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
321#define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
322#define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
323#define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
324
325
326#elif defined(_WIN32) // defined(__linux__)
327/* Windows-specific common code: */
328
329// Dynamic Loading:
330//#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION
331#define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION
332#define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION
333
334// Threads:
335#define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION
336#define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION
337
338// Thread mutex:
339#define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
340#define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
341#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
342#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
343
344
345#endif // defined(_WIN32)
346#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700347#endif /* LOADER_PLATFORM_H */