[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Public WebGL] Event on SwapBuffers

Forgive me if this has been brought up before, I'm fairly new to the mailing list.

I've been experimenting with the idea of creating a browser extension that would allow users to inject limited post-process effects into any WebGL canvas. Essentially the extension would work by redirecting gl.bindFramebuffer to force scenes to render into a texture, then just before end-of-frame render that texture with an arbitrary effect shader. The target effect would be FXAA but any image-based effect would work (greyscale, edge detection, etc.)

The catch to this whole thing is that you have to accurately detect when the frame is done rendering or, more accurately, when the backbuffer is going to be flipped. Unfortunately as WebGL handles that for you automatically it can be difficult to determine. My current hack of a solution is to also override requestAnimationFrame and setTimeout/Interval, detect if glDrawArrays/Elements is called within the callback of those methods, and if so perform my post process step before relinquishing control back to the browser. As you can imagine, however, this is a messy processes and is imperfect since it's possible to have applications that render outside the "expected" draw loop. For example, a paint application may render within the mouseMove event. Of course best practices would dictate otherwise, but we all know how often the web follows best practices in their development.

What really surprised me, however, was that upon looking at the code for Ben Vanik's WebGL Inspector I found a very similar procedure to my own, complete with the same concerns voiced in the comments. This leads me to believe that although the average WebGL developer will probably never need to know the details of when a buffer swap is going to happen, such information would be valuable to those writing tools or other "non-typical" applications.

As such, I would like to suggest that an API be made available for monitoring when SwapBuffers (or the platform's equivalent function) is called. For the use cases that I have seen a callback or an event that occurs just before the buffers are swapped, allowing developers to inject last-second draw calls for tool overlays or post-process effects as I described above. It may also be useful to allow the swap to be canceled from this event, though I can't think of a use case for it at the moment. It would also be useful to have an event that fires after the swap has occurred. I would imagine this would be used for performance profiling and statistics generation more than anything else.

Not being terribly familiar with the various browser's code bases I'm not sure if there are any practical matters that would prevent such an API from being implemented, but I'm certain that the development community could make good use of them should they be made available!

--Brandon Jones