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

Yet Another Rendering Queue Thread

5 posts in this topic

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.

 

 

 

0

Share this post


Link to post
Share on other sites

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.

2

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
1

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.

 

 

0

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
0

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