Z buffer and W Buffer

Source: Internet
Author: User

Almost all current 3D display chips have Z buffer or W buffer. However, we can often see that some
W buffer has some basic problems, such as the usage of Z buffer, Z buffer and W Buffer
Or accuracy issues. The purpose of this article is to briefly introduce Z buffer and W buffer.
What is the use of Z buffer and W buffer? Their main purpose is to remove the hidden surface, that is, the hidden surface.
Elimination (or find the visible surface detemination, which means the same ). In 3D
In a drawing, as long as there are more than two triangular surfaces, a certain triangular surface may cover another triangular surface. This is obvious, because near things always cover the distance (assuming these triangles are not transparent ). Therefore
In 3D scenarios, to draw correct results, you must solve this problem.
  
However, this problem is quite difficult, because it involves the relationship between triangles, not just a triangle. Therefore, when removing hidden faces, you need to consider all the three
Corner. This makes the problem quite complex. In addition, the triangle is often not covered, but only a part is covered. Therefore, this makes the problem more complicated.

  
The simplest way to remove hidden faces is Painter's
Algorithm ). The principle of this method is very simple, that is, to draw distant things and then draw near things. In this way, the near things will naturally cover the distant things. Because the painter of oil painting usually uses this method, this method is called the painter algorithm 」. Is an example:

The circle in red is the farthest, so it is first painted. Then there is a yellow triangle, and finally a gray square. You can remove hidden Surfaces by drawing them in the order of distance. Therefore, you only need
In 3D scenarios, the triangle surface is sorted by distance of the observer, and then painted from the far triangle surface. The correct result can be drawn.
  
However, it is not that ideal. In 3D
In a scenario, a triangle surface may be far from the ground and near some places, because the triangle surface has three vertices, and the distance between these three vertices and the observer is usually different. So which vertex is used for sorting? Or is it sorted in the center of a triangle? In fact, no matter what the sort is based on, there may be problems. It is a situation that the painter's algorithm cannot solve:

The three triangles cover each other, so no matter what order is used to draw, the correct results cannot be obtained. In addition, this method cannot deal with the case where the triangle is Cross.

  
Of course, if it is quite certain that such a strange situation will not occur in the scenario, the painter's algorithm can still be used. However, it has a big problem, that is, poor efficiency. First, the Paster algorithm needs to sort all the triangles within the perspective of the scene. The best sorting algorithm is also required.
O (n log n) time. That is to say, (in general) if the number of triangles changes from one thousand to 10 thousand, the sorting Time will change to about 13.3.
Times. In addition, it is not suitable to use special hardware for acceleration because all triangles in the scenario are required. In addition, this method also has a big problem, that is, it will spend a lot of time to draw some of the fundamental hidden parts, because every triangle
Pixel must be drawn. This will also make efficiency worse.
  
If the scenario is static (not moving) and only the observer changes, there is a way to speed up the sorting. A common method is binary space.
Paritioning (BSP ). This method requires a tree structure for the scenario in advance. After this structure is created, no matter the position and angle of the observer, the correct sequence of drawing can be quickly found. In addition, BSP
The triangle surface is cut as needed to deal with the situation where the three triangles cover each other.
  
However, BSP
Structure creation takes a lot of time, so it is unlikely to perform real-time operations. Therefore, it can only be used in the static part of the scenario, and the moving part still needs to be sorted separately. In addition, BSP
You often need to cut the triangle surface, which will also increase the number of triangles. In addition, BSP still cannot solve the problems that need to be drawn to the hidden pixel.
  
Another method to remove hidden surfaces is to take pixel as the unit, rather than triangle as the unit. The simplest method is
It was proposed in 1974, that is, Z buffer (or depth ).
Buffer ). This method is very simple and easily executed by specially designed hardware, so it becomes very popular when memory capacity is no longer a problem.
  
The principle of Z buffer is very simple. In 3D scenes, apart from the frame buffer that stores the drawing results
In addition, an extra space is used, that is, the Z buffer. On the Z buffer record frame buffer, each pixel
The distance from the observer, that is, the Z value. Before you start to draw a scenario, set the Z buffer
All values are first set to infinitely far. Then, when drawing the triangle surface, calculate the Z value of this pixel for each pixel of the triangle surface, and
The Z value stored in. If the Z value in Z buffer is large, it indicates the pixel to be drawn.
Is relatively close, so we should draw it and update the Z value in Z buffer at the same time. If the Z value in Z buffer is small, it indicates
Pixel is relatively far away and will be overwritten by pixel in the current frame buffer, so you do not need to draw or update Z
Value. In this way, you can draw these triangles in any order to get the correct drawing result. Is an example:

