深度值可以大于1吗?
在D3D HLSL中,我们可以直接将深度值输出到Pixel Shader中的深度寄存器中。但它要求该值应该在0和1之间。
如果我想将深度设置为大于1,我该怎么做?
对于我非常大的场景图来说,用于存储深度值的 24 位太有限了。
In D3D HLSL, we could output the depth value into depth register in Pixel Shader directly. But it request the value should be between 0 and 1.
If I want to set depth to be greater than 1 how can I do that?
24 bits for storing the depth value is too limited for my very large scene graph.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以输出 [0,1] 之外的值,但在用于深度测试(如果启用)之前以及写入深度目标之前,它将被限制在 [0,1] 范围内。对于 Z16/Z24 格式(甚至不能表示 [0,1] 之外的值)和 Z32F 格式都是如此。
为什么 24 位太有限了——您需要更大的范围还是需要更高的精度?
您的问题表明您需要比 [0,1] 更大的范围,但是您的场景图可以使用您想要的任何范围,只要顶点着色器将该范围映射到 [0,1](此范围映射通常发生在模型中) /视图/投影变换)。
如果您需要更高的精度,则无法在本地获得:整个 API 和硬件中使用的 32 位浮点数只有 23 位尾数,而隐式前导 1 有效地为您提供了 24 位精度。为了解决这个问题,典型的解决方案是将世界分割成多个片段(例如网格),并存储相对于它们所在片段的对象位置。当对象从一个片段移动到另一个片段时,您可以将其位置转换为与新段相关。渲染时,使用包含相机的段作为 (0,0,0) 并将位于视锥体内的段映射到 [0,1] 深度范围。
You can output a value outside of [0,1], but it will be clamped to the [0,1] range before being used in the depth test (if enabled) and before being written to the depth target. This is true for both Z16/Z24 formats (which can't even represent values outside [0,1]) and for the Z32F format.
Why is 24 bits too limited -- do you need a larger range or do you need more precision?
Your question suggests that you need a larger range than [0,1], but your scene graph can use any range you want as long as the vertex shader maps that range to [0,1] (this range mapping usually happens in the model/view/projection transforms).
If you need more precision, you can't get that natively: the 32-bit floats used throughout the API and hardware only have 23 mantissa bits, and the implicit leading 1 effectively gives you 24 bits of precision. To deal with this, the typical solution is to chop the world up into segments (e.g. a grid), and store object positions relative to the segment they're in. When an object moves from one segment to another, you translate its position to be relative to the new segment. When rendering use the segment containing the camera as (0,0,0) and map the segments that fall within the view frustum to the [0,1] depth range.