I'm building a video recorder type of app, and the method GetData()
from Texture2D is just too slow. It takes about 50% of CPU usage time, which means that the game is going to have fps drops, between 30-40, instead of 60.
Here´s the current code:
byte[] stream = new byte[textW * textH * 4];
try
{
texture.GetData(stream); // Very slow !!!
} catch (Exception e)
{
Console.WriteLine(e.Message);
}
var bmp1 = new Bitmap(textW, textH);
BitmapData data = bmp1.LockBits(new Rectangle(0, 0, textW, textH), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
IntPtr ptr = data.Scan0;
Marshal.Copy(stream, 0, ptr, textW * textH * 4);
unsafe
{
byte* ptrFirstPixel = (byte*)data.Scan0;
int bytesPerPixel = Bitmap.GetPixelFormatSize(data.PixelFormat) / 8;
int heightInPixxel = data.Height;
int widthInBytes = data.Width * bytesPerPixel;
Parallel.For(0, heightInPixxel, y =>
{
byte* currLine = ptrFirstPixel + (y * data.Stride);
for (int x = 0; x < widthInBytes; x += bytesPerPixel)
{
int b = currLine[x];
int r = currLine[x + 2];
currLine[x + 2] = (byte)b;
currLine[x] = (byte)r;
}
});
}
bmp1.UnlockBits(data);
bmp1.Save(outputPath, ImageFormat.Png);
bmp1.Dispose();
texture.Dispose();