Refactor vk_loader_platform.h
authorCharles Giessen <charles@lunarg.com>
Tue, 21 Jun 2022 15:57:37 +0000 (10:57 -0500)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Thu, 7 Jul 2022 20:41:01 +0000 (14:41 -0600)
Move includes of vulkan headers into loader_common.h, delete dead code
and unnecessary macro defines, and generally clean up the header.

loader/asm_offset.c
loader/dev_ext_trampoline.c
loader/extension_manual.c
loader/loader.c
loader/loader_common.h
loader/phys_dev_ext.c
loader/unknown_ext_chain.c
loader/vk_loader_platform.h

index c5021f6e2a3e95f3406bb3845d9ba069e24494e5..80b71065cd7e1bdca0e3aa8ed5dcc1253124b342 100644 (file)
@@ -23,7 +23,7 @@
 // This code generates an assembly file which provides offsets to get struct members from assembly code.
 
 #include <stdio.h>
-#include "loader.h"
+#include "loader_common.h"
 #include "log.h"
 
 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
index 9674d86a1c4f8ceb93b8ef39c60e35ec73092d48..9605c13fd9679c1803bee84940daadd2bb952687 100644 (file)
@@ -20,7 +20,6 @@
  */
 
 #include "loader.h"
-#include "vk_loader_platform.h"
 #if defined(__GNUC__) && !defined(__clang__)
 #pragma GCC optimize(3)  // force gcc to use tail-calls
 #endif
index 4f21107b7716d1bb3715a62371c7be7fe4b27b2d..bfa28510a7b74d6e11458f841a09b8173dd57011 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include <vulkan/vk_icd.h>
-
 #include "allocation.h"
 #include "debug_utils.h"
 #include "loader.h"
 #include "log.h"
-#include "vk_loader_extensions.h"
-#include "vk_loader_platform.h"
 #include "wsi.h"
 
 // ---- Manually added trampoline/terminator functions
index b1eca3880eeda70679c6eeca680b653638cd6fc2..e89c75777681f76e52c86ce6427039d208aa6110 100644 (file)
 #include <dirent.h>
 #endif  // _WIN32
 
-#include "vulkan/vk_icd.h"
-
 #include "allocation.h"
 #include "cJSON.h"
 #include "debug_utils.h"
 #include "get_environment.h"
 #include "gpa_helper.h"
-#include "loader.h"
 #include "log.h"
 #include "unknown_function_handling.h"
 #include "vk_loader_platform.h"
