Sign in to follow this  
Chris_J_H

Mesh : 0.3sec pause in game on first render

Recommended Posts

Chris_J_H    256
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->GenerateAdjacency(EPSILON,NULL);
m_mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,NULL,NULL,NULL,NULL);

Rendered using an effect (unique to this mesh) created by:
D3DXCreateEffectFromFile(gd3dDevice, file, 0, 0, D3DXSHADER_DEBUG | D3DXSHADER_PREFER_FLOW_CONTROL, pool, &m_fx, &errors);

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 this post


Link to post
Share on other sites
mhagain    13430
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 [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb205884%28v=vs.85%29.aspx"]IDirect3DResource9::Preload[/url] 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):

[code]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 ();
}
}[/code]

(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 this post


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

[b]Edit:[/b] 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 this post


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

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