Jump to content

  • Log In with Google      Sign In   
  • Create Account

Efficient Rendering of Hundreds of Billboards


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
8 replies to this topic

#1 PhillipHamlyn   Members   -  Reputation: 454

Like
0Likes
Like

Posted 22 December 2012 - 03:56 AM

Hi,

 

I have many hundreds of billboards (trees in a forest) to render, and I'm wondering if I've got my loop structure correct to give the best rendering speed - I've got no evidence its slow, but would value a second opion. My worry is that I'm having to call Effect.Apply() once per billboard, and it seems that what I should be doing is calculating my rotation and the subsequent World matrix in the VB instead of the CPU, however I can't quite see how I'd pass that data in other than for one billboard at a time... which doesn't seem much of a saving. I can't quite get my head around the fact I've got (for instance) 500 billboards to render, and here is a 500 item loop in my CPU code; somehow it seems like I should be leveraging the pipeline capabilities of the VB better.

 

Can anyone suggest improvements here ?

 

Thanks.

 

BillboardEffect = effectLibrary.BillboardEffect;

Foreach(Billboard in Billboards)
{
	device.SetVertexBuffer(Billboard.VB);
	device.SetIndexBuffer(Billboard.IB);
	foreach(Vector3 BillboardLocation in Locations)
	{
		rotation = <calculate rotation to face camera)>;
		
		Matrix worldMatrix = Matrix.Multiply(	
			Matrix.CreateRotation(rotation),
			Matrix.CreateTranslation(BillboardLocation));

		BillboardEffect.SetParameters(worldMatrix, ....);
		BillboardEffect.Apply();
		
		device.DrawIndexedPrimitives();
	}	

}

 

 



Sponsor:

#2 Tsus   Members   -  Reputation: 1034

Like
2Likes
Like

Posted 22 December 2012 - 04:26 AM

Hi!

You’re using XNA, right?
A common trick to render many billboards (if you don’t have geometry shaders available) is to send four vertices with identical position to the vertex shader and to expand the quad in view space, by offsetting each of the vertices using their texture coordinates. (The texture coordinate identifies in which corner to move the vertex.) This way, you don't need to calculate rotation matrices on the CPU (or the GPU). Thus you don't have to set additional effect constants and therefore you don't need to call Apply for every single billboard, but only if the texture changes. (So, batching, i.e. sorting per material, would be a good idea.)

Even better, you can render all billboards that share the same texture with a single draw call, by throwing all billboards into a single vertex buffer.
See here, for an example (Section 1.2).

Best regards!
 

PS: You do not necessarily need a quad. You could also use a single right triangle that covers the whole quad. (Of course, some area would be unused.) If you go down this road, you trade input assembler load against rasterizer load. You would need to profile to see what's better in your situation. For starters I would suggest to use quads, since they are more intuitive.



#3 Nyssa   Members   -  Reputation: 426

Like
0Likes
Like

Posted 22 December 2012 - 06:03 AM

If the billboards aren't going to move at runtime then I'd suggest creating a static buffer at initialization and adding all the verts in world space to that buffer. That way there is no looping at runtime. This would provide the best performance for static objects.

 

If the objects are going to move at runtime then maybe think about using hardware instancing. So buffer A (a static buffer) would contain the geometry for a single billboard and buffer B (a dynamic buffer) would contain the transform matrices for each instance. This way you are only calling DrawIndexedPrimitives() once. This option is ideal for instancing geometry with large numbers of verts. For billboards however (with 4 or 6 verts?), you way not gain any performance because there is some internal overhead involved with hardware instancing. So you'd probably need to test this in your case to see if there is a performance gain or not.



#4 PhillipHamlyn   Members   -  Reputation: 454

Like
0Likes
Like

Posted 22 December 2012 - 08:06 AM

