A depth buffer, also known as a z-buffer, is a type of
data buffer used in
computer graphics
Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great deal ...
to represent depth information of objects in
3D space from a particular
perspective. Depth buffers are an aid to
rendering a scene to ensure that the correct
polygons properly occlude other polygons. Z-buffering was first described in 1974 by Wolfgang Straßer in his PhD thesis on fast algorithms for rendering occluded objects. A similar solution to determining overlapping polygons is the
painter's algorithm, which is capable of handling non-opaque scene elements, though at the cost of efficiency and incorrect results.
In a
3D-rendering pipeline, when an object is projected on the screen, the depth (z-value) of a generated
fragment
Fragment may refer to:
Entertainment
Television and film
* "Fragments" (''Torchwood''), an episode from the BBC TV series
* "Fragments", an episode from the Canadian TV series ''Sanctuary''
* "Fragments" (Steven Universe Future), an episode f ...
in the projected screen image is compared to the value already stored in the buffer (depth test), and replaces it if the new value is closer. It works in tandem with the
rasterizer
In computer graphics, rasterisation (British English) or rasterization (American English) is the task of taking an image described in a vector graphics format (shapes) and converting it into a raster image (a series of pixels, dots or lines, whic ...
, which computes the colored values. The fragment output by the rasterizer is saved if it is not overlapped by another fragment.
When viewing an image containing partially or fully overlapping opaque objects or surfaces, it is not possible to fully see those objects that are farthest away from the viewer and behind other objects (i.e., some surfaces are hidden behind others). If there were no mechanism for managing overlapping surfaces, surfaces would render on top of each other, not caring if they are meant to be behind other objects. The identification and removal of these surfaces are called the
hidden-surface problem. To check for overlap, the computer calculates the z-value of a pixel corresponding to the first object and compares it with the z-value at the same pixel location in the z-buffer. If the calculated z-value is smaller than the z-value already in the z-buffer (i.e., the new pixel is closer), then the current z-value in the z-buffer is replaced with the calculated value. This is repeated for all objects and surfaces in the scene (often in
parallel). In the end, the z-buffer will allow correct reproduction of the usual depth perception: a close object hides one further away. This is called z-culling.
The z-buffer has the same internal data structure as an image, namely a 2D-array, with the only difference being that it stores a single value for each screen pixel instead of color images that use 3 values to create color. This makes the z-buffer appear black-and-white because it is not storing color information. The buffer has the same dimensions as the screen buffer for consistency.
Primary visibility tests (such as
back-face culling) and secondary visibility tests (such as overlap checks and screen clipping) are usually performed on objects' polygons in order to skip specific polygons that are unnecessary to render. Z-buffer, by comparison, is comparatively
expensive
In production, research, retail, and accounting, a cost is the value of money that has been used up to produce something or deliver a service, and hence is not available for use anymore. In business, the cost may be one of acquisition, in which ...
, so performing primary and secondary visibility tests relieve the z-buffer of some duty.
The granularity of a z-buffer has a great influence on the scene quality: the traditional
16-bit
16-bit microcomputers are microcomputers that use 16-bit microprocessors.
A 16-bit register can store 216 different values. The range of integer values that can be stored in 16 bits depends on the integer representation used. With the two mos ...
z-buffer can result in
artifacts (called "
z-fighting" or stitching) when two objects are very close to each other. A more modern
24-bit or
32-bit
In computer architecture, 32-bit computing refers to computer systems with a processor, memory, and other major system components that operate on data in 32- bit units. Compared to smaller bit widths, 32-bit computers can perform large calcula ...
z-buffer behaves much better, although the problem cannot be eliminated without additional algorithms. An
8-bit
In computer architecture, 8-bit integers or other data units are those that are 8 bits wide (1 octet). Also, 8-bit central processing unit (CPU) and arithmetic logic unit (ALU) architectures are those that are based on registers or data buses of ...
z-buffer is almost never used since it has too little precision.
Uses
The Z-buffer is a technology used in almost all contemporary computers, laptops, and mobile phones for performing
3D computer graphics
3D computer graphics, or “3D graphics,” sometimes called CGI, 3D-CGI or three-dimensional computer graphics are graphics that use a three-dimensional representation of geometric data (often Cartesian) that is stored in the computer for t ...
. The primary use now is for
video games
Video games, also known as computer games, are electronic games that involves interaction with a user interface or input device such as a joystick, controller, keyboard, or motion sensing device to generate visual feedback. This feedb ...
, which require fast and accurate processing of 3D scenes. The Z-buffer is implemented in hardware within consumer
graphics cards. The Z-buffer is also used (implemented as software as opposed to hardware) for producing computer-generated special effects for films.
Furthermore, Z-buffer data obtained from rendering a surface from a light's point-of-view permits the creation of shadows by the
shadow mapping technique.
Developments
Even with small enough granularity, quality problems may arise when
precision in the z-buffer's distance values are not spread evenly over distance. Nearer values are much more precise (and hence can display closer objects better) than values that are farther away. Generally, this is desirable, but sometimes it will cause artifacts to appear as objects become more distant. A variation on z-buffering which results in more evenly distributed precision is called w-buffering (see
below
Below may refer to:
*Earth
* Ground (disambiguation)
* Soil
* Floor
* Bottom (disambiguation)
* Less than
*Temperatures below freezing
* Hell or underworld
People with the surname
* Ernst von Below (1863–1955), German World War I general
* Fr ...
).
At the start of a new scene, the z-buffer must be cleared to a defined value, usually 1.0, because this value is the upper limit (on a scale of 0 to 1) of depth, meaning that no object is present at this point through the
viewing frustum.
The invention of the z-buffer concept is most often attributed to
Edwin Catmull
Edwin Earl "Ed" Catmull (born March 31, 1945) is an American computer scientist who is the co-founder of Pixar and was the President of Walt Disney Animation Studios. He has been honored for his contributions to 3D computer graphics, including th ...
, although Wolfgang Straßer described this idea in his 1974 Ph.D. thesis months before Catmull's invention.
On more recent PC graphics cards (1999–2005), z-buffer management uses a significant chunk of the available
memory
Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembered ...
bandwidth. Various methods have been employed to reduce the performance cost of z-buffering, such as
lossless compression
Lossless compression is a class of data compression that allows the original data to be perfectly reconstructed from the compressed data with no loss of information. Lossless compression is possible because most real-world data exhibits statistic ...
(computer resources to compress/decompress are cheaper than bandwidth) and ultra-fast hardware z-clear that makes obsolete the "one frame positive, one frame negative" trick (skipping inter-frame clear altogether using signed numbers to cleverly check depths).
Z-culling
In
rendering, z-culling is early pixel elimination based on depth, a method that provides an increase in performance when rendering of hidden surfaces is costly. It is a direct consequence of z-buffering, where the depth of each pixel candidate is compared to the depth of the existing geometry behind which it might be hidden.
When using a z-buffer, a pixel can be culled (discarded) as soon as its depth is known, which makes it possible to skip the entire process of lighting and
texturing a pixel that would not be
visible anyway. Also, time-consuming
pixel shaders will generally not be executed for the culled pixels. This makes z-culling a good optimization candidate in situations where
fillrate, lighting, texturing, or pixel shaders are the main
bottlenecks.
While z-buffering allows the geometry to be unsorted, sorting
polygon
In geometry, a polygon () is a plane figure that is described by a finite number of straight line segments connected to form a closed '' polygonal chain'' (or ''polygonal circuit''). The bounded plane region, the bounding circuit, or the two t ...
s by increasing depth (thus using a reverse
painter's algorithm) allows each screen pixel to be rendered fewer times. This can increase performance in fillrate-limited scenes with large amounts of overdraw, but if not combined with z-buffering it suffers from severe problems such as:
* polygons might occlude one another in a cycle (e.g.: triangle A occludes B, B occludes C, C occludes A), and
* there is no canonical "closest" point on a triangle (e.g.: no matter whether one sorts triangles by their
centroid
In mathematics and physics, the centroid, also known as geometric center or center of figure, of a plane figure or solid figure is the arithmetic mean position of all the points in the surface of the figure. The same definition extends to any ...
or closest point or furthest point, one can always find two triangles A and B such that A is "closer" but in reality B should be drawn first).
As such, a reverse painter's algorithm cannot be used as an alternative to Z-culling (without strenuous re-engineering), except as an optimization to Z-culling. For example, an optimization might be to keep polygons sorted according to x/y-location and z-depth to provide bounds, in an effort to quickly determine if two polygons might possibly have an occlusion interaction.
Mathematics
The range of depth values in
camera space
In computer vision a camera matrix or (camera) projection matrix is a 3 \times 4 matrix which describes the mapping of a pinhole camera from 3D points in the world to 2D points in an image.
Let \mathbf be a representation of a 3D point in homog ...
to be rendered is often defined between a
and
value of
.
After a
perspective transformation, the new value of
, or
, is defined by:
:
After an
orthographic projection
Orthographic projection (also orthogonal projection and analemma) is a means of representing three-dimensional objects in two dimensions. Orthographic projection is a form of parallel projection in which all the projection lines are orthogonal ...
, the new value of
, or
, is defined by:
:
where
is the old value of
in camera space, and is sometimes called
or
.
The resulting values of
are normalized between the values of -1 and 1, where the
plane is at -1 and the
plane is at 1. Values outside of this range correspond to points which are not in the viewing
frustum
In geometry, a (from the Latin for "morsel"; plural: ''frusta'' or ''frustums'') is the portion of a solid (normally a pyramid or a cone) that lies between two parallel planes cutting this solid. In the case of a pyramid, the base faces are ...
, and shouldn't be rendered.
Fixed-point representation
Typically, these values are stored in the z-buffer of the hardware graphics accelerator in
fixed point format. First they are normalized to a more common range which is by substituting the appropriate conversion
into the previous formula:
:
Simplifying:
:
Second, the above formula is multiplied by
where d is the depth of the z-buffer (usually 16, 24 or 32 bits) and rounding the result to an integer:
:
This formula can be inverted and derived in order to calculate the z-buffer resolution (the 'granularity' mentioned earlier). The inverse of the above
:
:
where
The z-buffer resolution in terms of camera space would be the incremental value resulted from the smallest change in the integer stored in the z-buffer, which is +1 or -1. Therefore, this resolution can be calculated from the derivative of
as a function of
:
:
Expressing it back in camera space terms, by substituting
by the above
:
:
This shows that the values of
are grouped much more densely near the
plane, and much more sparsely farther away, resulting in better precision closer to the camera. The smaller
is, the less precision there is far away—having the
plane set too closely is a common cause of undesirable rendering artifacts in more distant objects.
To implement a z-buffer, the values of
are
linearly interpolated across screen space between the
vertices of the current
polygon
In geometry, a polygon () is a plane figure that is described by a finite number of straight line segments connected to form a closed '' polygonal chain'' (or ''polygonal circuit''). The bounded plane region, the bounding circuit, or the two t ...
, and these intermediate values are generally stored in the z-buffer in
fixed point format.
W-buffer
To implement a w-buffer,
the old values of
in camera space, or
, are stored in the buffer, generally in
floating point
In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be r ...
format. However, these values cannot be linearly interpolated across screen space from the vertices—they usually have to be
inverted, interpolated, and then inverted again. The resulting values of
, as opposed to
, are spaced evenly between
and
. There are implementations of the w-buffer that avoid the inversions altogether.
Whether a z-buffer or w-buffer results in a better image depends on the application.
Algorithmics
The following pseudocode demonstrates the process of z-buffering:
// First of all, initialize the depth of each pixel.
d(i, j) = infinite // Max length
// Initialize the color value for each pixel to the background color
c(i, j) = background color
// For each polygon, do the following steps :
for (each pixel in polygon's projection)
See also
*
Z-fighting
*
Irregular Z-buffer
*
Z-order
*
A-buffer
*
Depth map
In 3D computer graphics and computer vision, a depth map is an image or image channel that contains information relating to the distance of the surfaces of scene objects from a viewpoint. The term is related (and may be analogous) to ''depth ...
*
HyperZ
*
Stencil buffer
References
External links
Learning to Love your Z-buffer
Notes
{{DEFAULTSORT:Z-Buffering
3D rendering