Write Mask

From OpenGL.org
Revision as of 00:05, 22 February 2013 by Alfonse (Talk | contribs) (Color Mask)

Jump to: navigation, search

The Write Mask is the part of the rendering pipeline that allows or prevents color, depth, or stencil components from being written to the current framebuffer.

The Fragment Shader can output one or more color values, as well as a depth value. The stencil state also causes the emitting of a stencil value. After blending, logic ops, and depth/stencil tests, the fragment values can be written to a sample in the framebuffer. The masking state can prevent certain components from being written.

Note that masking state affects all functions that modify the framebuffer. This includes:

Color Mask

The color(s) output from the fragment shader can be masked. Each individual color component, R, G, B, and A, has a separate mask. So it is possible to prevent certain color components from being written.

Each separate color buffer, as defined by the glDrawBuffers function, has a separate mask. The mask state is not framebuffer state, so it is not bound to the Framebuffer Object or the Default Framebuffer.

To set the mask for a particular draw buffer, use the glColorMaski function:

void glColorMaski(GLuint buf​, GLboolean red​, GLboolean green​, GLboolean blue​, GLboolean alpha​);

The buf​ parameter specifies which of the color buffers to set the mask for. This can be any value on the range [0, GL_MAX_DRAW_BUFFERS). The other values are set to GL_TRUE to enable writing that component and GL_FALSE to disable writing it.

The function glColorMask can be used to set the mask for all draw buffers. This is useful for quickly re-enabling write masks on all output buffers.

Depth Mask

The depth buffer write can be masked, thus preventing the depth buffer from being updated. This useful for implementing transparency. Masking is controlled by this function:

 void glDepthMask(GLboolean flag​);

Setting flag​ to GL_TRUE means that the depth is written.

Stencil Mask

Stencil write masking works slightly different. The stencil buffer works in terms of a bitfield (usually with 8-bits). Therefore instead of taking a boolean value, the stencil mask is an integer:

 void glStencilMask(GLuint mask​);

The bits that are set in mask​ are the bits that will be written.