user2618681
user2618681

Reputation: 13

WP7 Lagging when drawing lots of primitive cubes

I'm currently working on a 3D Arkanoid clone, and I'm drawing the cubes using primitives.

I have 6 rows, each with 49 blocks, so 294 blocks total. Normally I'm not to bothered about optomisation, but the game is ridiculously slower on lower end phones. I know the issue is with the drawing of the blocks, because the game runs fine with that code removed.

My code for setting up the primitives is

VertexPositionNormalTexture[] vertices1 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices2 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices3 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices4 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices5 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices6 = new VertexPositionNormalTexture[4];

public void SetupVertices(){
 vertices1[0].Position = new Vector3(position.X, position.Y, position.Z);
        vertices1[0].TextureCoordinate = new Vector2(1, 1);
        vertices1[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
        vertices1[1].TextureCoordinate = new Vector2(1, 0);
        vertices1[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
        vertices1[2].TextureCoordinate = new Vector2(0, 0);
        vertices1[3].Position = new Vector3(position.X + size.X, position.Y, position.Z);
        vertices1[3].TextureCoordinate = new Vector2(0, 1);
        //Another side

        vertices2[0].Position = new Vector3(position.X, position.Y, position.Z);
        vertices2[0].TextureCoordinate = new Vector2(0, 1);
        vertices2[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
        vertices2[1].TextureCoordinate = new Vector2(0, 0);
        vertices2[2].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
        vertices2[2].TextureCoordinate = new Vector2(1, 0);
        vertices2[3].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
        vertices2[3].TextureCoordinate = new Vector2(1, 1);
        //ANOTHER SIDE
        vertices3[0].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
        vertices3[0].TextureCoordinate = new Vector2(0, 1);
        vertices3[1].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
        vertices3[1].TextureCoordinate = new Vector2(0, 0);
        vertices3[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
        vertices3[2].TextureCoordinate = new Vector2(1, 0);
        vertices3[3].Position = new Vector3(position.X + size.X, position.Y, position.Z);
        vertices3[3].TextureCoordinate = new Vector2(1, 1);

        vertices4[0].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
        vertices4[0].TextureCoordinate = new Vector2(0, 1);
        vertices4[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
        vertices4[1].TextureCoordinate = new Vector2(0, 0);
        vertices4[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
        vertices4[2].TextureCoordinate = new Vector2(1, 0);
        vertices4[3].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
        vertices4[3].TextureCoordinate = new Vector2(1, 1);


        vertices5[0].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
        vertices5[0].TextureCoordinate = new Vector2(0, 0);
        vertices5[1].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
        vertices5[1].TextureCoordinate = new Vector2(1, 0);
        vertices5[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
        vertices5[2].TextureCoordinate = new Vector2(1, 1);
        vertices5[3].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
        vertices5[3].TextureCoordinate = new Vector2(0, 1);

        //bottom
        vertices6[0].Position = new Vector3(position.X, position.Y, position.Z);
        vertices6[0].TextureCoordinate = new Vector2(0, 0);
        vertices6[1].Position = new Vector3(position.X + size.X, position.Y, position.Z);
        vertices6[1].TextureCoordinate = new Vector2(1, 0);
        vertices6[2].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
        vertices6[2].TextureCoordinate = new Vector2(1, 1);
        vertices6[3].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
        vertices6[3].TextureCoordinate = new Vector2(0, 1);
        numTriangles = 4;
        indices = new short[numTriangles + 2];

        int i = 0;
        indices[i++] = 0;
        indices[i++] = 1;
        indices[i++] = 3;
        indices[i++] = 2;
}

My code for drawing the blocks is basic, but I have no idea how to optomise it.

//Game1.CS Code
GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp; // need to do this on reach     devices to allow non 2^n textures


        #region GamePlaying
        if (gameState == State.BREAKOUT || gameState == State.GAMEOVERBREAKOUT)
        {
            foreach (Block block in lastBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in fourthBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in thirdBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in secondBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in firstBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in fifthBlocks)
            {
                block.Draw(camera);
            }
}

Block.CS Draw Code

public void Draw(Camera camera)
    {
        effect.View = camera.View;
        effect.Projection = camera.Projection;
        effect.World = rotationMatrix;


        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices1, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices2, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices3, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices4, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices5, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices6, 0, 4, indices, 0, numTriangles);
        }

    }

Upvotes: 0

Views: 71

Answers (1)

Ani
Ani

Reputation: 10896

Calling DrawUserIndexedPrimitives per block is the problem. The idea behind this kind of draw call is to let you draw a large number of primitives with few calls, not make many calls that draw a little.

You have two options:

  1. Use SpriteBatch to draw the quads (this internally performs batching for you so you don't have to handle it).
  2. Instead of calling DrawUserIndexedPrimitives directly inside each Draw call, accumulate the vertices (and re-numbered indices) in a temporary buffer and submit this once (per Effect for a set of indices) when all objects have been accumulated.

Hope this helps.

Upvotes: 1

Related Questions