1 /* 2 Copyright (c) 2019-2021 Timur Gafarov. 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 module bindbc.wgpu.loader; 29 30 import core.stdc.stdint; 31 import bindbc.loader; 32 import bindbc.wgpu.types; 33 import bindbc.wgpu.funcs; 34 35 enum WGPUSupport 36 { 37 noLibrary, 38 badLibrary, 39 wgpu010 40 } 41 42 private 43 { 44 SharedLib lib; 45 WGPUSupport loadedVersion; 46 } 47 48 void unloadWGPU() 49 { 50 if (lib != invalidHandle) 51 { 52 lib.unload(); 53 } 54 } 55 56 WGPUSupport loadedWGPUVersion() 57 { 58 return loadedVersion; 59 } 60 61 bool isWGPULoaded() 62 { 63 return lib != invalidHandle; 64 } 65 66 WGPUSupport loadWGPU() 67 { 68 version(Windows) 69 { 70 const(char)[][1] libNames = 71 [ 72 "libwgpu.dll" 73 ]; 74 } 75 else version(OSX) 76 { 77 const(char)[][1] libNames = 78 [ 79 "libwgpu.dylib" 80 ]; 81 } 82 else version(Posix) 83 { 84 const(char)[][2] libNames = 85 [ 86 "libwgpu.so", 87 "/usr/local/lib/libwgpu.so", 88 ]; 89 } 90 else static assert(0, "libwgpu is not yet supported on this platform."); 91 92 WGPUSupport ret; 93 foreach(name; libNames) 94 { 95 ret = loadWGPU(name.ptr); 96 if (ret != WGPUSupport.noLibrary) 97 break; 98 } 99 return ret; 100 } 101 102 WGPUSupport loadWGPU(const(char)* libName) 103 { 104 lib = load(libName); 105 if(lib == invalidHandle) 106 { 107 return WGPUSupport.noLibrary; 108 } 109 110 auto errCount = errorCount(); 111 loadedVersion = WGPUSupport.badLibrary; 112 113 import std.algorithm.searching: startsWith; 114 static foreach(m; __traits(allMembers, bindbc.wgpu.funcs)) 115 { 116 static if (m.startsWith("da_")) 117 lib.bindSymbol( 118 cast(void**)&__traits(getMember, bindbc.wgpu.funcs, m[3..$]), 119 __traits(getMember, bindbc.wgpu.funcs, m[3..$]).stringof); 120 } 121 122 loadedVersion = WGPUSupport.wgpu010; 123 124 if (errorCount() != errCount) 125 return WGPUSupport.badLibrary; 126 127 return loadedVersion; 128 }