Why is RenderingContext.drawElements clearing the screen before it draws?

陌路散爱 提交于 2019-12-11 10:27:00

问题


Is RenderingContext.drawElements correct when it clears the screen before it draws?

Consider these screenshots that show a step across a call to drawElements with the object already drawn being erased.


回答1:


WebGL effectively clears the screen after the page has been composited. When you're stepping through stuff one line at a time it's going to be composited every time you stop.

If you don't want it to be cleared ask for preserveDrawingBuffer: true when you create the WebGL context as in

gl = someCanvas.getContext("webgl", { preserveDrawingBuffer: true });

As for why, from the spec

While it is sometimes desirable to preserve the drawing buffer, it can cause significant performance loss on some platforms. Whenever possible this flag should remain false and other techniques used. Techniques like synchronous drawing buffer access (e.g., calling readPixels or toDataURL in the same function that renders to the drawing buffer) can be used to get the contents of the drawing buffer. If the author needs to render to the same drawing buffer over a series of calls, a Framebuffer Object can be used.

Implementations may optimize away the required implicit clear operation of the Drawing Buffer as long as a guarantee can be made that the author cannot gain access to buffer contents from another process. For instance, if the author performs an explicit clear then the implicit clear is not needed.

The TL;DR version is preserveDrawingBuffer: false (the default) allows WebGL to swap buffers when compositing (that doesn't mean it will swap buffers but it can if it chooses to). preserveDrawingBuffer: true means it can't swap buffers, it must copy buffers. Copying is much slower than swapping.



来源:https://stackoverflow.com/questions/27812153/why-is-renderingcontext-drawelements-clearing-the-screen-before-it-draws

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!