• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
simpler

Help with improving drawing in DX9

6 posts in this topic

We're working on a 2D game and we use DirectX 9.0c for graphics. We've discovered that our drawing functions takes way to much resources and they are slowing the game down, a lot. If there's 15 tiles on the map the FPS drops to around 30. We aren't sure how we are suppose to do the drawing process in a good way. We got a solution that works but it feels like it's not the ideal one.

We got a global graphics class that each object can reach and use in their draw functions. It looks like this:

[code]
class Graphics
{
public:
// Draws a texture
void drawTexture(IDirect3DTexture9 *texture, float x, float y, int width, int height, float rotation = 0);
private:

// Buffer for vertices of the type TextureVertex
IDirect3DVertexBuffer9* mVB_texture;
}
[/code]


Graphics::drawTexture(...) looks like this:

[code]void Graphics::drawTexture(IDirect3DTexture9 *texture, float x, float y, int width, int height, float rotation)
{
// Texture coordinates range from 0-1, 0 is the most left and 1 the most right in the X direction for example
Rect drawRect;
drawRect.left = x-(width/2);
drawRect.right = x+(width/2);
drawRect.top = y-(height/2);
drawRect.bottom = y+(height/2);

gd3dDevice->SetVertexDeclaration(TextureVertex::Decl);
gd3dDevice->SetStreamSource(0, mVB_texture, 0, sizeof(TextureVertex));

TextureVertex *vertices = 0;
mVB_texture->Lock(0, 0, (void**)&vertices, 0);

// Setup vertices
vertices[0].pos.x = (float) drawRect.left;
vertices[0].pos.y = (float) drawRect.top;
vertices[0].pos.z = 0;
vertices[0].tex0.x = 0.0f;
vertices[0].tex0.y = 0.0f;

vertices[1].pos.x = (float) drawRect.right;
vertices[1].pos.y = (float) drawRect.top;
vertices[1].pos.z = 0;
vertices[1].tex0.x = 1.0f;
vertices[1].tex0.y = 0.0f;

vertices[2].pos.x = (float) drawRect.right;
vertices[2].pos.y = (float) drawRect.bottom;
vertices[2].pos.z = 0;
vertices[2].tex0.x = 1.0f;
vertices[2].tex0.y = 1.0f;

vertices[3].pos.x = (float) drawRect.left;
vertices[3].pos.y = (float) drawRect.bottom;
vertices[3].pos.z = 0;
vertices[3].tex0.x = 0.0f;
vertices[3].tex0.y = 1.0f;

// Unlock the vertex buffer
mVB_texture->Unlock();

// Set texture
gd3dDevice->SetTexture (0, texture);

// Draw content in buffer
gd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
gd3dDevice->SetTexture (0, NULL);
}[/code]


An example when using drawTexture(...) :

[code]
Player::draw()
{
gGraphics->drawTexture(getTexture(), getX(), getY(), getWidth(), getHeight());
}
[/code]


So we basicly uses the same vertex buffer for every object in the game and just change it's vertex attributes. It seems like all the calls to the Lock(...) and Unlock() functions is one of the reasons to the slow down.

We've thought about each object having their own vertex buffer and only change it when the object moves, scales or rotates. Is that a viable way to do it?

What's your opinion about our solution? What can we change and how would you write an efficient drawing algorithm?

Cheers :)
0

Share this post


Link to post
Share on other sites
You don't need to change the vertices when moving, rotating or scaling the sprite, just alter the sprite's world matrix for that!

Hope this helps, ask if something remains unclear!

EDIT: Oh and are you creating the vertex buffer with D3DUSAGE_DYNAMIC? You should if you end up accessing the vertex buffer a lot during runtime!
1

Share this post


Link to post
Share on other sites
calling
gd3dDevice->SetTexture (0, texture);

Every time you draw something is a huge bottle neck.
If you can draw your scene by only binding your texture only a few times you will get a huge FPS increase.

1

Share this post


Link to post
Share on other sites
[quote name='d k h' timestamp='1323697440' post='4893105']
You don't need to change the vertices when moving, rotating or scaling the sprite, just alter the sprite's world matrix for that!
[/quote]

