Help!How to draw all the objects in only one DrawPrimitive() call?

Started by
19 comments, last by AlanWu 11 years, 4 months ago
Hello,everybody.I think i am new,i am alan,i am from China.I moved to NewZealand one year ago.
But my English still not good.Please don't mind.
I got some questions when i was programming and i don't know how to do,so i just ask some questions.
Some people said if you call DrawPrimitive for every objects it will be very slow.I understood it.
And they said we can draw all the objects in only one DrawPrimitive call.They said put all the object's vertex in a vertex buffer then call SetStreamSource and call DrawPrimitive for only once time.
First,i don't understand how to put all the objects in only one vertex buffer.
Second,i want to draw the texture in the objects,and they have the different texture,if it is only once DrawPrimitive call,how could i set the different textures for every objects?
I don't understand it at all.Could everybody help me please?
Oh,and i am making a 2D render,it doesn't need to be 3D but i hope it can be fast.
Advertisement
Hello Alan, welcome to Gamedev.
Answers to your questions:
1. DrawPrimitive() – it takes a buffer of vertices and draws the specified primitive (the first parameter). If you are rendering, for example, three triangles, DrawPrimitive() draws one triangle defined by points (counting from zero) 0, 1, 2, second triangle defined by points 3, 4, 5 and third triangle defined by 6, 7, 8. What the people tried to tell you is that you should put those points into one continuous (without gaps) buffer and call DrawPrimitive() once instead of calling it for every triangle.
2. Two different objects (like human or ball) should be drawn with one DrawPrimitive() each (there is a technique called instancing to go around that, but that is a very advanced topic). An important thing is that every object should (ideally) have only one texture, so you can draw it in one function call. It becomes problematic with more textures.


P.S.: Your written English is actually quite good (you get the point across and that is what counts smile.png ).
0,1,2 3,4,5 6,7,8
i think 2 and 3,5 and 6 have to be connect.If it is continue,it will be connect.But i want to draw 3 triangle in a different position,how do i do?
I had a look about the instance technique,but i don't exactly understand it,and do all the video-card can use this technique?
thank you!

0,1,2 3,4,5 6,7,8
i think 2 and 3,5 and 6 have to be connect.If it is continue,it will be connect.But i want to draw 3 triangle in a different position,how do i do?


They do not have to be connected. What you're referring to is re-use of identical vertices through indexing. Strictly drawing with drawPrimitive (drawing a triangle list primitive) will consume three vertices at a time, drawing a triangle comprised of those three vertices. With indexing, the draw call would read through an index list instead, using the vertices that correspond to the locations in the vertex buffer.

An example:
(Vertex Buffer: 0,1,2,3,4,5,6,7,8) (vertex count = 9)
(Index Buffer: 0,1,2,2,3,4,4,5,6,6,7,8) (index count = 12)

DrawPrimitive (without indexing) : draws triangles with 0,1,2; 3,4,5; 6,7,8

DrawPrimitive (with index buffer) : reads three indices at a time and uses the vertices at those locations
draws triangles with 0,1,2; 2,3,4; 4,5,6; 6,7,8
(I believe it is called DrawIndexedPrimitive depending on the API you're using)

Not all hardware supports (hardware-based) instancing, but that's not something you need to worry about right now anyway. What you can do is batch together all like-textured objects and combine their vertex information into one buffer, that will reduce your number of draw calls at least (rather than one per object). There's quite a number of optimizations you can do to organize and draw your geometry more effectively, but don't let pre-optimization slow you down too much if you're not even sure you have a problem that needs optimizing.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)


Hello Alan, welcome to Gamedev.
Answers to your questions:
1. DrawPrimitive() – it takes a buffer of vertices and draws the specified primitive (the first parameter). If you are rendering, for example, three triangles, DrawPrimitive() draws one triangle defined by points (counting from zero) 0, 1, 2, second triangle defined by points 3, 4, 5 and third triangle defined by 6, 7, 8. What the people tried to tell you is that you should put those points into one continuous (without gaps) buffer and call DrawPrimitive() once instead of calling it for every triangle.
2. Two different objects (like human or ball) should be drawn with one DrawPrimitive() each (there is a technique called instancing to go around that, but that is a very advanced topic). An important thing is that every object should (ideally) have only one texture, so you can draw it in one function call. It becomes problematic with more textures.


