Skip to main content
GameDev.net gamedev.net
🔒 Locked

SwapBuffer trouble

Started by Greg K Aug 12, 2003 at 7:35 AM 24 replies 16.3k views
Original Post
Greg K
Greg K
In my program, swapbuffers() takes 100 milliseconds. I know that openGL queues up commands and does them all in one go but I am only rendering at most 140 quads per frame! And not only that but they are 2D quads! Does anyone have an explanation/tip on how to speed this up? -Greg Reverie Entertainment
ShlomiSteinberg
ShlomiSteinberg
OpenGL doesn''t queue up commands. swapbuffers just waits till all the commands are complete. Post your code.

PS: What do you mean 2D quads?!

"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
Member of the Shove Pouya Off A Cliff club, SPOAC. (If you wish to join, add this line to your signature.)Member of "Un-Ban nes8bit" association, UNA (to join put this in your sig)
exa_einstein
exa_einstein
somebody told me anything about glFlush() or glFinish() and that they should be called before glSwapBuffers().

If it won''t help, try to check whether you really have TWO buffers, not one(can''t swap->copying->slow) or three (need special accelerated functions->copying->slow).
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
Greg K
Greg K

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be

{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor

1, // Version Number

PFD_DRAW_TO_WINDOW | // Format Must Support Window

PFD_SUPPORT_OPENGL | // Format Must Support OpenGL

PFD_DOUBLEBUFFER, // Must Support Double Buffering

PFD_TYPE_RGBA, // Request An RGBA Format

16, // Select Our Color Depth

0, 0, 0, 0, 0, 0, // Color Bits Ignored

0, // No Alpha Buffer

0, // Shift Bit Ignored

0, // No Accumulation Buffer

0, 0, 0, 0, // Accumulation Bits Ignored

16, // 16Bit Z-Buffer (Depth Buffer)

0, // No Stencil Buffer

0, // No Auxiliary Buffer

PFD_MAIN_PLANE, // Main Drawing Layer

0, // Reserved

0, 0, 0 // Layer Masks Ignored

};

ShowWindow(hWnd,SW_SHOW);
SetForegroundWindow(hWnd);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0,-1.0f,1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


glBindTexture(GL_TEXTURE_2D, nTexID);

glBegin(GL_QUADS);
glTexCoord2s( 0, 0 );glVertex2s(nX , nY );
glTexCoord2s( 1, 0 );glVertex2s(nX+nWidth, nY );
glTexCoord2s( 1, 1 );glVertex2s(nX+nWidth, nY-nHeight);
glTexCoord2s( 0, 1 );glVertex2s(nX , nY-nHeight);
glEnd();

SwapBuffers(hDC);




Here are some code tidbits. Hope this helps...

[edited by - Greg K on August 12, 2003 9:25:04 AM]
exa_einstein
exa_einstein
huh no idea...

ma brain almost dead now, but try not to send type _short_ arguments, opengl computes only with floats.


Yes, i understand. this hasn't any chance to survive...



--edit

AND TRY TO RUN WITH MORE COLORS, 16-bit is nearly palletized.
nothing? again?

--edit2

have you sent your pfd to windows? i don't see that

[edited by - exa_einstein on August 12, 2003 9:35:43 AM]
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
Greg K
Greg K
of course I sent it to windows. It wouldn't run otherwise would it? Those are just snippets. I don't see why I shouldn't use shorts, there is an ogl function that accepts them and maybe they can do some compression on them when sending them to vid hardware? Also, how would 32 bits speed anything up? I would think it would slow it down.
-Greg

[edited by - Greg K on August 12, 2003 9:44:22 AM]
exa_einstein
exa_einstein
...it was only idea. You never know what windows want...
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
exa_einstein
exa_einstein
glFlush(); //forces ogl computing
glSwapBuffers(hDC);
//or
glFinish(); //waits for finishing ogl comkputing
glSwapBuffers(hDC);
//or
//...or
//shit...
//yeah:
try to check your videocard (if you are using some tweaker, check it too) and look for some threads/processes/viruses, they can slow it down. and then...RESET WINDO.S


...i hate smiles.
but my favourite smile is...

<:-=| SADDAM
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
Sander
Sander
Check your profiler again (or do it from memory). Exactly WHAT is takin 100 milliseconds? Just the SwapBuffers() function or the entire renderloop?

