본문으로 바로가기
반응형


Unity shader에서 정의하는 DATA type은


High precision: float

Highest precision floating point value; generally 32 bits (just like float from regular programming languages).

Full float precision is generally used for world space positions, texture coordinates, or scalar computations involving complex functions such as trigonometry or power/exponentiation.

Medium precision: half

Medium precision floating point value; generally 16 bits (range of –60000 to +60000, with about 3 decimal digits of precision).

Half precision is useful for short vectors, directions, object space positions, high dynamic range colors.

Low precision: fixed

Lowest precision fixed point value. Generally 11 bits, with a range of –2.0 to +2.0 and 1/256th precision.

Fixed precision is useful for regular colors (as typically stored in regular textures) and performing simple operations on them.


로 정의 된다. 간략하게 정리하면 float : 32bit 부동소수점 방식. half : 16bit. fixed : 11bit 고정 소수점 방식.


Shader에서 fixed는 11bit의 고정소수부를 가지는 구조이므로 1bit는 양수 음수 판정, 2bit는 고정 소수부, 8bit는 정수부로 최대 -2~2(2^8승이니 -255~256) 의 값을 가진다고 알고 있었는데 왜 셰이더에서 이 이상의 값을 fixed로 계산해도 멀쩡히 나오는가?


1. 컴파일러 단계에서 알아서 변환해준다. half나 fixed+fixed(이럼 half 쓰는거 보다 손해니 나가린데...)

2. 빌드할때 이 이상의 값은 짤린다(근데 빌드해봄 멀쩡히 나옴.. 옛날엔 문제 있다던 기억도 나는데...)

3. 셰이더에서 음수판정을 제외한 10bit를 정수로 무조건 소수점 두자리로 처리한다.(그럼 1024니 10까지 값인데 이이상도 잘 나오던데....;; 아니 이럼 fixed가...)

4. 우주의 기운이 나서서 도와준다...;;;


퇴근길 다시 생각나 찾아보니 의의로 정답이 금방 찾아졌다..



Precision, Hardware Support and Performance

One complication of float/half/fixed data type usage is that PC GPUs are always high precision. That is, for all the PC (Windows/Mac/Linux) GPUs, it does not matter whether you write float, half or fixed data types in your shaders. They always compute everything in full 32-bit floating point precision.

The half and fixed types only become relevant when targeting mobile GPUs, where these types primarily exist for power (and sometimes performance) constraints. Keep in mind that you need to test your shaders on mobile to see whether or not you are running into precision/numerical issues.

Even on mobile GPUs, the different precision support varies between GPU families. Here’s an overview of how each mobile GPU family treats each floating point type (indicated by the number of bits used for it):


GPU Family float half fixed
PowerVR Series 6/7 32 16
PowerVR SGX 5xx 32 16 11
Qualcomm Adreno 4xx/3xx 32 16
Qualcomm Adreno 2xx 32 vertex 24 fragment
ARM Mali T6xx/7xx 32 16
ARM Mali 400/450 32 vertex 16 fragment
NVIDIA X1 32 16
NVIDIA K1 32
NVIDIA Tegra 3/4 32 16


Most modern mobile GPUs actually only support either 32-bit numbers (used for float type) or 16-bit numbers (used for both half and fixed types). Some older GPUs have different precisions for vertex shader and fragment shader computations.

Using lower precision can often be faster, either due to improved GPU register allocation, or due to special “fast path” execution units for certain lower-precision math operations. Even when there’s no raw performance advantage, using lower precision often uses less power on the GPU, leading to better battery life.

A general rule of thumb is to start with half precision for everything except positions and texture coordinates. Only increase precision if half precision is not enough for some parts of the computation.



Support for infinities, NaNs and other special floating point values

Support for special floating point values can be different depending on which (mostly mobile) GPU family you’re running.

All PC GPUs that support Direct3D 10 support very well-specified IEEE 754 floating point standard. This means that float numbers behave exactly like they do in regular programming languages on the CPU.

Mobile GPUs can have slightly different levels of support. On some, dividing zero by zero might result in a NaN (“not a number”); on others it might result in infinity, zero or any other unspecified value. Make sure to test your shaders on the target device to check they are supported.



참고 링크 : https://docs.unity3d.com/Manual/SL-DataTypesAndPrecision.html


반응형