Revision as of 11:51, 8 September 2012 by Alfonse (talk | contribs) (recategorization)

Jump to: navigation, search

Blending is the stage of OpenGL rendering pipeline that takes the fragment color outputs and combines them with the destination colors that they are adding to. Blending parameters can allow the source and destination colors for each output to be combined.


Blending can be used to make objects appear transparent. This is not its only use, but it is one of its main uses. Doing this requires that you take certain steps, some of them quite difficult to deal with. See the linked page for details.

Fragment Outputs




Draw Buffers Blend

Draw Buffers Blend
Core in version 4.5
Core since version 4.0
ARB extension ARB_draw_buffers_blend

Given the availability of this feature, the user can specify different blending parameters or equations for each buffer.

Note: All AMD/ATI 3.x-class hardware, the HD2000, 3000, and 4000 serieses, implement ARB_draw_buffers_blend. No pre-4.0 hardware from NVIDIA implements this.


Note that the glEnablei/glDisablei functions are not part of this functionality. The ability to enable/disable blending to specific buffers is core 3.0 functionality. The only post-3.x blending feature that changed was the ability to specify different blend parameters for different buffers.

Source, Destination, and the buffer


These represent all of the components of that color. Srgb is just the RGB components of the source color. Da is the alpha component of the destination color.


The source color S output from the fragment shader will be clamped to [0, 1] if the image format of the destination buffer that it is destined for is a normalized integer format. Otherwise, it will be used as given.

The destination color D read from the buffer can undergo changes as well. If the buffer in question uses an sRGB image format, and GL_FRAMEBUFFER_SRGB is currently enabled, then the value being read will be linearized, converting from the sRGB colorspace to a linear one. This happens before blending. Similarly, O will be converted from linear to sRGB if the enum is defined and if the buffer is an sRGB format.

If the buffer that D comes from contains an integral format (actual, non-normalized integers), then blending for that buffer is skipped entirely. S will be written directly to O.

Blend enabling

Blending must be enabled with a call to glEnablei(GL_BLEND, i), where i​ is an integer representing the buffer to enable blending on. Each buffer being written to by a fragment shader can have blending separately enabled and disabled. glDisablei can be used to disable blending.

Blending on all buffers can be enabled or disabled with glEnable and glDisable.

When blending is disabled to a buffer, the color from the fragment shader will be written directly to that buffer. When blending is enabled, one of the following blend equations will be used to determine the color written to that buffer.

Blend Equations



The two equations are set with this function:

 void glBlendEquationSeparate(GLenum modeRGB​, GLenum modeAlpha​);

The modeRGB​ is the equation to use for the RGB portion of the output color, and modeAlpha​ is the equation to use for the alpha portion.

The possible equations are as follows:

  • GL_FUNC_ADD: The source and destination colors are added to each other. O = sS + dD. The The s and d are blending parameters that are multiplied into each of S and D before the addition.
  • GL_FUNC_SUBTRACT: Subtracts the destination from the source. O = sS - dD. The source and dest are again multiplied by blending parameters.
  • GL_FUNC_REVERSE_SUBTRACT: Subtracts the source from the destination. O = sD - dS. The source and dest are multiplied by blending parameters.
  • GL_MAX: The output color is the component-wise maximum value of the source and dest colors. The parameters s and d are ignored for this equation.

Blending Parameters

Three of the blending equations modify the source and destination colors by parameters. These equations can be broken down into two separate equations:

* Orgb = srgb*Srgb + drgb * Drgb
* Oa = sa*Sa + da * Da

The same goes for the two subtraction equations. This separates the RGB component computation from the alpha computation. So there are 4 possible blending parameters.

These parameters are specified by this function:

 void glBlendFuncSeparate(GLenum srcRGB​, GLenum dstRGB​, GLenum srcAlpha​, GLenum dstAlpha​)

The srcRGB​ value specifies the srgb parameter. dstRGB​ specifies drgb. srcAlpha​ is for sa, while dstAlpha​ specifies da.

For blend equations that do not use parameters (GL_MIN and GL_MAX), the parameter values are meaningless. They do not affect the output values, and you do not have to set them.

The values that the parameters can take is as follows.

Constant Values

The parameters GL_ONE and GL_ZERO are constant values. GL_ONE means 1.0, and GL_ZERO means 0.0.




GL_SRC_COLOR and GL_DST_COLOR represent S and D themselves, as appropriate for the particular equation. So setting GL_SRC_COLOR in the RGB equation means getting Srgb; setting it in the alpha equation means getting Sa.


There are also GL_ONE_MINUS_SRC_COLOR and GL_ONE_MINUS_DST_COLOR. These are exactly what they say: 1.0 - the value of those colors. This is a component-wise subtraction. Note that source and dest are not limited to the [0, 1] range if the destination buffer is a floating-point buffer.


The traditional alpha blend is set up as follows:

glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);



glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);

Blend Color


 void glBlendColor(GLclampf red​, GLclampf green​, GLclampf blue​, GLclampf alpha​);



Dual Source Blending





 void glBindFragDataLocationIndexed(uint program​, uint colorNumber​, uint index​, const char * name​);

program​ is the program object, colorNumber​ is the buffer index, index​ is the color number (0 or 1), and name​ is the string name of the output variable.


