Buffer Updates - Question

Started by
3 comments, last by carb 21 years ago
Say I were doing a 2D tetris game in DirectX9 ... Every so often, I''d want to change the position of the blocks. Wouldn''t I need to update the vertex buffer with all the new coordinates? I''m confused about this, because several people on the forum have said, quite plainly, "this is bad" ... but if that''s true, what''s another way to go about it? Is there a way to translate just a _set_ of coordinates? - Ben
- Ben
Advertisement
Well tetris just happens to be an odd example, as everything is made up of small textured quads.

You could set the world transform, draw a quad, set the world transform, draw another quad, but this would be really slow.

You could keep all the blocks that have made it to the bottom be part of a static buffer, and update it only when another piece reaches the bottom, adding and/or removing bits as needed.

The best bet for a tetris game though, is to use a dyanamic buffer, and transform the coordinates yourself before putting them into the buffer and leave the world transform at identity.

Since it''s 2D, you probably want to put pre-transformed XYZRHW coordinates into your buffer. It''s easier to deal with for 2D, and will render faster.

Constantly updating a static buffer is bad, and will kill performance. Updating dynamic buffers is fine. That''s what they''re there for... Data that changes all the time.
What is the difference between a static and dynamic buffer?

Basically, I''ve gone through a plethora of tutorials which do little more than show how to draw one shape, and rotate/texture/etc. onto it. I''m trying to figure out how to move from this to many shapes, some moving, some not, etc. So many tutorials give you the _absolute_ basics, and then that''s it - you''re left to fend for yourself from that point on.

Appreciate the reply, btw.
- Ben
- Ben
Read this for info on how to use dynamic buffers correctly.

http://www.gamedev.net/community/forums/topic.asp?topic_id=148414

Data that doesn''t change should go into a static buffer. Character models, terrain, houses, trees, rocks, etc. They can be placed, rotated, and scaled with a world transform, or animated via a shader, but the data in the buffer is never modified.

Data that does change should go into a dynamic buffer.

Static data is stored on the GPU once, and the GPU uses it whenever you ask it to. Dynamic data is transferred to the GPU everytime you draw. Each type of buffer has it''s uses.

Now, a tetris block never changes, it''s just translated and drawn, so you may think using a static buffer is the way to go... except that you want to reduce your draw calls to the bare minimum. Each one has tons of overhead.

How do we reduce the number of draw calls? By drawing more than one block at a time. Since placement of each block will constantly change, and because each block is very few polygons, it''s now more of a situation for a dynamic buffer.

Using a dynamic buffer will be slightly slower than using a static buffer, but this slowdown is more than outweighed by the overhead in drawing each block seperately.

Now you ask "But my blocks are going to have different textures or colors... how do you draw them all together. You can''t switch textures half way though a draw call."

Use one large texture, and use a portion of it for each block. In a 256x256 texture you can have 256 unique looking 16x16 blocks. Or 64 unique 32x32 blocks. Just adjust the UV coordinates to show only the part of the image you want.

If your blocks are just colored not textured, use vertex colors to draw each block in an appropriate color rather than material color.

Use these texture states for plain textures
0, D3DTSS_COLOROP, D3DTOP_SELECTARG1
0, D3DTSS_COLORARG1, D3DTA_TEXTURE
1, D3DTSS_COLOROP, D3DTOP_DISABLE


Or set these render states for colors:
D3DRS_COLORVERTEX, TRUE
D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1
D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1

And these texture states for colors:
0, D3DTSS_COLOROP, D3DTOP_SELECTARG1
0, D3DTSS_COLORARG1, D3DTA_DIFFUSE
1, D3DTSS_COLOROP, D3DTOP_DISABLE

So, basically, you can use as many buffers as you want? Is there some limit or any reason not to?

Can you perform world transformations on an indvidual buffer?
- Ben
- Ben

This topic is closed to new replies.

Advertisement