Jump to content

  • Log In with Google      Sign In   
  • Create Account


Fastest way to draw rectangles with slimdx / sharpdx


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.

  • You cannot reply to this topic
12 replies to this topic

#1 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 11 December 2012 - 10:04 AM

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]

but this method is too slow...
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)

Sponsor:

#2 Gavin Williams   Members   -  Reputation: 643

Like
2Likes
Like

Posted 11 December 2012 - 11:53 AM

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.

#3 Memories are Better   Prime Members   -  Reputation: 769

Like
1Likes
Like

Posted 11 December 2012 - 01:49 PM

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?

#4 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 12 December 2012 - 01:52 AM

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?


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!

#5 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 12 December 2012 - 02:07 AM

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.


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, 12 December 2012 - 03:57 AM.


#6 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 12 December 2012 - 03:19 AM

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


#7 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 12 December 2012 - 03:57 AM


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?


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!


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

#8 Memories are Better   Prime Members   -  Reputation: 769

Like
1Likes
Like

Posted 12 December 2012 - 06:03 AM

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?


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

See the SharpDX / SlimDX for examples

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


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

#9 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 13 December 2012 - 08:30 AM

Is it possible to draw rectangles, not triangles? There is not rectangles in PrimitiveType enum...

#10 Memories are Better   Prime Members   -  Reputation: 769

Like
2Likes
Like

Posted 13 December 2012 - 09:59 AM

Is it possible to draw rectangles, not triangles? There is not rectangles in PrimitiveType enum...


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, 13 December 2012 - 10:14 AM.


#11 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 14 December 2012 - 07:58 AM


Is it possible to draw rectangles, not triangles? There is not rectangles in PrimitiveType enum...


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?


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, 14 December 2012 - 09:28 AM.


#12 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 17 December 2012 - 05:42 AM

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, 17 December 2012 - 05:43 AM.


#13 Morphex   Members   -  Reputation: 298

Like
1Likes
Like

Posted 17 December 2012 - 09:59 AM

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)

Check out my new blog: Morphexe 





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.



PARTNERS