• Advertisement
Sign in to follow this  

Memory is full with vertex buffer

This topic is 1661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having a pretty strange problem that I didn't think I'd run into. I was able to store a 50x50 grid in one vertex buffer finally, in hopes of better performance. Before I had each cube have an individual vertex buffer and with 4 50x50 grids, this slowed down my game tremendously. But it still ran. With 4 50x50 grids with my new code, that's only 4 vertex buffers. With the 4 vertex buffers, I get a memory error. When I load the game with 1 grid, it takes forever to load and with my previous version, it started up right away. So I don't know if I'm storing chunks wrong or what but it stumped me -.-

for (int x = 0; x < 50; x++)
            {
                for (int z = 0; z < 50; z++)
                {
                    for (int y = 0; y <= map[x, z]; y++)
                    {
                        SetUpVertices();
                        SetUpIndices();
                        cubes.Add(new Cube(device, new Vector3(x, map[x, z] - y, z), grass));
                    }
                }
            }

            vertexBuffer = new VertexBuffer(device, typeof(VertexPositionTexture), vertices.Count(), BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionTexture>(vertices.ToArray());

            indexBuffer = new IndexBuffer(device, typeof(short), indices.Count(), BufferUsage.WriteOnly);
            indexBuffer.SetData(indices.ToArray());

Thats how theyre stored. The array I'm reading from is a byte array which defines the coordinates of my map. Now with my old version, I used the same loading from an array so that hasn't changed. The only difference is the one vertex buffer instead of 2500 for a 50x50 grid. cubes is just a normal list that holds all my cubes for the vertex buffer

Edited by burnt_casadilla

Share this post


Link to post
Share on other sites
Advertisement

Do you ever release the cubes that you are creating at the inside of the loop?  Also, it appears that your vertex buffer must allow storage of 50 x 50 x H cubes, where H is the height of each element in your array.

 

Have you calculated how much memory is being allocated for your vertex buffers?

Share this post


Link to post
Share on other sites

I have not released the cubes, or calculated how much memory is in my vertex buffers because I don't know how to do either. I'm still a noob lol

Share this post


Link to post
Share on other sites

Anything that you create with 'new' is allocated on the heap, and has to be released with the 'delete' keyword when you are finished with it.  If you don't release the objects, then the objects continue to stack up until all of the memory is consumed.

 

So you should delete the array of objects after the data is copied into the vertex buffer.

Share this post


Link to post
Share on other sites

Ah. I was under the impression that any kind of "garbage collection" was done automatically with c#. Or maybe that was Java...anywho I'll give it a shot

Share this post


Link to post
Share on other sites

It wasn't clear from your post that you were using C# (it's easy to miss the 'xna' tag), but you are correct. As long as nothing is holding a reference to the allocated memory, it will get collected.

 

Where does the memory error occur?

 

Also, what do SetUpVertices and SetUpIndices look like?

Share this post


Link to post
Share on other sites

I figured out the error. I had put effect = new BasicEffect(); in my cube constructor. Stupid mistake. I don't really need 100,000 effects for each cube. But anyway now that I have the 4 grids loaded my fps is down again, this time  7~9. I have a feeling it has something to do with my draw method, as each grid has its own buffer and I should not be getting as many draw calls as I have.

public void DrawMap(GameTime gameTime)
        {

            foreach (Cube block in cube.cubes)
            {

                effect.VertexColorEnabled = false;
                effect.TextureEnabled = true;
                effect.Texture = grass;

                Matrix center = Matrix.CreateTranslation(new Vector3(-0.5f, -0.5f, -0.5f));
                Matrix scale = Matrix.CreateScale(1f);
                Matrix translate = Matrix.CreateTranslation(block.cubePosition);

                effect.World = center * scale * translate;
                effect.View = cam.view;
                effect.Projection = cam.proj;

                effect.FogEnabled = false;
                effect.FogColor = Color.CornflowerBlue.ToVector3();
                effect.FogStart = 1.0f;
                effect.FogEnd = 50.0f;

                cube.Draw(effect);
                noc++;
            }
        } 

Is there any way around this? I kinda need to the for loop to set the position of each cube, but I think its part of my fps drop issue.

 

