Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Memory is full with vertex buffer


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 01 July 2013 - 12:40 AM

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, 01 July 2013 - 12:43 AM.

If you see a post from me, you can safely assume its C# and XNA :)


Sponsor:

#2 Jason Z   Crossbones+   -  Reputation: 5163

Like
1Likes
Like

Posted 01 July 2013 - 04:54 AM

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?



#3 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 01 July 2013 - 12:21 PM

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


If you see a post from me, you can safely assume its C# and XNA :)


#4 Jason Z   Crossbones+   -  Reputation: 5163

Like
1Likes
Like

Posted 01 July 2013 - 07:52 PM

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.



#5 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 01 July 2013 - 09:08 PM

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


If you see a post from me, you can safely assume its C# and XNA :)


#6 Dave Hunt   Crossbones+   -  Reputation: 2469

Like
0Likes
Like

Posted 01 July 2013 - 09:28 PM

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?



#7 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 01 July 2013 - 09:51 PM

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, 01 July 2013 - 09:57 PM.

If you see a post from me, you can safely assume its C# and XNA :)


#8 Dave Hunt   Crossbones+   -  Reputation: 2469

Like
0Likes
Like

Posted 02 July 2013 - 08:59 AM

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.



#9 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 02 July 2013 - 01:27 PM

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

If you see a post from me, you can safely assume its C# and XNA :)


#10 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 04 July 2013 - 07:52 AM

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


We are now on Tumblr, so please come and check out our site! 

 

http://xpod-games.com


#11 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 04 July 2013 - 05:35 PM

Since my cubes will only be one texture on all six sides, would I still need an atlas?


If you see a post from me, you can safely assume its C# and XNA :)


#12 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 05 July 2013 - 02:44 PM

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


We are now on Tumblr, so please come and check out our site! 

 

http://xpod-games.com





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS