emscripten-core / emscripten-core/emscripten

glDrawArrays appears to incorrectly share/overwrite memory with pthreads unless sync'd to browser thread

Open
#16,404 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

This report may be insufficient to move forward, but I'd like to share our observations.
EMSDK 3.1.2
FULL_ES2 when linking

glDrawArrays is called frequently from a pthread.
If we don't do a workaround, what is drawn is distorted and feels like the vectors are either the wrong vectors, or overritten.
NOTE: This is the only gl call that we have observed as having a problem.

Here is a portion of the screen showing the distortion.
image
Image NOTE: The grey triangle and the black are both signs of the problem

When correctly rendered, it would look like this:
![image](https://user-images.githubusercontent.com/4826107/156249316-714b647c-9aff-4022-a9b2-65a19f8edb2b.png)

Image NOTE:The upper part of that screenshot is react components. The visuals below Home and Workspace is where our emscripten canvas starts. The 2 white/blue images are not drawn via glDrawArrays and are unaffected by the problem.

The following workarounds work: (doing either of these operations right after each glDrawArrays)
- glGetError() This is a sync call that just gets an error code from the main browser thread.
- MAIN_THREAD_EM_ASM( { // Do nothing});}. After checking what glGetError did, we suspected the sync to main thread was what prevented the problem. This too suppresses the problem
- usleep(1000). Adding a large enough sleep worked "most of the time". We could still see cases where there would be display anomolies

We share much of this code with another platform, Android, and on that platform, we do not see these anomolies and suspect that it may be at the Emscripten layer.

In reading the glDrawArrays implementation it is not clear if the memory indirectly passed in is transient and used immediately, or if there is some async/queueing that is in play either by Emscripten or by WebGl that needs to be considered.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.