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

Fastest way to draw rectangles with slimdx / sharpdx

12 posts in this topic

Hi,
My objective is to draw near 60 000 3D rectangles as fast as it possible.
I'm using C# and SharpDX.
I've tried to use an array of VertexBuffer's:
[source lang="csharp"]
vertices = new VertexBuffer[elements.Length];
for (int i = 0; i < elements.Length; i++)
{
List<Node> list = elements[i].Nodes;
vertices[i] = new VertexBuffer(d3dDevice, list.Count * 20, SharpDX.Direct3D9.Usage.WriteOnly,
VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.PointSize, Pool.Managed);
Vertex[] points = new Vertex[list.Count];
for (int j = 0; j < list.Count; j++)
{
points[j] = new Vertex() { Color = SharpDX.Color.Red, Position = new Vector4(list[j].X, list[j].Y, list[j].Z, 1.0f) };
}
vertices[i].Lock(0, 0, LockFlags.None).WriteRange(points);
vertices[i].Unlock();
}
var vertexElems = new[] {
new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0),
new VertexElement(0, 16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
VertexElement.VertexDeclarationEnd
};
[/source]

Each Buffer contain 8 points (1 rectangle).

I draw rectangles that way:

[source lang="csharp"]
d3dDevice.Clear(ClearFlags.Target, SharpDX.Color.White, 1.0f, 0);
d3dDevice.BeginScene();
d3dDevice.VertexFormat = VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.PointSize;
for (int i = 0; i < elements.Length; i++)
{
d3dDevice.SetStreamSource(0, vertices[i], 0, 20);
d3dDevice.DrawPrimitives(PrimitiveType.LineStrip, 0, 7);
}

d3dDevice.EndScene();
d3dDevice.Present();
[/source]

[b]but this method is too slow...[/b]
I need something working fast and give me a possibility do fill that rectangles with some color.
I'm newbie in DirectX graphics but I haven't much time to resolve this problem, so I'm asking for a good advise, or, even better, an example, how to draw 3D rectangle by points (x,y,z)
0

Share this post


Link to post
Share on other sites
You can't draw 60,000 objects per frame with a draw call for each object. I don't know what the limit is these days, but I would say about 5000. My personal experience with rendering shadowed lights slows down at around 1000 draw calls. The numbers will be tied to your code and your expertise in managing the pipeline and resources.

So some strategies you might like to look into include :

Geometry instancing - drawing a single object multiple times using instanced hardware rendering, reducing draw calls.
Storing multiple objects in a single vertex buffer - maintaining the buffer to represent your different objects, reducing draw calls
dynamic surface construction - constructing larger surfaces from contiguous ones, thereby reducing the number of surfaces / objects you need to draw, reducing draw calls

There's a start for you.
2

Share this post


Link to post
Share on other sites
Unless you really need to use DX9 I would suggest taking advantage of DX10/DX11, I cant remember where I read this (probably nVidia) but instancing in DX9 is meant to be ugly to implement compared to DX10/DX11

Also your performance will be worse in Debug mode, this doesnt mean you should create your device without debug flags on, it just means not to be surprised if things are laggy when debugging large scenes

Not entirely sure what you need 60k rectangles for, seems a little excessive, what is it you want to do?
1

Share this post


Link to post
Share on other sites
[quote name='Dynamo_Maestro' timestamp='1355255380' post='5009524']
Unless you really need to use DX9 I would suggest taking advantage of DX10/DX11, I cant remember where I read this (probably nVidia) but instancing in DX9 is meant to be ugly to implement compared to DX10/DX11

Also your performance will be worse in Debug mode, this doesnt mean you should create your device without debug flags on, it just means not to be surprised if things are laggy when debugging large scenes

Not entirely sure what you need 60k rectangles for, seems a little excessive, what is it you want to do?
[/quote]

It doesn't meter to me what DX to use. I've just thought that DX10/11 have performance in graphics (realistic textures, smoke etc) but not in speed. If you say so, I'll rebuild my project with DX10.

I'm a student of the mathematical department and I need to build finite elements triangulation with quadrangular elements of some 3D surface. I've made a triangulation and now I have a list of points, which I should represent graphically. This list can contain from 500 to 5 000 000 elements, depends of surface.

Thank you for reply!
0

Share this post


Link to post
Share on other sites
[quote name='Gavin Williams' timestamp='1355248384' post='5009470']
You can't draw 60,000 objects per frame with a draw call for each object. I don't know what the limit is these days, but I would say about 5000. My personal experience with rendering shadowed lights slows down at around 1000 draw calls. The numbers will be tied to your code and your expertise in managing the pipeline and resources.

So some strategies you might like to look into include :

Geometry instancing - drawing a single object multiple times using instanced hardware rendering, reducing draw calls.
Storing multiple objects in a single vertex buffer - maintaining the buffer to represent your different objects, reducing draw calls
dynamic surface construction - constructing larger surfaces from contiguous ones, thereby reducing the number of surfaces / objects you need to draw, reducing draw calls

There's a start for you.
[/quote]

Storing multiple objects in a single vertex buffer REALLY WORKS! It draws much-much-much faster!
I'll try to implement other strategies too.
But I have one more problem. I need to fill my rectangles with some colour. It is impossible while I draw it line-by-line.
Any suggestions?

Thanks for help! Edited by Yura
0

Share this post


Link to post
Share on other sites
I have one more problem. I need to fill my rectangles with some colour. It is impossible while I draw it line-by-line.
Any suggestions? Edited by Yura
0

Share this post


