I am having a performance problem when trying to draw two kinds of 2D textures together in XNA (see code below).
Drawing just the captured frame from a webcam with a mask overlain, I get solid 30 FPS.
Drawing just the Gaussian dots (even if it's a high number of dots), I get solid high FPS, too.
But drawing captured, frame, mask and Gaussian dots together, FPS drop to 15. I however need higher FPS for my application
My question is, how can I achieve higher frame rates (like stable 30 FPS) in this case. What would I have to change?
I am puzzled by a thing: I thought performance would go down with increasing number of Textures2d drawn. However, even if I just draw the frame, mask and add a single Gaussian dot, it goes straight down to 15 FPS - uncreasing the number of Gaussian dot 2d Textures (i.e. the total number of textures drawn) doesn't seem to make a difference any more. So the problem must lie in combining these two different things together.
Can anyone help!
Thanks a lot!
// if residual is enabled, blend the video with the mask
if (configPanel.GetResidualEnabled())
{
// Turn alpha blending on
GraphicsDevice.BlendState = BlendState.Additive;
Texture2D frame = configPanel.GetFrame();
// Set the channels to write to the R, G, B channels
// and draw the first texture using a sprite batch
spriteBatch.Draw(frame, new Rectangle(
0, 0, graphics.PreferredBackBufferWidth,
graphics.PreferredBackBufferHeight),
new Rectangle(0, 0, frame.Width, frame.Height),
Color.White, 0.0f,
new Vector2(0, 0), myEffect, 0.0f);
// Set channels to alpha only, and draw the alpha mask
spriteBatch.Draw(configPanel.GetMask(), new Vector2(0, 0), Color.White);
GraphicsDevice.BlendState = BlendState.Opaque;
}
// render the gaussian dots
if (configPanel.GetgaussianDotsEnabled())
{
Texture2D gaussianDotTexture = configPanel.GetgaussianDot();
foreach (GaussianDot gaussianDot in configPanel.GetGaussianDotList())
{
// Draw the gaussianDot with the correct amount of transparency
// and shift them by 1/2 of gaussianDot's size
if (!configPanel.GetHorizontalFlip())
spriteBatch.Draw(gaussianDotTexture,
new Vector2(gaussianDot.centreX -
gaussianDotTexture.Width / 2,
gaussianDot.centreY -
gaussianDotTexture.Height / 2),
Color.White * gaussianDot.alpha);
// if the box is checked, flip the horizontal by shifting the centres
else
spriteBatch.Draw(configPanel.GetgaussianDot(),
new Vector2(graphics.PreferredBackBufferWidth -
(gaussianDot.centreX + gaussianDotTexture.Width / 2),
gaussianDot.centreY - gaussianDotTexture.Height / 2),
Color.White * gaussianDot.alpha);
}
}
Can't really tell what's the cause in your code. You should profile your application using the Analyze menu in Visual Studio, leave it running for a while then stop it and look at the Sample Profiling Report.
Then find which places in your code are marked as hot such as the example below.