Although the red triangle is drawn first, the Z
Buffer, so the yellow block after painting will only cover the appropriate part, rather than even the closer part. This shows the effect of Z buffer.
  
In fact, the number that can be stored in the Z buffer is limited to a certain extent, so the Z value is usually reduced to 0 ~ The range of 1. Therefore
In this scenario, you need
The value must be within a certain range. Generally, two planes are used in parallel with the projection plane to cut all the triangles out of the two planes. These two planes are generally called Z near and
Z far indicates the closer plane and the farther plane, respectively. The Z value in the Z near plane is 0, and the Z value in Z far is 1.
  
Efficiency Z buffer
It is not necessarily faster than the painter's algorithm. However, it is relatively simple. Furthermore, its efficiency does not have much to do with the number of triangles, but with the pixel
Number. Therefore, it is easy to design a specific 3D hardware for this action, instead of the CPU. While Z buffer
The extra memory is not very important today. So now almost all 3D display chips use Z buffer.
  
However, Z buffer
Not all problems. A big problem is accuracy. If two triangles are very close, and one of them is completely before the other, you can only see one triangle. However, if
Z buffer is not accurate enough, so the Z value of each pixel of the two triangles may be very close. Add the calculated Z
There must be an error in the value. Therefore, it is very likely that there will be a triangle that should be covered, but some pixel will not be covered. This situation is called Z
Fighting. In, the shadow of the ball on the ground is an example:

To avoid this kind of problem, it is necessary to avoid a triangle that is too close and close to parallel in the scene. This is not the case in general scenarios. However, Z buffer
The accuracy problem is not just that. This issue will be described in more detail in the next section.
  
In the previous section, I made a rough description of the principle of Z buffer. It sounds like Z buffer is an ideal technology. However, in fact, Z buffer
A big problem is accuracy.
  
As mentioned later in the previous page, Z fighting occurs in two very close planes.
In fact, it is quite rare and easy to avoid. Of course, some games will still see this situation. However, Z buffer
The most serious problem is that it is far away from the observer. If Z buffer
If the precision is not enough, and the scene is far away, there will be some very strange phenomena in the distance. Is an example:

Z aliasing

No Z aliasing
Of course, the above example is an extreme situation. In fact, in general, there is no such exaggerated Z aliasing
Symptom. However, I believe that everyone has seen similar situations in games with large scenarios.
  
