Jump to content
  • Advertisement
Sign in to follow this  
CombatWombat

Yet Another Rendering Queue Thread

This topic is 1433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've read everything I could find on these things, and I'm starting to implement this and questions are arising.  Sometimes I find typing all this out helps get my head in order...A lot of this might be duplicates of prior threads asked in a different way and much of it will be just plain hypotheticals...  Without further ado...

 

I am using SFML and want to sort by texture, and then by depth (really layer, since this is a 2d game). 

 

So.  I can store all the sf::Drawables in a vector (that gets filled by the scenegraph/culling/whatever system).

std::vector<sf::Drawable> allOpaqueDrawables;
std::vector<sf::Drawable> allAlphaDrawables;

Should these be pointers to the drawable which is instead stored in the game objects themselves? 

Should there be a master list of all drawables in the game that "owns" them, letting both the game object and these "sub-drawables-within-the-scene" use pointers to this master list?

Some other third option?

 

 

Now, I need to sort this.  So I can make a render queue which is just indicies into the vector of all drawables.

I'm not positive how to implement this... a first try might be something like:

std::map<sortKey, int16_t> renderQOpaque;
std::map<sortKey, int16_t> renderQAlpha;
// with sortkey being a POD integer type built from the texureID and layer value of the drawable

I guess the idea is that the small int16 keys are the only thing that needs to be copied around to sort the map?

 

1) Is this a sane way of doing this part?

2) Is std::map a reasonable choice for the implementation?

 

Then the map can be iterated through taking the int16_t as indicies into the vector allDrawables to call draw() on each one.

 

A few problems:

1) Where to store and generate the sortKey, if it needs to be stored at all?

       a) I need an integer representation of a texture.

       b) (At least part of) the sortKey needs to be updated if the objects layer (depth) changes.

 

My texture storage assigns a unique integer ID number and prevents duplicates from loading.  Does it make sense to actually store this value in the texture?  Right now it is only kept in the texture storage (as a std::map<texID, sf::Texture>).

I could inheret from sf::Texture and create a version that contains the texID.  Unnecessary?

 

SFML uses an sf::Drawable which contains the geometry to be drawn as well as the "rules" to draw it (actual draw() call).

It also uses an sf::RenderStates which holds...

1) *Texture

2) *Shader (if used)

3) *Transform

4) BlendType

 

Since this RenderState holds damn near everything I need to generate the sortKey, it almost sounds reasonable to create my own RenderStates which derives from sf::RenderStates so that I can contain the sortKey in it and regenerate the sortKey anytime one of those three items are changed.  Yea/Nay?

 

I realize most of this is just my thinking out-loud/rambling.  Any feedback is appreciated.

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

Should these be pointers to the drawable which is instead stored in the game objects themselves? 
Should there be a master list of all drawables in the game that "owns" them, letting both the game object and these "sub-drawables-within-the-scene" use pointers to this master list?

Yep, probably. You can use indices into the 'owner' list, or pointers to the items.
 

1) Is this a sane way of doing this part?
2) Is std::map a reasonable choice for the implementation?

No. std::map is a red-black tree, which is not a very good way to sort a vector.
 
Personally, I would have something like
struct DrawableKey { u32 sortKey; Drawable* item; }; vector<DrawableKey> renderQ;
and then use std::sort to sort the vector using the key. If the vector contains thousands of items, then a radix sort / counting sort might be faster.

a) I need an integer representation of a texture.

You just need different textures to have different integers. Different textures already have different pointers, so you can use some ugly code to convert a pointer to an integer:
Texture* texturePtr = ...;
ptrdiff_t texturePtrAsInteger = *(ptrdiff_t*)&texturePtr;

 

Since this RenderState holds damn near everything I need to generate the sortKey, it almost sounds reasonable to create my own RenderStates which derives from sf::RenderStates so that I can contain the sortKey in it and regenerate the sortKey anytime one of those three items are changed.  Yea/Nay?

That could work.

Share this post


Link to post
Share on other sites

If you haven't already seen it, I found this to be very useful for queuing and sorting draw calls: http://realtimecollisiondetection.net/blog/?p=86.  As he draws the scene, he fills an array with key value pairs where the key is the sort key and the value is a pointer to the draw call data like described by Hodgman above.

Edited by megadan

Share this post


Link to post
Share on other sites

Personally, I would have something like
struct DrawableKey { u32 sortKey; Drawable* item; }; vector<DrawableKey> renderQ;
and then use std::sort to sort the vector using the key. If the vector contains thousands of items, then a radix sort / counting sort might be faster.

 

Going to try this.  Might do an index instead of the pointer for round one.

 

 

You just need different textures to have different integers. Different textures already have different pointers, so you can use some ugly code to convert a pointer to an integer:
Texture* texturePtr = ...;
ptrdiff_t texturePtrAsInteger = *(ptrdiff_t*)&texturePtr;

Apparently I'm kinda stuck with something like this. 

I tried creating my own texture inheriting from the sf::Texture to add the texID integer in there.

Then I started inheriting a new RenderStates type from sf::RenderStates.  But then painted myself into a corner because it's implementation uses an sf::Texture and there isn't really a way I can see to override that to use my type without reimplementing the entire thing.  Kinda hokey.

 

 

If you haven't already seen it, I found this to be very useful for queuing and sorting draw calls: http://realtimecollisiondetection.net/blog/?p=86.  As he draws the scene, he fills an array with key value pairs where the key is the sort key and the value is a pointer to the draw call data like described by Hodgman above

Yeah, I've seen that one.  Also there's a few good ones on LSpiro's page.  Good explanation of the concept, but not enough ideas for implementation for my programming inept skill level.

 

 

Share this post


Link to post
Share on other sites

You shouldn't hold sf::Drawables in a vector directly - definitely make them pointers (or something else) instead, otherwise you'd get object slicingsad.png

 

Is your code already running slow? If not, you might be prematurely optimizing.

Are you sure you're measuring your performance in 'release mode'? smile.png

 

If you think you'll "later" need more speed, don't forget that FPS isn't linear, and that graphical rendering definitely has some 'economy of scale' aspects to it.

 

Still need speed? I found these things to help:

 

A) Make sure you aren't drawing sprites that are offscreen. SFML doesn't automatically stop them from drawing.

B) If you just need a moderate performance boost, sorting by texture will help a significant amount.

C) For additional speedups, consider bulk rendering using sf::VertexArray. This only works if all the objects being drawn in that one array use the same textures. 

D) With SFML, as it currently stands, sorting by shader and then by texture doesn't help much (or rather, doesn't help at all). You might as well do it anyway, though.

 

Even so, the best optimization possible is finding areas where you can just entirely remove work. You can optimize all you want, but if you can find ways to just simply avoid drawing things that don't need to be drawn (like culling off-screen objects), you usually get better results.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
Hi. I think it would be wise to add to your list in order and not sort.
What I'm saying it sort on insertion when you add item to the render list. No point adding then sorting.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!