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

Vertex Array speed

Started by Schinizer Oct 4, 2010 at 10:38 PM 6 replies 1.8k views
Original Post
Schinizer
Schinizer
Hi all,

Just to ask, is there performance difference for these 2 code?

Say that I am drawing 1000 quads. vert is the pointer to the vertices and texCoord is pointer to the texture coordinates. count is the number of vertices.

Method 1:
glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_TEXTURE_COORD_ARRAY);glVertexPointer(2, GL_FLOAT, 0, vert);glTexCoordPointer(2, GL_FLOAT, 0, texCoord);glDrawArrays(GL_QUADS, 0, count);glDisableClientState(GL_VERTEX_ARRAY);glDisableClientState(GL_TEXTURE_COORD_ARRAY);


Method 2:
glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_TEXTURE_COORD_ARRAY);glVertexPointer(2, GL_FLOAT, 0, vert);glTexCoordPointer(2, GL_FLOAT, 0, texCoord);for(int a=0; a<count; a+=4)     glDrawArray(GL_QUADS, a, a+3);glDisableClientState(GL_VERTEX_ARRAY);glDisableClientState(GL_TEXTURE_COORD_ARRAY);
karwosts
karwosts
You should definitely prefer method 1. #2 is exactly the same thing, but just creates a bunch more commands for the driver and gpu to process, there's no benefit and a definite potential loss of performance. That's one of the big reason to use these commands is so you don't have to spam commands for each primitive.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Hodgman
Hodgman
You should prefer #1, but you should also be using VBOs instead of VAs ;)
Schinizer
Schinizer
Ok so how do you handle texture bindings? Say there is 5 different textures among the 1000 quads and you have to do alpha sorting. Any references will be appreciated :D
Rasmadrak
Rasmadrak
Usually you sort stuff after materials (textures + shaders) and render all type A first, then type B, type C ... etc.
"Game Maker For Life, probably never professional thou." =)
21st Century Moose
21st Century Moose
Use something like this:
int firstvertex = 0;int numvertexes = 0;int lasttexture = -1;qsort (obj, count, sizeof (*obj), UserProvidedObjectSortFunction);for (int i = 0; i < count; i++, obj++){	if (obj->texture != lasttexture)	{	    glBindTexture (GL_TEXTURE_2D, obj->texture);            if (numvertexes)            {	        glDrawArrays (GL_QUADS, firstvertex, numvertexes);	        numvertexex = 0;	    }	    lasttexture = obj->texture;	    firstvertex += numvertexes;	}	numvertexes += 4;}if (numvertexes)    glDrawArrays (GL_QUADS, firstvertex, numvertexes);

I've just typed this in off the top of my head, by the way, so don't copy and paste it directly; it's intention is to illustrate the principle rather than to be strictly correct.
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 
onfu
onfu
When you need to render multiple textures, the way you organize your objects prior to OpenGl instructions is even more important. It's up to you to manage the way you supply the data, but always (as said above) try to lump polygons that sample from the same texture together.

If you take your 2nd example in the original post, the performance would be insanely bad if you were not only firing one draw cal per quad, but also binding backwards and forwards through your textures arbitrarily at the same time as well.

rule of thumb:
* minimize the number of instructions
* minimize state changes

Topic Locked

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

Sign in to reply to this topic.