title: XNA 4.0 - Using spriteBatch and basicEffect lags
body: <p>I am currently working on a game, in which we need to combine the use of <strong>DrawUserIndexedPrimitives</strong> and normal <strong>spriteBatch.Draw</strong>. Not combined as we use them at the same time, but we first have to draw some 2d sprites using spriteBatch, where after we disable spriteBatch to enable basicEffect and draw the primitives, and at last enable spriteBatch again. The code below shows the section of the code, where the problem is occurring.</p>
  
  <pre><code>spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GraphicsDevice));
                  levelController.Draw(spriteBatch);
  
  
                  if (gameReady)
                  {
                      foreach (GameObject.GameObject go in gameObjects)
                      {
                          go.Draw(spriteBatch);
                      }
                      spriteBatch.End();
  
                      foreach (GameObject.GameObject go in gameObjects)
                      {
                          if (go is GameObject.Enemy.Enemy)
                          {
                              GameObject.Enemy.Enemy enemy = (GameObject.Enemy.Enemy)go;
  
                              basicEffect = new BasicEffect(graphics.GraphicsDevice);
                              basicEffect.VertexColorEnabled = true;
                              basicEffect.CurrentTechnique.Passes[0].Apply();
  
                              GraphicsDevice.DrawUserIndexedPrimitives&lt;VertexPositionColor&gt;(PrimitiveType.TriangleList, enemy.GetCollisionTriangle.Triangle, 0, 3, enemy.GetCollisionTriangle.Indices, 0, 1);
                          }
                      }
                  }
  
                  spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GraphicsDevice));
  </code></pre>
  
  <p>If the code below gets quoted out, the lag stops.</p>
  
  <pre><code>basicEffect = new BasicEffect(graphics.GraphicsDevice);
                          basicEffect.VertexColorEnabled = true;
                          basicEffect.CurrentTechnique.Passes[0].Apply();
  
                          GraphicsDevice.DrawUserIndexedPrimitives&lt;VertexPositionColor&gt;(PrimitiveType.TriangleList, enemy.GetCollisionTriangle.Triangle, 0, 3, enemy.GetCollisionTriangle.Indices, 0, 1)
  </code></pre>
  
  <p>Can it really be that we can't both use spriteBatch and basicEffect without the game lags a lot? it has been tested on 3 different computers, from a very old laptop, to a brand new gamer pc.
  The game is unplayable with the lag that occurs.</p>