Revolt Game No Z Buffer Fighting

Depth precision is a pain in the ass that every graphics programmer has to struggle with sooner or later. Many articles and papers have been written on the topic, and a variety of different depth buffer formats and setups are found across different games, engines, and devices.

And was first distributed as a closed Beta to all. Automatically launch Re-Volt when WolfR4 is started (no need to. Articles euro truck simulator 2 fullEuro Truck Simulator 2 1.14 public beta opens. Download z buffer for revolt no. Classic PC Games: Free Software: Download & Streaming: Internet Archive. This game is quite simple. Z-fighting is a common problem with depth buffers and it's generally more noticeable when objects are further away (because the depth buffer has less precision at larger z-values). Z-fighting can't be completely prevented, but there are a few tricks that will help to mitigate or completely prevent z-fighting in your scene. On this page we have listed the most popular games on Poki which are all available to play for free online. This list contains the most popular games on Poki based on total number of game plays this month. Each month over 30,000,000 people play games on Poki. 🕹️ What are the best popular games in 2020? Minecraft Classic; Subway Surfers. Street Fighting Man Lyrics: Everywhere I hear the sound of marching, charging feet, boy / 'Cause summer's here, and the time is right for fighting in the street, boy / Well, what can a poor boy do.

Because of the way it interacts with perspective projection, GPU hardware depth mapping is a little recondite and studying the equations may not make things immediately obvious. To get an intuition for how it works, it's helpful to draw some pictures.

This article has three main parts. In the first part, I try to provide some motivation for nonlinear depth mapping. Second, I present some diagrams to help understand how nonlinear depth mapping works in different situations, intuitively and visually. The third part is a discussion and reproduction of the main results of Tightening the Precision of Perspective Rendering by Paul Upchurch and Mathieu Desbrun (2012), concerning the effects of floating-point roundoff error on depth precision.

GPU hardware depth buffers don't typically store a linear representation of the distance an object lies in front of the camera, contrary to what one might naĂŻvely expect when encountering this for the first time. Instead, the depth buffer stores a value proportional to the reciprocal of world-space depth. I want to briefly motivate this convention.

In this article, I'll use d to represent the value stored in the depth buffer (in [0, 1]), and z to represent world-space depth, i.e. distance along the view axis, in world units such as meters. In general, the relationship between them is of the form

where a,b are constants related to the near and far plane settings. In other words, Bufferd is always some linear remapping of 1/z.

On the face of it, you can imagine taking d to be any function of z you like. So why this particular choice? There are two main reasons.

First, 1/z fits naturally into the framework of perspective projections. This is the most general class of transformation that is guaranteed to preserve straight lines—which makes it convenient for hardware rasterization, since straight edges of triangles stay straight in screen space. We can generate linear remappings of 1/z by taking advantage of the perspective divide that the hardware already performs:

The real power in this approach, of course, is that the projection matrix can be multiplied with other matrices, allowing you to combine many transformation stages together in one.

The second reason is that 1/z is linear in screen space, as noted by Emil Persson. So it's easy to interpolate d across a triangle while rasterizing, and things like hierarchical Z-buffers, early Z-culling, and depth buffer compression are all a lot easier to do.

Graphing Depth Maps

Equations are hard; let's look at some pictures!

The way to read these graphs is left to right, then down to the bottom. Start with d, plotted on the left axis. Because d can be an arbitrary linear remapping of 1/z, we can place 0 and 1 wherever we wish on this axis. The tick marks indicate distinct depth buffer values. For illustrative purposes, I'm simulating a 4-bit normalized integer depth buffer, so there are 16 evenly-spaced tick marks.

Trace the tick marks horizontally to where they hit the 1/z curve, then down to the bottom axis. That's where the distinct values fall in the world-space depth range.

The graph above shows the “standard”, vanilla depth mapping used in D3D and similar APIs. You can immediately see how the 1/z curve leads to bunching up values close to the near plane, and the values close to the far plane are quite spread out.

It's also easy to see why the near plane has such a profound effect on depth precision. Pulling in the near plane will make the d range skyrocket up toward the asymptote of the 1/z curve, leading to an even more lopsided distribution of values:

Similarly, it's easy to see in this context why pushing the far plane all the way out to infinity doesn't have that much effect. It just means extending the d range slightly down to 1/z=0:

What about floating-point depth? The following graph adds tick marks corresponding to a simulated float format with 3 exponent bits and 3 mantissa bits:

There are now 40 distinct values in [0, 1]—quite a bit more than the 16 values previously, but most of them are uselessly bunched up at the near plane where we didn't really need more precision.

A now-widely-known trick is to reverse the depth range, mapping the near plane to d=1 and the far plane to d=0:

Much better! Now the quasi-logarithmic distribution of floating-point somewhat cancels the 1/z nonlinearity, giving us similar precision at the near plane to an integer depth buffer, and vastly improved precision everywhere else. The precision worsens only very slowly as you move farther out.

The reversed-Z trick has probably been independently reinvented several times, but goes at least as far back as a SIGGRAPH ’99 paper by Eugene Lapidous and Guofang Jiao (no open-access link available, unfortunately). It was more recently re-popularized in blog posts by Matt Pettineo and Brano Kemen, and by Emil Persson's Creating Vast Game Worlds SIGGRAPH 2012 talk.

Z-buffer Algorithm

All the previous diagrams assumed [0, 1] as the post-projection depth range, which is the D3D convention. What about OpenGL?

OpenGL by default assumes a [-1, 1] post-projection depth range. This doesn't make a difference for integer formats, but with floating-point, all the precision is stuck uselessly in the middle. (The value gets mapped into [0, 1] for storage in the depth buffer later, but that doesn't help, since the initial mapping to [-1, 1] has already destroyed all the precision in the far half of the range.) And by symmetry, the reversed-Z trick will not do anything here.

