• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ChristianFrantz

Memory is full with vertex buffer

11 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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?

1

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

0

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.

1

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

0

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?

0

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
0

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.

0

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
0

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

0

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0