Seemed to make an attempt at running but no valid adapter found on laptop
This commit is contained in:
parent
45fbf0abca
commit
cbd4b0f0e9
1 changed files with 34 additions and 39 deletions
75
src/main.c
75
src/main.c
|
|
@ -3,48 +3,35 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <webgpu/webgpu.h>
|
#include <webgpu/webgpu.h>
|
||||||
|
|
||||||
WGPUAdapter requestAdapterSync(WGPUInstance instance,
|
static void OnRequestAdapter(WGPURequestAdapterStatus status,
|
||||||
WGPURequestAdapterOptions const *options) {
|
WGPUAdapter adapter, WGPUStringView message,
|
||||||
typedef struct {
|
void *userdata1, void *userdata2) {
|
||||||
WGPUAdapter adapter;
|
// TODO handle messages and errors better
|
||||||
bool requestEnded;
|
if (status != WGPURequestAdapterStatus_Success) {
|
||||||
} UserData;
|
printf("%s", message.data);
|
||||||
|
|
||||||
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;
|
assert(status == WGPURequestAdapterStatus_Success);
|
||||||
|
|
||||||
|
WGPUAdapter *result = userdata1;
|
||||||
|
if (*result == NULL) {
|
||||||
|
*result = adapter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void requestAdapterSync(WGPUInstance instance,
|
||||||
|
WGPURequestAdapterOptions const *options,
|
||||||
|
WGPUAdapter *adapter) {
|
||||||
|
|
||||||
|
WGPURequestAdapterCallbackInfo info = {.mode = WGPUCallbackMode_WaitAnyOnly,
|
||||||
|
.callback = &OnRequestAdapter,
|
||||||
|
.userdata1 = adapter};
|
||||||
|
WGPUFuture future = wgpuInstanceRequestAdapter(instance, options, info);
|
||||||
|
WGPUFutureWaitInfo wait = {
|
||||||
|
.future = future,
|
||||||
};
|
};
|
||||||
|
WGPUWaitStatus status = wgpuInstanceWaitAny(instance, 1, &wait, 0);
|
||||||
// Call to the WebGPU request adapter procedure
|
assert(status == WGPUWaitStatus_Success);
|
||||||
wgpuInstanceRequestAdapter(instance /* equivalent of navigator.gpu */,
|
assert(adapter);
|
||||||
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() {
|
||||||
|
|
@ -65,6 +52,14 @@ int main() {
|
||||||
// copied around without worrying about its size).
|
// copied around without worrying about its size).
|
||||||
printf("WGPU instance: %p\n", instance);
|
printf("WGPU instance: %p\n", instance);
|
||||||
|
|
||||||
|
printf("Requesting Adapter.\n");
|
||||||
|
WGPUAdapter adapter = NULL;
|
||||||
|
WGPURequestAdapterOptions adapterOpts = {0};
|
||||||
|
requestAdapterSync(instance, &adapterOpts, &adapter);
|
||||||
|
printf("Got Adapter: %p\n", &adapter);
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
wgpuAdapterRelease(adapter);
|
||||||
wgpuInstanceRelease(instance);
|
wgpuInstanceRelease(instance);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue