Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Allocating memory problem (terrain)


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
9 replies to this topic

#1 gnomgrol   Members   -  Reputation: 628

Like
0Likes
Like

Posted 21 June 2012 - 04:01 AM

Hey guys,
I have a pretty nasty problem in my terrainengine.
It calculates the whole terrain on the start and then splits it up into chunks.
This is working fine, but only if I dont create more then around 60 chunks.
Each chunk has 256*256 vertices each and 255*255*6 indices.

As soon as i try to create more, i get a "std::bad_alloc" error. After a little research I found that its probably me, requesting to much memory.
Building the vertex/index array is done like the following:
[source lang="cpp"]VertexPosNormalTexColor *vertices;unsigned long* indices;vertices = new VertexPosNormalTexColor[chunkVnum];indices = new unsigned long[chunkInum];[/source]

I think that I have to release the memory, after creating the Vertex/Indexlist, right? (howto? just "delete vertices;" ?)
But I still need this lists to draw them (dont I?), to make things follow the terrain, and I would like to do some dynamic terraindeformstuff later.

How to solve this?

Thanks a lot, just request if you need to see some more of the code or something

Edited by gnomgrol, 21 June 2012 - 04:21 AM.


Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1164

Like
1Likes
Like

Posted 21 June 2012 - 05:26 AM

I am going to assume that each vertex takes up 64 bytes (12* for position, 12* for normal, 8 for texture co-ordinates and 32 for color), so each chunk would occupy 5,754,904 bytes (4,194,304 from the vertex data and 1,560,600 from the indices). That means that 60 chunks would take up 345,294,240 bytes. I do not think that would cause problems with memory allocation unless you had some other large data structures, so I can't say any more about your problem without looking at some more of the code. Incidentally, if you want to delete a dynamically-allocated array you want delete[] vertices;.

*this is based on the assumption that you are using 3 floats each for position and normal.

#3 gnomgrol   Members   -  Reputation: 628

Like
0Likes
Like

Posted 21 June 2012 - 05:43 AM

Thanks for your quick reply Ruler,
Everything you assumed is correct. Well, around 350 mb is a lot of data, and I intended of going to have a way larger terrain than 60 chunks.
Is there any way to cut the data smaller?, since I will have to allocate more Memory later anyway.
I really cant see what my problem is. There are no other large structures yet, and the problem only occures on a large amount of chunks.
(Now that I think of it, I had this problem since I started with engine, so it has to be something in the earlier parts of the code)

Sorry, posting the whole source is to long, and way to much work for you to read throught. Ill keep working on it and post if I find the problem.
Btw, could it be that delete[] takes fucklots of time?

Edited by gnomgrol, 21 June 2012 - 05:48 AM.


#4 Amr0   Members   -  Reputation: 1161

Like
-2Likes
Like

Posted 21 June 2012 - 05:44 AM

Assuming you're creating a grid, you don't really need index data and can just use one or more triangle strips. What API are you using for drawing?
Edit: See Reply #9. Thanks mhagain, very useful.

Edited by Amr0, 21 June 2012 - 11:42 AM.


#5 samoth   Crossbones+   -  Reputation: 5044

Like
2Likes
Like

Posted 21 June 2012 - 05:46 AM

[...] so each chunk would occupy 5,754,904 bytes [...] 60 chunks would take up 345,294,240 bytes. I do not think that would cause problems

Note that allocations are done per chunk, not in total, thus the allocation that fails is only around 5MB (not 300+MB).

Yet, you cannot assume that this is generally no problem. Due to memory fragmentation, a 5MB allocation can quite possibly fail even when in fact there are still 50 or 100 or 500MB free. This can happen because of using too many DLLs, or because of an "unhealthy" memory usage pattern.

It is even possible that a single allocation worth 100 chunks at program start succeeds while 50 chunk-sized allocations some time later fail.

In general, one has to assume that memory is not unlimited and that there are people playing your game who maybe really only have 500MB of memory available. It's unlikely because even cheap supermarket PCs come with 4GB nowadays, but it is not impossible.

One should determine how much "budget" one has available and generate as many chunks as possible (allocating that memory early, and only once), served by a "chunk manager" or something which is basically a cache. If all fits into the cache, you're happy. If all does not fit, you must regenerate chunks on the fly every now and then (reusing the memory, not allocating new!) -- which is maybe not what you've dreamed of, but it will work.

Something that works is acceptable, even if it does not work 100% perfectly under harsh conditions. Something that just crashes or throws an exception when some assumption about system resources was wrong is not acceptable.

Edited by samoth, 21 June 2012 - 05:48 AM.


#6 gnomgrol   Members   -  Reputation: 628

Like
0Likes
Like

Posted 21 June 2012 - 06:02 AM

Thanks a lot you guys, id never expected that fast replys! Great Forum!
Back to the problem, I just found two incredible large lists I never dealloced. I added that now, but setting up the chunks how takes much longer then before. Im currently at initialising chunk .. uhm... 105. It seems to go much better now, but it takes ~4 sec to set up one chunk.

