How to grab pixels from a floating point rendertexture in unity?

Viewed 696

I use the following code to grab the pixels from a rendertexture, pixelsRT, which has the format RenderTextureFormat.ARGBFloat

public Color[] getPixels()
{
    Texture2D temp = new Texture2D(width, height, TextureFormat.RGBAFloat, false);
    var oldRT = RenderTexture.active;
    RenderTexture.active = pixelsRT;
    temp.ReadPixels(new Rect(0, 0, temp.width, temp.height), 0, 0, false);
    temp.Apply();
    RenderTexture.active = oldRT;
    Color[] pixels = temp.GetPixels();
    qitools.Tools.Destroy(temp);
    return pixels;
}

This works fine as long as the shader writes values in the 0-59 range, but once it uses the full -inf to inf range, the pixel values returned gets clamped.

To make sure Unity indeed can handle the full range, I wrote this test code:

Texture2D tex = new Texture2D(10, 10, TextureFormat.RGBAFloat, false);
Color[] col = new Color[10 * 10];
col[0] = new Color(-10000, 0, 0, 1);
tex.SetPixels(col);
tex.Apply();
col=tex.GetPixels();
Debug.Log("Tester: "+col[0]);

This outputs (-10000,0,0,1) as expected.

So, it seems conversion from RenderTexture to Texture2D is not able to retain the full range. Also, I suspect the precision is lowered during the conversion as well.

I need to read the full range of the pixels in the RenderTexture on the CPU, but I am stuck. Can anyone help?

EDIT: I should point out this is not for rendering pixels, but for computing data. So access to the full precision and range that the floating point texture natively supports is needed.

EDIT2: I discovered the problem is exclusively with HDRP, in the regular pipeline all works well. Unfortunately my project is HDRP only.

Apparently some unwanted post processing pass is capping the data written by the shaders. I have tried to disable all post processing in the volumes, but I have not been able to locate the culprit...

0 Answers
Related