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

Efficient Rendering of Hundreds of Billboards

8 posts in this topic

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();
	}	

}

 

 

0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

[quote name='Tsus' timestamp='1356172009' post='5013374']
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).
[/quote]

 

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,

0

Share this post


Link to post
Share on other sites

[quote name='Nyssa' timestamp='1356177812' post='5013383']




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.
[/quote]

 

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,

0

Share this post


Link to post
Share on other sites

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!
 

 

0

Share this post


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

0

Share this post


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

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