- Home /
Using Graphics.Blit with the same source and destination
I would like to use Graphics.Blit in order to pass a RenderTexture through a shader. I don't need the results of the RenderTexture from before the Blit, so I tried using Graphics.Blit with the same RenderTexture as the source and the destination. However, this fails.
When I use two different RenderTextures as the source and destination, Graphics.Blit works fine, but it seems wasteful to create two different RenderTextures when I only ultimately ever need the final result. Is there a way to use Graphics.Blit with the same source and destination, or is the dual texture setup the only way?
Answer by BastianUrbach · Jun 11, 2021 at 06:26 AM
You need two textures. Afaik this is a technical limitation of GPUs. An alternative would be to use a ComputeShader since those can read and write the same texture. Note that this requires "enableRandomWrite" to be enabled on the RenderTexture, which you can't do after it's been created so this only works if you create the RenderTexture yourself (as opposed to e.g. using the ones passed to OnRenderImage). You can also get a temporary RenderTexture using RenderTexture.GetTemporary. This should be faster since it attempts to reuse RenderTextures instead of creating new ones. Remember to release it with RenderTexture.ReleaseTemporary when you're done.