Id may tell you a little what Im wanting to do.
I would like to have a very large, fully explorable world. Without loadingscreens when swiching between areas (forest to desert eg), if possible.
How are other (MMO)RPG games doing this? Can someone may tell me a little about it?

Thanks again!

Edited by gnomgrol, 21 June 2012 - 06:09 AM.


#7 smasherprog   Members   -  Reputation: 432

Like
1Likes
Like

Posted 21 June 2012 - 07:42 AM

You can also do away with most of the the position of the grid, as well as the texcoords. You can generate the actual position of the verts in the vertex shader by supplying an offset that you pass in for each chunk drawn. Create a stream of two floats( x and z) and make it go from (top left) 0, 0 to (1, 1) on the bottom right. Then when ypu draw it in the shader, you can compute the texcoords from it and you just scale the grid up its full size with a multiplication, then add the offset to each vertex and you have placed you4r grid correctly. To get the correct hights, you create a seperate highstream for each grid and pass that in for each drawing operating as well.

In the end, you reuse the x,z grid mentioned above for all grid patches, since its always the same. Resuse the same index buffer for all grids since its also the same. The only differences will be your extra data. You will need a highstream for each grid, and your normals + tangets.

This is very breif as I am heading off to work, but hopfully you get the idea on reducing a decent amount of vertex memory.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#8 yckx   Prime Members   -  Reputation: 1291

Like
0Likes
Like

Posted 21 June 2012 - 08:15 AM

The general idea is that you don't keep the entire terrain in memory, but only the chunks within a given range of the player. As the player moves around, chunks that get too far away are unloaded while approaching chunks are loaded into memory. As was mentioned above, it's more practical to simply load new data into existing chunks than it is to continually delete distant chunks and allocate new ones.

You may be able to use camera tricks to your advantage; in Dungeon Siege the camera always angled down so that the horizon and sky were never visible. This allowed them to keep the zone of loaded terrain chunks relatively small.

#9 mhagain   Crossbones+   -  Reputation: 8287

Like
1Likes
Like

Posted 21 June 2012 - 08:41 AM

Assuming you're creating a grid, you don't really need index data and can just use one or more triangle strips. What API are you using for drawing?


Adjust that. A regular grid is actually the best-case scenario for using indexes; here's Tom Forsyth on Strippers (source: http://home.comcast..../blog.wiki.html):

The theoretical limit for a regular triangulated plane with an infinitely large vertex cache is 0.5 verts/tri (think of a regular 2D grid - there's twice as many triangles as vertices)


The OP has already and quite clearly optimized the life out of his vertex usage and is using on average just over one vertex per 2 triangles - 0.503929 verts/tri I make it:

Each chunk has 256*256 vertices each and 255*255*6 indices.


Strips aren't going to save memory here. The theoretical upper limit for strips (with an infinite grid) is 1 vert/tri, but the OP is already getting twice as good as that. Strips will just double the storage requirement and make things worse.

Edited by mhagain, 21 June 2012 - 08:50 AM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#10 gnomgrol   Members   -  Reputation: 628

Like
0Likes
Like

Posted 21 June 2012 - 09:50 AM

Thanks again, you guys are awesome!
I get the idea of it now, but im still pretty unsure how to implement that in my code.
Im gonna write some pseudocode of what is happening im my programm:

[source lang="cpp"]terrain::Init(){ CalculatePositionAndHeightFromMapOfAllVertices(); SmoothHeights(); vertexStruct *ThisChunksPartOfTheWorldgrid; foreach(chunk in chunkList){ ThisChunksPartOfTheWorldgrid = findThisChunksPartOfTheWorldgrid(); chunkList.push_back(new chunk()); chunkList[index]->Init(ThisChunksPartOfTheWorldgrid); }}chunk::Init([left][background=rgb(250, 251, 252)]ThisChunksPartOfTheWorldgrid[/background][/left]){ vertices = new VertexPosNormalTexColor[chunkVnum]; vertices = ThisCunksPartOfWorldgrid; ComputeIndicesColorAndTexturecoord(); ComputeNormals(); SaveVerticesAndIndicesToTheirBuffers();}terrain::Draw(){ foreach(chunk in chunkList){ if(visibleInFC){ chunkList[index]->Draw(); }}chunk::Draw(){ d3d11DevCon->IASetIndexBuffer( indexBuffer, DXGI_FORMAT_R32_UINT, 0); d3d11DevCon->IASetVertexBuffers( 0, 1, &vertBuffer, &stride, &offset ); d3d11DevCon->DrawIndexed(chunkInum, 0, 0 );}[/source]

Im currently working on the stuff that smashergrog suggested, Ill post again if I manage to do it this way! Thank you!

Edited by gnomgrol, 21 June 2012 - 11:10 AM.





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