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

2d optimizations

Started by KevArnold Jun 7, 2010 at 6:26 PM 1 replies 550+ views
Original Post
KevArnold
KevArnold
I have been working on a 2d engine/game for a while now and its finally playable and becoming quite enjoyable so now I would like to go back and improve upon it. From reading articles about 2d drawing I think I have some fundamental issues that may be easy to fix but I don't fully understand how to do it and was hoping for some tips.

Most of my render code ends up calling one function,
void Renderer::drawImage(Image* image, Box_BL* dest, float rotate, bool flip){    float X;    float Y;    D3DXMATRIX matTranslation;    D3DXMATRIX matScaling;    D3DXMATRIX matTransform;        d3ddev->SetFVF (D3DFVF_TLVERTEX);    //Get coordinates    X = dest->left() - (float)(this->width) / 2;    Y = -height + ((float)(this->height) / 2) + dest->top();     float dWidth = dest->width();    float dHeight = dest->height();    //Setup translation and scaling matrices    D3DXMatrixScaling (&matScaling, dWidth, dHeight, 1.0f);    D3DXMatrixTranslation (&matTranslation, X, Y, 0.0f);    matTransform = matScaling * matTranslation;    //Check if quad is rotated    if (rotate)    {        D3DXMATRIX matRotate;        //Create rotation matrix about the z-axis        D3DXMatrixRotationZ (&matRotate, rotate);        //Multiply matrices together        matTransform *= matRotate;    }    if (flip)	d3ddev->SetStreamSource(0, reverseVertexBuffer, 0, sizeof(TLVERTEX));    else	d3ddev->SetStreamSource(0, vertexBuffer, 0, sizeof(TLVERTEX));    //Draw the quad    d3ddev->SetTransform (D3DTS_WORLD, &matTransform);    d3ddev->SetTexture (0, image->ppTexture);    d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);}


One of the tips I often read about optimizing 2d drawing is to limit the DrawPrimitive calls. The problem is I dont see how I can do this. In my draw function I create a list of visible actors in order of their z position and then render them. Each actor may use a different texture which I thought made this impossible but maybe I misunderstand how batch drawing works.

I also often see that its better for performance to use 1 big texture than a group of smaller textures but it is not clear to me the intended goal. Is this to limit switching the active texture or just to limit the texture count?

Any hints or tips would be greatly appreciated. Or even a good book recommendation. I have looked for books but most are general DirectX programming which I know would help but I'd love to find something focused on 2d.
freeworld
freeworld
batching is a big help. instead of drawing one quad at a time try grouping them together into one big vertex buffer and draw that all at once.

Grouping sprites together onto one texture lowers the amount of texture switches that need to be made, and I might be wrong but i though texture and shader switches made more of an impact compared to the amount of draw primitive calls you make.

also switching to using index buffers along with batching can help lower the amount of data that has to be passed to the card, but really shouldn't make too much of a difference unless you're draw A LOT of sprites in one call.

Adam_42
Adam_42
As a rough guideline a few hundred draw calls per frame should give you a decent frame rate. A few thousand draw calls (or more) will probably be slow. Exact numbers depend on how fast your PC is.

One thing you should be doing to limit the number of draw calls for sprites, is avoiding drawing off screen ones. If that doesn't get you down to a reasonable number of draw calls then try and batch them up some more.

The standard procedure for sprite batching is to have a fixed size dynamic vertex buffer which you add sprites to. When the texture changes (or the buffer is full) draw the current buffer, and throw away the contents (i.e. lock it with the discard flag).

Once you have that set up using one big texture instead of lots of small ones can improve performance by reducing the number of draw calls, but watch out for quality issues, especially if you scale the sprites.

Topic Locked

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

Sign in to reply to this topic.