A common trick to render many billboards (if you don’t have geometry shaders available) is to send four vertices with identical position to the vertex shader and to expand the quad in view space, by offsetting each of the vertices using their texture coordinates. (The texture coordinate identifies in which corner to move the vertex.) This way, you don't need to calculate rotation matrices on the CPU (or the GPU). Thus you don't have to set additional effect constants and therefore you don't need to call Apply for every single billboard, but only if the texture changes. (So, batching, i.e. sorting per material, would be a good idea.)

Even better, you can render all billboards that share the same texture with a single draw call, by throwing all billboards into a single vertex buffer.
See here, for an example (Section 1.2).

 

Tsus,

 

I see now ; I would have an "artifically" large VB and alter the vertex position in the shader. Even though they are all the same mesh, I would do the location transform when loading up the VB rather than using a matrix to transform it at render time. Is the resultant call so quick that I dont need to bother about culling ? Or would you subdivide the IBs into sets based on your world graph so that you dont end up rendering invisible billboards ?

 

Thanks,



#5 PhillipHamlyn   Members   -  Reputation: 454

Like
0Likes
Like

Posted 22 December 2012 - 08:09 AM

Posted Today, 12:03 PM


If the billboards aren't going to move at runtime then I'd suggest creating a static buffer at initialization and adding all the verts in world space to that buffer. That way there is no looping at runtime. This would provide the best performance for static objects.

 

Nyssa,

 

I see - here I would be trading off GPU RAM against the number of draw calls, given I've got only one very small VB for my billboard quads at the moment, but I'd guessing the trade off of having one single large VB constituting all my location transformed billboard quads is overwhelmingly better ?

 

Thanks,



#6 Adam_42   Crossbones+   -  Reputation: 2505

Like
0Likes
Like

Posted 22 December 2012 - 09:08 AM

You might find http://blogs.msdn.com/b/shawnhar/archive/2011/01/12/spritebatch-billboards-in-a-3d-world.aspx useful.



#7 Tsus   Members   -  Reputation: 1034

Like
0Likes
Like

Posted 22 December 2012 - 09:35 AM

Hi!

 

A common trick to render many billboards (if you don’t have geometry shaders available) is to send four vertices with identical position to the vertex shader and to expand the quad in view space, by offsetting each of the vertices using their texture coordinates. (The texture coordinate identifies in which corner to move the vertex.) This way, you don't need to calculate rotation matrices on the CPU (or the GPU). Thus you don't have to set additional effect constants and therefore you don't need to call Apply for every single billboard, but only if the texture changes. (So, batching, i.e. sorting per material, would be a good idea.)

Even better, you can render all billboards that share the same texture with a single draw call, by throwing all billboards into a single vertex buffer.
See here, for an example (Section 1.2).

 

 

Tsus,

 

I see now ; I would have an "artifically" large VB and alter the vertex position in the shader. Even though they are all the same mesh, I would do the location transform when loading up the VB rather than using a matrix to transform it at render time. Is the resultant call so quick that I dont need to bother about culling ? Or would you subdivide the IBs into sets based on your world graph so that you dont end up rendering invisible billboards ?

 

Thanks,

 


Using one large VB in a single draw call is definitely faster than drawing each quad individually. Though, sooner or later you will run into scalability problems, too. Thus, at some point culling is advisable. You could try it out and see, whether you can live without culling. (No point in optimizing things, if you don’t know how hard they hit the performance, right?) The actual performance depends on the size of the quads on the screen (and their overlap, i.e., increase in fillrate), the complexity of the shaders, the blending operations you apply, etc.

For the culling, two options come to my mind:

 

1. As you said, you can update your buffers (either having a static vertex buffer and a dynamic index buffer, or directly a dynamic vertex buffer) to draw only quads that are visible. The problem is you need dynamic resources even for objects that are actually static.

 

2. You can divide your scene into small static blocks (aligned in a grid or a grid hierarchy) and cull them conservatively. This means, you would render sometimes a few billboards that are not on the screen, since you only cull entire blocks. On the plus side, all static objects would reside in static buffers.

 

