Jump to content

  • Log In with Google      Sign In   
  • Create Account


Yet Another Rendering Queue Thread


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

#1 CombatWombat   Members   -  Reputation: 456

Like
0Likes
Like

Posted 14 July 2014 - 04:51 PM

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.

 

 

 



Sponsor:

#2 Hodgman   Moderators   -  Reputation: 29302

Like
2Likes
Like

Posted 14 July 2014 - 05:06 PM

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.



#3 megadan   Members   -  Reputation: 450

Like
1Likes
Like

Posted 14 July 2014 - 05:31 PM

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, 14 July 2014 - 05:32 PM.


#4 CombatWombat   Members   -  Reputation: 456

Like
0Likes
Like

Posted 14 July 2014 - 07:02 PM

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.

 

 



#5 Servant of the Lord   Crossbones+   -  Reputation: 18478

Like
0Likes
Like

Posted 14 July 2014 - 08:07 PM

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, 14 July 2014 - 08:09 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#6 ankhd   Members   -  Reputation: 1211

Like
0Likes
Like

Posted 16 July 2014 - 07:45 PM

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.




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