Oh right. But will that work when the drawing calls are between a [color=#000066][font=Fixedsys, monospace][size=2]LPD3DXSPRITE::Begin() and End()?[/size][/font][/color]
[font="Arial"][size="2"]The reason for all the draw calls being within Begin() and End() is to get 2D screen coordintes, without mixing with the projection matrix.[/size][/font]
[color=#000066][font="Arial"][size="2"]
[/size][/font][/color]
[font="Arial"][size="2"]Should each object have their own vertex buffer?[/size][/font]
[color="#000066"][font="Arial"][size="2"]
[/size][/font][/color]

1

Share this post


Link to post
Share on other sites
I have almost no experience with the D3DXSPRITE interface unfortunately but I'm pretty sure you can't use your 'average' world matrix transformations with it.

I personally prefer to work in 3D using an orthogonal projection. You can use the z to control the order in which elements are drawn. Your screen space will of course be from -1/-1 to 1/1 so if you want to continue using your 2D coordinate system, so to speak, you need to convert coordinates before drawing:

[code]
// convert coordinates from range [0,screen_size] to [-1,1]
pixel.x = ( pixel.x / screen_width ) * 2.0f - 1.0f;
pixel.y = ( pixel.y / screen_height ) * 2.0f - 1.0f;
[/code]

With that method, of course, you can transform your sprites as you would in a standard 3D game using the world * view * projection matrix pipeline.

I am sure, however, that you should not actually suffer performance problems from rebuilding a couple dynamic (!) vertex buffers every frame like you describe you are, so I'm not sure what to suggest assuming you want to stay within the D3DXSPRITE interface.
1

Share this post


Link to post
Share on other sites
[quote name='simpler' timestamp='1323725095' post='4893253']
[quote name='d k h' timestamp='1323697440' post='4893105']
You don't need to change the vertices when moving, rotating or scaling the sprite, just alter the sprite's world matrix for that!
[/quote]

Oh right. But will that work when the drawing calls are between a [color="#000066"][font="Fixedsys, monospace"][size="2"]LPD3DXSPRITE::Begin() and End()?[/size][/font][/color]
[font="Arial"][size="2"]The reason for all the draw calls being within Begin() and End() is to get 2D screen coordintes, without mixing with the projection matrix.[/size][/font]
[color="#000066"][font="Arial"] [/font][/color]
[font="Arial"][size="2"]Should each object have their own vertex buffer?[/size][/font]
[color="#000066"][font="Arial"] [/font][/color]


[/quote]
The other problem is the lock on the VB, ideally you would like to construct your VB's at the end of your loading process and never ever touch them again. Sending over data from Main memory/CPU to GPU is a slow process and a major bottleneck.

Next to that you might want to set the VB to created with Dynamic as well as that tells the DX runtime at least that your going to change it often.

The oher thing is that GPU's like big vertex buffers to draw alot in one go, on a home project I had a HD4850 fall over when I was drawing 500-1000 (about 0.5 Million polys) planes each in their own VB. When I switched to a batched version all my performance issues went away. You can bunch up all your tile data for one texture in one big vertex buffer and draw all of that in one go, this will be your biggest performance gain.
1

Share this post


Link to post
Share on other sites
[quote name='NightCreature83' timestamp='1323854498' post='4893782']
The other problem is the lock on the VB, ideally you would like to construct your VB's at the end of your loading process and never ever touch them again. Sending over data from Main memory/CPU to GPU is a slow process and a major bottleneck.
[/quote]

Slow process here is rather relative concept. I have been able to push several megabytes of data per frame (under D3D9) while maintaining interactive >60fps speed and that was few years ago. The data transfer rate per second was easily 100 megabytes and 250 megabytes at top. That was few years ago.
Of course, it is always better to have static buffers.

When drawing few hundres 2D tiles, there is no way that you are able to saturate your CPU to GPU transfer, unless you are doing something really wrong.

Consider following:

- collect all tiles / objects using same texture(s)
- set vertex buffer once

For each different tile/object group:
- set texture once
- set shader once
- draw the tiles (which use the same texture) with one draw command. Either construct the vertex buffer every draw call or use instancing

Important part here is to reduce D3D api calls and improve batching. Even simplest form of drawing many object at once should bring you a lot of performance.

Cheers!
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0