Read float values from RGBAFloat texture
It seems people aren't discussing much around floating point textures. I used them to do some computations and then forward the result to another surface shader (to obtain some specific deformations) and that's cool ,it always works for me if I digest the results in a shader but this time I need to get those values CPU side so I get a float[] array with the results (just after calling Graphics.Blit that fills the floating point texture). How can this be achieved?
On a side note: the only guy that I saw using this method so far is Keijiro, for example in his Kvant Wall; if you have other sources I'd be grateful if you let me know.
ps: please don't waste keyboard clicks saying there are compute shaders and OpenCL and CUDA. This is the method I need now.
This seems to be a very specific question :) Perhaps it's better suited for the forums? If you already made a thread there, disregard please.
Answer by Bunny83 · Oct 19, 2016 at 10:33 AM
Uhm, since you already mentioned that project, have you actually take a look at it? Especially the Wall.cs script?
The most important parts are that you create the RenderTexture as an ARGBFloat texture
/*#292*/ var buffer = new RenderTexture(_columns, _rows, 0, RenderTextureFormat.ARGBFloat);
To read the content of a RenderTexture you have to use Texture2D.ReadPixels of an actual Texture2D with the same format. Finally just use GetPixels to read the Color array. "Color" is made of 4 float values, so you can directly access all 4 components of each "pixel".
Note: ReadPixels as well as GetPixels are quite heavy methods depending on the size of the image. In the project he actually doen't "read" the content at all on the CPU side. He just reuses them inside another shader by directly using the rendertexture as input for the shader, just like you're currently doing.
There's no way around ReadPixels and GetPixels. GPU buffers live on the GPU and you don't have direct access to it. You need to actually lock the buffer and copy the content over. That's what ReadPixels and finally GetPixels does.
Thank you Bunny83, I do know what you are talking about, I studied that project in depth and I know how to initialize a floating point texture. I am investigating methods to download the values. For now I think I found a solution using GetRawTextureData but I will definitely try to trust the GetPixels function as you suggest it doesn't break precision and test asap. Thanks
Well GetPixels just smoothly works! I didn't realize it would keep the floating point format intact. Thank you.
If your problem is solved, don't forget to accept an answer to mark the question as answered.