Why is there such a phenomenon? This will begin with the Z buffer structure. If the previous page says that the display chip generally limits the Z value to 0 ~ 1
And then use a fixed number of points to represent it. For example, a 16-bit Z buffer may use 0 ~ 65535 (a 16
The value range that can be expressed by a bitwise number ~ Z value between 1.
  
If the Z buffer is distributed in the eye space
It is linear, that is, if the interval between each number is equal, then such accuracy should be very high. Because, if the observer can see something a kilometer away, then each interval is about
1.5 cm. If you use a number with a higher accuracy (such as a 24-bit dollar number), the accuracy will be higher. However, Z buffer
Space is not linear. It is linear in projection space.
  
If you think these sounds like alien words, you need to "translate" these alien words now. First, let's look at one:

It is a red plane in the Perspective Projection of an eye. The plane close to the eye (with yellow points on it) represents the projection plane, that is, 3D
The screen in the drawing. The yellow dots, red flags, are projected onto the pixel of the screen. Of course they are equal-distance. However, pay attention to the pixel of these "equi-spacing ".
The Z value (that is, the gray points on the Z axis) is not equal spacing. In fact, the longer pixel is, the larger the distance on the Z axis.

This is actually an obvious property of perspective projection. In the case of perspective projection, the farther things look, the smaller the size, so the same spacing on the screen will become larger in a relatively far place. Therefore, although the triangle is a plane
The Z value on pixel is not linear. Therefore, linear interpolation cannot be used to calculate the Z value of pixel within a triangle. However, you must calculate each
The Z value on Pixel requires a division action, while division is a very annoying and time-consuming action.

Early display chips could not spend a splitter on the Z buffer. Therefore, one method is to do not store the eye in the Z buffer.
The Z value of space, instead of the Z value of projection space. In this way, the value of Z is in projection space.
It will become linear, and we can simply use linear interpolation to calculate the Z value of pixel inside the triangle. This is also the Z buffer of almost all display chips.
.

However, the Z value in projection space, as shown in the figure above, has a very important feature: the eye
The larger the Z-value interval of space, the farther it is. Therefore, if the accuracy of the Z buffer is
If you look at it (this makes sense), it will become an uneven distribution. The closer the observer is, the higher the accuracy. The change in accuracy will depend on Z
The position of the near plane and the Z far plane. The closer the Z near plane is to the observer, and the Z far plane
The farther the plane is from the observer, the greater the change in accuracy, that is, the worse the accuracy of the distant place.

In the first two figures on this page, the position of the Z-far plane is the same, but the position of the Z-near plane in the left graph is better than that of the Z-near plane in the right graph.
The position of the plane is nearly one thousand times. Therefore, a serious z aliasing phenomenon occurs in the left image, but this phenomenon does not occur in the right image.

Therefore, to avoid the Z aliasing phenomenon as much as possible, we should try to pull the Z near plane as far as possible, and put the Z far
Closer the plane. However, in many cases, such a design is not allowed. For example, in a scenario, a player may see 50
A large base located one kilometer away from the table. Therefore, the Z-near plane cannot be set farther than 50 centimeters, but z-far
The plane is one kilometer away. According to the 16-bit Z buffer, the distance (that is, a kilometer away) will reach 30 meters, that is, if two
If the pixel distance is less than 30 meters, Z buffer cannot tell the correct order! And it is at Z near (that is, 50
The accuracy is as high as 0.0000076. This shows that the accuracy distribution is so uneven and inappropriate. If you use a 24-bit Z buffer
Then, the situation will be improved to a considerable extent, and the accuracy within one kilometer will be increased to about 12 cm. This is why the 24-bit Z buffer rarely shows Z
Aliasing.

However, even the 24-bit Z buffer is not necessarily ideal. In the preceding example, if the Z near plane is moved to 10
The accuracy in the distance is reduced from 12 to 60. Some people may think that in a kilometer away, who can tell 60 or 12 points?
What about the public score? However, the problem is that when the distance between two large planes is less than 60 times
If the correct sequence cannot be distinguished, a plane may be drawn in this box, but the next box may be painted in another plane. If the colors of the two planes are very different, they will flash, and anyone will easily notice it.

Some display chips adopt some methods to solve this problem. A simple idea is in Z buffer
Use a floating point instead of a fixed number of points. After proper design, floating point numbers can be near a specific number to provide a greater range of precision (generally near 0 ). The average
Z buffer requires a higher accuracy near Z far. Therefore, we can express Z buffer in the Z far plane as 0, while Z buffer
The near plane is expressed as 1. In this way, higher accuracy can be obtained. However, floating point numbers are difficult to calculate. In particular, Z buffer
In the calculation, addition and comparison operations are often required, which is much more troublesome than the calculation of fixed points.

Another method is to use a non-linear Z buffer. For example, you can cut the Z buffer into multiple cells, and each cell is a general linear Z
Buffer. However, more cells can be allocated in the distance to improve its accuracy. This is also a way to solve the Accuracy Problem.

In fact, to solve the Z buffer Accuracy Problem, the simplest method is to perform linear interpolation in eye space. However, as mentioned earlier
The linearity in space, in projection Space
It is not necessarily linear, so it requires additional delimiters. However, there is a way to avoid using the divisor, but only the "Countdown" is required. The "Countdown" is simpler than the complete divisor. This method is based on high accuracy
In projection space, linear interpolation is performed on Z. Calculate the reciprocal of each result obtained by interpolation with the countdown, that is, the so-called W value. This w
The value accuracy can be low because its distribution in eye space is average. Finally, the W value and the "W buffer"
To get the correct order. I believe some people have guessed this method, that is, W buffering.

Of course, there are other ways to implement the W buffer. However, no matter what method is used to implement the W buffer, its most important property is
Linear Distribution in eye space. Therefore, the accuracy of the 16-bit W buffer in the distance is ideal. In the previous example
The accuracy of Z buffer in a kilometer is only 12 centimeters. However, the 16-bit W buffer is 1.5 accuracy in every place.
Public score. Therefore, in this example, the 16-bit W buffer performs better in the distance than the 24-bit Z buffer.

In addition, W buffer has another advantage, that is, its W near plane (relative to Z near in Z buffer
Plane) is not important. That is to say, W buffer can take into account both the current table and a huge base several kilometers away. While using Z buffer
If you want to correctly display a large base several kilometers away, you may have to sacrifice your desk.

However, because the accuracy of W buffer is evenly distributed, its accuracy at Z near is obviously inferior to that at Z buffer. Although Z
The accuracy of the buffer at Z near is too high (such as 0.0000076 m), but w Buffer
But it may be too low. For example, 1.5
The accuracy of the Public score is absolutely sufficient for distant objects, but it is obviously insufficient for objects close to the observer. For example, a table may have a thickness less than 1.5
A public score. At this time, the accuracy of 1.5 cm is not enough.

It sounds like W buffer cannot solve the problem! This is not the case. If there is a 24-bit W
Buffer. At the same time, we can see things 10 kilometers away (this should be very far away), so its accuracy is about 0.6.
About a centimeter. This accuracy is generally enough. In addition, W buffer is also very easy to use and does not require any major modifications to the program.

Currently, the biggest problem with W buffer is insufficient support. Some display chips do not support W buffer at all, while others only support 16-bit W buffer.
Buffer. However, at present, many display chips have started to support W buffer, so more games will be available in the future!

 

 

There are two types of depth-buffer (deep cache): Z-buffer and W-buffer. Here we discuss the differences between these two types of deep cache and how to convert them.

Meaning of W

The coordinates of 3D spatial points are (x, y, z). In order to make the matrix multiplication function of translation transformation, we use the points (X, Y, Z, W) in 4D space) to represent vertices (x', y', Z') in a 3D space. The relationship between these two points is:

    x' = x / w
y' = y / w
z' = z / w

In this way, a four-dimensional spatial point is used to represent a three-dimensional spatial point, or a n + 1 dimensional spatial point is called a homogeneous coordinate representation ".

In actual use, in the process of model-> world conversion, world-> View conversion, W is always the same, always equal to one, the first three components of homogeneous coordinates correspond to the three coordinate components of 3D spatial points. However, after the projection transformation, W will get a proportional value. For example, the general perspective projection transformation matrix is:

 

    | W   0   0   0 |

| 0 H 0 0 |

| 0 0 Q 1 |

| 0 0 -QZn 0 |


The zcoordinate OF Zn = near cropping Surface

ZF = Z coordinate of the far cropping plane

W = 2 * Zn/view width

H = 2 * Zn/view height

Q = Zf / (Zf - Zn)

Multiply the point (X, Y, Z, 1) by this matrix, W is no longer one, and the corresponding 3D space coordinate (x/W, Y/W, z/W)

A scaling effect is displayed. At the same time, because the value of W is usually proportional to the Z coordinate (for example, after the transformation of the matrix above, the value of W is actually the value of Z coordinate ),

Therefore, after the projection transformation, the object will produce near-large and small effects.

Difference between Z-buffer and W-Buffer

In short, the difference between Z-buffer and W-buffer is that the former stores the Z coordinate of the vertex, while the latter stores the W coordinate of the vertex.

Specifically, because the stored values have different meanings, the actual results may be different.

The Z-buffer stores the Z after the projection transformation.
Coordinates, as mentioned earlier, after the projection, the object will produce near big and small effects, so the resolution of the Z coordinate is relatively large, while that of the distant is relatively small, in other words
Z
Coordinates are in the value range. For physical distance changes that exit the eyes, they are not linear (I .e. uneven distribution ), one advantage of this is that the near object gets a high depth resolution, but the distant object
Deep judgment may cause errors.

W-buffer stores the W coordinates after the projection transformation, while W
Coordinates are usually the same as Z in the world coordinate system.
The coordinates are directly proportional, so after being transformed to the projection space, the values are still linearly distributed, so that no matter the distance or near the object, there is the same depth resolution, which is its advantage, of course, the disadvantage is that it cannot be used.
A high depth resolution is used to represent near objects.

From the perspective of hardware implementation, almost all hardware 3D acceleration CARDS support Z-buffer, while W-buffer does not support Z-buffer as widely. In addition, early direct3d versions do not seem to support w-buffer.

Z-buffer and W-buffer Conversion

According to the matrix transformation above, you can easily export the formula for converting w-buffer into Z-buffer:

    zDepth = Q * ( wDepth - Zn ) / wDepth
= Zf / ( Zf - Zn ) * ( wDepth - Zn ) / wDepth

What is the use of this conversion formula? For example, 3DS MAX uses
W-buffer. If depth information is exported from 3DS MAX to direct3d and used as the pre-rendering background, the above conversion may be used. Of course, if
If w-buffer is used in d3d, the rendering result will fail if z-buffer is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.