emscripten-core / emscripten-core/emscripten
[Question] Wasm Workers and Module
- Lingua principale
- C++
- Stelle
- 27.6k
- Fork
- 3.6k
- Merge medio
- 1g 14h
- PR unite (30g)
- 125
Descrizione
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...
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Leggere la documentazione collegata di Wasm Workers, quindi confrontare il relativo modello di worker e moduli con il percorso load_from_url() di JavaScript e la chiamata glTexSubImage2D in C. Una risoluzione utile stabilirebbe se questa configurazione è supportata e individuerebbe il percorso di integrazione documentato appropriato, ma l’issue non indica alcun file sorgente né alcun test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- c, javascript, wasm
- Ambito
- computer-graphics, web-dev
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100