I would disagree with your statement that 2 different objects should be drawn with one Draw call each, especially if Alan is using 2-d rendering, in which case he might easily be able to create a Texture Atlas with all his textures on it and indeed organize his 2-d scene into 1 draw call instead of one for each tile.
Well,i found my problem,i use D3DPT_TRIANGLESTRIP to draw because some articles said D3DPT_TRIANGLESTRIP is much faster than D3DPT_TRIANGLELIST,i change to D3DPT_TRIANGLESTRIP just now,it is ok.
Thank you!
i make a experiment just now ,i think we can ignore the speed between D3DPT_TRIANGLELIST and D3DPT_TRIANGLESTRIP,it is very small.

[quote name='ifthen' timestamp='1354539767' post='5006583']
Hello Alan, welcome to Gamedev.
Answers to your questions:
1. DrawPrimitive() – it takes a buffer of vertices and draws the specified primitive (the first parameter). If you are rendering, for example, three triangles, DrawPrimitive() draws one triangle defined by points (counting from zero) 0, 1, 2, second triangle defined by points 3, 4, 5 and third triangle defined by 6, 7, 8. What the people tried to tell you is that you should put those points into one continuous (without gaps) buffer and call DrawPrimitive() once instead of calling it for every triangle.
2. Two different objects (like human or ball) should be drawn with one DrawPrimitive() each (there is a technique called instancing to go around that, but that is a very advanced topic). An important thing is that every object should (ideally) have only one texture, so you can draw it in one function call. It becomes problematic with more textures.


I would disagree with your statement that 2 different objects should be drawn with one Draw call each, especially if Alan is using 2-d rendering, in which case he might easily be able to create a Texture Atlas with all his textures on it and indeed organize his 2-d scene into 1 draw call instead of one for each tile.
[/quote]

how to create a Texture Atlas?
Does directx 9 has Texture Atlas?
If it has,could you give me a example or some articles?
Thank you!

Hello Alan, welcome to Gamedev.
Answers to your questions:
1. DrawPrimitive() – it takes a buffer of vertices and draws the specified primitive (the first parameter). If you are rendering, for example, three triangles, DrawPrimitive() draws one triangle defined by points (counting from zero) 0, 1, 2, second triangle defined by points 3, 4, 5 and third triangle defined by 6, 7, 8. What the people tried to tell you is that you should put those points into one continuous (without gaps) buffer and call DrawPrimitive() once instead of calling it for every triangle.
2. Two different objects (like human or ball) should be drawn with one DrawPrimitive() each (there is a technique called instancing to go around that, but that is a very advanced topic). An important thing is that every object should (ideally) have only one texture, so you can draw it in one function call. It becomes problematic with more textures.


P.S.: Your written English is actually quite good (you get the point across and that is what counts smile.png ).


thank you for your praise!
And thank you for your helping!

[quote name='AlanWu' timestamp='1354593427' post='5006959']
0,1,2 3,4,5 6,7,8
i think 2 and 3,5 and 6 have to be connect.If it is continue,it will be connect.But i want to draw 3 triangle in a different position,how do i do?


They do not have to be connected. What you're referring to is re-use of identical vertices through indexing. Strictly drawing with drawPrimitive (drawing a triangle list primitive) will consume three vertices at a time, drawing a triangle comprised of those three vertices. With indexing, the draw call would read through an index list instead, using the vertices that correspond to the locations in the vertex buffer.

An example:
(Vertex Buffer: 0,1,2,3,4,5,6,7,8) (vertex count = 9)
(Index Buffer: 0,1,2,2,3,4,4,5,6,6,7,8) (index count = 12)

DrawPrimitive (without indexing) : draws triangles with 0,1,2; 3,4,5; 6,7,8

DrawPrimitive (with index buffer) : reads three indices at a time and uses the vertices at those locations
draws triangles with 0,1,2; 2,3,4; 4,5,6; 6,7,8
(I believe it is called DrawIndexedPrimitive depending on the API you're using)

Not all hardware supports (hardware-based) instancing, but that's not something you need to worry about right now anyway. What you can do is batch together all like-textured objects and combine their vertex information into one buffer, that will reduce your number of draw calls at least (rather than one per object). There's quite a number of optimizations you can do to organize and draw your geometry more effectively, but don't let pre-optimization slow you down too much if you're not even sure you have a problem that needs optimizing.
[/quote]
Thank you very much!
But i still have some questions,could you help me?
Now i know how to put all the vertices together,but how to put the texture into one buffer?How to make the textures connect to the vertices?
And what kind of optimizations you think i still can do?

This topic is closed to new replies.

Advertisement