# Mesh : 0.3sec pause in game on first render

This topic is 1869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Help! - I get around a 0.3 second processing pause in my game when a Mesh first becomes visible and is fine thereafter. The problem disappears when I comment out the m_mesh->DrawSubSet(s); line but otherwise leave everything unchanged (including all the mesh creation, pre and post render processing). Nothing is flagged up using Directx9 debug output.

Initiation:
D3DXCreateMesh (m_meshData.m_attributes.size(),num_verts,D3DXMESH_MANAGED | D3DXMESH_WRITEONLY, elems, gd3dDevice, &m_mesh);

Optimization (same problem with or without):
m_mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,NULL,NULL,NULL,NULL);

Rendered using an effect (unique to this mesh) created by:

Could this be something happening on the GPU (eg. buffer locking issue... or somesuch)?- I show the various Directx9 usage flags....

Thanks for any help/ideas,

##### Share on other sites
Using the managed pool, you've got 2 copies of each vertex buffer and index buffer (and a D3DX mesh is really just vertex buffers and index buffers behind the scenes) - one stored in system memory and the other stored in video memory. When you first load a mesh (or any other managed pool resource) the system memory copy is what is loaded, so when you first use one for drawing with, the video memory copy needs to be brought up to date before the draw can occur. Hence the stall.

The solution is to get access to the vertex and index buffers behind the mesh and call IDirect3DResource9::Preload on them, which - per the documentation - "indicates that the application will need this managed resource shortly" - i.e. load it directly into the video RAM copy and avoid the stall on first draw.

Sample code to do this might look something like this (warning: written off the top of my head and untested so please don't just copy/paste it):

void PreloadMesh (ID3DXMesh *mesh) { IDirect3DVertexBuffer9 *meshVB = NULL; IDirect3DIndexBuffer9 *meshIB = NULL; if (SUCCEEDED (mesh->GetVertexBuffer (&meshVB))) { meshVB->PreLoad (); meshVB->Release (); } if (SUCCEEDED (mesh->GetIndexBuffer (&meshIB))) { meshIB->PreLoad (); meshIB->Release (); } }

(Additional note/disclaimer: it's not stated in the documentation but I'm assuming here that GetVertexBuffer and GetIndexBuffer will AddRef the buffers and therefore also require a Release - I don't use the ID3DXMesh interface myself so I don't have confirmation of this). Edited by mhagain

##### Share on other sites
What mhagain said. D3DX needs a copy for itself in case your device is lost.

Also if you happen to own an ATI Radeon HD 4000 to 7000, the drivers apparently do some black magic inside regarding storing the index buffers into system memory (rather than GPU memory) and introduce a small hiccup. You may experience this happens with all games that don't pre-render a few frames before showing actual input to give the drivers time to "warm up" (particularly, it's quite obvious in LA. Noire for these cards) as well in some SDK demos with heavy vertex & index count.

Edit: Also, you do realise that you're passing D3DXSHADER_DEBUG to generate your shaders? (which isn't bad, but may explain the initial slowdown) Edited by Matias Goldberg

##### Share on other sites
Thanks Guys - what you say makes absolute sense to me. However - calling Preload() on the mesh buffers upon mesh creation (and OnRestore() for good measure) seems to make no difference - I understand that under some circumstance the GPU can ignore the Preload() command?... Also, there are no textures with the mesh - just geometry. My next step is to refactor my Mesh Class so that it manages the buffers itself... Will post progress. Also, the idea of doing a dummy initial render of all the objects to ensure they are fully loaded may be the way to go...

Update: It actually seems to be the ID3DXEffect resource causing the stall (the effect is unique to the mesh). If I render a dummy mesh initially using an identical effect (but duplicated), I still get the stall - but if I render the dummy mesh using the same effect resource I do not get the stall when my real mesh in the game first becomes visible. There doesn't seem to be a PreLoad() equivalent for ID3DXEffect... perhaps the only easy answer is to render all initially in some dummy frames (as suggested). Edited by Chris_J_H