How to loop through a render texture pixels

Hello

After processing my render texture on a compute shader, i want to loop through each pixel and debug log the data in C# to validate the values are correct.

How do you do this? I can’t find any way to read pixels of a render texture in the documentation which is kind’ve weird.

If you want to read the individual pixels of a render texture from C#, you need to copy it back to a Texture2D asset using ReadPixels().

Though this only works if the format you’re using for your render texture is supported by that function. And you need to make sure the Texture2D is of the appropriate TextureFormat to match the RenderTextureFormat to get accurate data, and the enums don’t match exactly so you have to do that by hand.

Though I would suggest using RenderDoc instead.

Do i have to set the render texture the compute shader ran to the RenderTexture.active first before i can read the pixels to my texture2d?

Yes.

But, again, just use RenderDoc and save yourself the pain.

1 Like

Understand the RenderTexture only exists on the GPU, which is why it’s not accessible from script until it’s copied back from the GPU to the CPU side memory (which is what ReadPixels() is doing). RenderDoc does that for you, and makes sure the values you see when hoving over pixels is exactly the same as what the GPU sees.

1 Like