Slow engine. Have 6 fps displaying 300 triangles untextured

Started by
23 comments, last by CBT 18 years, 10 months ago
Aha! No, I use the D3DDEVTYPE_HAL
Advertisement
if CreateDevice() fails, what do you do?
you have written that the GPU is (almost?) Idle. it seems that it is not used at all!
I'm thinking one of two things is wrong.

Option A:
You're using the debug runtime and your code is generating many many warnings through the debug console. This would slow your app significantly.
To check if this is the case, go to the Control Panel, open up DirectX, go to the Direct3D tab and on the top right, select the Retail Version. This would increase your render speed to normal (using the debug runtimes is still recommended, this is just to check what the problem is).

Option B:
You're not calling your render loop enough. This could be many things, really, like a sleep() call somewhere, or maybe something else. If option A doesn't work, try posting your message pumping function and all window related code you think might be related, especially your main While loop (hopefully you have one :)).

Good luck.
Sirob Yes.» - status: Work-O-Rama.
I'm not doing anything :-) Is that bad?
The gpu is idle alwas (says NVIDIA NvPerhud program..)
Maybe you should post some code to see how you've set up D3D. And, how does the loop looks like?
Here is some code:

void drawScene()
{
directx.dxDev->Clear(0, NULL, D3DCLEAR_TARGET| D3DCLEAR_ZBUFFER , 0x00FFFFFF, 1.0f, 0 );
directx.dxDev->BeginScene();

directx.drawPrimitives();
font.drawFPS();

directx.dxDev->EndScene();
directx.dxDev->Present(NULL, NULL, NULL, NULL );
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
window.createWindow(hInstance,CmdShow);
directx.direct3DInit(&window);
directx.createVertexBuffer(300*3); //create vertexbuffer containing 900 vertices
world.drawWorld(&directx);

font.initFont(&directx);


MSG msg;
while(1)
{
if (PeekMessage(&msg,0,0,0,PM_NOREMOVE))
{
int result = GetMessage(&msg,0,0,0);
if(!result) return msg.wParam;

TranslateMessage(&msg);
DispatchMessage(&msg);


if (GetAsyncKeyState(VK_ESCAPE))
{
PostQuitMessage(0);
return(0);
}
}

drawScene();
}

directx.release();
directx.releaseVB();
}

////////////////////////////////////////////////////////////

bool DirectX::direct3DInit(Window *pwindow)
{

dxD3D = Direct3DCreate9( D3D_SDK_VERSION);
D3DDISPLAYMODE displaymode;
dxD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displaymode);
D3DPRESENT_PARAMETERS pp;
ZeroMemory(&pp, sizeof(pp));

pp.BackBufferFormat= displaymode.Format;
pp.Windowed= TRUE;
pp.BackBufferCount= 1;
//pp.MultiSampleType= D3DMULTISAMPLE_NONE;
//pp.MultiSampleQuality= 0;
pp.SwapEffect= D3DSWAPEFFECT_DISCARD;
pp.hDeviceWindow= pwindow-> hWnd;
pp.FullScreen_RefreshRateInHz= D3DPRESENT_RATE_DEFAULT;
pp.PresentationInterval= D3DPRESENT_INTERVAL_IMMEDIATE;
pp.EnableAutoDepthStencil= TRUE;
pp.AutoDepthStencilFormat= D3DFMT_D16;

dxD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, pwindow->hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &dxDev);

dxD3D->CreateDevice(AdapterToUse, DeviceType, pwindow->hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &dxDev);

setupView();
setupRenderState();
}

//////////////////////
some code here setting upp viewmat and stuff
/////////////


void DirectX::createVertexBuffer(int maxNrVertices)
{
dxDev->CreateVertexBuffer(maxNrVertices*sizeof(Vertex), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &dxVB, NULL);
}

void DirectX::fillVertexBuffer(Vertex vertex[], int nrVertices)
{
VOID* pVertices;
dxVB->Lock(0, 0, (void**)&pVertices, 0);
memcpy(pVertices, vertex, sizeof(Vertex)*nrVertices);
dxVB->Unlock();
}

void DirectX::drawPrimitives()
{
dxDev->SetFVF(D3DFVF_CUSTOMVERTEX);
dxDev->SetStreamSource(0, dxVB, 0, sizeof(Vertex));
dxDev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 300);
}
/////////////////////////////////////////////////

void World::drawWorld(DirectX *directx)
{
srand ( time(NULL) );

int r,b,g;

Vertex vertex[1000];
int i=0;
float y=1;
float z = 0;

for(int j=0; j<300;j++)
{
z+=0.01;
y+=0.01;
r = rand()%256;
g = rand()%256;
b = rand()%256;

initVertex(vertex, i++, 0.0f, 0.0f, z, D3DCOLOR_XRGB(r,g,b));
initVertex(vertex, i++, 1.0f, y, z, D3DCOLOR_XRGB(r,g,b));
initVertex(vertex, i++, 1.0f, 0.0f, z, D3DCOLOR_XRGB(r,g,b));
}

directx->fillVertexBuffer(vertex,300*3); // 300*3 = nrVertices
}

void World::initVertex(Vertex vertex[], int index, float x, float y, float z, unsigned long color)
{
vertex[index].color = color;
vertex[index].x = x;
vertex[index].y = y;
vertex[index].z = z;

}
Noticed a little fault in the code I uploaded. I do not call CreateDevice twice! The second CreateDevice is not supposed to be there :-)
I noticed two things, first you draw 300 triangles on top of each other, second i think you draw from back to front. Try setting Z to the max and then -= instead.

Though it doesn't seem to be that big a problem considering how many triangles you are drawing...
hmmmmmmmmmmmmmmm.....
pp.SwapEffect = D3DSWAPEFFECT_DISCARD ;) ?? ??
What should I initialize the SwapEffect to instead?
I don't even know what that thing do...

This topic is closed to new replies.

Advertisement