# OpenGL Only 5.000 Sprites

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

## Recommended Posts

Topic says it.
I tried to get the maximum out of my Library and just created insane amounts of sprites wandering over the screen.
With 5000 Sprites I got down to 15-20fps which is the bottom line of a playable Game.
So, I need some performance Tipps.

Here is the drawing Code:

 void NLSpriteList::renderObject() { m_texture->attach(); m_shader->attach(); glBindVertexArray(m_vao); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); // Enable Attribute Sets glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); // Draw glDrawArrays(GL_TRIANGLES, 0, m_list.size()*6); // Disable Attribute Sets glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glDisableVertexAttribArray(2); // Unbind glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); m_shader->detach(); m_texture->detach(); } void NLSpriteList::update(u32 delta, u32 time) { if ( m_vao == 0 || m_vbo == 0 ) this->createBatch(); //m_list.sort(NLBatchedSprite::OrderByZ()); // Update data NLVertexData* data = createData(delta, time); // Bind glBindVertexArray(m_vao); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); // Enable Attribute Sets glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); // Send Vertex data glBufferData(GL_ARRAY_BUFFER, 6*m_list.size()*sizeof(NLVertexData), data, GL_STREAM_DRAW); // Disable Attribute Sets glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glDisableVertexAttribArray(2); // Unbind glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); // Delete buffer delete [] data; } NLVertexData* NLSpriteList::createData(u32 delta, u32 time) { NLVertexData* buffer = new NLVertexData[m_list.size()*6]; memset(buffer, 0, sizeof(buffer)); TSpriteList::iterator it = m_list.begin(); int i = 0; for ( it; it != m_list.end(); it++ ) { NLBatchedSprite& s = *it; s.update(delta, time); memcpy(&buffer, &it->m_vertices, sizeof(NLVertexData)*6); i += 6; } return buffer; } 
As you can see, I send the vertices each frame. Okay, in this case it is needed since I have transformations every frame and I do transforms on the cpu.
Any hints?
Oh and there are no texture-/shaderchanges during the rendering, OpenGL Calls are constant 31 per Frame and no glGet/Redundant state changes.
Picture was taken on an AMD 2600+ DualCore with ATI HD4890 with 11.4 drivers.

##### Share on other sites
Okay, I was dumb enough to do this with a debug executable.
In Release Mode I can get up to 30.000 Sprites.
But if anyone sees some Room for improvements, I am open to suggestions.

##### Share on other sites
How about an image of your game, so that we may see in what way you are using the sprites?

Not that it will make your game run much faster, but this is pointless, you have no need to modify attribArrays to send bufferdata.

// Enable Attribute Sets
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

// Send Vertex data
glBufferData(GL_ARRAY_BUFFER, 6*m_list.size()*sizeof(NLVertexData), data, GL_STREAM_DRAW);

// Disable Attribute Sets
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);

You probably don't need to bind/unbind VAO either, but I don't work with VAO that much so I can't be sure.

##### Share on other sites
Thanks for the reply. Well, in this case it is only a lot of sprite wandering from left to right and back. Nothing fancy. I just stress test my code.
Removing the Attribute Sets was fine and gave me maybe 1-2 fps.
About the binds: I bind/unbind at the end of every function because I want to leave the OpenGL StateMachine in the state it was before.
Maybe this is a bad strategy?
The class used here is a batcher class which does nothing but batching the sprites you feed to it. One batcher is required per SpriteSheet.
There are also sprites which take a single texture, they cannot be batched. So I am nevery sure if my objects are still bound. A glGet is probably
more expensive than binding it? Or would you buffer the OGL State here?

On the other Hand: Who makes 2D Games with more than 200 Sprites visible at once... and I still have to do Point Sprites for a Leightweight Particle-Engine, so that goes extra.
The current Sprites are 2 Triangles which are making up a quad.
Maybe I get lost in micro-optimisations here?

And here is the requested image:

##### Share on other sites

On the other Hand: Who makes 2D Games with more than 200 Sprites visible at once... and I still have to do Point Sprites for a Leightweight Particle-Engine, so that goes extra.