Link to post
Share on other sites
[quote name='Yura' timestamp='1355298749' post='5009738']
[quote name='Dynamo_Maestro' timestamp='1355255380' post='5009524']
Unless you really need to use DX9 I would suggest taking advantage of DX10/DX11, I cant remember where I read this (probably nVidia) but instancing in DX9 is meant to be ugly to implement compared to DX10/DX11

Also your performance will be worse in Debug mode, this doesnt mean you should create your device without debug flags on, it just means not to be surprised if things are laggy when debugging large scenes

Not entirely sure what you need 60k rectangles for, seems a little excessive, what is it you want to do?
[/quote]

It doesn't meter to me what DX to use. I've just thought that DX10/11 have performance in graphics (realistic textures, smoke etc) but not in speed. If you say so, I'll rebuild my project with DX10.

I'm a student of the mathematical department and I need to build finite elements triangulation with quadrangular elements of some 3D surface. I've made a triangulation and now I have a list of points, which I should represent graphically. This list can contain from 500 to 5 000 000 elements, depends of surface.

Thank you for reply!
[/quote]

Remake project from DX9 to DX11 is not so easy as I thought)
0

Share this post


Link to post
Share on other sites
[quote name='Yura' timestamp='1355303974' post='5009757']
I have one more problem. I need to fill my rectangles with some colour. It is impossible while I draw it line-by-line.
Any suggestions?
[/quote]

You are using line strip, if you want to fill the rectangle use triangle lists / strips

See the SharpDX / SlimDX for examples

[quote name='Yura' timestamp='1355306263' post='5009764']
Remake project from DX9 to DX11 is not so easy as I thought)
[/quote]

It depends on how much you have done already in DX9, but from what you want I would suggest you take full advantage of the features in DX10/DX11.

Personally I found DX10/11 to be much cleaner and easier to work with than DX9
1

Share this post


Link to post
Share on other sites
Is it possible to draw rectangles, not triangles? There is not rectangles in PrimitiveType enum...
0

Share this post


Link to post
Share on other sites
[quote name='Yura' timestamp='1355409031' post='5010229']
Is it possible to draw rectangles, not triangles? There is not rectangles in PrimitiveType enum...
[/quote]

You make a rectangle from triangles, 2 triangles make one square / rectangle, unless you mean using quads?

I noticed you were using 8 points for a rectangle too, which would make sense if you were using linelist, eitherway with triangles you would only need 6 points (4 if indexed). You can always convert single points to quads (rectangles) using the geometry shader, which is probably better for what you want doing

How are you learning DirectX btw? Edited by Dynamo_Maestro
2

Share this post


Link to post
Share on other sites
[quote name='Dynamo_Maestro' timestamp='1355414358' post='5010255']
[quote name='Yura' timestamp='1355409031' post='5010229']
Is it possible to draw rectangles, not triangles? There is not rectangles in PrimitiveType enum...
[/quote]

You make a rectangle from triangles, 2 triangles make one square / rectangle, unless you mean using quads?

I noticed you were using 8 points for a rectangle too, which would make sense if you were using linelist, eitherway with triangles you would only need 6 points (4 if indexed). You can always convert single points to quads (rectangles) using the geometry shader, which is probably better for what you want doing

How are you learning DirectX btw?
[/quote]

I can triangulate any 3D surface, so, as a result of triangulation I receive list of elements. Each element consists of 3-8 points, but mostly it has 4 or 8 points, because it is triangulation with quadrangular elements. You are right, I'm using line list do draw each element, so, if it has 4 points, I must put in VertexBuffer 8 points, to draw it completely. It's a bit excessive, but I don't know how to make it another way and it works nice.
[source lang="csharp"]List<Vertex> points = new List<Vertex>();
for (int i = 0; i < elements.Length; i++)
{
List<Node> list = elements[i].Nodes;

int listCount = list.Count;
for (int j = 0; j < listCount - 1; j++)
{
points.Add(new Vertex() { Color = SharpDX.Color.Red, Position = new Vector4(list[j].X, list[j].Y, list[j].Z, 1.0f) });
points.Add(new Vertex() { Color = SharpDX.Color.Red, Position = new Vector4(list[j + 1].X, list[j + 1].Y, list[j + 1].Z, 1.0f) });
}
points.Add(new Vertex()
{
Color = SharpDX.Color.Red,
Position = new Vector4(list[listCount - 1].X, list[listCount - 1].Y,
list[listCount - 1].Z, 1.0f) });
points.Add(new Vertex() { Color = SharpDX.Color.Red, Position = new Vector4(list[0].X, list[0].Y, list[0].Z, 1.0f) });
}[/source]

Look at this links, I'll show what I have:
this is lines http://imageshack.us/photo/my-images/194/capturerse.png/
this is triangles http://imageshack.us/photo/my-images/51/capture3be.png/
and this is what I need http://imageshack.us/photo/my-images/546/capture2pu.png/
Quads must be filled with some colour, but must have visible lines. I'm not sure it is possible to make with triangle list.

About DirectX, I'm learning it directly from the internet: forums, code examples etc. I do not read any thick books, if you asking about it)
Thanks for losing your time on me) Edited by Yura
0

Share this post


Link to post
Share on other sites
Another problem is that triangles have invisible sides ( look here http://imageshack.us/photo/my-images/51/capture3be.png/ )
When I rotate a shape I can only see her front side.. Triangles that are backwards to me invisible Edited by Yura
0

Share this post


Link to post
Share on other sites
Because you probably have back culling enable you can only see the frontal face of the triangles, check where you are creating your device for back culling (I am no sure where it is set in DirectX, but I am pretty sure it is somewhere in the device creation)
1

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