Jump to content
  • Advertisement
Sign in to follow this  
Endar

repeated calls using immediate mode

This topic is 4439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know that people have said that you shouldn't have a call to glBegin and it's corresponding glEnd inside a loop because it slows things down. Exactly how much?
#define LIMIT 1000000

float temp;
echo::util::CHiResTimer timer;
timer.init();

echo::util::array<float> arr;

for(int j=0; j < 20; ++j){
	driver->beginScene();

	glLoadIdentity();

	// so we get just the time taken to render everything inside the loop
	timer.getElapsedSeconds();

	for(int i=0; i < LIMIT; ++i){
glBegin(GL_LINES);
			// first point
			glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
			glVertex3f(-0.5f, 1.0f, -3.0f);

			// second point
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			glVertex3f(0.5f, 1.0f, -3.0f);
glEnd();
	}


	temp = timer.getElapsedSeconds();

	printf("\n Time taken for %d lines: %f seconds", LIMIT, temp);
	arr.push_back(temp);

	engine->getDebugConsole()->render();

	driver->endScene();
}

float temp2 = 0.0f;
// print out the average time taken
for(int i=0; i < arr.getSize(); ++i)
	temp2 += arr;

printf("\n Average time taken: %f\n", temp2 / (float)arr.getSize() );


I ran this code with the glBegin and glEnd, inside (as they are now) and also outside the loop. The time with them inside was an average of 0.919 seconds, and the time with them outside was an average of 0.911 seconds. Is it because I'm just drawing a simple line? Is that the reason why there's not much difference? If I were drawing a quad, would inside be slower than outside? Or am I wrong, and there is just no difference whatsoever?

Share this post


Link to post
Share on other sites
Advertisement
I think it's very implementation specific. Ie. If you run that program with another ogl implementation, it may be slower.

Share this post


Link to post
Share on other sites
You are correct in thinking that including the Immediate Mode (glBegin and glEnd ) specification commands inside that kind of loop will slow things down. There is quite a bit of overhead involved with setting up immediate mode (not really that much involved in ending it, I think) ...so break down the loop of your logic a little bit more. ...You specify that you are drawing a line, you submit the points, then you tell OpenGL that you are done drawing lines. Then you specify that you are drawing a line, you submit the points, then you tell OpenGL that you are done drawing lines.

Yes, it would run slower if you were specifying different polygons. There is a lot less overhead in setting up 'GL_LINES point submission' than in 'GL_POLYGON point submission'

...That procedure is kind of counterintuitive to the natural operation of OpenGL. The standard algorithm for doing something like that is to format the points for OpenGL by telling it a poly format. You then submit all of your points that specify objects of that format, then glEnd. There is no reason to start|stop start|stop start|stop start|stop. The 'starts' are expensive. ...Imagine if you refreshed your whole scene like that, especially with any other bottlenecks that might come from implementing other features and you can quickly run into a program that will be impossible to manage.

Share this post


Link to post
Share on other sites
Quote:
Original post by Joakim_ar
I think it's very implementation specific. Ie. If you run that program with another ogl implementation, it may be slower.


Just to expand on this a bit. It really depends on when the implementation decides to validate state. OGL doesn't actually require the implementation to do anything with the data you submitted until you call flush. It is free to store up the data in a buffer somewhere. The main advantage here is reducing overhead by collecting may vertices before submitting them. The implementation may wait until a predefined number of primitives are available, or it may wait until there is a state change, or even until it sees a flush command.

There will probably be some overhead from calling glBegin/end more often than you need to. At the very least you are paying for the function call. However, the ammount of overhead will be dependant on your vendor, and could potentially be very small.

A bigger problem is the overhead of using immediate mode at all. If you are trying to improve the performance of vertex submission, take a look at vertex arrays or VBOs.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!