简体   繁体   English

金属阴影语言中的浮点精度问题

[英]Float precision problem in metal shading language

I pass a float param with value 0.00291545 to fragment shader. 我将值0.00291545的float参数传递给片段着色器。 But fragment function got n/a . 但是片段功能不适用。 swift code fragment shader 快速代码 片段着色器

In Xcode debugger's "Bound Resources" view, it shows that the value is 0.003 Bound Resources 在Xcode调试器的“绑定资源”视图中,该值显示为0.003 绑定资源

In Xcode shader debugger view, I made some testes. 在Xcode着色器调试器视图中,我做了一些测试。 It shows that 0.0004 will be casted to 0 and 0.0005 will be casted to 0.001 . 它显示将0.0004强制转换为0,将0.0005强制转换为0.001。 The float value behaves like half value. 浮点值的行为类似于半值。 test code in shader debugger 着色器调试器中的测试代码

So, my question is: 所以,我的问题是:

  1. Why 0.00291545 is first casted to 0.003, then casted to 0 ? 为什么先将0.00291545强制转换为0.003,然后强制转换为0?

  2. Why float value has the same precision with half ? 为什么浮点值的精度与一半相同?

  3. How could I use float value that has normal precision ? 如何使用具有正常精度的浮点值?

I found what the problem was! 我发现了问题所在!

Xcode debugger misled me. Xcode调试器误导了我。

0.00291545 was not casted to 0.003 and was not casted to n/a. 0.00291545未转换为0.003,也未转换为n / a。 The debugger' display is not accurate, but the value is accurate actually. 调试器的显示不准确,但实际值是准确的。

Thanks for Ken Thomases 感谢Ken Thomases

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM