I created a simple webcam that detects “motion edges”, so I draw a texture that shows where the pixels of the current frame are significantly different from the previous frame. This is my code:
Color[] differenceData = new Color[CurrentTexture.Width * CurrentTexture.Height];
Color[] currentData = new Color[CurrentTexture.Width * CurrentTexture.Height];
Color[] lastData = new Color[LastTexture.Width * LastTexture.Height];
CurrentTexture.GetData<Color>(currentData);
LastTexture.GetData<Color>(lastData);
for (int i = 0; i < currentData.Length; i++)
{
int sumCD = ColorSum(currentData[i]);
int sumLD = ColorSum(lastData[i]);
if ((sumCD > sumLD - Variance) && (sumCD < sumLD + Variance))
differenceData[i] = new Color(0, 0, 0, 0);
else
differenceData[i] = new Color(0, (byte)Math.Abs(sumCD - sumLD), 0);
}
DifferenceTexture = new Texture2D(game1.GraphicsDevice, CurrentTexture.Width, CurrentTexture.Height);
DifferenceTexture.SetData<Color>(differenceData);
LastTexture = new Texture2D(game1.GraphicsDevice,CurrentTexture.Width, CurrentTexture.Height);
LastTexture.SetData<Color>(currentData);
Is there a way to offload this calculation to a GPU using shaders (it can go at about 25/26 frames per second using the above method, but it's a bit slower)? I have a basic understanding of how HLSL shaders work and do not wait for a complete solution, I just want to know if it will be possible and how to get the "difference" texture data from the shader, and if it will actually be faster.
Thanks in advance.