• 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
Anand Baumunk

Allocating memory problem (terrain)

8 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
[s]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?[/s]
Edit: See [url="http://www.gamedev.net/topic/626725-allocating-memory-problem-terrain/page__view__findpost__p__4951356"]Reply #9[/url]. Thanks mhagain, very useful. Edited by Amr0
-2

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1340277968' post='4951297'][...] 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[/quote]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 [i]work[/i].

Something that works is [i]acceptable[/i], 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
2

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='Amr0' timestamp='1340279056' post='4951304']
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?
[/quote]

Adjust that. A regular grid is actually the best-case scenario for using indexes; here's Tom Forsyth on Strippers (source: [url="http://home.comcast.net/~tom_forsyth/blog.wiki.html"]http://home.comcast..../blog.wiki.html[/url]):
[quote]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)[/quote]

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:[quote]Each chunk has 256*256 vertices each and 255*255*6 indices.[/quote]

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
1

Share this post


Link to post
Share on other sites
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
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