Skip to content

Commit 11d8ef7

Browse files
committed
Refactor: Added explicit loop termination while waiting for the device callback.
1 parent d570ca5 commit 11d8ef7

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/gpu/webgpu/SDL_gpu_webgpu.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ typedef struct WebGPURenderer
362362
WGPUInstance instance;
363363
WGPUAdapter adapter;
364364
WGPUDevice device;
365+
bool deviceError;
365366
WGPUQueue queue;
366367

367368
WindowData **claimedWindows;
@@ -1150,9 +1151,11 @@ static void WebGPU_RequestDeviceCallback(WGPURequestDeviceStatus status, WGPUDev
11501151
WebGPURenderer *renderer = (WebGPURenderer *)userdata;
11511152
if (status == WGPURequestDeviceStatus_Success) {
11521153
renderer->device = device;
1154+
renderer->deviceError = false;
11531155
SDL_Log("WebGPU device requested successfully");
11541156
} else {
11551157
SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to request WebGPU device: %s", message);
1158+
renderer->deviceError = true;
11561159
}
11571160
}
11581161

@@ -1414,6 +1417,8 @@ static void *WebGPU_MapTransferBuffer(
14141417
return NULL;
14151418
}
14161419

1420+
// Must be called to yield control to the browser so that it can tick the device for us.
1421+
// Without this, the mapping operation will never complete.
14171422
SDL_Delay(1);
14181423
}
14191424

@@ -4458,6 +4463,7 @@ static SDL_GPUDevice *WebGPU_CreateDevice(bool debug, bool preferLowPower, SDL_P
44584463
SDL_zero(*renderer);
44594464
renderer->debug = debug;
44604465
renderer->preferLowPower = preferLowPower;
4466+
renderer->deviceError = false;
44614467

44624468
// Initialize WebGPU instance so that we can request an adapter and then device
44634469
renderer->instance = wgpuCreateInstance(NULL);
@@ -4480,7 +4486,10 @@ static SDL_GPUDevice *WebGPU_CreateDevice(bool debug, bool preferLowPower, SDL_P
44804486
// This seems to be necessary to ensure that the device is created before continuing
44814487
// This should probably be tested on all browsers to ensure that it works as expected
44824488
// but Chrome's Dawn WebGPU implementation needs this to work
4483-
while (!renderer->device) {
4489+
// See: https://eliemichel.github.io/LearnWebGPU/basic-3d-rendering/input-geometry/playing-with-buffers.html
4490+
//
4491+
// This will not loop infinitely as the callback will set the device or deviceError
4492+
while (!renderer->device && !renderer->deviceError) {
44844493
SDL_Delay(1);
44854494
}
44864495

0 commit comments

Comments
 (0)