[转]Linear Depth Buffer(线性深度缓冲区)
The Problem with Z-Buffer
If you are a graphics programmer, you are bound to deal with the use of z-buffer for effects. For example, the depth buffer can be used for per pixel fog, soft particles, motion blur, SSAO, depth of field, etc. Using the standard depth, i.e. the value that is obtained by multiplying with World View Projection matrices followed by perspective divide, is often not enough.
The main problem with standard depth is that the depth value is not linear. Most of the time, the first 10% of the scene (the near scene) will be mapped to 0.0 to 0.9 range. In other words, 90% of the precision is used up in the first 10 percent of the viewing distance. If your algorithm relies heavily on depth comparison, you are screwed.
The key to handle this problem is to make the depth value linear or making it almost linear. I will describe the two approaches below:
Linear Depth Buffer
Notice that multilying World, View, Projection matrices with your position will make it ends up in clip space, (x, y, z, w) = (x_in_clip, y_in_clip, z_in_clip, z_in_view) except that w = z_in_view. We can exploit this fact and instead of storing the standard depth which is obtained by z/w = z_in_clip/z_in_view, we can store linear depth value. There are several variations for mapping z_in_view linearly. For example, you can do:
1 | z_final = z_in_view / camera_far_plane |
or you can also do:
1 | z_final = (z_in_view - camera_near_plane) / (camera_far_plane - camera_near_plane) |
W-Buffer
The idea of w-buffer is to provide a more distributed linear mapping than for z-buffers by inverting the depth value. By doing this, you effectively map the last 90% of the scene (the far scene) to 0.0 to 0.9 range. In other words, w buffer can still produce artifacts when many of the objects in the scene are close to the camera. In comparison to z-buffer, w-buffer has a lot more precision available for objects in the middle to far distance from the viewer. In code language, you would output:
1 | z_final = 1 / z_in_view |
1 | |
In conclusion
I've discussed several techniques to alleviate the non-linearity of z-buffer. I hope it can give you light why you may have a lot of artifacts using the standard z-buffer.
more flexible depth buffer control titled "quasi-linear depth buffer".
http://dl.acm.org/citation.cfm?id=383530 Autodesk Canada Co.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构