-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating a webgl context in a webworker #10764
Comments
IIRC the model is to create the context on the main thread, so it is connected to the place where rendering appears. Then it is transferred to a worker (which emscripten should do automatically with those flags). Can see |
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant. |
Hi everyone, EmscriptenWebGLContextAttributes attr;
emscripten_webgl_init_context_attributes(&attr);
attr.explicitSwapControl = false; // all frames are implicitly displayed to the user, if true the frame must be commited
attr.proxyContextToMainThread = EMSCRIPTEN_WEBGL_CONTEXT_PROXY_DISALLOW;
attr.renderViaOffscreenBackBuffer = false;
auto ctx = emscripten_webgl_create_context("#canvas", &attr);
If I understand it correctly, i have to create a context on the main thread first, before i can transfer it using One thing that I also don't understand is if it's necessary to explicitly set Module["canvas]. var Module = {
onRuntimeInitialized: function() {
try {
Module["canvas"] = document.getElementById("canvas");
//call c++ functions
} catch (exception) {
// handle errors
}
} It would be very nice if you could clarify the things a bit for me @kripken 🙂 |
Hmm so i tried as @kripken suggested and created a canvas on the main thread before and then handed the context over to the worker thread. // Create a context on the main thread first:
EmscriptenWebGLContextAttributes webglAttr;
emscripten_webgl_init_context_attributes(&webglAttr);
emscripten_webgl_create_context("#canvas", &webglAttr);
// Then create a pthread and transfer the canvas to the created thread
pthread_attr_t attr;
pthread_attr_init(&attr);
emscripten_pthread_attr_settransferredcanvases(&attr, "#canvas");
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&m_rcLoopThread, &attr, [](void* myThis)-> void*{
reinterpret_cast<MyClass*>(myThis)->MyRenderFunction(); pthread_exit(0);
}, this);
Unfortunately tho this gives me a new error right when I create the thread: So transfering the control of a canvas element that has an existing context, doesn't seem to be the right way... |
I finally found the reason for this. That being said, I was always extremely confused how a test like this could pass then: https://github.com/emscripten-core/emscripten/blob/main/tests/gl_in_pthread.cpp Turns out that this thread is actually disabled on Chrome: https://github.com/emscripten-core/emscripten/blob/main/tests/test_browser.py#L4388 I guess the reason to solve this problem is not blocking on the main thread and instead use a proxy to the main thread (PROXY_TO_PTHREAD). |
Using version 1.39.11 and Firefox developer edition 75.0b8 (64-bit)
I'm trying to use WebGL and Offscreen rendering in a web worker. Flags for linking are:
The program gets stuck inside create context and I'm wondering what's the problem. As I understand the first parameter of
emscripten_webgl_create_context
is the name of the canvas - but since I use offscreen rendering I guess that is not needed?The text was updated successfully, but these errors were encountered: