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

## Recommended Posts

AlanWu    241
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.

##### Share on other sites
ifthen    820
Hello Alan, welcome to Gamedev.
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] ).

##### Share on other sites
AlanWu    241
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!

##### Share on other sites
BCullis    1955
[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?
[/quote]

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.

##### Share on other sites
Steve_Segreto    2080
[quote name='ifthen' timestamp='1354539767' post='5006583']
Hello Alan, welcome to Gamedev.
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.
[/quote]

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. Edited by Steve_Segreto

##### Share on other sites
AlanWu    241
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!

##### Share on other sites
AlanWu    241
i make a experiment just now ,i think we can ignore the speed between D3DPT_TRIANGLELIST and D3DPT_TRIANGLESTRIP,it is very small.

##### Share on other sites
AlanWu    241
[quote name='Steve_Segreto' timestamp='1354600852' post='5006980']
[quote name='ifthen' timestamp='1354539767' post='5006583']
Hello Alan, welcome to Gamedev.
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.
[/quote]

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!

##### Share on other sites
AlanWu    241
[quote name='ifthen' timestamp='1354539767' post='5006583']
Hello Alan, welcome to Gamedev.
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] ).
[/quote]

And thank you for your helping!

##### Share on other sites
AlanWu    241
[quote name='BCullis' timestamp='1354599138' post='5006976']
[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?
[/quote]

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?

##### Share on other sites
BCullis    1955
[quote name='AlanWu' timestamp='1354606053' post='5006992']
how to put the texture into one buffer?How to make the textures connect to the vertices?
[/quote]
That depends on what API you're using. Is this DirectX11? XNA? Something else?
In most cases, (assuming the vertices also have texture coordinates associated with them) you pass the texture and the texture coordinates to the pixel shader drawing the mesh.

[quote name='AlanWu' timestamp='1354606053' post='5006992']
And what kind of optimizations you think i still can do?
[/quote]

Optimizations could include:
View-frustum culling (don't even bother drawing something the camera can't see)
Storing geometry in a partitioning tree to cull entire branches that aren't visible (makes the culling process more efficient than checking per-mesh)
Instancing (if you have a lot of identical/similar meshes)
Batching by material/effect to reduce changes to the rendering pipeline
...the list goes on. There are a lot of articles and blogs about how different studios grabbed those last extra frames-per-second by some code trickery, but like I said before, pre-optimization is a trap you can fall into, making your task overly-complex without confirming you even NEED to based on your use cases.

##### Share on other sites
AlanWu    241
[quote name='BCullis' timestamp='1354635543' post='5007103']
[quote name='AlanWu' timestamp='1354606053' post='5006992']
how to put the texture into one buffer?How to make the textures connect to the vertices?
[/quote]
That depends on what API you're using. Is this DirectX11? XNA? Something else?
In most cases, (assuming the vertices also have texture coordinates associated with them) you pass the texture and the texture coordinates to the pixel shader drawing the mesh.

[quote name='AlanWu' timestamp='1354606053' post='5006992']
And what kind of optimizations you think i still can do?
[/quote]

Optimizations could include:
View-frustum culling (don't even bother drawing something the camera can't see)
Storing geometry in a partitioning tree to cull entire branches that aren't visible (makes the culling process more efficient than checking per-mesh)
Instancing (if you have a lot of identical/similar meshes)
Batching by material/effect to reduce changes to the rendering pipeline
...the list goes on. There are a lot of articles and blogs about how different studios grabbed those last extra frames-per-second by some code trickery, but like I said before, pre-optimization is a trap you can fall into, making your task overly-complex without confirming you even NEED to based on your use cases.
[/quote]

ok,thank you!
I will try it.

##### Share on other sites
AlanWu    241
[quote name='BCullis' timestamp='1354635543' post='5007103']
[quote name='AlanWu' timestamp='1354606053' post='5006992']
how to put the texture into one buffer?How to make the textures connect to the vertices?
[/quote]
That depends on what API you're using. Is this DirectX11? XNA? Something else?
In most cases, (assuming the vertices also have texture coordinates associated with them) you pass the texture and the texture coordinates to the pixel shader drawing the mesh.

[quote name='AlanWu' timestamp='1354606053' post='5006992']
And what kind of optimizations you think i still can do?
[/quote]

Optimizations could include:
View-frustum culling (don't even bother drawing something the camera can't see)
Storing geometry in a partitioning tree to cull entire branches that aren't visible (makes the culling process more efficient than checking per-mesh)
Instancing (if you have a lot of identical/similar meshes)
Batching by material/effect to reduce changes to the rendering pipeline
...the list goes on. There are a lot of articles and blogs about how different studios grabbed those last extra frames-per-second by some code trickery, but like I said before, pre-optimization is a trap you can fall into, making your task overly-complex without confirming you even NEED to based on your use cases.
[/quote]

well,i don't know how to do it,do you have some tutorial about using pixel shader to draw the mesh?

##### Share on other sites
AlanWu    241
could you give me some tutorial or some example?
i just learnt how to use pixel shader yesterday.
thank you!

##### Share on other sites
BCullis    1955
Loading and sampling a texture is very standard stuff as far as pixel shaders, wherever you learned how to use the shader should also cover texturing. You'll greatly benefit from learning how to search for references on google.

A few minutes of poking around dug up [url="http://www.two-kings.de/tutorials/dxgraphics/dxgraphics18.html"]this[/url], which incidentally I found by searching this very site and running across [url="http://www.gamedev.net/topic/596825-basic-directx-9-shader-tutorial/"]this thread[/url].

##### Share on other sites
AlanWu    241
[quote name='BCullis' timestamp='1354740629' post='5007514']
Loading and sampling a texture is very standard stuff as far as pixel shaders, wherever you learned how to use the shader should also cover texturing. You'll greatly benefit from learning how to search for references on google.

A few minutes of poking around dug up [url="http://www.two-kings.de/tutorials/dxgraphics/dxgraphics18.html"]this[/url], which incidentally I found by searching this very site and running across [url="http://www.gamedev.net/topic/596825-basic-directx-9-shader-tutorial/"]this thread[/url].
[/quote]

thank you!

##### Share on other sites
AlanWu    241
[quote name='BCullis' timestamp='1354740629' post='5007514']
Loading and sampling a texture is very standard stuff as far as pixel shaders, wherever you learned how to use the shader should also cover texturing. You'll greatly benefit from learning how to search for references on google.

A few minutes of poking around dug up [url="http://www.two-kings.de/tutorials/dxgraphics/dxgraphics18.html"]this[/url], which incidentally I found by searching this very site and running across [url="http://www.gamedev.net/topic/596825-basic-directx-9-shader-tutorial/"]this thread[/url].
[/quote]

i still don't understand.i learnt how to use Pixel Shaders.
But,how to use Pixel Shaders to change the texture?

##### Share on other sites
BCullis    1955
You don't use a pixel shader to change the texture, you change the texture a pixel shader is referencing (nuances of english syntax, so this might just be confusing at the moment).

In other words, a pixel shader will sample from a texture and draw that to the polygons' surface. For the shader to know which texture to use, you have to pass it the texture in your application code. If you want a different texture to be used, you pass in a new texture to that shader before drawing more polygons.

If you "learned how to use Pixel Shaders" then you should know how to do this. Otherwise you haven't learned how to use pixel shaders yet.

Read through the tutorial I linked before, as well as [url="http://www.two-kings.de/tutorials/dxgraphics/dxgraphics05.html"]this[/url] tutorial on the same site. If you're just looking for code, the last line in that second tutorial shows how to change the texture.

##### Share on other sites
AlanWu    241
well,i want to use 100 different textures in one DrawPrimitive() call.
I know use tex2D(sampler,tex coord); to get texture pixel then return it.but,how could i get the texture in PixelShader?
I think i can use SetTexture(xx, Tex) and multiple sampler,but PixelShader 2.0 only support 16 samplers.

##### Share on other sites
AlanWu    241
And ,do you have a facebook?

##### Share on other sites
AlanWu    241
can anybody help me ?