2d drawing pipeline, how to get good performance, i just figured out i got no clue what i am doing.

Started by
2 comments, last by BaneTrapper 9 years, 8 months ago

Hello, after i written 500+ characters long boring story, i decided to go with a simpler and easier approach.

I require a tutorial / (example made) to 2d graphics pipeline tutorial, i want to learn how to draw stuff, how textures should be applied/used/managed, and what to do in what order to achieve fastest drawing.
Essentially i am unsure how to manage my objects, i don't know what sizes of my textures should be, should i when drawing, force to draw stuff that requires current texture, or can i freely switch back and forth textures and not worry about it, and much more.

What i just want to know is how to manage my "Entities" and how should i handle their drawing.

If you could suggest a tutorial that explains stuff noted above, i would be gratefull!

What i currently will face is:


//Draw 8 vertices with texture1
//Draw 24 vertices with texture2
//Draw 8 vertices with texture1
//draw 4 vertices with texture3
//Draw 8 vertices with texture2
//Draw 16 vertices with texture2
//Draw 24 vertices with texture1
//draw 8 vertices with texture3

I got few ideas for optimization, but as i stated in the topic name "i just figured out i got no clue what i am doing"

Advertisement

It sometimes can't be helped, but in most cases you can have many objects in the same texture (within reason).

So, while I've made many renderers in my time, right now I'm making a 2D game.

And for each layer in my game, I have to sort by each objects sorting mechanism, eg. by the bottom end of the sprite as its drawn.

For that to happen, I have to sort by layer then by texture and then by its sorting mechanism.

It ended up being pretty fast, much faster than I thought it would be

I have a class that contains all renderable/visible objects in a specific layer, eg. a wrapper for a vector, then under that I have a class that represents sorted sprites (another wrapper for a vector).

Lots of dynamic allocation, except it only happens until the vectors reach a certain growth level, then they stop growing. The allocations stop, and even after new frame begins nothing is going to be allocated again unless I spam even more objects.

So, vector of layers which contain vector of sprites. Each sprite know their texture, but they also have a drawing order. I sort by that drawing order, then by texture. It works out OK in the end.

After all that is done, I generate vertices for everything and set markers for when I need to change texture.

Then I upload ALL the vertices once (DYNAMIC_DRAW), and render in the given order by changing texture as needed.

I basically stream data from CPU to GPU, and it's working pretty well for me.

In my case the objects are pretty static (they are sprites,) but even so, I gain alot by simply generating them on the fly, and rendering as much as possible in one go.

My reasoning for different textures is simply for convinience. The textures represents sprites that differ greatly in functionality in my game.

Maybe for you the textures are unique per object, something that you should avoid if you can.

In my case, I could probably live with a single texture, but it's also a matter of making life easier.

Btw. Are you sure you have a performance issue? And if so, are you sure it's where you think it is? Any data?

1. Don't worry about switching textures, as long as you are not overdoing it. Do some profiling to make sure you need to do it, if you think you do. tongue.png

2. In a 2D game you can probably use a megashader to avoid ever changing shader. That means you can more easily sort by texture where needed.

3. You need to know the sizes of your textures to generate proper texcoords. It's not always possible to use Texture Arrays, so sometimes you just have to do it the old-fashioned way where you subtract 1/2 to 1/64th of a pixel to avoid pixel bleeding.

I was working on an iOS game and found that sprite batching made a huge difference in performance. Keep in mind I didn't try sprite batching until I found it to be a bottleneck.

For sprite batching you pack separate images into a single image then define texture coordinates that 'cut' out the image you want from the packed image. When drawing your scene, you create a single vertex buffer that contains all of the images you want to draw. You need to move and rotate the points of the quad you are drawing on the CPU before putting them into the vertex buffer.

You wont want to batch all draw calls, just the ones that make up the largest portion of your sprites. The most obvious one is a tilemap. If your scene contains hundreds of tiles, you don't want each tile to consume a draw call. Instead, you put all of the tiles into the same image then create a vertex buffer that contains all of the tiles. This way you can draw the whole map in a single draw call.

On mobile platforms, it is even faster to repopulate a vertex buffer every frame from pre-transformed data generated on the CPU than it is to have hundreds of separate draw calls.

The disadvantage to this approach is it can complicate your drawing code and you have less control over draw order. If you still need to enforce a specific draw order you can simply use a depth buffer and have the z component of each vertex determine the draw order.

My current game project Platform RPG

Thank you on suggestions.

This topic is closed to new replies.

Advertisement