In the second approach you would need one draw call per block (for the static objects). Depending on your scene data structure, you may already have a space-partition of your scene. If your scene is already organized in a grid, you could cook up the vertex buffers for each grid cell individually.
Essentially, the grid size is a trade-off of the number of objects you can cull and the size of the batches. (The smaller the grid cells, the better you can cull. The larger the grid cells, the faster are the draw calls.) Billboards are usually pretty light-weight so it is no catastrophe if you miss a few in your culling.

 

Best regards!
 

 



#8 PhillipHamlyn   Members   -  Reputation: 454

Like
0Likes
Like

Posted 27 December 2012 - 11:28 AM

You might find http://blogs.msdn.com/b/shawnhar/archive/2011/01/12/spritebatch-billboards-in-a-3d-world.aspx useful.

Hi Adam_42,

 

I read through this article and it seems that its only useful to use a SpriteBatch when all the items are on the same plane - its not really the same as Billboarding where each sprite is oriented to the camera - unless I'm mistaken it will lead to a "side on" view of the billboard as it falls off from the centre of view ?

 

Thanks for the reply.

 

Phillip



#9 PhillipHamlyn   Members   -  Reputation: 454

Like
0Likes
Like

Posted 27 December 2012 - 11:48 AM

Hi!

 

A common trick to render many billboards (if you don’t have geometry shaders available) is to send four vertices with identical position to the vertex shader and to expand the quad in view space, by offsetting each of the vertices using their texture coordinates. (The texture coordinate identifies in which corner to move the vertex.) This way, you don't need to calculate rotation matrices on the CPU (or the GPU). Thus you don't have to set additional effect constants and therefore you don't need to call Apply for every single billboard, but only if the texture changes. (So, batching, i.e. sorting per material, would be a good idea.)

Even better, you can render all billboards that share the same texture with a single draw call, by throwing all billboards into a single vertex buffer.
See here, for an example (Section 1.2).

 

 

Tsus,

 

I see now ; I would have an "artifically" large VB and alter the vertex position in the shader. Even though they are all the same mesh, I would do the location transform when loading up the VB rather than using a matrix to transform it at render time. Is the resultant call so quick that I dont need to bother about culling ? Or would you subdivide the IBs into sets based on your world graph so that you dont end up rendering invisible billboards ?

 

Thanks,

 


Using one large VB in a single draw call is definitely faster than drawing each quad individually. Though, sooner or later you will run into scalability problems, too. Thus, at some point culling is advisable. You could try it out and see, whether you can live without culling. (No point in optimizing things, if you don’t know how hard they hit the performance, right?) The actual performance depends on the size of the quads on the screen (and their overlap, i.e., increase in fillrate), the complexity of the shaders, the blending operations you apply, etc.

For the culling, two options come to my mind:

 

1. As you said, you can update your buffers (either having a static vertex buffer and a dynamic index buffer, or directly a dynamic vertex buffer) to draw only quads that are visible. The problem is you need dynamic resources even for objects that are actually static.

 

2. You can divide your scene into small static blocks (aligned in a grid or a grid hierarchy) and cull them conservatively. This means, you would render sometimes a few billboards that are not on the screen, since you only cull entire blocks. On the plus side, all static objects would reside in static buffers.

 

In the second approach you would need one draw call per block (for the static objects). Depending on your scene data structure, you may already have a space-partition of your scene. If your scene is already organized in a grid, you could cook up the vertex buffers for each grid cell individually.
Essentially, the grid size is a trade-off of the number of objects you can cull and the size of the batches. (The smaller the grid cells, the better you can cull. The larger the grid cells, the faster are the draw calls.) Billboards are usually pretty light-weight so it is no catastrophe if you miss a few in your culling.

 

Best regards!
 

 

Ok, I understand. Since this is a terrain then I have a natural segmentation of the world space, and my billboards can be organised around the terrain quads themselves. I guess for the nearest (or current) terrain quad, I would still have to render them individually, since some of the billboards will drop out to become fully fledged 3D meshes as the camera gets close to them - but still; being able to render all the other terrain quad billboard content as batches makes a lot of sense and I'll see where I get to.






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