|
| 1 | +/** |
| 2 | + * Dlang vulkan lib loader retrieving vkGetInstanceProcAddr for windows and posix systems |
| 3 | + * |
| 4 | + * Copyright: Copyright 2015-2016 The Khronos Group Inc.; Copyright 2016 Alex Parrill, Peter Particle. |
| 5 | + * License: $(https://opensource.org/licenses/MIT, MIT License). |
| 6 | + * Authors: Copyright 2016 Alex Parrill, Peter Particle |
| 7 | + */ |
| 8 | +module erupted.vulkan_lib_loader; |
| 9 | + |
| 10 | +import erupted.functions; |
| 11 | +import core.stdc.stdio : fprintf, stderr, FILE; |
| 12 | + |
| 13 | +nothrow @nogc: |
| 14 | + |
| 15 | + |
| 16 | +/// private helper functions for windows platform |
| 17 | +version( Windows ) { |
| 18 | +private: |
| 19 | + import core.sys.windows.windows; |
| 20 | + HMODULE vulkan_lib = null; |
| 21 | + auto loadLib() { return LoadLibrary( "vulkan-1.dll" ); } |
| 22 | + auto freeLib() { return FreeLibrary( vulkan_lib ) != 0; } |
| 23 | + auto loadSym() { return cast( PFN_vkGetInstanceProcAddr )GetProcAddress( vulkan_lib, "vkGetInstanceProcAddr" ); } |
| 24 | + void logLibError( FILE* log_stream, const( char )* message ) { |
| 25 | + fprintf( log_stream, "%svulkan-1.dll! Error code: 0x%x\n", message, GetLastError()); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +/// private helper functions for posix platforms |
| 31 | +version( Posix ) { |
| 32 | +private: |
| 33 | + import core.sys.posix.dlfcn : dlerror, dlopen, dlclose, dlsym, RTLD_LAZY, RTLD_LOCAL; |
| 34 | + void* vulkan_lib = null; |
| 35 | + auto loadLib() { return dlopen( "libvulkan.so.1", RTLD_LAZY | RTLD_LOCAL ); } |
| 36 | + auto freeLib() { return dlclose( vulkan_lib ) == 0; } |
| 37 | + auto loadSym() { return cast( PFN_vkGetInstanceProcAddr )dlsym( vulkan_lib, "vkGetInstanceProcAddr" ); } |
| 38 | + void logLibError( FILE* log_stream, const( char )* message ) { |
| 39 | + fprintf( log_stream, "%slibvulkan.so.1! Error: %s\n", message, dlerror ); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +/// tries to load the platform vulkan dynamic link library |
| 45 | +/// the library handle / pointer is stored privately in this module |
| 46 | +/// errors are reported to a specifiable stream which is standard error by default |
| 47 | +/// Params: |
| 48 | +/// log_stream = file stream to receive error messages, default stderr |
| 49 | +/// Returns: true if the vulkan lib could be loaded, false otherwise |
| 50 | +bool loadVulkanLib( FILE* log_stream = stderr ) { |
| 51 | + vulkan_lib = loadLib; |
| 52 | + if( !vulkan_lib ) { |
| 53 | + logLibError( log_stream, "Could not load " ); |
| 54 | + return false; |
| 55 | + } else { |
| 56 | + return true; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +/// tries to load the vkGetInstanceProcAddr function from the module private lib handle / pointer |
| 62 | +/// if the lib was not loaded so far loadVulkanLib is called |
| 63 | +/// errors are reported to a specifiable stream which is standard error by default |
| 64 | +/// Params: |
| 65 | +/// log_stream = file stream to receive error messages, default stderr |
| 66 | +/// Returns: vkGetInstanceProcAddr if it could be loaded from the lib, null otherwise |
| 67 | +PFN_vkGetInstanceProcAddr loadGetInstanceProcAddr( FILE* log_stream = stderr ) { |
| 68 | + if( !vulkan_lib && !loadVulkanLib( log_stream )) { |
| 69 | + fprintf( log_stream, "Cannot not retrieve vkGetInstanceProcAddr as vulkan lib is not loaded!" ); |
| 70 | + return null; |
| 71 | + } |
| 72 | + auto getInstanceProcAddr = loadSym; |
| 73 | + if( !getInstanceProcAddr ) |
| 74 | + logLibError( log_stream, "Could not retrieve vkGetInstanceProcAddr from " ); |
| 75 | + return getInstanceProcAddr; |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +/// tries to free / unload the previously loaded platform vulkan lib |
| 80 | +/// errors are reported to a specifiable stream which is standard error by default |
| 81 | +/// Params: |
| 82 | +/// log_stream = file stream to receive error messages, default stderr |
| 83 | +/// Returns: true if the vulkan lib could be freed, false otherwise |
| 84 | +bool freeVulkanLib( FILE* log_stream = stderr ) { |
| 85 | + if( !vulkan_lib ) { |
| 86 | + fprintf( log_stream, "Cannot free vulkan lib as it is not loaded!" ); |
| 87 | + return false; |
| 88 | + } else if( freeLib ) { |
| 89 | + logLibError( log_stream, "Could not unload " ); |
| 90 | + return false; |
| 91 | + } else { |
| 92 | + return true; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +/// Combines loadVulkanLib, loadGetInstanceProcAddr and loadGlobalLevelFunctions( PFN_vkGetInstanceProcAddr ) |
| 98 | +/// from module erupted.functions. If this function succeeds the function vkGetInstanceProcAddr |
| 99 | +/// from module erupted.functions can be used freely. Moreover the required functions to initialize a |
| 100 | +/// vulkan instance a vkEnumerateInstanceExtensionProperties, vkEnumerateInstanceLayerProperties and vkCreateInstance |
| 101 | +/// are available as well. To get all the other functions an vulkan instance must be created and with it |
| 102 | +/// loadInstanceLevelFunctions be called from either erupted.functions or through a custom tailored module |
| 103 | +/// with mixed in extensions through the erupted.platform.mixin_extensions mechanism. |
| 104 | +/// Additional device based functions can then be loaded with loadDeviceLevelFunctions passing in the instance or |
| 105 | +/// with creating a vulkan device beforehand and calling the same function with it. |
| 106 | +/// |
| 107 | +/// Note: as this function indirectly calls loadVulkanLib loading the vulkan lib, freeVulkanLib should be called |
| 108 | +/// at some point in the process to cleanly free / unload the lib |
| 109 | +/// all errors during vulkan lib loading and vkGetInstanceProcAddr retrieving are reported to log_stream, default stderr |
| 110 | +/// log_stream = file stream to receive error messages, default stderr |
| 111 | +/// Returns: true if the vulkan lib could be freed, false otherwise |
| 112 | +bool loadGlobalLevelFunctions( FILE* log_stream = stderr ) { |
| 113 | + auto getInstanceProcAddr = loadGetInstanceProcAddr( log_stream ); |
| 114 | + if( !getInstanceProcAddr ) return false; |
| 115 | + erupted.functions.loadGlobalLevelFunctions( getInstanceProcAddr ); |
| 116 | + return true; |
| 117 | +} |
| 118 | + |
| 119 | + |
0 commit comments