Fortunately, in desktop OpenGL you can fix this with the widely-supported ARB_clip_control extension (now also core in OpenGL 4.5 as glClipControl). Unfortunately, in GL ES you're out of luck.

The 1/z mapping and the choice of float versus integer depth buffer are a big part of the precision story, but not all of it. Even if you have enough depth precision to represent the scene you're trying to render, it's easy to end up with your precision controlled by error in the arithmetic of the vertex transformation process.

As mentioned earlier, Upchurch and Desbrun studied this and came up with two main recommendations to minimize roundoff error:

  1. Use an infinite far plane.
  2. Keep the projection matrix separate from other matrices, and apply it in a separate operation in the vertex shader, rather than composing it into the view matrix.

Upchurch and Desbrun came up with these recommendations through an analytical technique, based on treating roundoff errors as small random perturbations introduced at each arithmetic operation, and keeping track of them to first order through the transformation process. I decided to check the results using direct simulation.

My source code is here—Python 3.4 with numpy. It works by generating a sequence of random points, ordered by depth, spaced either linearly or logarithmically between the near and far planes. Then it passes the points through view and projection matrices and the perspective divide, using 32-bit float precision throughout, and optionally quantizes the final result to 24-bit integer. Finally, it runs through the sequence and counts how many times two adjacent points (which originally had distinct depths) have either become indistiguishable because they mapped to the same depth value, or have actually swapped order. In other words, it measures the rate at which depth comparison errors occur—which corresponds to issues like Z-fighting—under different scenarios.

Here are the results obtained for near = 0.1, far = 10K, with 10K linearly spaced depths. (I tried logarithmic depth spacing and other near/far ratios as well, and while the detailed numbers varied, the general trends in the results were the same.)

In the table, “indist” means indistinguishable (two nearby depths mapped to the same final depth buffer value), and “swap” means that two nearby depths swapped order.

Precomposed view-
projection matrix
Separate view and
projection matrices
float32int24float32int24
Unaltered Z values
(control test)
0% indist
0% swap
0% indist
0% swap
0% indist
0% swap
0% indist
0% swap
Standard projection45% indist
18% swap
45% indist
18% swap
77% indist
0% swap
77% indist
0% swap
Infinite far plane45% indist
18% swap
45% indist
18% swap
76% indist
0% swap
76% indist
0% swap
Reversed Z0% indist
0% swap
76% indist
0% swap
0% indist
0% swap
76% indist
0% swap
Infinite + reversed-Z0% indist
0% swap
76% indist
0% swap
0% indist
0% swap
76% indist
0% swap
GL-style standard56% indist
12% swap
56% indist
12% swap
77% indist
0% swap
77% indist
0% swap
GL-style infinite59% indist
10% swap
59% indist
10% swap
77% indist
0% swap
77% indist
0% swap

Apologies for not graphing these, but there are too many dimensions to make it easy to graph! In any case, looking at the numbers, a few general results are clear.

  • There is no difference between float and integer depth buffers in most setups. The arithmetic error swamps the quantization error. In part this is because float32 and int24 have almost the same-sized ulp in [0.5, 1] (because float32 has a 23-bit mantissa), so there actually is almost no additional quantization error over the vast majority of the depth range.
  • In many cases, separating the view and projection matrices (following Upchurch and Desbrun’s recommendation) does make some improvement. While it doesn't lower the overall error rate, it does seem to turn swaps into indistinguishables, which is a step in the right direction.
  • An infinite far plane makes only a miniscule difference in error rates. Upchurch and Desbrun predicted a 25% reduction in absolute numerical error, but it doesn't seem to translate into a reduced rate of comparison errors.

