深度緩衝區Depth buffers

深度緩衝區 (或稱 z 緩衝區) 儲存深度資訊來控制所呈現而不是隱藏的多邊形區域。A depth buffer, or z-buffer, stores depth information to control which areas of polygons are rendered rather than hidden from view.

概觀Overview

深度緩衝區 (通常稱為 z 緩衝區或 w 緩衝區),是裝置屬性,用來儲存深度資訊供 Direct3D 使用。A depth buffer, often called a z-buffer or a w-buffer, is a property of the device that stores depth information to be used by Direct3D. 當 Direct3D 將場景轉譯至目標表面時,它可以使用相關深度緩衝區表面中的記憶體做為工作區,來決定點陣化多邊形的像素如何彼此遮蓋。When Direct3D renders a scene to a target surface, it can use the memory in an associated depth-buffer surface as a workspace to determine how the pixels of rasterized polygons occlude one another. Direct3D 使用螢幕外的 Direct3D 表面做為目標,將最終色彩值寫入其中。Direct3D uses an off-screen Direct3D surface as the target to which final color values are written. 與轉譯目標表面相關的深度緩衝區表面用來儲存深度資訊,告知 Direct3D 每一個可見像素在場景中的深度。The depth-buffer surface that is associated with the render-target surface is used to store depth information that tells Direct3D how deep each visible pixel is in the scene.

當場景點陣化並啟用深度緩衝處理時,轉譯表面上的每一個點都會經過測試。When a scene is rasterized with depth buffering enabled, each point on the rendering surface is tested. 深度緩衝區中的值可以是點的 z 座標,或其同質 w 座標,來自投射空間中點的 (x,y,z,w) 位置。The values in the depth buffer can be a point's z-coordinate or its homogeneous w-coordinate - from the point's (x,y,z,w) location in projection space. 使用 z 值的深度緩衝區通常稱為 z 緩衝區,使用 w 值的則稱為 w 緩衝區。A depth buffer that uses z values is often called a z-buffer, and one that uses w values is called a w-buffer. 每一種深度緩衝區都有優點和缺點,將於稍後討論。Each type of depth buffer has advantages and disadvantages, which are discussed later.

在測試一開始,深度緩衝區中的深度值會設為場景的可能最大值。At the beginning of the test, the depth value in the depth buffer is set to the largest possible value for the scene. 轉譯表面上的色彩值會設為該點的背景色彩值或背景紋理的色彩值。The color value on the rendering surface is set to either the background color value or the color value of the background texture at that point. 場景中的每個多邊形都會經過測試,查看是否與轉譯表面上目前的座標 (x,y) 交集。Each polygon in the scene is tested to see if it intersects with the current coordinate (x,y) on the rendering surface.

如果確實交集,目前點的深度值 (會是 z 緩衝區中的 z 座標,在 w 緩衝區中則為 w 座標) 會進行測試,查看它是否小於儲存在深度緩衝區中的深度值。If it does intersect, the depth value - which will be the z coordinate in a z-buffer, and the w coordinate in a w-buffer - at the current point is tested to see if it is smaller than the depth value stored in the depth buffer. 如果多邊形值的深度較小,它會儲存在深度緩衝區中,來自多邊形的色彩值會寫入轉譯表面上目前的點。If the depth of the polygon value is smaller, it is stored in the depth buffer and the color value from the polygon is written to the current point on the rendering surface. 如果該點上多邊形的深度值較大,則會測試清單中下一個多邊形。If the depth value of the polygon at that point is larger, the next polygon in the list is tested. 此程序顯示在下圖。This process is shown in the following diagram.

測試深度值的圖

緩衝處理技術Buffering techniques

雖然大部分應用程式不會使用此功能,但是您可以變更 Direct3D 用來判斷哪些值位於深度緩衝區中的比較,以及後續的轉譯目標表面。Although most applications don't use this feature, you can change the comparison that Direct3D uses to determine which values are placed in the depth buffer and subsequently the render-target surface. 在某些硬體上,變更比較功能可能會停用階層 z 測試。On some hardware, changing the compare function may disable hierarchical z testing.

