Search code examples
unity-game-engineshaderhlslcompute-shader

HLSL odd behaviour adding/subtracting small numbers


When adding/subtracting numbers around 0.002 in a HLSL compute shader, there is some really odd behaviour.

#pragma kernel Fade

RWTexture2D<float4> Result;

[numthreads(8,8,1)]
void Fade (uint3 id : SV_DispatchThreadID)
{
    Result[id.xy] = Result[id.xy] + 0.001;
}

Result is the texture that is displayed, it's initially passed in as black. The idea is to achieve a slow fade to white, which works when I set the number to 0.0023 or above but not when below.

Here's a list of cases which result in different behaviour:

  • 0.0022 ends up with a grey which doesn't lighten further
  • 0.00221 lights up fine again.
  • 0.00219 doesn't light up.

When subtracting from a non-black colour:

  • 0.00195 doesn't subtract
  • 0.00196 does subtract

Any hints towards this would be appreciated.


Solution

  • I found out the answer: for some reason Unity was defaulting to a very limited RenderTextureFormat. i have now modified my render texture definition to read

    renderTexture = new RenderTexture(width, height, 32, RenderTextureFormat.ARGBFloat);
    

    And now everything works flawlessly.