The above points are practically irrelevant, though, because the real result that matters here is: the reversed-Z mapping is basically magic. Check it out:

  • Reversed-Z with a float depth buffer gives a zero error rate in this test. Now, of course you can make it generate some errors if you keep tightening the spacing of the input depth values. Still, reversed-Z with float is ridiculously more accurate than any of the other options.
  • Reversed-Z with an integer depth buffer is as good as any of the other integer options.
  • Reversed-Z erases the distinctions between precomposed versus separate view/projection matrices, and finite versus infinite far planes. In other words, with reversed-Z you can compose your projection matrix with other matrices, and you can use whichever far plane you like, without affecting precision at all.

I think the conclusion here is clear. In any perspective projection situation, just use a floating-point depth buffer with reversed-Z! And if you can't use a floating-point depth buffer, you should still use reversed-Z. It isn't a panacea for all precision woes, especially if you're building an open-world environment that contains extreme depth ranges. But it's a great start.

Nathan is a Graphics Programmer, currently working at NVIDIA on the DevTech software team. You can read more on his blog here.

12.010 How do I make depth buffering work?

Your application needs to do at least thefollowing to get depth buffering to work:

  1. Ask for a depth buffer when you createyour window.
  2. Place a call to glEnable (GL_DEPTH_TEST) inyour program's initialization routine, after acontext is created and made current.
  3. Ensure that your zNear and zFarclipping planes are set correctly and in a waythat provides adequate depth buffer precision.
  4. Pass GL_DEPTH_BUFFER_BIT as a parameter toglClear, typically bitwise OR'd with other valuessuch as GL_COLOR_BUFFER_BIT.

There are a number of OpenGL exampleprograms available on the Web, which use depth buffering. Ifyou're having trouble getting depth buffering to workcorrectly, you might benefit from looking at an exampleprogram to see what is done differently. This FAQ contains links toseveral web sites that have example OpenGL code.

12.020 Depth buffering doesn't work in myperspective rendering. What's going on?

Make sure the zNear and zFar clippingplanes are specified correctly in your calls to glFrustum()or gluPerspective().

A mistake many programmers make is tospecify a zNear clipping plane value of 0.0 or anegative value which isn't allowed. Both the zNear andzFar clipping planes are positive (not zero ornegative) values that represent distances in front of the eye.

Specifying a zNear clipping planevalue of 0.0 to gluPerspective() won't generate an OpenGLerror, but it might cause depth buffering to act as if it'sdisabled. A negative zNear or zFar clippingplane value would produce undesirable results.

A zNear or zFar clippingplane value of zero or negative, when passed to glFrustum(),will produce an error that you can retrieve by callingglGetError(). The function will then act as a no-op.

12.030 How do I write a previously stored depthimage to the depth buffer?

Use the glDrawPixels() command, with theformat parameter set to GL_DEPTH_COMPONENT. You may want tomask off the color buffer when you do this, with a call toglColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); .

12.040 Depth buffering seems to work, butpolygons seem to bleed through polygons that are in front of them.What's going on?

You may have configured your zNear andzFar clipping planes in a way that severely limitsyour depth buffer precision. Generally, this is caused by a zNearclipping plane value that's too close to 0.0. As the zNearclipping plane is set increasingly closer to 0.0, theeffective precision of the depth buffer decreasesdramatically. Moving the zFar clipping plane furtheraway from the eye always has a negative impact on depthbuffer precision, but it's not one as dramatic as moving the zNearclipping plane.

The OpenGLReference Manual descriptionfor glFrustum() relates depth precision to the zNear andzFar clipping planes by saying that roughly log2(zFar/zNear)bits of precision are lost. Clearly, as zNearapproaches zero, this equation approaches infinity.

While the blue book description is good atpointing out the relationship, it's somewhat inaccurate. Asthe ratio (zFar/zNear) increases, less precision isavailable near the back of the depth buffer and moreprecision is available close to the front of the depth buffer.So primitives are more likely to interact in Z if they arefurther from the viewer.

It's possible that you simply don't haveenough precision in your depth buffer to render your scene.See the last questionin this section for more info.

It's also possible that you are drawingcoplanar primitives. Round-off errors or differences inrasterization typically create 'Z fighting' forcoplanar primitives. Here are some options to assist youwhen rendering coplanar primitives.

Game

12.050 Why is my depth buffer precision so poor?

The depth buffer precision in eyecoordinates is strongly affected by the ratio of zFar tozNear, the zFar clipping plane, and how faran object is from the zNear clipping plane.

You need to do whatever you can to push thezNear clipping plane out and pull the zFar planein as much as possible.

To be more specific, consider thetransformation of depth from eye coordinates

xe, ye, ze,we