@@ -2096,12 +2093,8 @@ static VkResult loader_read_layer_json(const struct loader_instance *inst, struc
                 rel_base = loader_platform_dirname(name_copy);
                 loader_expand_path(library_path_str, rel_base, MAX_STRING_SIZE, fullpath);
             } else {
-// A filename which is assumed in a system directory
-#if defined(DEFAULT_VK_LAYERS_PATH)
-                loader_get_fullpath(library_path_str, DEFAULT_VK_LAYERS_PATH, MAX_STRING_SIZE, fullpath);
-#else
+                // A filename which is assumed in a system directory
                 loader_get_fullpath(library_path_str, "", MAX_STRING_SIZE, fullpath);
-#endif
             }
         }
     } else if (NULL != component_layers) {
index 7fcfd816db3dd95fddc4c5677dbad69ef4467809..55584cf4702e604762f9f86f97aa0381ac79109f 100644 (file)
 
 #pragma once
 
+#include "vulkan/vk_platform.h"
+#include "vulkan/vk_sdk_platform.h"
+#include <vulkan/vulkan.h>
+#include <vulkan/vk_layer.h>
+#include <vulkan/vk_icd.h>
+
 #include "vk_loader_platform.h"
+#include "vk_loader_layer.h"
+#include "vk_layer_dispatch_table.h"
+#include "vk_loader_extensions.h"
 
 typedef enum VkStringErrorFlagBits {
     VK_STRING_ERROR_NONE = 0x00000000,
index 665e33f04ea358d05e4db3aed16a7f615bddf87d..5e840ebabbd044c9e416ff9737be7230e65c343d 100644 (file)
@@ -29,7 +29,6 @@
 // code since we don't want to optimize it out.
 
 #include "loader.h"
-#include "vk_loader_platform.h"
 
 #if defined(__GNUC__) && !defined(__clang__)
 #pragma GCC optimize(3)  // force gcc to use tail-calls
index 5b613eda2c142d0467d052b9fdf922dcc2cf1e8c..3ce7f3b1c974ca16b64280a32b8b2e69a6fcc2db 100644 (file)
@@ -28,7 +28,6 @@
 
 #include "loader.h"
 #include "log.h"
-#include "vk_loader_platform.h"
 
 #if defined(__GNUC__) && !defined(__clang__)
 #pragma GCC optimize(3)  // force gcc to use tail-calls
index 993fe83060c17894bcc620990422f30967f9b76c..44e0730c5a4e0503ff51a6268210e23ea626054c 100644 (file)
 #include <direct.h>
 #endif  // defined(_WIN32)
 
-#include "vulkan/vk_platform.h"
-#include "vulkan/vk_sdk_platform.h"
-#include <vulkan/vulkan.h>
-#include <vulkan/vk_layer.h>
-#include <vulkan/vk_icd.h>
-
-#include "vk_loader_layer.h"
-#include "vk_layer_dispatch_table.h"
-#include "vk_loader_extensions.h"
 #include "stack_allocation.h"
 
 #if defined(__GNUC__) && __GNUC__ >= 4
 // Override layer information
 #define VK_OVERRIDE_LAYER_NAME "VK_LAYER_LUNARG_override"
 
+#define LAYERS_PATH_ENV "VK_LAYER_PATH"
+#define ENABLED_LAYERS_ENV "VK_INSTANCE_LAYERS"
+
 #if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__)
 /* Linux-specific common code: */
 
 #define VK_ELAYERS_INFO_REGISTRY_LOC ""
 #define VK_ILAYERS_INFO_REGISTRY_LOC ""
 
-#if !defined(DEFAULT_VK_LAYERS_PATH)
-#define DEFAULT_VK_LAYERS_PATH ""
-#endif
-#if !defined(LAYERS_SOURCE_PATH)
-#define LAYERS_SOURCE_PATH NULL
+#if defined(__QNXNTO__)
+#define SYSCONFDIR "/etc"
 #endif
-#define LAYERS_PATH_ENV "VK_LAYER_PATH"
-#define ENABLED_LAYERS_ENV "VK_INSTANCE_LAYERS"
 
 // C99:
 #define PRINTF_SIZE_T_SPECIFIER "%zu"
 
+// Dynamic Loading of libraries:
+typedef void *loader_platform_dl_handle;
+
+// Threads:
+typedef pthread_t loader_platform_thread;
+
+// Thread IDs:
+typedef pthread_t loader_platform_thread_id;
+
+// Thread mutex:
+typedef pthread_mutex_t loader_platform_thread_mutex;
+
+typedef pthread_cond_t loader_platform_thread_cond;
+
+#elif defined(_WIN32)  // defined(__linux__)
+
+// VK Library Filenames, Paths, etc.:
+#define PATH_SEPARATOR ';'
+#define DIRECTORY_SYMBOL '\\'
+#define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE
+#define DEFAULT_VK_REGISTRY_HIVE_STR "HKEY_LOCAL_MACHINE"
+#define SECONDARY_VK_REGISTRY_HIVE HKEY_CURRENT_USER
+#define SECONDARY_VK_REGISTRY_HIVE_STR "HKEY_CURRENT_USER"
+
+#define VK_DRIVERS_INFO_RELATIVE_DIR ""
+#define VK_SETTINGS_INFO_RELATIVE_DIR ""
+#define VK_ELAYERS_INFO_RELATIVE_DIR ""
+#define VK_ILAYERS_INFO_RELATIVE_DIR ""
+
+#ifdef _WIN64
+#define HKR_VK_DRIVER_NAME API_NAME "DriverName"
+#else
+#define HKR_VK_DRIVER_NAME API_NAME "DriverNameWow"
+#endif
+#define VK_DRIVERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\" API_NAME "\\Drivers"
+#define VK_SETTINGS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\" API_NAME "\\Settings"
+#define VK_ELAYERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\" API_NAME "\\ExplicitLayers"
+#define VK_ILAYERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\" API_NAME "\\ImplicitLayers"
+
+#define PRINTF_SIZE_T_SPECIFIER "%Iu"
+
+// Dynamic Loading:
+typedef HMODULE loader_platform_dl_handle;
+
+// Threads:
+typedef HANDLE loader_platform_thread;
+
+// Thread IDs:
+typedef DWORD loader_platform_thread_id;
+
+// Thread mutex:
+typedef CRITICAL_SECTION loader_platform_thread_mutex;
+
+typedef CONDITION_VARIABLE loader_platform_thread_cond;
+
+#else  // defined(_WIN32)
+
+#error The "vk_loader_platform.h" file must be modified for this OS.
+
+// NOTE: In order to support another OS, an #elif needs to be added (above the
+// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
+// contents of this file must be created, or extend one of the existing OS specific
+// sections with the necessary changes.
+
+#endif  // defined(_WIN32)
+
+// Returns true if the DIRECTORY_SYMBOL is contained within path
+static inline bool loader_platform_is_path(const char *path) { return strchr(path, DIRECTORY_SYMBOL) != NULL; }
+
+// The once init functionality is not used when building a DLL on Windows. This is because there is no way to clean up the
+// resources allocated by anything allocated by once init. This isn't a problem for static libraries, but it is for dynamic
+// ones. When building a DLL, we use DllMain() instead to allow properly cleaning up resources.
+#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var)
+#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var)
+#define LOADER_PLATFORM_THREAD_ONCE(ctl, func)
+
+#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__)
+
 // File IO
 static inline bool loader_platform_file_exists(const char *path) {
     if (access(path, F_OK))
@@ -149,6 +216,8 @@ static inline bool loader_platform_file_exists(const char *path) {
         return true;
 }
 
+// Returns true if the given string appears to be a relative or absolute
+// path, as opposed to a bare filename.
 static inline bool loader_platform_is_path_absolute(const char *path) {
     if (path[0] == '/')
         return true;
@@ -158,9 +227,9 @@ static inline bool loader_platform_is_path_absolute(const char *path) {
 
 static inline char *loader_platform_dirname(char *path) { return dirname(path); }
 
+// loader_platform_executable_path finds application path + name.
+// Path cannot be longer than 1024, returns NULL if it is greater than that.
 #if defined(__linux__)
-
-// find application path + name. Path cannot be longer than 1024, returns NULL if it is greater than that.
 static inline char *loader_platform_executable_path(char *buffer, size_t size) {
     ssize_t count = readlink("/proc/self/exe", buffer, size);
     if (count == -1) return NULL;
@@ -232,11 +301,9 @@ static inline char *loader_platform_executable_path(char *buffer, size_t size) {
 #endif
 
 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
-#define LOADER_ADDRESS_SANITIZER
+#define LOADER_ADDRESS_SANITIZER  // TODO: Add proper build flag for ASAN support
 #endif
 
-// Dynamic Loading of libraries:
-typedef void *loader_platform_dl_handle;
 // When loading the library, we use RTLD_LAZY so that not all symbols have to be
 // resolved at this time (which improves performance). Note that if not all symbols
 // can be resolved, this could cause crashes later. Use the LD_BIND_NOW environment
@@ -271,68 +338,14 @@ static inline void *loader_platform_get_proc_address(loader_platform_dl_handle l
 }
 static inline const char *loader_platform_get_proc_address_error(const char *name) { return dlerror(); }
 
-// Threads:
-typedef pthread_t loader_platform_thread;
-
-// The once init functionality is not used on Linux
-#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var)
-#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var)
-#define LOADER_PLATFORM_THREAD_ONCE(ctl, func)
-
-// Thread IDs:
-typedef pthread_t loader_platform_thread_id;
-static inline loader_platform_thread_id loader_platform_get_thread_id() { return pthread_self(); }
-
 // Thread mutex:
-typedef pthread_mutex_t loader_platform_thread_mutex;
 static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { pthread_mutex_init(pMutex, NULL); }
 static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { pthread_mutex_lock(pMutex); }
 static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { pthread_mutex_unlock(pMutex); }
 static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { pthread_mutex_destroy(pMutex); }
-typedef pthread_cond_t loader_platform_thread_cond;
-static inline void loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { pthread_cond_init(pCond, NULL); }
-static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, loader_platform_thread_mutex *pMutex) {
-    pthread_cond_wait(pCond, pMutex);
-}
-static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { pthread_cond_broadcast(pCond); }
 
 #elif defined(_WIN32)  // defined(__linux__)
 
-// VK Library Filenames, Paths, etc.:
-#define PATH_SEPARATOR ';'
-#define DIRECTORY_SYMBOL '\\'
-#define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE
-#define DEFAULT_VK_REGISTRY_HIVE_STR "HKEY_LOCAL_MACHINE"
-#define SECONDARY_VK_REGISTRY_HIVE HKEY_CURRENT_USER
-#define SECONDARY_VK_REGISTRY_HIVE_STR "HKEY_CURRENT_USER"
-
-#define VK_DRIVERS_INFO_RELATIVE_DIR ""
-#define VK_SETTINGS_INFO_RELATIVE_DIR ""
-#define VK_ELAYERS_INFO_RELATIVE_DIR ""
-#define VK_ILAYERS_INFO_RELATIVE_DIR ""
-
-#ifdef _WIN64
-#define HKR_VK_DRIVER_NAME API_NAME "DriverName"
-#else
-#define HKR_VK_DRIVER_NAME API_NAME "DriverNameWow"
-#endif
-#define VK_DRIVERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\" API_NAME "\\Drivers"
-#define VK_SETTINGS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\" API_NAME "\\Settings"
-#define VK_ELAYERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\" API_NAME "\\ExplicitLayers"
-#define VK_ILAYERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\" API_NAME "\\ImplicitLayers"
-
-#if !defined(DEFAULT_VK_LAYERS_PATH)
-#define DEFAULT_VK_LAYERS_PATH ""
-#endif
-#if !defined(LAYERS_SOURCE_PATH)
-#define LAYERS_SOURCE_PATH NULL
-#endif
-#define LAYERS_PATH_ENV "VK_LAYER_PATH"
-#define ENABLED_LAYERS_ENV "VK_INSTANCE_LAYERS"
-
-#define PRINTF_SIZE_T_SPECIFIER "%Iu"
-
-#if defined(_WIN32)
 // Get the key for the plug n play driver registry
 // The string returned by this function should NOT be freed
 static inline const char *LoaderPnpDriverRegistry() {
@@ -371,7 +384,6 @@ static inline const wchar_t *LoaderPnpILayerRegistryWide() {
     IsWow64Process(GetCurrentProcess(), &is_wow);
     return is_wow ? L"VulkanImplicitLayersWow" : L"VulkanImplicitLayers";
 }
-#endif
 
 // File IO
 static bool loader_platform_file_exists(const char *path) {
@@ -389,6 +401,8 @@ static bool loader_platform_file_exists(const char *path) {
         return true;
 }
 
+// Returns true if the given string appears to be a relative or absolute
+// path, as opposed to a bare filename.
 static bool loader_platform_is_path_absolute(const char *path) {
     if (!path || !*path) {
         return false;
@@ -438,7 +452,6 @@ static inline char *loader_platform_executable_path(char *buffer, size_t size) {
 }
 
 // Dynamic Loading:
-typedef HMODULE loader_platform_dl_handle;
 static loader_platform_dl_handle loader_platform_open_library(const char *lib_path) {
     int lib_path_utf16_size = MultiByteToWideChar(CP_UTF8, 0, lib_path, -1, NULL, 0);
     if (lib_path_utf16_size <= 0) {
@@ -456,7 +469,7 @@ static loader_platform_dl_handle loader_platform_open_library(const char *lib_pa
     }
     return lib_handle;
 }
-static char *loader_platform_open_library_error(const char *libPath) {
+static const char *loader_platform_open_library_error(const char *libPath) {
     static char errorMsg[512];
     (void)snprintf(errorMsg, 511, "Failed to open dynamic library \"%s\" with error %lu", libPath, GetLastError());
     return errorMsg;
@@ -467,41 +480,21 @@ static void *loader_platform_get_proc_address(loader_platform_dl_handle library,
     assert(name);
     return (void *)GetProcAddress(library, name);
 }
-static char *loader_platform_get_proc_address_error(const char *name) {
+static const char *loader_platform_get_proc_address_error(const char *name) {
     static char errorMsg[120];
     (void)snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
     return errorMsg;
 }
 
-// Threads:
-typedef HANDLE loader_platform_thread;
-
-// The once init functionality is not used when building a DLL on Windows. This is because there is no way to clean up the
-// resources allocated by anything allocated by once init. This isn't a problem for static libraries, but it is for dynamic
-// ones. When building a DLL, we use DllMain() instead to allow properly cleaning up resources.
-#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var)
-#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var)
-#define LOADER_PLATFORM_THREAD_ONCE(ctl, func)
-
-// Thread IDs:
-typedef DWORD loader_platform_thread_id;
-static loader_platform_thread_id loader_platform_get_thread_id() { return GetCurrentThreadId(); }
-
 // Thread mutex:
-typedef CRITICAL_SECTION loader_platform_thread_mutex;
 static void loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); }
 static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); }
 static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); }
 static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); }
-typedef CONDITION_VARIABLE loader_platform_thread_cond;
-static void loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { InitializeConditionVariable(pCond); }
-static void loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, loader_platform_thread_mutex *pMutex) {
-    SleepConditionVariableCS(pCond, pMutex, INFINITE);
-}
-static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { WakeAllConditionVariable(pCond); }
+
 #else  // defined(_WIN32)
 
-#error The "loader_platform.h" file must be modified for this OS.
+#error The "vk_loader_platform.h" file must be modified for this OS.
 
 // NOTE: In order to support another OS, an #elif needs to be added (above the
 // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
@@ -511,7 +504,3 @@ static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond *p
 // files with "WIN32" in it, as a quick way to find files that must be changed.
 
 #endif  // defined(_WIN32)
-
-// returns true if the given string appears to be a relative or absolute
-// path, as opposed to a bare filename.
-static inline bool loader_platform_is_path(const char *path) { return strchr(path, DIRECTORY_SYMBOL) != NULL; }