Skip to content

Commit 3f05471

Browse files
committed
Add helper t owebgl1.c for extracting target thread from context. NFC
Also, make one of the pthread_keys static since its only used in this one file. Split out from emscripten-core#20678
1 parent 93b2135 commit 3f05471

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

system/lib/gl/webgl1.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#if defined(__EMSCRIPTEN_PTHREADS__) && defined(__EMSCRIPTEN_OFFSCREEN_FRAMEBUFFER__)
2020

21-
pthread_key_t currentActiveWebGLContext;
21+
static pthread_key_t currentActiveWebGLContext;
2222
pthread_key_t currentThreadOwnsItsWebGLContext;
2323
static pthread_once_t tlsInit = PTHREAD_ONCE_INIT;
2424

@@ -27,6 +27,10 @@ static void InitWebGLTls() {
2727
pthread_key_create(&currentThreadOwnsItsWebGLContext, NULL);
2828
}
2929

30+
static pthread_t GetCurrentTargetThread() {
31+
return *(void**)(pthread_getspecific(currentActiveWebGLContext) + 4);
32+
}
33+
3034
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE emscripten_webgl_create_context(const char *target, const EmscriptenWebGLContextAttributes *attributes) {
3135
GL_FUNCTION_TRACE();
3236
if (!attributes) {
@@ -111,7 +115,7 @@ void glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage
111115
if (size < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
112116
void *ptr = memdup(data, size);
113117
if (ptr || !data) { // glBufferData(data=0) can always be handled asynchronously
114-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIIII, &emscripten_glBufferData, ptr, target, size, ptr, usage);
118+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIIII, &emscripten_glBufferData, ptr, target, size, ptr, usage);
115119
return;
116120
}
117121
// Fall through on allocation failure and run synchronously.
@@ -130,7 +134,7 @@ void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void
130134
if (size < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
131135
void *ptr = memdup(data, size);
132136
if (ptr || !data) {
133-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIIII, &emscripten_glBufferSubData, ptr, target, offset, size, ptr);
137+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIIII, &emscripten_glBufferSubData, ptr, target, offset, size, ptr);
134138
return;
135139
}
136140
// Fall through on allocation failure and run synchronously.
@@ -266,7 +270,7 @@ void glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei widt
266270
if (!pixels || (sz >= 0 && sz < 256*1024)) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
267271
void *ptr = memdup(pixels, sz);
268272
if (ptr || !pixels) {
269-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIIIIIIIII, &emscripten_glTexImage2D, ptr, target, level, internalformat, width, height, border, format, type, ptr);
273+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIIIIIIIII, &emscripten_glTexImage2D, ptr, target, level, internalformat, width, height, border, format, type, ptr);
270274
return;
271275
}
272276
// Fall through on allocation failure and run synchronously.
@@ -291,7 +295,7 @@ void glTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, G
291295
if (!pixels || (sz >= 0 && sz < 256*1024)) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
292296
void *ptr = memdup(pixels, sz);
293297
if (ptr || !pixels) {
294-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIIIIIIIII, &emscripten_glTexSubImage2D, ptr, target, level, xoffset, yoffset, width, height, format, type, ptr);
298+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIIIIIIIII, &emscripten_glTexSubImage2D, ptr, target, level, xoffset, yoffset, width, height, format, type, ptr);
295299
return;
296300
}
297301
// Fall through on allocation failure and run synchronously.
@@ -313,7 +317,7 @@ void glUniform1fv(GLint location, GLsizei count, const GLfloat *value) {
313317
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
314318
void *ptr = memdup(value, sz);
315319
if (ptr) {
316-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIII, &emscripten_glUniform1fv, ptr, location, count, (GLfloat*)ptr);
320+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIII, &emscripten_glUniform1fv, ptr, location, count, (GLfloat*)ptr);
317321
return;
318322
}
319323
// Fall through on allocation failure and run synchronously.
@@ -335,7 +339,7 @@ void glUniform1iv(GLint location, GLsizei count, const GLint *value) {
335339
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
336340
void *ptr = memdup(value, sz);
337341
if (ptr) {
338-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIII, &emscripten_glUniform1iv, ptr, location, count, (GLint*)ptr);
342+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIII, &emscripten_glUniform1iv, ptr, location, count, (GLint*)ptr);
339343
return;
340344
}
341345
// Fall through on allocation failure and run synchronously.
@@ -356,7 +360,7 @@ void glUniform2fv(GLint location, GLsizei count, const GLfloat *value) {
356360
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
357361
void *ptr = memdup(value, sz);
358362
if (ptr) {
359-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIII, &emscripten_glUniform2fv, ptr, location, count, (GLfloat*)ptr);
363+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIII, &emscripten_glUniform2fv, ptr, location, count, (GLfloat*)ptr);
360364
return;
361365
}
362366
// Fall through on allocation failure and run synchronously.
@@ -378,7 +382,7 @@ void glUniform2iv(GLint location, GLsizei count, const GLint *value) {
378382
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
379383
void *ptr = memdup(value, sz);
380384
if (ptr) {
381-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIII, &emscripten_glUniform2iv, ptr, location, count, (GLint*)ptr);
385+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIII, &emscripten_glUniform2iv, ptr, location, count, (GLint*)ptr);
382386
return;
383387
}
384388
// Fall through on allocation failure and run synchronously.
@@ -399,7 +403,7 @@ void glUniform3fv(GLint location, GLsizei count, const GLfloat *value) {
399403
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
400404
void *ptr = memdup(value, sz);
401405
if (ptr) {
402-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIII, &emscripten_glUniform3fv, ptr, location, count, (GLfloat*)ptr);
406+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIII, &emscripten_glUniform3fv, ptr, location, count, (GLfloat*)ptr);
403407
return;
404408
}
405409
// Fall through on allocation failure and run synchronously.
@@ -421,7 +425,7 @@ void glUniform3iv(GLint location, GLsizei count, const GLint *value) {
421425
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
422426
void *ptr = memdup(value, sz);
423427
if (ptr) {
424-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIII, &emscripten_glUniform3iv, ptr, location, count, (GLint*)ptr);
428+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIII, &emscripten_glUniform3iv, ptr, location, count, (GLint*)ptr);
425429
return;
426430
}
427431
// Fall through on allocation failure and run synchronously.
@@ -442,7 +446,7 @@ void glUniform4fv(GLint location, GLsizei count, const GLfloat *value) {
442446
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
443447
void *ptr = memdup(value, sz);
444448
if (ptr) {
445-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIII, &emscripten_glUniform4fv, ptr, location, count, (GLfloat*)ptr);
449+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIII, &emscripten_glUniform4fv, ptr, location, count, (GLfloat*)ptr);
446450
return;
447451
}
448452
// Fall through on allocation failure and run synchronously.
@@ -464,7 +468,7 @@ void glUniform4iv(GLint location, GLsizei count, const GLint *value) {
464468
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
465469
void *ptr = memdup(value, sz);
466470
if (ptr) {
467-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIII, &emscripten_glUniform4iv, ptr, location, count, (GLint*)ptr);
471+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIII, &emscripten_glUniform4iv, ptr, location, count, (GLint*)ptr);
468472
return;
469473
}
470474
// Fall through on allocation failure and run synchronously.
@@ -483,7 +487,7 @@ void glUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, cons
483487
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
484488
void *ptr = memdup(value, sz);
485489
if (ptr) {
486-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIIII, &emscripten_glUniformMatrix2fv, ptr, location, count, transpose, (GLfloat*)ptr);
490+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIIII, &emscripten_glUniformMatrix2fv, ptr, location, count, transpose, (GLfloat*)ptr);
487491
return;
488492
}
489493
// Fall through on allocation failure and run synchronously.
@@ -502,7 +506,7 @@ void glUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, cons
502506
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
503507
void *ptr = memdup(value, sz);
504508
if (ptr) {
505-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIIII, &emscripten_glUniformMatrix3fv, ptr, location, count, transpose, (GLfloat*)ptr);
509+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIIII, &emscripten_glUniformMatrix3fv, ptr, location, count, transpose, (GLfloat*)ptr);
506510
return;
507511
}
508512
// Fall through on allocation failure and run synchronously.
@@ -521,7 +525,7 @@ void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, cons
521525
if (sz < 256*1024) { // run small buffer sizes asynchronously by copying - large buffers run synchronously
522526
void *ptr = memdup(value, sz);
523527
if (ptr) {
524-
emscripten_dispatch_to_thread(*(void**)(pthread_getspecific(currentActiveWebGLContext) + 4), EM_FUNC_SIG_VIIII, &emscripten_glUniformMatrix4fv, ptr, location, count, transpose, (GLfloat*)ptr);
528+
emscripten_dispatch_to_thread(GetCurrentTargetThread(), EM_FUNC_SIG_VIIII, &emscripten_glUniformMatrix4fv, ptr, location, count, transpose, (GLfloat*)ptr);
525529
return;
526530
}
527531
// Fall through on allocation failure and run synchronously.

system/lib/gl/webgl_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ void _emscripten_proxied_gl_context_activated_from_main_browser_thread(EMSCRIPTE
6262

6363
#include <pthread.h>
6464

65-
extern pthread_key_t currentActiveWebGLContext;
6665
extern pthread_key_t currentThreadOwnsItsWebGLContext;
6766

6867
// When building with multithreading, return pointers to C functions that can perform proxying.

0 commit comments

Comments
 (0)