My best guess anyway is that you are running in software mode. Query for GL_VENDOR. If it sais your driver, then something fishy is going on somewhere. If it says Windows, then you are in software mode and that''s why you are slow.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!
Greg K
Greg K
Just swapbuffers, and it is in hardware mode (NVIDIA Corporation)
-Greg
Greg K
Greg K
I now have it down to around 50 milliseconds. Yes, it is just that one line of code.

DWORD x = timeGetTime();
SwapBuffers(hDC);
x = timeGetTime() - x;

x = around 30-50, why?
ShlomiSteinberg
ShlomiSteinberg
quote:
Original post by exa_einstein
glFinish(); //waits for finishing ogl comkputing
glSwapBuffers(hDC);



WTF NO! Don''t use glFinish with double buffering as it is useless. Use glFlush(). glFinish waits until everything is drawn so no need to swap buffers.
Because gl commands are being proccesed after being called it is good to have the rendering loop that:
firstly does CPU calculations,
then calls swapbuffers(),
then draws everything.
so while everything is being drawn your program can pump messages, do the calculations and then swapbuffers when everything is already drawn. Much more effective.

"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
Member of the Shove Pouya Off A Cliff club, SPOAC. (If you wish to join, add this line to your signature.)Member of "Un-Ban nes8bit" association, UNA (to join put this in your sig)
Greg K
Greg K
I still don''t understand how 100-200 quads could take this long. Depth testing isn''t even on.
-Greg
exa_einstein
exa_einstein
quote:
Original post by Greg K
I now have it down to around 50 milliseconds. Yes, it is just that one line of code.

DWORD x = timeGetTime();
SwapBuffers(hDC);
x = timeGetTime() - x;

x = around 30-50, why?



Do you REALLY measure your time in miliseconds? if you get the time with QueryPerfurmanceTimer(), one cpu cycle (returned value) will be equal to a millisecond only if you have 1kHZ CPU.
-->You must divide that value by QueryPerformanceFrequency()
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
Greg K
Greg K
guh, I posted code! Yes, I really do measure my time in milliseconds just as the code shows! Unless I am mistaken and timeGetTime() actually returns something else...

-Greg
exa_einstein
exa_einstein
ALLAH SAVE ALL SWAPPING BUFFERS...


no idea... don''t you have you videocard onboard or aren''t you using shared ram memory or haven''t you S3Trio3D like me



...::why don''t we have assembler forum?::...
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
Trienco
Trienco
how long does it take when you dont draw anything or disable texturing?
f@dzhttp://festini.device-zero.de
reaptide
reaptide
Try to avoid using glFlush() and glFinish(). It is not necessary to use them with double buffering. They will cause your program to wait until all rendering commands are finished before continuing. SwapBuffers() already does this, so its a bit redundant.

Assuming that you are using a videocard that is rather recent (Any newer than a TNT or some such) use 32-bit floats, not shorts. The vast majority of videocards prefer to get their data in 32-bit chunks. Any 32-bit system is like this really. I suspect you won''t benifit from any sort of compression since your system will just pad the remaining 16-bits with zeros.

I see you got everything running in hardware mode which is good to say the least.

Looking at your code I notice that you are binding a texture every frame. If you are only using one texture than this is not necessary. OpenGL is a state machine and it remembers the last thing you did until you tell it to do something else. So if you are indeed only using one texture, then bind it once in your initialization function (Right under the glEnable(GL_TEXTURE_2D) should do) and leave it at that. The texture bind doesn''t change until you want it to. As well I hope you aren''t loading and building the texture anew each frame. Once you got the texture object created it sticks around until you call glDeleteTextures().

Well that''s about all I have to say. Hope some of that helps.
Greg K
Greg K
Okay, I will switch over to 32 bit floats. Thanks for the advice. Unfortunatly I do need to bind the texture every draw because almost 100% of the time it will be a diff texture. I think I found out why it is going so slow though. I am rendering about 340 triangles per frame but these triangles cover the whole screen twice over (lots of overlapping). There is no way to get around the overlapping but I was thinking maybe I could get ogl to ignore the pixels that overlap? Will that speed it up? For instance will color keying out all the pixels I don''t need to be drawn speed it up?
-Greg

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.