regl-project / regl

👑 Functional WebGL
https://regl-project.github.io/
MIT License
5.21k stars 322 forks source link

[Q] How to prevent drawing of duplicate frames? #645

Open tim-steg opened 2 years ago

tim-steg commented 2 years ago

Hi,

I am currently developing a video player in Regl that allows the user to zoom and pan whilst playing a live video feed. In an effort to reduce system resource consumption, I would like to make sure a frame is not drawn to the screen if nothing has changed from the last frame (which I have successfully kept track of), but is there a way to keep the old frame around and temporarily not draw anything in the regl.frame() loop?

Would something like this work?:

regl.frame(() => {
  if (dontDraw() === true) {
    return;
  }

  drawVideoShader({
     // shader params go here
  });
});

Thank you!

rreusser commented 2 years ago

Yes, the above should work just fine! Come to think of it, I don't know exactly what triggers this mechanism, but since WebGL uses double-buffering, if you don't draw anything the image should stay on the screen.

rreusser commented 2 years ago

Ah, maybe this is roughly what triggers it. I'd just always taken it for granted and hadn't thought about how it works: https://stackoverflow.com/questions/33327585/why-webgl-clear-draw-to-front-buffer

Anytime you do anything that effects the WebGL drawingBuffer (think "backbuffer) it gets marked to swap/copy.