3 ms·
Some more background info why this project might be an important proof of concept, because it may allow to move a game's main loop into a web worker: In a brow
by flohofwoe 12y ago
Some more background info why this project might be an important proof of concept, because it may allow to move a game's main loop into a web worker:
In a browser environment (and other event driven platforms like iOS or Android) it is usually a bad idea to block the main thread (aka UI/browser/event thread) for more then a couple dozen milliseconds because the application would feel unresponsive. If the blocking goes on for seconds, the application will be terminated by the operating system or browser. For a game this means that the game 'cannot own the game loop' like on game consoles or Windows, instead all execution must happen in small per-frame-slices.
As a workaround it is a good idea to move the actual game loop into a thread, and keep the main/UI/browser thread free for UI/input event handling, and other stuff that needs to happen on the app's main thread (varies by platform, in the browser, calls to WebGL currently must happen on the main thread).
In emscripten this was difficult to implement because WebWorkers have a different threading model then traditional 'pthreads' (or may be it was always possible but no-one dared going there). Alon's work may show that it is feasable also in JS+WebGL to move the main loop into a worker thread (I haven't read the code yet whether the worker can actually run completely decoupled from the main thread).
Before Google Native Client was able to call GL from threads I implemented something similar (wrap all GL function calls and write them to a command buffer, and then on the main thread read the command buffer and issue the calls to GL). It worked but I wasn't very happy with it because: all GL calls which return a result would stall the producer thread, and GL is very chatty/verbose, lots of very small calls (e.g. glVertexAttribPointer, glUniform...), and each call now had some tiny additional overhead. In the end I wrote an engine-specific message protocol which was more abstract, thus reducing the number of commands drastically (by 10x or so). But the latency problem is still there (for instance in Chrome, all WebGL calls are encoded again into a command buffer, and decoded in another process), so in my current experimental/minimalistic 3D engine I'm back to not doing any threading at all (good for latency, bad for making use of available CPU resources).