How do I get half precision floating point numbers from a metal texture with Swift?

My metal core is written in a texture with the format MTLPixelFormat.RG16Float, with half the precision of floating point numbers. This is due to the fact that Metal does not support writing 32-bit floating point textures.

Do I need to read these half-digits in my quick program? I moved the texture to a Swift UInt8 array, but I cannot figure out how to convert a half precision float into Swift floats.

+5
source share
2 answers

EDIT: The bottom answer was written for Swift v1. Later versions of Swift added support through the float16_t type. Manual conversion may be useful in some cases.


There is no built-in method for interpreting halves as a float, you need to convert it yourself. The semi-data type is a floating point format following IEEE 754. You can read the values ​​in your quick program using UInt16 and then convert from that value to a float value. I don’t know of any conversion programs written in Swift, but here are two libraries written in C ++ that can be converted quite easily:

http://mrob.com/pub/math/s10e5.h.txt

http://half.sourceforge.net/

In the first library, this is the operator float () function that needs to be converted, which takes the UInt16 _h member variable and outputs a float.

+3
source

Actually, @Muzza's answer is incorrect. You could read them using the float16_t pointer and translate them into regular float32_t . No need to use external libraries. Just import the arm_neon header.

+3
source

Source: https://habr.com/ru/post/1205334/


All Articles