Last active
April 5, 2023 00:53
-
-
Save mattebb/69aa7c504e292761229ce78fb4db02c1 to your computer and use it in GitHub Desktop.
Waiting for webgl to finish rendering using fenceSync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// assembled from some stackoverflow answers | |
const start = performance.now(); | |
// Draw the fullscreen quad | |
twgl.drawBufferInfo(gl, quad_bufferInfo); | |
let sync = gl.fenceSync(gl.SYNC_GPU_COMMANDS_COMPLETE, 0); | |
checkSync = () => { | |
const status = gl.clientWaitSync(sync, 0, 0); | |
switch (status) { | |
case gl.TIMEOUT_EXPIRED: | |
return setTimeout(checkSync); | |
case gl.WAIT_FAILED: | |
throw new Error('should never get here'); | |
default: | |
gl.deleteSync(sync); | |
const duration = (performance.now() - start); | |
console.log(`finished rendering in ${duration.toFixed(1)}ms`); | |
// trigger preview capture now that GL has finished rendering | |
triggerPreview(); | |
} | |
} | |
setTimeout(checkSync); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment