-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple.cpp
69 lines (60 loc) · 2.71 KB
/
simple.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-FileCopyrightText: Copyright (c) Jaswant Sai Panchumarti
// SPDX-License-Identifier: Apache-2.0
#include <cstdlib>
#include <iostream>
#define WGPU_SKIP_DECLARATIONS
#include <webgpu.h>
#if defined(_WIN32)
#include <windows.h>
#define LOAD_LIBRARY(name) LoadLibrary(TEXT(name))
#define UNLOAD_LIBRARY(lib) FreeLibrary(lib)
#define LIBRARY_HANDLE_TYPE HINSTANCE
#define LOAD_SYMBOL(symbol, pSymbol) \
do { \
symbol = (pSymbol)GetProcAddress(webgpuImpl, #symbol); \
if (symbol == nullptr) { \
std::cerr << "Failed to load " #symbol << "\n"; \
return EXIT_FAILURE; \
} \
} while (0)
#define LOAD_WGPU_SYMBOL(name) LOAD_SYMBOL(wgpu##name, WGPUProc##name)
#else
#include <dlfcn.h>
#define LOAD_LIBRARY(name) dlopen(name, RTLD_LAZY)
#define UNLOAD_LIBRARY(lib) dlclose(lib)
#define LIBRARY_HANDLE_TYPE void *
#define LOAD_SYMBOL(symbol, pSymbol) \
do { \
symbol = (pSymbol)dlsym(webgpuImpl, #symbol); \
if (symbol == nullptr) { \
std::cerr << "Failed to load " #symbol << "\n"; \
return EXIT_FAILURE; \
} \
} while (0)
#define LOAD_WGPU_SYMBOL(name) LOAD_SYMBOL(wgpu##name, WGPUProc##name)
#endif
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Usage: simple_cpp /path/to/<webgpu_implementation_lib>\n";
return EXIT_FAILURE;
}
LIBRARY_HANDLE_TYPE webgpuImpl = LOAD_LIBRARY(argv[1]);
if (webgpuImpl == nullptr) {
return EXIT_FAILURE;
}
WGPUProcCreateInstance wgpuCreateInstance = nullptr;
LOAD_WGPU_SYMBOL(CreateInstance);
std::cerr << wgpuCreateInstance << '\n';
WGPUInstance instance = wgpuCreateInstance(nullptr);
if (instance == nullptr) {
std::cerr << "wgpuCreateInstance failed to create an instance!\n";
return EXIT_FAILURE;
}
WGPUProcInstanceRequestAdapter wgpuInstanceRequestAdapter = nullptr;
LOAD_WGPU_SYMBOL(InstanceRequestAdapter);
WGPURequestAdapterOptions adapterOpts = {};
adapterOpts.nextInChain = nullptr;
WGPUProcInstanceProcessEvents wgpuInstanceProcessEvents = nullptr;
LOAD_WGPU_SYMBOL(InstanceProcessEvents);
return EXIT_SUCCESS;
}