市面上幾乎所有加速器都支援 z 緩衝,因此 z 緩衝區是目前最常見的深度緩衝區類型。Nearly all accelerators on the market support z-buffering, making z-buffers the most common type of depth buffer today. 無論有多普遍,z 緩衝區仍有缺點。However ubiquitous, z-buffers have their drawbacks. 由於涉及的數學計算,z 緩衝區中產生的 z 值較不平均分散在 z 緩衝區範圍中 (通常是 0.0 到 1.0 (含))。Due to the mathematics involved, the generated z values in a z-buffer tend not to be distributed evenly across the z-buffer range (typically 0.0 to 1.0, inclusive).

具體而言,遠近裁剪平面之間的比例對 z 值分佈不平均影響很大。Specifically, the ratio between the far and near clipping planes strongly affects how unevenly z values are distributed. 使用遠平面距離到近平面距離比例為 100、深度緩衝區範圍的 90% 會用在場景深度範圍的前 10%。Using a far-plane distance to near-plane distance ratio of 100, 90 percent of the depth buffer range is spent on the first 10 percent of the scene depth range. 一般用於娛樂或視覺化模擬的應用程式含有外部場景,經常需要遠平面/近平面比例介於 1,000 到 10,000 之間。Typical applications for entertainment or visual simulations with exterior scenes often require far-plane/near-plane ratios of anywhere between 1,000 to 10,000. 比例為 1,000 時,98% 的範圍會用在深度範圍的前 2%,分佈情形會比採用高比例時更糟。At a ratio of 1,000, 98 percent of the range is spent on the first 2 percent of the depth range, and the distribution becomes worse with higher ratios. 這可能造成隱藏的表面成品在遠方物件中,尤其是使用 16 位元深度緩衝區時,這是最常見的支援位元深度。This can cause hidden surface artifacts in distant objects, especially when using 16-bit depth buffers, the most commonly supported bit-depth.

另一方面來說,w 型深度緩衝區通常比 z 緩衝區分佈較平均,在遠近裁剪平面之間。A w-based depth buffer, on the other hand, is often more evenly distributed between the near and far clip planes than a z-buffer. 主要優點在於,遠近裁剪平面的距離比例不再是問題。The key benefit is that the ratio of distances for the far and near clipping planes is no longer an issue. 這可讓應用程式支援大型最大範圍,同時仍讓相對精確的深度緩衝靠近眼睛視覺點。This allows applications to support large maximum ranges, while still getting relatively accurate depth buffering close to the eye point. w 型深度緩衝區並不完美,有時可能會呈現近的物件的隱藏表面成品。A w-based depth buffer isn't perfect, and can sometimes exhibit hidden surface artifacts for near objects. w 緩衝方法的另一個缺點與硬體支援相關:w 緩衝不如 z 緩衝一般在硬體之間廣受支援。Another drawback to the w-buffered approach is related to hardware support: w-buffering isn't supported as widely in hardware as z-buffering.

使用 z 緩衝區在轉譯時需要額外負荷。Using a z-buffer requires overhead during rendering. 各種不同的技術都可用來最佳化使用 z 緩衝區時的轉譯。Various techniques can be used to optimize rendering when using z-buffers. 使用 z 緩衝和紋理處理時,應用程式可提高效能,藉由確保場景從前到後轉譯。Applications can increase performance when using z-buffering and texturing by ensuring that scenes are rendered from front to back. 有紋理的 z 緩衝基本類型會針對 z 緩衝區預先測試,以掃描列為基礎。Textured z-buffered primitives are pretested against the z-buffer on a scan line basis. 如果掃描列被掀錢轉譯的多邊形隱藏,系統會快速且有效率地拒絕它。If a scan line is hidden by a previously rendered polygon, the system rejects it quickly and efficiently. Z 緩衝可以提升效能,但此技術在場景多次繪製相同像素時最實用。Z-buffering can improve performance, but the technique is most useful when a scene draws the same pixels more than once. 這很難精確計算,但是您通常可以算出近似值。This is difficult to calculate exactly, but you can often make a close approximation. 如果相同像素繪製少於兩次,將 z 緩衝關閉並從後到前轉譯場景,將可達到最佳效能。If the same pixels are drawn less than twice, you can achieve the best performance by turning z-buffering off and rendering the scene from back to front.

實際的深度值解譯為轉譯器特定。The actual interpretation of a depth value is specific to the renderer.

相關主題Related topics

深度和樣板緩衝區Depth and stencil buffers