• 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
AlanWu

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

20 posts in this topic

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

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] ).
1

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
[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.
[/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
1

Share this post


Link to post
Share on other sites
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!
0

Share this post


Link to post
Share on other sites
i make a experiment just now ,i think we can ignore the speed between D3DPT_TRIANGLELIST and D3DPT_TRIANGLESTRIP,it is very small.
0

Share this post


Link to post
Share on other sites
[quote name='Steve_Segreto' timestamp='1354600852' post='5006980']
[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.
[/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!
0

Share this post


Link to post
Share on other sites
[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.


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]

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

Share this post


Link to post
Share on other sites
[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?
0

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
[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?
0

Share this post


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

Share this post


Link to post
Share on other sites
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].
0

Share this post


Link to post
Share on other sites
[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!
0

Share this post


Link to post
Share on other sites
[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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
well,i want to use 100 different textures in one DrawPrimitive() call.
I used PixelShader,but how to pass 100 textures into the PixelShader?
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.
0

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