First fun C++ to C problem
This commit is contained in:
parent
dfccf4d01b
commit
45fbf0abca
1 changed files with 46 additions and 0 deletions
46
src/main.c
46
src/main.c
|
|
@ -1,6 +1,52 @@
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <webgpu/webgpu.h>
|
#include <webgpu/webgpu.h>
|
||||||
|
|
||||||
|
WGPUAdapter requestAdapterSync(WGPUInstance instance,
|
||||||
|
WGPURequestAdapterOptions const *options) {
|
||||||
|
typedef struct {
|
||||||
|
WGPUAdapter adapter;
|
||||||
|
bool requestEnded;
|
||||||
|
} UserData;
|
||||||
|
|
||||||
|
UserData userData = {.adapter = NULL, .requestEnded = false};
|
||||||
|
|
||||||
|
// Callback called by wgpuInstanceRequestAdapter when the request returns
|
||||||
|
// This is a C++ lambda function, but could be any function defined in the
|
||||||
|
// global scope. It must be non-capturing (the brackets [] are empty) so
|
||||||
|
// that it behaves like a regular C function pointer, which is what
|
||||||
|
// wgpuInstanceRequestAdapter expects (WebGPU being a C API). The workaround
|
||||||
|
// is to convey what we want to capture through the pUserData pointer,
|
||||||
|
// provided as the last argument of wgpuInstanceRequestAdapter and received
|
||||||
|
// by the callback as its last argument.
|
||||||
|
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status,
|
||||||
|
WGPUAdapter adapter, char const *message,
|
||||||
|
void *pUserData) {
|
||||||
|
UserData &userData = *reinterpret_cast<UserData *>(pUserData);
|
||||||
|
if (status == WGPURequestAdapterStatus_Success) {
|
||||||
|
userData.adapter = adapter;
|
||||||
|
} else {
|
||||||
|
std::cout << "Could not get WebGPU adapter: " << message << std::endl;
|
||||||
|
}
|
||||||
|
userData.requestEnded = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Call to the WebGPU request adapter procedure
|
||||||
|
wgpuInstanceRequestAdapter(instance /* equivalent of navigator.gpu */,
|
||||||
|
options, onAdapterRequestEnded, (void *)&userData);
|
||||||
|
|
||||||
|
// We wait until userData.requestEnded gets true
|
||||||
|
{
|
||||||
|
{Wait for request to end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(userData.requestEnded);
|
||||||
|
|
||||||
|
return userData.adapter;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// We create a descriptor
|
// We create a descriptor
|
||||||
WGPUInstanceDescriptor desc = {};
|
WGPUInstanceDescriptor desc = {};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue