wjpu
@wjpu

TLDR: I saved this as a draft to post as a rubber ducky, life got in the way and i had to take a nap :3 now, refreshed, i immediatly noticed that i was calling wgpuAdapterRequestDevice with the address of userData which here is a pointer but in the original sourcecode was a variable on the stack.

Why did I switch that around?
Because for some reason at the time it was easier for me to think of both the result variable and the callback userData of scratchmem. The result var had to be a pointer to scratchmem so i had to allocate scratchmem anyway. i guess this goes to show: memory is all the same, just with different lifetimes xD ok so here is the rubber ducky before i went to bed:

in my latest commit, i'm having trouble getting a device, im using the same "retrieval mechanism" as with the adapter in my requests so i dont know what to do next.

the code is as follows, i will make it a bit more literate with comments and debuglines:

// this gets called by the request upon completion:
void onDeviceRequestEnded_callback(
                                   // how did the request pan out:
                                   WGPURequestDeviceStatus status,
                                   // the sought after device
                                   WGPUDevice device,
                                   // explanations in case of failure
                                   char const * message,
                                   // a place to place the device for the requesteur to pick up from
                                   void * pUserData) {
    // casting goes brrrrr
    UserData *userData = (UserData*)(pUserData);
    // so u got one?
    if (status == WGPURequestDeviceStatus_Success) {
        // obvio
        printf("Got WebGPU device!\n");
        // gotta place it down
        userData->device = device;
    } else {
        // obvion't
        printf("Could not get WebGPU device: ");
        printf(message);
    }
    // this is the end of the waiting game:
    userData->requestEnded = true;
}


/**
 * Utility function to get a WebGPU device, so that
 *     WGPUAdapter device = requestDevice(adapter, options);
 * is roughly equivalent to
 *     const device = await adapter.requestDevice(descriptor);
 * It is very similar to requestAdapter
 */
WGPUDevice *requestDevice(WGPUAdapter adapter,
                          WGPUDeviceDescriptor const * descriptor) {
    // need some mem for our return envelope:
    UserData *userData = janet_smalloc(sizeof(UserData));

    // set everything up for placing
    userData->device = NULL;
    // and waiting
    userData->requestEnded = false;
    // one last look at it
    printf("requesting device %I64d\n", userData->device);

    // off it goes
    wgpuAdapterRequestDevice(
        adapter,
        descriptor,
        onDeviceRequestEnded_callback,
        (void*)&userData
    );

    // not long and we will have a device
    printf("will wait for device now\n");
    int64_t i = 1;
    for(; i != 0 || !userData->requestEnded;)
    {
      i++; // in fact, it doesnt even hit the incrementer on my machine even once
// now im unsure if the waiting condition is correct... but i do get a device... and it is here where it is crashing..
    }
    printf("got device after %I64d cycles\n", i);
    printf("device %I64d\n", userData->device);
    
    printf("do we need to free userData? %I64d\n", userData);
    WGPUDevice *result = janet_smalloc(sizeof(WGPUDevice));
    *result = userData->device;
    printf("device still is %I64d\n", *result);
    janet_sfree(userData);
    printf("so do we need to free or not? %I64d\n", userData);
    return result;
}



You must log in to comment.