I am using glMapBufferRange with GL_MAP_UNSYNCHRONIZED_BIT to display a buffer object. Then I pass the returned pointer to the worker thread to asynchronously compute the new vertices. The object is buffered twice, so I can display one object while the other is being written. Using GL_MAP_UNSYNCHRONIZED_BIT gives me significantly better performance (mainly because glUnmapBuffer returns earlier), but I get some visual artifacts (despite double buffering), so I assume that either the GPU starts rendering while the DMA is still loading, or Workflow begins to write to the top too soon. If I understand glFenceSync, glWaitSync and glClientWaitSync correctly, then I should solve these problems as follows:
A: avoid rendering the GPU of the buffer object before completing the DMA process: immediately after glUnmapBufferRange, call the main thread
GLsync uploadSync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
glFlush();
glWaitSync(uploadSync, 0, GL_TIMEOUT_IGNORED);
B: avoid writing to the buffer from the workflow before the GPU finishes rendering it: direclty after glDrawElements, call the main thread
GLsync renderSync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
and in the workflow, just before you start writing data to a pointer that was previously returned from glMapBufferRange
glClientWaitSync(renderSync,0,100000000);
...start writing to the mapped pointer
1: Is my approach to explicit synchronization set correctly?
2: How can I handle the second case? I want to wait in a workflow (I don't want to make my main thread), but I can not release glCommands from the workflow. Is there any other way to check if GLsync was signaled other than calling gl?