Creating a WebGL context is slow, should only be done if needed
#511

Assignees

Labels

Projects

Milestone

4 participants

I've come across some sites that create WebGL contexts on page load, when they really don't need them. These include github.com and economist.com.

Creating a WebGL context forces the browser to synchronously create a OpenGL context. That is slow, in a way that the browser can't control (depends on OS and drivers). To give you an order of magnitude, on linux, OpenGL context creation typically takes:

50 ms with Mesa drivers

120 ms with NVIDIA driver

200 ms with FGLRX driver

I don't think that GitHub really knew that they were slowing down their page load times by such amounts, when they started doing that with Modernizr. WebGL context creation should really only be done when you have a definite use for WebGL.

just an idea which came in my mind when i read this issue. maybe the testsuite should provide some sort of benchmark which measures the time which each test consumes. this could give you some further hints if some other tests also slow down the page loading process

Well, again, at least 2 mainstream websites are creating WebGL contexts on page load, through their usage of Modernizr, so I suspect the issue is widespread, even if it's only a documentation / communication issue.

But I guess I'll just try to delay OpenGL context creation until the first actual WebGL call is made, so that unused WebGL contexts don't cost so much.

benoit, in that test page i would recommend 1) not adding the canvas to the DOM and 2) getting your first timing before you create the canvas element. At least, that'd better match what we're trying to address with the issue.

By the way, this problem gets much worse on dual GPU MacBooks: if you're using the integrated GPU when you create the canvas, WebGL context creation needs to activate discrete graphics mode and takes around 1 second. (Imagine browsing GitHub and having that pause on every page load).

For Chromium that's been true a bit longer, at least for the past 2-3 weeks.

Explanation: on dual-GPU macs, it is 'officially' (word from Apple) a security issue, with current drivers, to let WebGL contexts switch GPUs during their lifetime, so we have no choice but to switch and force stay on the discrete GPU before creating a WebGL context.