ThatOpen / engine_components

MIT License
328 stars 129 forks source link

Adapt culling to only read pixels when GPU is ready #26

Closed harrycollin closed 1 year ago

harrycollin commented 1 year ago

https://github.com/IFCjs/components/blob/278bab5592e6875f0a7128823790a90c65d9e41b/library/src/fragment/fragment-culling.ts#L167

We can adapt this behaviour to only read the pixels when the GPU is ready. As far as I understand the function renderer.readRenderTargetPixels() is synchronous and will block everything until the GPU has fully finished rendering. In testing, I noticed this caused a significant delay of at least 15ms on my machine. With the snippet below, we can delay this function call until the GPU is ready. Credit goes to this Stackoverflow answer for the snippet.

// Here a piece of code for checking if the GPU is ready before doing readpixels, place this after the draw
this.webGLSync = gl.fenceSync(gl.SYNC_GPU_COMMANDS_COMPLETE, 0);

// Then you can check if the samples are ready with
checkSamplesReady() {
  return this.gl.clientWaitSync(this.webGLSync, 0, 0) === this.gl.CONDITION_SATISFIED;
}

I've tested a version of this locally and looks to be working. I will make a PR with the implementation soon.

agviegas commented 1 year ago

I think this is done!