I wrote a shader to collect some statistics and saved them to the alpha channel of the output texture.
Now, I need to get an average value of them, so I tried to read the last level of the RenderTexture (which means a 1x1 Texture) in OnRenderImage(RenderTexture src, RenderTexture dest):
RenderTexture tempBuffer = RenderTexture.GetTemporary(src.width, src.height, 0, src.format, RenderTextureReadWrite.Linear);
tempBuffer.useMipMap = true;
mipTex = new Texture2D(1, 1, src.graphicsFormat, TextureCreationFlags.None);
Graphics.Blit(src, tempBuffer, _Material, 0);
Graphics.CopyTexture(tempBuffer, 0, tempBuffer.mipmapCount - 1, mipTex, 0, 0);
Color c = mipTex.GetPixel(0, 0);
Graphics.Blit(tempBuffer, dest, _Material, 1);
However, I got a negative value of c.a in Unity Editor, which is obviously wrong.
Update:
It seems to work on my Hololens 1, but the fps dropped heavily from ~50 to ~25, and the memory usage increased from 300+MB to 800+MB. It’s confusing, I just read a fullscreen texture and return its colors in pass 1 like this:
fixed4 frag (v2f i) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
fixed4 renderTex = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv);
return renderTex;
}
How can it be so costly?
Update 2:
It did not work on Hololens. c.a is always 0.804. Besides, if I remove tempBuffer and blit from src to dest twice like this:
Graphics.Blit(src, dest, _Material, 0);
Graphics.Blit(src, dest, _Material, 1);
the fps keeps ~50. I’m extremely confused about this problem…