Categories

WebGL Alpha

I’ve noticed some OpenGL developers having issues with how WebGL treats alpha in the backbuffer (ie, the canvas), so I thought it might be good to go over some of the differences between WebGL and OpenGL related to alpha.

The biggest difference between OpenGL and WebGL is that OpenGL renders to a backbuffer that is not composited with anything so, or effectively not composited with anything by the OS’s window manager, so it doesn’t matter what your alpha is.

WebGL is composited by the browser with the web page and the default is to use pre-multiplied alpha the same as .png <img> tags with transparency and 2d canvas tags.

WebGL has several ways to make this more like OpenGL.

#1) Tell WebGL you want it composited with non-premultiplied alpha

gl = canvas.getContext(
        "experimental-webgl", 
        {  
           premultipliedAlpha: false  // Ask non-premultiplied alpha
        }
     );

The default is true.

Of course the result will still be composited over page with whatever background color ends up being under the canvas (the canvas’s background color, the canvas’s container background color, the page’s background color, the stuff behind the canvas if the canvas has a z-index > 0, etc….) in other words, the color CSS defines for that area of the webpage.

I really good way to find if you have any alpha problems is to set the canvas’s background to a bright color like red. You’ll immediately see what is happening.

<canvas style="background: red;"> </canvas>

You could also set it to black which will hide any alpha issues you have.

#2) Tell WebGL you don’t want alpha in the backbuffer

gl = canvas.getContext("experimental-webgl", { alpha: false }};

This will make it act more like OpenGL since the backbuffer will only have RGB. This is probably the best option because a good browser could see that you have no alpha and actually optimize the way WebGL is composited. Of course that also means it actually won’t have alpha in the backbuffer so if you are using alpha in the backbuffer for some purpose that might not work for you. Few apps that I know of use alpha in the backbuffer. I think arguably this should have been the default.

#3) Clear alpha at the end of your rendering

  ..
  renderScene();
  ..
  // Set the backbuffer's alpha to 1.0
  gl.clearColor(1, 1, 1, 1);
  gl.colorMask(false, false, false, true);
  gl.clear(gl.COLOR_BUFFER_BIT);

Clearing is generally very fast as there is a special case for it in most hardware. I did this in most of my demos. If I was smart I’d switch to method #2 above. Maybe I’ll do that right after I post this. It seems like most WebGL libraries should default to this method. Those few developers that are actually using alpha for compositing effects can ask for it. The rest will just get the best perf and the least surprises.

#4) Clear the alpha once then don’t render to it anymore

  // At init time. Clear the back buffer.
  gl.clearColor(1,1,1,1);
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Turn off rendering to alpha
  gl.colorMask(true, true, true, false); 

Of course if you are rendering to your own framebuffers you may need to turn rendering to alpha back on and then turn it off again when you switch to rendering to the canvas.

#5) Handling Images

Also, if you are loading PNG files with alpha into textures, the default is that their alpha is pre-multiplied which is generally NOT the way most games do things. If you want to prevent that behavior you need to tell WebGL with

  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);

#6) Using a blending equation that works with pre-multiplied alpha.

Almost all OpenGL apps I’ve writing or worked on use

   gl.blendFunc(gl.SRC_ALPHA, gl_ONE_MINUS_SRC_ALPHA);

That works for non pre-multiplied alpha textures.

If you actually want to work with pre-multiplied alpha textures then you probably want

   gl.blendFunc(gl.ONE, gl_ONE_MINUS_SRC_ALPHA);

Those are the methods I’m aware of. If you know of more please post them below.

  • http://codeflow.org/ Florian Bösch

    Note that setting premultiplied alpha on the context affects alpha blending. You will have to multiply the output color by the output alpha yourself. Note also that almost all frameworks get this badly wrong (for instance three.js) which uses mixed multiply code, that sometimes does the multiply in the shader, and sometimes does not. The effect of which is that you might get white haloes or black borders.

  • http://codeflow.org/ Florian Bösch

    Note that setting premultiplied alpha on the context affects alpha blending. You will have to multiply the output color by the output alpha yourself. Note also that almost all frameworks get this badly wrong (for instance three.js) which uses mixed multiply code, that sometimes does the multiply in the shader, and sometimes does not. The effect of which is that you might get white haloes or black borders.

  • Mr.doob

    Uhm… do you think that would be a good idea if three.js would default to alpha: false? Is it more performant?

  • http://greggman.com greggman

    It ‘can’ be more preformant because the browser can turn off blending when compositing. Chrome does this. How much it impacts perf I don’t know.

  • http://greggman.com greggman

    Correct. Though just to be clear, premultiplied alpha is the default.

  • http://twitter.com/cosinusoidally Liam Wilson

    If you don’t have layers acceleration, and your browser is smart enough to bypass the compositor, then it will be vastly faster. The software compositers seem to eat all your CPU.

    If you are luck enough to have layers acceleration, it will still be faster as you are not wasting fillrate (which is a scarce resource on an integrated GPU) compositing an opaque surface on top of an other surface.

  • Mark Ivey

    In addition to turning off premultipliedAlpha, I’m finding it helpful to change the blending equation so the buffer’s transparency can’t increase when rendering.

    The standard gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) results in this blending equation for the alpha channel:
    A = Asrc * Asrc + Adest * (1 – Asrc)

    If you start with an opaque buffer (alpha=1) and render a transparent polygon (alpha=.5), the result is that the buffer is now partly transparent (alpha=.75).

    Using gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA), the blending equation becomes:
    A = Asrc + Adest(1 – Asrc)

    With this, rendering can only make the buffer more opaque. This matches how the 2D API works.