From: pocopepe <[email protected]>
Replace emscripten_sleep() with a webgpu_await() helper that falls
back to wgpuInstanceProcessEvents() on native builds.
---
configure | 4 +++-
libavutil/hwcontext_webgpu.c | 23 ++++++++++++++++++++---
2 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/configure b/configure
index fe853f459c..dea2cae968 100755
--- a/configure
+++ b/configure
@@ -4251,6 +4251,7 @@ zscale_filter_deps="libzimg const_nan"
scale_vaapi_filter_deps="vaapi"
scale_vt_filter_deps="videotoolbox VTPixelTransferSessionCreate"
scale_vulkan_filter_deps="vulkan spirv_compiler spirv_library"
+scale_webgpu_filter_deps="webgpu"
vpp_qsv_filter_deps="libmfx"
vpp_qsv_filter_select="qsvvpp"
xfade_opencl_filter_deps="opencl"
@@ -7746,7 +7747,8 @@ enabled vdpau &&
enabled vdpau &&
check_lib vdpau_x11 "vdpau/vdpau.h vdpau/vdpau_x11.h"
vdp_device_create_x11 -lvdpau -lX11
-enabled webgpu && check_headers webgpu/webgpu.h
+enabled webgpu && { check_headers webgpu/webgpu.h ||
+ die "ERROR: WebGPU enabled but webgpu/webgpu.h not found."; }
# Check for the Vulkan headers
if enabled_all vulkan vulkan_static; then
diff --git a/libavutil/hwcontext_webgpu.c b/libavutil/hwcontext_webgpu.c
index ba1b206ade..aefde69683 100644
--- a/libavutil/hwcontext_webgpu.c
+++ b/libavutil/hwcontext_webgpu.c
@@ -18,7 +18,10 @@
#include <string.h>
+#ifdef __EMSCRIPTEN__
#include <emscripten.h>
+#endif
+
#include <webgpu/webgpu.h>
#include "hwcontext.h"
@@ -46,6 +49,20 @@ typedef struct {
int error;
} MapContext;
+/* Yield until a WebGPU callback sets *flag. Emscripten requires
+ * emscripten_sleep() to hand control back to the JS event loop;
+ * native builds use wgpuInstanceProcessEvents() instead. */
+static av_always_inline void webgpu_await(WGPUInstance instance, volatile int
*flag)
+{
+#ifdef __EMSCRIPTEN__
+ while (!*flag)
+ emscripten_sleep(1);
+#else
+ while (!*flag)
+ wgpuInstanceProcessEvents(instance);
+#endif
+}
+
static void on_adapter_ready(WGPURequestAdapterStatus status, WGPUAdapter
adapter,
WGPUStringView message, void *userdata1, void
*userdata2)
{
@@ -96,7 +113,7 @@ static int webgpu_device_create(AVHWDeviceContext *ctx,
const char *device,
priv->async_done = 0;
wgpuInstanceRequestAdapter(priv->p.instance, &adapter_opts, adapter_cb);
- while (!priv->async_done) emscripten_sleep(10);
+ webgpu_await(priv->p.instance, &priv->async_done);
if (!priv->p.adapter) {
av_log(ctx, AV_LOG_ERROR, "Failed to get WebGPU adapter.\n");
@@ -112,7 +129,7 @@ static int webgpu_device_create(AVHWDeviceContext *ctx,
const char *device,
priv->async_done = 0;
wgpuAdapterRequestDevice(priv->p.adapter, &dev_desc, device_cb);
- while (!priv->async_done) emscripten_sleep(10);
+ webgpu_await(priv->p.instance, &priv->async_done);
if (!priv->p.device) {
av_log(ctx, AV_LOG_ERROR, "Failed to get WebGPU device.\n");
@@ -284,7 +301,7 @@ static int webgpu_transfer_data_from(AVHWFramesContext
*hwfc, AVFrame *dst, cons
};
wgpuBufferMapAsync(staging_buffer, WGPUMapMode_Read, 0, buf_size, cb_info);
- while (!map_ctx.done) emscripten_sleep(10);
+ webgpu_await(priv->p.instance, &map_ctx.done);
if (map_ctx.error) {
wgpuBufferRelease(staging_buffer);
--
2.53.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]