My SetUp methods are:

        private void SetUpIndices()
        {
            /*
            //Front face
            //bottom right triangle
            indices.Add(0);
            indices.Add(2);
            indices.Add(3);

            indices.Add(0);
            indices.Add(1);
            indices.Add(2);

            indices.Add(1);
            indices.Add(5);
            indices.Add(6);

            indices.Add(1);
            indices.Add(6);
            indices.Add(2);

            indices.Add(2);
            indices.Add(6);
            indices.Add(7);

            indices.Add(2);
            indices.Add(7);
            indices.Add(3);

            indices.Add(4);
            indices.Add(7);
            indices.Add(6);

            indices.Add(4);
            indices.Add(6);
            indices.Add(5);

            indices.Add(1);
            indices.Add(4);
            indices.Add(5);

            indices.Add(1);
            indices.Add(0);
            indices.Add(4);

            indices.Add(0);
            indices.Add(7);
            indices.Add(4);

            indices.Add(0);
            indices.Add(3);
            indices.Add(7);*/
            #region IndicesArray
            indices = new short[36];

            indices[0] = 0;
            indices[1] = 2;
            indices[2] = 3;
            //top left triangle
            indices[3] = 0;
            indices[4] = 1;
            indices[5] = 2;
            //back face
            //bottom right triangle
            indices[6] = 1;
            indices[7] = 5;
            indices[8] = 6;
            //top left triangle
            indices[9] = 1;
            indices[10] = 6;
            indices[11] = 2;
            //Top face
            //bottom right triangle
            indices[12] = 2;
            indices[13] = 6;
            indices[14] = 7;
            //top left triangle
            indices[15] = 2;
            indices[16] = 7;
            indices[17] = 3;
            //bottom face
            //bottom right triangle
            indices[18] = 4;
            indices[19] = 7;
            indices[20] = 6;
            //top left triangle
            indices[21] = 4;
            indices[22] = 6;
            indices[23] = 5;
            //left face
            //bottom right triangle
            indices[24] = 1;
            indices[25] = 4;
            indices[26] = 5;
            //top left triangle
            indices[27] = 1;
            indices[28] = 0;
            indices[29] = 4;
            //right face
            //bottom right triangle
            indices[30] = 0;
            indices[31] = 7;
            indices[32] = 4;
            //top left triangle
            indices[33] = 0;
            indices[34] = 3;
            indices[35] = 7;
            
            #endregion IndicesArray

        }

       

        private void SetUpVertices()
        {
            #region VPNormalTexture
            /*
            //front left bottom corner
            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector2(1, 0)));
            //front left upper corner
            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector2(0, 0)));
            //front right upper corner
            vertices.Add(new VertexPositionNormalTexture(new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector2(0, 1)));
            //front lower right corner
            vertices.Add(new VertexPositionNormalTexture(new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector2(1, 1)));
            //back left lower corner
            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector2(1, 0)));
            //back left upper corner
            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, 1, -1), new Vector3(0, 1, -1), new Vector2(0, 0)));
            //back right upper corner
            vertices.Add(new VertexPositionNormalTexture(new Vector3(1, 1, -1), new Vector3(1, 1, -1), new Vector2(0, 1)));
            //back right lower corner
            vertices.Add(new VertexPositionNormalTexture(new Vector3(1, 0, -1), new Vector3(1, 0, -1), new Vector2(1, 1)));
            */
            #endregion VPNormalTexture
            vertices = new VertexPositionTexture[8];
            //front left bottom corner ok
            vertices[0] = (new VertexPositionTexture(new Vector3(0, 0, 0), new Vector2(1, 0)));
            //front left upper corner
            vertices[1] = (new VertexPositionTexture(new Vector3(0, 1, 0), new Vector2(1, 1)));
            //front right upper corner ok
            vertices[2] = (new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(0, 1)));
            //front lower right corner
            vertices[3] = (new VertexPositionTexture(new Vector3(1, 0, 0), new Vector2(0, 0)));
            //back left lower corner ok
            vertices[4] = (new VertexPositionTexture(new Vector3(0, 0, -1), new Vector2(0, 1)));
            //back left upper corner
            vertices[5] = (new VertexPositionTexture(new Vector3(0, 1, -1), new Vector2(1, 1)));
            //back right upper corner ok
            vertices[6] = (new VertexPositionTexture(new Vector3(1, 1, -1), new Vector2(1, 0)));
            //back right lower corner
            vertices[7] = (new VertexPositionTexture(new Vector3(1, 0, -1), new Vector2(0, 0)));
            #region VPColor
            /*
            //front left bottom corner
            vertices.Add(new VertexPositionColor(new Vector3(0, 0, 0), color));
            //front left upper corner
            vertices.Add(new VertexPositionColor(new Vector3(0, 1, 0), color));
            //front right upper corner
            vertices.Add(new VertexPositionColor(new Vector3(1, 1, 0), color));
            //front lower right corner
            vertices.Add(new VertexPositionColor(new Vector3(1, 0, 0), color));
            //back left lower corner
            vertices.Add(new VertexPositionColor(new Vector3(0, 0, -1), color));
            //back left upper corner
            vertices.Add(new VertexPositionColor(new Vector3(0, 1, -1), color));
            //back right upper corner
            vertices.Add(new VertexPositionColor(new Vector3(1, 1, -1), color));
            //back right lower corner
            vertices.Add(new VertexPositionColor(new Vector3(1, 0, -1), color)); 
            * */
            #endregion VPColor
        } 

The commented out code are just things  I've tried that I might need to go back to later. I don't know if its better to store my vertices and indices in a list or array, but both seem to work. Oh and if anybody can help me out with the U and V values of my vertices that'd be awesome. My texture is drawing horribly wrong, but it's not that big of an issue right now

Edited by burnt_casadilla

Share this post


Link to post
Share on other sites

You are not making 4 draw calls. You are still making 50x50x4 draw calls because you are drawing each cube individually. Also, although you are calling SetUpVertices and SetUpIndices for each cube, those methods are just creating new index/buffer arrays and thowing away their previous values on each call without ever using them. Only the last index/vertex array is getting used to build the index/vertex buffers, which now contain a single cube.

 

To get down to 4 draw calls, you need to rethink how you are building those buffers. You want to wind up with 4 index/vertex buffers that contain 50x50 cubes that are properly positioned relative to one another. Then, when you draw, you don't call draw for each cube, you do a DrawIndexedPrimitives call once for each of the 4 buffers.

Share this post


Link to post
Share on other sites
Would it matter if i stored the indices and vertices in a list instead of an array? That way the array wouldnt be deleted each time

Share this post


Link to post
Share on other sites

burnt_casadilla maybe this example on the MSDN Create website might help you, http://xbox.create.msdn.com/en-US/education/catalog/sample/mesh_instancing
 

With instancing you can create the cube once, then use an array of structures to represent the voxels in your world (which are then copied into a dynamic vertex buffer). This could effectively reduce the number of draw calls you make to just 1 (as long as you use a texture atlas). The example although complex should give you some insight into what is needed to draw the number of primitives you are looking to draw at a real-time friendly speed.

 

Aimee

Share this post


Link to post
Share on other sites

no, if you use more than one texture though you will need at-least one draw call per texture. The texture atlas thing is a something you could do if you want to add more performance for massive batches.

 

Aimee

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement