I'm currently creating a system that essentially creates a world at run-time based on a seed I choose. In this system, I made it so that there are certain "chunks" that divide up different sections of the world, and in these sections, I have different meshes that are completely unique to that chunk. Each chunk is also divided into 4 subchunks, which are exactly half the width and height of it's chunk.
The purpose of the chunks and subchunks is so as the player, you could walk around the level, with the current chunk, as well as the adjacent chunks stored into memory with their meshes, and those adjacent chunks, which cannot be seem from where the player is, are loaded, however their meshes are not currently loaded, and as the player moves, the system will load and unload these chunks meshes into memory, that way only the meshes needed to render are loaded in at this time.
The problem that I'm having is when I move from one chunk to another, and load the new chunks meshes, and unload the unnecessary chunks meshes, there's a lag spike that only lasts for about half a second. (But it quite noticable) The spike IS NOT coming from the loading and unloading of the mesh, (my memory manager is fast with allocation and deallocation and the loading and unloading are handled on a separate thread) the problem is when I go to change the memory inside the vertex and index buffers. (Which whenever there is a swap, adds AND removes 4 times each during one game loop.
Does anyone have any idea on how I should handle my open world objects? A good reference would be something along the lines of Skyrim or World of Warcraft; They have the large terrain in these worlds and they are most definitely not 100% loaded in the game at once, so how would I go about making it that so as you progress through the world, objects are handled with little to no skip?
Thanks a lot for the help in advance, and if there is clarification needed, simply ask.
Edit: Added code showing my AddVerts function, which is used to pass vertex data to the GPU.
// unsigned int* AddVerts(const VertexFormat *pVerts, const unsigned int nNumVerts) // returns : unsigned int* offset in buffer to where the mesh is located // ins : const VertexFormat* vertices to pass on to the GPU // const unsigned int number of vertices allocated for the const VertexFormat* void *buffer = nullptr; unsigned int nSize = nNumVerts*sizeof(VertexFormat); if( ( m_unBufferFlags & D3DUSAGE_DYNAMIC ) != 0 ) m_pVertexBuffer->Lock(nVertLocation*sizeof(VertexFormat), nSize, &buffer, D3DLOCK_NOOVERWRITE); else m_pVertexBuffer->Lock(nVertLocation*sizeof(VertexFormat), nSize, &buffer, 0); // copy data to buffer memcpy(buffer, pVerts, nNumVerts*sizeof(VertexFormat)); m_pVertexBuffer->Unlock(); m_dllOffsets.addTail(nVertLocation); return &(m_dllOffsets.GetTail()->element);