More to the point, are you making such a game? "You ain't gunna need it" is the rule of thumb to follow here.

##### Share on other sites
First of,
you using VAOs wrong, making them useless in your case.
1. in the render function, get rid of all the client state en/disables and the buffer binds.
You need this only while initialization.
2. in the update function, you don't need to bind the VAO just the buffer.

when a VAO is bound all of these specific OpenGL stats are stored in the VAO instead of the "standard" context(VAO, glBindVertexArray(0)

also, it don't make any sens to "unbind" a shader, its just extra work. In many case also true for the textures.

Finaly there is something what u can optimize, in your update function.
glBufferData(GL_ARRAY_BUFFER, size, null, GL_STREAM_DRAW); glSubBufferData(GL_ARRAY_BUFFER, 0, size, data);

this lets the driver optimize the upload better and can reduce stalls

##### Share on other sites
Thanks for all the replies.

@Ezbez
I am developing a multi purpose Library and just trying to make it as good as I can.
But I feel already pretty comfortable with this drawable amount of sprites.

@Danny02

Could go into detail with the VAO? I have the OpenGL Superbible and some Tutorials on OpenGL3, but Articles about it are really rare and reading the specs is not a good documentation for me. "Too technical" if you can understand what I mean.
If I get rid of all the glEnableVertexAttrib()-binds in the render function, nothing is displayed at all. Thats not the only place I am using a VAO/VBO.
Does not yield any performance gain. I already tell OpenGL that I will throw away the content of the VBO by using GL_STREAM_DRAW. glBufferSubData yields some problems regarding updating the sprites, esp. dynamic Text like FPS Display.

But well, I think 30.000 Sprites at once are pretty good in Release Mode. But that there is such a huge difference in performance of 25.000 Sprites between Debug and Release is astounding.

Here is my updated code so far:

 void NLSpriteList::renderObject() { m_texture->attach(); m_shader->attach(); glBindVertexArray(m_vao); // Enable Attribute Sets glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); // Draw glDrawArrays(GL_TRIANGLES, 0, m_list.size()*6); // Disable Attribute Sets glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glDisableVertexAttribArray(2); // Unbind glBindVertexArray(0); m_shader->detach(); m_texture->detach(); } void NLSpriteList::update(u32 delta, u32 time) { if ( m_vao == 0 || m_vbo == 0 ) this->createBatch(); //m_list.sort(NLBatchedSprite::OrderByZ()); // Update data NLVertexData* data = createData(delta, time); // Bind glBindBuffer(GL_ARRAY_BUFFER, m_vbo); // Send Vertex data glBufferData(GL_ARRAY_BUFFER, 6*m_list.size()*sizeof(NLVertexData), data, GL_STREAM_DRAW); // Unbind glBindBuffer(GL_ARRAY_BUFFER, 0); // Delete buffer delete [] data; } NLVertexData* NLSpriteList::createData(u32 delta, u32 time) { NLVertexData* buffer = new NLVertexData[m_list.size()*6]; memset(buffer, 0, sizeof(buffer)); TSpriteList::iterator it = m_list.begin(); int i = 0; for ( it; it != m_list.end(); it++ ) { (*it).update(delta, time); memcpy(&buffer, &it->m_vertices, sizeof(NLVertexData)*6); i += 6; } return buffer; } 

##### Share on other sites
Ok, VAOs are a special kind of OpenGL object. Instead of saving data they save state.
In order to draw something with BOs u normally have to set up a lot of state, like enabling vertex attributes, setting the attribute settings and specifying an element buffer.

These states are set through the following OpenGL commands:
• glEnableClientState , glDisableClientState
• all the [font=sans-serif][size=2]gl*Pointer functions[/font]
• [font=sans-serif][size=2]binding the [/font][font=sans-serif][size=2]GL_ELEMENT_ARRAY_BUFFER buffer[/font]
[font="sans-serif"]good overview over the data structure http://www.opengl.org/wiki/Vertex_Array_Object[/font]
[font="sans-serif"]
[/font]
[font="sans-serif"][/font][color=#1C2837][size=2][color=#000000]

### [font=sans-serif][size=2][/font]If I get rid of all the glEnableVertexAttrib()-binds in the render function, nothing is displayed at all. Thats not the only place I am using a VAO/VBO. [font=sans-serif][size=2][/quote][/font] [font="sans-serif"]Have u put that code in your VAO initialization? The same spot where u set up all the Attribute info.[/font] [font=sans-serif][size=2] [/font]

##### Share on other sites
Although you're right that 30.000 sprites should be enough, here are some points for discussion:

Does not yield any performance gain. I already tell OpenGL that I will throw away the content of the VBO by using GL_STREAM_DRAW. glBufferSubData yields some problems regarding updating the sprites, esp. dynamic Text like FPS Display.

When OpenGL enqueues commands for later execution, you don't know whether the VBO is already available (i.e. there is no commands pending that requires access to that VBO). You would know if you do synchronization, but that obviously wastes some time. If you "blindly" write to the VBO memory the driver may defer your routine until no pending command requires the VBO anymore. Hence the trick is to do double buffering for vertices, and that is why

 glBufferData(GL_ARRAY_BUFFER, size, null, GL_STREAM_DRAW); glSubBufferData(GL_ARRAY_BUFFER, 0, size, data); 
is used, because that "null" allows the driver to allocate a second memory area, leaving the 1st memory area until no pending commands require it anymore while you are writing to the 2nd one. So using GL_STREAM_DRAW by itself isn't sufficient to gain advantage from double buffering.

There is no guarantee though that the driver behaves this way, but it is the best you can do in situations where you need to be compatible. In OpenGL 3 / the ARB_map_buffer_range extension there is glMapBufferRange which allows an explicit double buffering.

Moreover, how looks your vertex layout? It is probably best if multiples of vertex data boundaries lie on 32 byte boundaries. E.g. a vertex occupying 16 bytes would be fine. My current development uses 3 floats for position and 2 shorts for (u,v) with auto-normalizing the (u,v) pair in the input pipeline. But I have no performance comparisons made yet.

At least, using vertex indices may be of benefit. Assuming you need 16 bytes per vertex and each sprite is mapped on a quad (i.e. 2 triangles), then you need
6 * sizeof(Vertex) = 6 * 16 bytes = 96 bytes
for each sprite. If you use INT indices (at least for the stress test), then you need
6 * sizeof(INT) + 4 * sizeof(Vertex) = 6 * 4 bytes + 4 * 16 bytes = 88 bytes
for each sprite. If you use SHORT indices (obviously allowing for >10.000 sprites), then you need
6 * sizeof(SHORT) + 4 * sizeof(Vertex) = 6 * 4 bytes + 4 * 16 bytes = 76 bytes
for each sprite. It may be that the sprites all together live long enough so that the index buffer need not be rebuild every frame, reducing the transfer amount asymptotically to 64 bytes.

1. Using less bandwidth (okay, the difference between 96 and 88 may be eaten by the additional mapping; you have to try).
2. Allowing the GPU to use the vertex cache for 2 of the 6 vertices of a sprite.

##### Share on other sites
My Vertex Layout is:

 #pragma pack(push, 1) /** * \brief Vertex coords for storage in OpenGL * * * A normal user does not need to care about them. */ struct NLVertexData { /// \brief Constructor NLVertexData() : x(0), y(0), z(0), r(1), g(1), b(1), a(1), s(0), t(0) {} /// \brief X f32 x; /// \brief Y f32 y; /// \brief Z f32 z; /// \brief Red f32 r; /// \brief Green f32 g; /// \brief Blue f32 b; /// \brief Alpha f32 a; /// \brief t f32 s; /// \brief t f32 t; }; #pragma pack(pop) 

And when I use glBufferData(NULL) and then glBufferSubData, I loose around 5 Frames. At least on my ATI.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011514
• ### Who's Online (See full list)

There are no registered users currently online

×