Jump to content

  • Log In with Google      Sign In   
  • Create Account

Mesh : 0.3sec pause in game on first render


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

#1 Chris_J_H   Members   -  Reputation: 256

Like
0Likes
Like

Posted 11 December 2012 - 06:56 AM

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,

Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 8275

Like
1Likes
Like

Posted 11 December 2012 - 07:35 AM

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, 11 December 2012 - 07:38 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.


#3 Matias Goldberg   Crossbones+   -  Reputation: 3696

Like
1Likes
Like

Posted 11 December 2012 - 12:33 PM

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, 11 December 2012 - 12:35 PM.


#4 Chris_J_H   Members   -  Reputation: 256

Like
0Likes
Like

Posted 12 December 2012 - 07:10 AM

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, 13 December 2012 - 08:32 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