to window coordinates

xw, yw, zw

with a perspective projection matrixspecified by

glFrustum(l, r, b, t, n, f);

and assume the default viewport transform.The clip coordinates of zc and wc are

zc = -ze* (f+n)/(f-n)- we* 2*f*n/(f-n)

wc = -ze

Why the negations? OpenGL wants to presentto the programmer a right-handed coordinate system beforeprojection and left-handed coordinate system after projection.

and the ndc coordinate:

zndc = zc /wc = [ -ze * (f+n)/(f-n) - we* 2*f*n/(f-n) ] / -ze

= (f+n)/(f-n) + (we / ze)* 2*f*n/(f-n)

The viewport transformation scales andoffsets by the depth range (Assume it to be [0, 1]) and thenscales by s = (2n-1) where n is the bit depth ofthe depth buffer:

zw = s * [ (we /ze) * f*n/(f-n) + 0.5 * (f+n)/(f-n) + 0.5 ]

Let's rearrange this equation to express ze/ we as a function of zw

ze / we = f*n/(f-n)/ ((zw / s) - 0.5 * (f+n)/(f-n) - 0.5)

= f * n / ((zw / s) * (f-n)- 0.5 * (f+n) - 0.5 * (f-n))

= f * n / ((zw / s) * (f-n)- f) [*]

Now let's look at two points, the zNearclipping plane and the zFar clipping plane:

zw = 0 => ze/ we = f * n / (-f) = -n

zw = s => ze /we = f * n / ((f-n) - f) = -f

In a fixed-point depth buffer, zwis quantized to integers. The next representable z bufferdepth away from the clip planes are 1 and s-1:

zw = 1 => ze /we = f * n / ((1/s) * (f-n) - f)

zw = s-1 => ze/ we = f * n / (((s-1)/s) * (f-n) - f)

Now let's plug in some numbers, for example,n = 0.01, f = 1000 and s = 65535 (i.e., a 16-bit depth buffer)

zw = 1 => ze /we = -0.01000015

zw = s-1 => ze/ we = -395.90054

Think about this last line. Everything ateye coordinate depths from -395.9 to -1000 has to map intoeither 65534 or 65535 in the z buffer. Almost two thirds ofthe distance between the zNear and zFar clippingplanes will have one of two z-buffer values!

To further analyze the z-buffer resolution,let's take the derivative of [*] with respect to zw

d (ze / we) / d zw= - f * n * (f-n) * (1/s) / ((zw / s) * (f-n)- f)2

Now evaluate it at zw = s

d (ze / we) / d zw= - f * (f-n) * (1/s) / n

= - f * (f/n-1) / s [**]

If you want your depth buffer to be usefulnear the zFar clipping plane, you need to keep thisvalue to less than the size of your objects in eye space (formost practical uses, world space).

12.060 How do I turn off the zNearclipping plane?

See thisquestion in the Clipping section.

12.070 Why is there more precision at the frontof the depth buffer?

After the projection matrix transforms theclip coordinates, the XYZ-vertex values are divided by theirclip coordinate W value, which results in normalized devicecoordinates. This step is known as the perspective divide.The clip coordinate W value represents the distance from theeye. As the distance from the eye increases, 1/W approaches 0.Therefore, X/W and Y/W also approach zero, causing therendered primitives to occupy less screen space and appearsmaller. This is how computers simulate a perspective view.

As in reality, motion toward or away fromthe eye has a less profound effect for objects that arealready in the distance. For example, if you move six inchescloser to the computer screen in front of your face, it'sapparent size should increase quite dramatically. On theother hand, if the computer screen were already 20 feet awayfrom you, moving six inches closer would have littlenoticeable impact on its apparent size. The perspectivedivide takes this into account.

As part of the perspective divide, Z isalso divided by W with the same results. For objects that arealready close to the back of the view volume, a change indistance of one coordinate unit has less impact on Z/W thanif the object is near the front of the view volume. To put itanother way, an object coordinate Z unit occupies a largerslice of NDC-depth space close to the front of the viewvolume than it does near the back of the view volume.

In summary, the perspective divide, by itsnature, causes more Z precision close to the front of theview volume than near the back.

A previous question in thissection contains related information.

Z Buffer Support

Revolt Game No Z Buffer Fighting

12.080 There is no way that a standard-sizeddepth buffer will have enough precision for my astronomicallylarge scene. What are my options?

Revolt Game No Z Buffer Fighting 2.9

The typical approach is to use a multipasstechnique. The application might divide the geometry databaseinto regions that don't interfere with each other in Z. Thegeometry in each region is then rendered, starting at thefurthest region, with a clear of the depth buffer before eachregion is rendered. This way the precision of the entiredepth buffer is made available to each region.