I convert an image to grayscale with the code below and my GC per frame baloons to 60MB. I have several images so this becomes a killer. I’m getting these results on Unity 2019.2.2f1 and 2019.3 on a fresh standalone built-in render pipeline project.
Texture2D graph = thisTexture;
//convert texture
Texture 2D grayImg = new Texture2D(graph.width, graph.height, graph.format, false);
Graphics.CopyTexture(graph, grayImg);
Color32[] pixels = grayImg.GetPixels32();
for (int x = 0; x < grayImg.width; x++)
{
for (int y = 0; y < grayImg.height; y++)
{
Color32 pixel = pixels[x + y * grayImg.width];
int p = ((256 * 256 + pixel.r) * 256 + pixel.b) * 256 + pixel.g;
int b = p % 256;
p = Mathf.FloorToInt(p / 256);
int g = p % 256;
p = Mathf.FloorToInt(p / 256);
int r = p % 256;
float l = (0.2126f * r / 255f) + 0.7152f * (g / 255f) + 0.0722f * (b / 255f);
Color c = new Color(l, l, l, 1);
grayImg.SetPixel(x, y, c);
}
}
grayImg.Apply(false);
return grayImg;
I can’t quite understand what’s going on from profiler, can someone please shed some light what’s going on?
Image for this GC result is attached sampleImg.jpg
Thanks a bunch for your help