Followers 0

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

Create an account

Register a new account