Clarity/Best Practice for datatype of COLOR semantic - half/fixed/float etc.

I have no particular expertise in graphics programming, so when writing shaders I apply knowledge that I’ve picked up from various books/websites, together with intuition around general coding best practice, which suggests:

  • You should always use the smallest datatype capable of representing the precision and range of a given variable. Colours and unit length vectors can use fixed, for other variables prefer fixed over half, and half over float.
  • You should avoid converting values between types if possible.
  • Particularly on mobile, applying swizzles to fixed types is expensive.

With those rules in mind, I’m looking through the Unity example shaders (which I’m assuming represent best practice :slight_smile: and trying to explain the justification of why the COLOR semantic returned by the fragment shader is

Can anyone provide a justification why these examples don’t return COLOR as a fixed4? (Should all fragment shaders return a fixed4?)

First off, unless you’re targeting mobile, that is OpenGL ES, you can safely forget that precision qualifiers even exist, just use floats everywhere. They have literally zero impact on other platforms… the GPUs don’t support it and the compilers discard it anyways.

Assuming you do have mobile in mind, my guess is that a good idea would be to use the precision that matches the render target format. If you’re drawing onto any 8 bit per component format (LDR), use fixed, if it’s 16 bit per component (HDR), use half and if its full precision (depth buffer, etc…), output a float.

1 Like

Thankyou - that’s helpful. I once got caught out having to spend ages debugging some obscure Android device whose GPU threw a hissy fit because of the return type of the fragment shader, and I think I’ve been ultra-cautious about it ever since!