• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

20 replies to this topic

### #1AlanWu  Members

241
Like
0Likes
Like

Posted 03 December 2012 - 02:56 AM

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.

### #2ifthen  Members

820
Like
1Likes
Like

Posted 03 December 2012 - 07:02 AM

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 ).

### #3AlanWu  Members

241
Like
0Likes
Like

Posted 03 December 2012 - 09:57 PM

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!

### #4BCullis  Members

1951
Like
0Likes
Like

Posted 03 December 2012 - 11:32 PM

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)

### #5Steve_Segreto  Members

2008
Like
1Likes
Like

Posted 04 December 2012 - 12:00 AM

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.

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, 04 December 2012 - 12:01 AM.

### #6AlanWu  Members

241
Like
0Likes
Like

Posted 04 December 2012 - 12:34 AM

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!

### #7AlanWu  Members

241
Like
0Likes
Like

Posted 04 December 2012 - 01:04 AM

i make a experiment just now ,i think we can ignore the speed between D3DPT_TRIANGLELIST and D3DPT_TRIANGLESTRIP,it is very small.

### #8AlanWu  Members

241
Like
0Likes
Like

Posted 04 December 2012 - 01:05 AM

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.

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.

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!

### #9AlanWu  Members

241
Like
0Likes
Like

Posted 04 December 2012 - 01:16 AM

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 ).

And thank you for your helping!

### #10AlanWu  Members

241
Like
0Likes
Like

Posted 04 December 2012 - 01:27 AM

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.

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?

### #11BCullis  Members

1951
Like
0Likes
Like

Posted 04 December 2012 - 09:39 AM

how to put the texture into one buffer?How to make the textures connect to the vertices?

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.

And what kind of optimizations you think i still can do?

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.

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

### #12AlanWu  Members

241
Like
0Likes
Like

Posted 04 December 2012 - 11:05 PM

how to put the texture into one buffer?How to make the textures connect to the vertices?

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.

And what kind of optimizations you think i still can do?

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.

ok,thank you!
I will try it.

### #13AlanWu  Members

241
Like
0Likes
Like

Posted 05 December 2012 - 01:43 AM

how to put the texture into one buffer?How to make the textures connect to the vertices?

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.

And what kind of optimizations you think i still can do?

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.

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

### #14AlanWu  Members

241
Like
0Likes
Like

Posted 05 December 2012 - 12:46 PM

could you give me some tutorial or some example?
i just learnt how to use pixel shader yesterday.
thank you!

### #15BCullis  Members

1951
Like
0Likes
Like

Posted 05 December 2012 - 02:50 PM

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 this, which incidentally I found by searching this very site and running across this thread.

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

### #16AlanWu  Members

241
Like
0Likes
Like

Posted 05 December 2012 - 09:34 PM

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 this, which incidentally I found by searching this very site and running across this thread.

thank you!

### #17AlanWu  Members

241
Like
0Likes
Like

Posted 06 December 2012 - 09:53 PM

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 this, which incidentally I found by searching this very site and running across this thread.

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

### #18BCullis  Members

1951
Like
0Likes
Like

Posted 07 December 2012 - 10:07 AM

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 this 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.

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

### #19AlanWu  Members

241
Like
0Likes
Like

Posted 07 December 2012 - 03:15 PM

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.

### #20AlanWu  Members

241
Like
0Likes
Like

Posted 07 December 2012 - 03:16 PM

And ,do you have a facebook?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.