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

Draw Call Sorting Using std::map

10 posts in this topic

Hello,

 

I just read the following article which I believe has been mentioned here before as well.

 

http://realtimecollisiondetection.net/blog/?p=86

 

As I have a fair understanding of the DirectX 11 API for most of my needs and a somewhat fair understanding of OOP I want to start

designing a render loop (short of calling it game loop since there's no AI or physx yet) around a more scale-able architecture that could ultimately grow into a game... That means separating responsibilities in different classes and decoupling what can be, and ideally have a render class rather than have objects draw themselves.

 

Mind you I'm still at rastertek tutorial level of understanding LOL and it's mostly how to connect all the dots that's difficult more so than each separate topic such as shadows, skyboxes, reflections, render to texture or what-have-you.

 

It's how games elegantly add creatures, remove them, iterate through them, draw them in the most optimal order etc that seems to be the crux of videogame development imho. I believe this article pretty much hits the nail on the head and draw call sorting might make everything fall into place...

 

Now initially that sounded weird... sorting "function calls" ?!? lolwut? but of course it's the data that has to be rendered that's sorted by renderstate. Fine. Now in the article they mention "using standard sorting algorithms" and "key/value" pairs. That to me screams std::map but is this what is usually used by professionals? I tried a bit of dummy example code with std::map to see how fast it is and it appeared that even iterating through a couple of entries on my i5 laptop with 256 kb of cache takes a couple of milliseconds, an eternity when you've got only 16 ms per frame and you are supposed to iterate 2000 draw calls. I admit I haven't tried to actually sort 2000 entries as I had to come up with 2000 things to fill it with but could it be that it won't take much longer as some of this time was overhead? Or is std::map just not the right solution? What I liked about it is that it's actually automatically sorted but maybe some unordered map followed by a sort is better? (then again my measurement was done for iterating only, after I had inserted values so the timing would even be worse otherwise)

Maybe my measuring tool wasn't appropriate? I used "time.h" clock() like so:

 

t=clock()

//iterate std::map with barely 6 key/value pairs in it

t=clock()-t;

cout << t <<endl;

 

I know there are higher resolution timers out there but since we're talking milliseconds here I guess I'd measure the same. Now if it's simply an inaccurate tool I'll reconsider...

 

0

Share this post


Link to post
Share on other sites

Don't use std::map, use std::vector, and store the key in the render instance you submit. Then you can write a custom sort function:

 

std::stable_sort(m_vInstances.begin(), m_vInstances.end(), QueueSorter());

		struct QueueSorter
		{
			inline bool operator()(const RenderInstance* pLeft, const RenderInstance* pRight)
			{
				return pLeft->sortKey.bits > pRight->sortKey.bits; 
			}
		};

Make sure you put the sorting function in the header and as a functor struct, that way its likely going to be inlined and thus way faster.

2

Share this post


Link to post
Share on other sites

I'm not sure I understand this answer. Functors? Is that some C++11 construct? I guess I have some more reading to do...

Vectors don't have the notion of key/value and I also don't get what you mean by "and store the key in the render instance you submit"

0

Share this post


Link to post
Share on other sites

Functor is simply that what my "QueueSorter" is. A struct with a ()-operator, that is called instead of a "QueueSort"-function, since the compiler most likely won't be able to inline the latter.

 

Well, just look at what the () operator does. It takes two RenderInstances, and compare their keys. As I said, store them inside whatever structure you would store in the map. Like this:

 

		/*******************************************
		* Render instance
		********************************************/
		struct RenderInstance
		{
			Key64 sortKey;

			const DrawCall* pCall;
			const StateGroup** pStateGroups;
		};

The realmtimecollisiondetection-article doesn't explain that bit in too great detail, I suggest you have a close read at this topic: http://www.gamedev.net/topic/604899-frostbite-rendering-architecture-question/

2

Share this post


Link to post
Share on other sites

OK thanks. I think I got the functor now after some googling. Weird they didn't mention this in learncpp's chapter on operator() overloading.

I'll try to see where this can take me.

0

Share this post


Link to post
Share on other sites

The article you refer to touches upon many different ideas used by people to improve the performance of game engines; more specifically, rendering speed.

 

Just one comment on that. While it is generally true that state monitoring can improve performance, the methond mentioned in the article does not serve the main puprose of improving performance. The author himself notes that he is sacrificing (some) performance in advance of flexibility. Also, this method has nothing to do with state monitoring per se. In a primitive approach, or if you are too lazy like me to come up with a good solution, you will still set state after state after state without caring what the last state was. So at all, you will loose a litte performance - there is no way sorting and pre-processing draw calls is going to be faster than directly submitting them. With state monitoring, you pull workload from the gpu to the cpu. There, it might eventually pay out. Also you can easier use multithreading with such a queue (multiple cores submit render states to it, one core draws them afterwards).

 

So while it might actually lead to that, gaining performance is, once again, NOT the main purpose of this rendering architecture, just to make this clear. One of the main purposes is, from what I understand, to further decouple high and low level rendering, and adding some flexibility - it doesn't care now when you submit a render command. The key will tell when it needs to be drawn - be it the first insertion or the last. Without such a render queue, precautions have to be made that the opaque geometry gets drawn before the transparent, and the transparent needs to be sorted back to front. Now the queue takes care of this. That is also why it doesn't make sense to add a time delta to the submitted command. We don't sort by call time, we sort by predefined settings, determined in the sort key.

0

Share this post


Link to post
Share on other sites

OK thanks all I think I've understood it now. And to Snowman thanks for the code and detailed explanation. This will prove very useful.

0

Share this post


Link to post
Share on other sites

No one mentioned one of the most important things: You don’t sort the renderstates (or whatever object you put into your render queue) directly; you sort the indices to that list.

Copying around objects wastes time.  Only copy the 32-bit indices and use that to draw in sorted order.

 

http://lspiroengine.com/?p=96

 

 

L. Spiro

2

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