emscripten-core / emscripten-core/emscripten

[Question] Wasm Workers and Module

Offen
#19,682 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C++
Sterne
27.6k
Forks
3.6k
Ø Merge
1 T. 14 Std.
Gemergte PRs (30 T.)
125

Beschreibung

I am loading media into my WebGL C code. Videos, WebCam streams and images. I use the browser to decode them and send them to my c code, where they are uploaded to the GPU. Here is the image case:
```javascript
async function load_from_url(url) {
try {
const response = await fetch(url);
const blob = await response.blob();
const bitmap = await createImageBitmap(blob);

const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
const ctx = canvas.getContext('2d');

ctx.drawImage(bitmap, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const dataPtr = Module._malloc(imageData.data.length);

Module.HEAPU8.set(imageData.data, dataPtr);
Module.ccall(
'media_setup',
'number',
['number', 'number', 'number'],
[dataPtr, canvas.width, canvas.height]
);
Module._free(dataPtr);
} catch (err) {
console.error(err);
}
};
```
The c code does essentially just:
```c
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
```
Works great!
Of course I am able to decode the blob using SDL2_Image, but once we get to webcams and hardware accelerated video decode, this approach of decoding in WASM breaks down. The overhead is quite painful, since we cannot access the browsers decode functions directly to get pixel data, but have to draw it to an invisible canvas and get those pixels copied. (Any grave misunderstandings from my side this far?)

So I want to do as much of this in a separate web worker. Simply spawning a worker is a no-go though, since `Module` isn't known by the worker. So I guess I have to use `-s WASM_WORKERS` and call `load_from_url()` from the C code? Is this logic sound? Will this even work? Reading through https://emscripten.org/docs/api_reference/wasm_workers.html I get the feeling, that I am gravely misusing the WASM API here...

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Lesen Sie die verlinkte Wasm Workers-Dokumentation und vergleichen Sie anschließend deren Worker- und Modulmodell mit dem JavaScript-Pfad load_from_url() und dem C-Aufruf glTexSubImage2D. Eine hilfreiche Klärung würde feststellen, ob dieses Setup unterstützt wird, und den passenden dokumentierten Integrationspfad identifizieren, aber das Issue nennt keine Quelldatei und keinen Test.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, javascript, wasm
Bereich
computer-graphics, web-dev
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.