Sign in to follow this  
mytre

Need help on vertex buffers optimisation

Recommended Posts

mytre    112
Can somebody help me whit this (optimisation or idea (DX8.1)) I'am trying to make tiles across the screen and I'am getting only 31 fps on 64 vram vcard, and 34 fps on 128 vram vcard
//this function is called from render:
void setTilleVB (void)
{
for (vbY = 5.0f; vbY > -5.0f; vbY=vbY-2.0f){
	for (vbX = -6.0f; vbX < 6.0f; vbX=vbX+2.0f){
Vertex z_quadVertices3[] =
{
	{ vbX ,      vbY   ,     0.0f,  0.0f,0.0f },
	{ vbX+2,   vbY  ,      0.0f,  1.0f,0.0f },
	{ vbX  ,     vbY - 2,   0.0f,  0.0f,1.0f },
	{ vbX+2,   vbY - 2,   0.0f,  1.0f,1.0f }
};
	g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), D3DUSAGE_WRITEONLY, 
                                      D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, 
                                      &g_pVertexBuffer3 );
    void *pVertices3 = NULL;
    g_pVertexBuffer3->Lock( 0, sizeof(z_quadVertices3), (BYTE**)&pVertices3, 0 );
    memcpy( pVertices3, z_quadVertices3, sizeof(z_quadVertices3) );
    g_pVertexBuffer->Unlock();

	//render tex
    g_pd3dDevice->SetTexture( 0, pMainTexture );
    g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer3, sizeof(Vertex) );
    g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
	    //if( g_pVertexBuffer3 != NULL ) 
       g_pVertexBuffer3->Release();
											};
										}
}
[/source[
//thanks.

[Edited by - Muhammad Haggag on November 30, 2005 4:38:43 AM]

Share this post


Link to post
Share on other sites
mytre    112
I've allready moved
g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), D3DUSAGE_WRITEONLY,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&g_pVertexBuffer3 );
in differen call, but won't render,
ass long as it called from here works.


//this function is called from render:
void setTilleVB (void)
{
for (vbY = 5.0f; vbY > -5.0f; vbY=vbY-2.0f){
for (vbX = -6.0f; vbX < 6.0f; vbX=vbX+2.0f){
Vertex z_quadVertices3[] =
{
{ vbX , vbY , 0.0f, 0.0f,0.0f },
{ vbX+2, vbY , 0.0f, 1.0f,0.0f },
{ vbX , vbY - 2, 0.0f, 0.0f,1.0f },
{ vbX+2, vbY - 2, 0.0f, 1.0f,1.0f }
};
/* called before rendering
g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), D3DUSAGE_WRITEONLY,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&g_pVertexBuffer3 );*/

void *pVertices3 = NULL;
g_pVertexBuffer3->Lock( 0, sizeof(z_quadVertices3), (BYTE**)&pVertices3, 0 );
memcpy( pVertices3, z_quadVertices3, sizeof(z_quadVertices3) );
g_pVertexBuffer->Unlock();

//render tex
g_pd3dDevice->SetTexture( 0, pMainTexture );
g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer3, sizeof(Vertex) );
g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
//if( g_pVertexBuffer3 != NULL )
g_pVertexBuffer3->Release();
};
}
}



//thanks.

[Edited by - Muhammad Haggag on November 30, 2005 4:53:44 AM]

Share this post


Link to post
Share on other sites
Jacob Roman    120
You not only need to have this line in another function before your main loop:

g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVertexBuffer3 );

...but also this:

void *pVertices3 = NULL;
g_pVertexBuffer3->Lock( 0, sizeof(z_quadVertices3), (BYTE**)&pVertices3, 0 );
memcpy( pVertices3, z_quadVertices3, sizeof(z_quadVertices3) );
g_pVertexBuffer->Unlock();

Just be sure that it's not just one Vertex Buffer per tile. Use only one Vertex buffer that has all the tiles' vertices within it. ;)


Share this post


Link to post
Share on other sites
mytre    112
To Roman:
So you mean that I should not split vertices position in "for" loop, but to predefine them and load all in one vertex buffer?

Share this post


Link to post
Share on other sites
Jacob Roman    120
Basically what you've been doing was drawing each tile per loop, which is time consuming. If you predefine the tile vertices before your main loop where your game data is loaded, then all you would need to do is draw the primitive only once per frame. Unless you are needing different textures, then you would need to be drawing all the tiles that have that texture, and do it again for the next texture.

Depending on the game you are making, you can have one large texture (like 1024x1024) with tiled textures within it, then all you would have to do is have texture coordinates predefined for every polygon, which in return will allow you to set the texture and draw the primitive once per frame. But if it's a big game with lots and lots of textures, then that's not the way to go.

Share this post


Link to post
Share on other sites
mytre    112
Yes, you are completlly right.
I just realised two things:
First, I didn't fully explained what was I trying to do. (and I explain later)
Second, I removed some values from code ,which created confusion.

First, I'll use diferent textures, (that's for sure) that is one of the reasons that I'am updating vertices in every frame, But more important is this

in "for" loop there are tvo values changing on user input,
like movX (inceasing, and decreasing) for user input on left-right and same for up-down.
So, code would be something like this:

void setTilleVB (void)
{
for (vbY = 5.0f; vbY > -5.0f; vbY=vbY-2.0f){
for (vbX = -6.0f; vbX < 6.0f; vbX=vbX+2.0f){

Vertex z_quadVertices3[] =
Vertex z_quadVertices3[] =
{
{ vbX - vbMovX, vbY - vbMovY, 0.0f, 0.0f,0.0f },
{ vbX+2- vbMovX, vbY - vbMovY, 0.0f, 1.0f,0.0f },
{ vbX - vbMovX, vbY - 2- vbMovY, 0.0f, 0.0f,1.0f },
{ vbX+2- vbMovX, vbY - 2- vbMovY, 0.0f, 1.0f,1.0f }};

// THAN RESETING vbMovX, vbMovY
//...rest of the code
// code for reading texture is not implemented yet.
//I presume that will consume more fps.




,That is why I must update vertices here, so all textures can move in the same time.
This is something that I've saw for VB, smooth scrolling, and adapted it here.
It should be basicly scroll map.

As for second part of your answer, copying textures to one large texture, I know that
copyrect is slow, that is why I'am avoiding it.

Other method that I can think of is using d3dxsprite, which I rather don't use.
(althought it prooved fine (76 fps on 64 vram)


Thanks, and sorry for waisting your time.

[Edited by - Muhammad Haggag on November 30, 2005 4:33:30 AM]

Share this post


Link to post
Share on other sites
mytre    112
to Muhammad: Sorry,it won't happen again.

Anyway:
I've tried everything that I've read searching the forums.
I've removed createvb from call and called it before render.
Nothing signicficant change. Then I've put all vertices in one buffer,
same again. Anyway that is useles because in this particular case vb must use lock unlock on every frame render. Matrix must stay fixed,otherwise it will complicate things infinitly. I didn't try indexed vb but a doubt that will help much.
All this on 40 fps and I haven't even started calling draweprimitive.

Share this post


Link to post
Share on other sites
mytre    112
No, I haven't, but it is good idea.
Anyway I know where the problem is, I was just looking for ways to optimised it.
Problems are in (Lock / Unlock)* 3 for each frame rendered, and must be done that or similar way, because of translation during rendering.
40 fps , and not even caled draweprimitive.

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