Vulkan 归一化值公式(例如 VK_FORMAT_R8_UNORM)?
Vulkan normalized value formula (eg VK_FORMAT_R8_UNORM)?
在 Vulkan 中,VK_FORMAT_R8_UNORM
等格式将 [0.0f,1.0f]
范围内的单精度浮点数映射到 8 位无符号整数。 float->uint8_t方向的公式是exactly:
uint8_t unorm(float x) {
return roundf(x*255.0f);
}
标准 C 函数 round 在哪里?或者是别的什么?还是实现定义的?
(请注意,上面给出的 uint8_t(0) 和 uint8_t(255) 的值是其他值 uint8_t(1) 的一半, uint8_t(2) 到 uint8_t(254).)
来自 3.9.2。从浮点数到归一化定点数的转换
The conversion from a floating-point value f to the corresponding unsigned normalized fixed-point value c is defined by first clamping f to the range [0,1], then computing
c = convertFloatToUint(f × (2b - 1), b)
where convertFloatToUint(r,b) returns one of the two unsigned binary integer values with exactly b bits which are closest to the floating-point value r. Implementations should round to nearest. If r is equal to an integer, then that integer value must be returned. In particular, if f is equal to 0.0 or 1.0, then c must be assigned 0 or 2b - 1, respectively.
在 Vulkan 中,VK_FORMAT_R8_UNORM
等格式将 [0.0f,1.0f]
范围内的单精度浮点数映射到 8 位无符号整数。 float->uint8_t方向的公式是exactly:
uint8_t unorm(float x) {
return roundf(x*255.0f);
}
标准 C 函数 round 在哪里?或者是别的什么?还是实现定义的?
(请注意,上面给出的 uint8_t(0) 和 uint8_t(255) 的值是其他值 uint8_t(1) 的一半, uint8_t(2) 到 uint8_t(254).)
来自 3.9.2。从浮点数到归一化定点数的转换
The conversion from a floating-point value f to the corresponding unsigned normalized fixed-point value c is defined by first clamping f to the range [0,1], then computing
c = convertFloatToUint(f × (2b - 1), b)
where convertFloatToUint(r,b) returns one of the two unsigned binary integer values with exactly b bits which are closest to the floating-point value r. Implementations should round to nearest. If r is equal to an integer, then that integer value must be returned. In particular, if f is equal to 0.0 or 1.0, then c must be assigned 0 or 2b - 1, respectively.