Implementing a Render Queue for Games


Rendering a complex scenes is far from a straightforward task. In a world with Directx 11 multithreading, multicores CPUs, lots of multipass techniques, agressive culling, lots of GPU state transitions …. it is not simple to design a render that handles well all of those requirements. This article will show how to implement a simple strategy that tries to fulfill in a fashion way all of these needs.

Render Queue is just a regular queue that contains “Commands” (instructions to control the rendering) that will be processed by the CPU/GPU. Those “Commands” need to be processed in a ordered way (but they are probably generated  by different threads at different times).

Example of Commands are:

  • Clear the Screen
  • Create a Render Target and set it active
  • Draw a model
  • Set the BlenderState of the GPU (for example to perform alpha blending)
  • ….
The implementation of the Render Queue i propose (the classical one) is very simple, all your commands have an ID (an 64 bits integer for example) that is composed by small of parts. (by bitwise operations)
Each frame, each “System” generates lots of commands to draw the scene and put then in a queue. Then a second process goes and SORT the queue by the commands ID. Finally the ordered queue is processed.
Sumarizing:
  • Commands are generated when the scene is processed
  • Commands are then sorted by their ID
  • Commands are processed in order by a render component

The id of the Command is the smartest point of the idea. It let the systems processing the scene to be able to control the order of the execution of the commands without knowing everything else.

The following image contains the components of the ID (a possible choice, you design this to accommodate your needs)  

Elements Description: (Original idea here ) — to being consider that Commands are elements that need to be draw (models, text, billboards …)

  • Fullscreen layer. Are we drawing to the game layer, a fullscreen effect layer, or the HUD?
  • Viewport. There could be multiple viewports for e.g. multiplayer splitscreen, for mirrors or portals in the scene, etc.
  • Viewport layer. Each viewport could have their own skybox layer, world layer, effects layer, HUD layer.
  • Translucency. Typically we want to sort opaque geometry and normal, additive, and subtractive translucent geometry into separate groups.
  • Depth sorting. We want to sort translucent geometry back-to-front for proper draw ordering and perhaps opaque geometry front-to-back to aid z-culling.
  • Material. We want to sort by material to minimize state settings (textures, shaders, constants). A material might have multiple passes.

An Example makes everything clear: lets see how to implement Alpha Blending with render queue — as we know, we have to draw the full opaque scene first, then we activate the alpha blending state and draw the transparent objects on the top of the scene

  • The transparent objects will have the translucency bits of the ID set to 11 (integer 3) and the normal objects will have these bits set to 00
  • When we sort commands (by the ids) and draw the render queue in sequence, all the normal objects will be draw first ….

Okay, this is a dumb example, but show the idea … (Encode your draw call in Commands, and let the ID decide the order of drawing)

The next level is to include Graphics Commands (like clear the screen) inside the ID, so we can sort them also.

The following image shows a more powerfull ID design:

(The command Bit should be the less significative bit — the one in the most right)
Here we also included a bit to tell if a Command is a graphical instruction (clear the screen) or is an object to be draw (like before)

With this layout we can implement for example shadow/reflection/post effects ….:
Example:

  • Pull the Render Target RT – A command with Command Bit Set
  • Draw Object X,Y,Z,W – same as before
  • Pull Render Target RT2 – A command with Command Bit Set
  • Draw all objects (some might be using the Render Traget RT as a texture — for shadow/reflection effect for example)
  • Do some post processing …. – COllection of Commands
  • Draw some text overlay
As you can see this technique is very powerfull and is commonly used in games.
If you are using C++ you can encode a function pointer in the ID and call it during the rendering (similar idea can be done in C#, JAVA ….)
The following part shows how to implement this idea (the first one, without the graphics instruction part …. — adding it is left to the readers as an exercise =P)
Used C#, but the idea is the same in all languages ….
This code is DUMMMY, just to show a concept ….

Render Queue Id Definition:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EngineTestes.RQueue
{
    public class RenderQueueId
    {
        public override string ToString()
        {
            return Convert.ToString(id, 2);
        }

        /*
         * 2 Fullscreen layer. Are we drawing to the game layer, a fullscreen effect layer, or the HUD?
           3 Viewport. There could be multiple viewports for e.g. multiplayer splitscreen, for mirrors or portals in the scene, etc.
           3 Viewport layer. Each viewport could have their own skybox layer, world layer, effects layer, HUD layer.
           2 Translucency. Typically we want to sort opaque geometry and normal, additive, and subtractive translucent geometry into separate groups.
           6 Extra
           24 Depth sorting. We want to sort translucent geometry back-to-front for proper draw ordering and perhaps opaque geometry front-to-back to aid z-culling.
           24 Material. We want to sort by material to minimize state settings (textures, shaders, constants). A material might have multiple passes.
         * =64
         * */
        long id;

        public readonly long fullscreenLayer;
        public readonly long viewPort;
        public readonly long viewportLayer;
        public readonly long translucency;
        public readonly long depthSorting;
        public readonly long materialid;
        public readonly long extra;
        bool flipMaterialWithSorting = false;

        public RenderQueueId(int fullscreenLayer, int viewPort, int viewportLayer, int translucency, int extra, int depthSorting, int material)
        {
            this.fullscreenLayer = fullscreenLayer;
            this.viewportLayer = viewportLayer;
            this.viewPort = viewPort;
            this.depthSorting = depthSorting;
            this.materialid = material;
            this.extra = extra;
        }

        public long CachedId
        {
            get
            {
                return id;
            }
        }

        //public long GetMaterialMask()
        //{
        //    if (flipMaterialWithSorting)
        //    {
        //        return (16777215L << 24);
        //    }
        //    else
        //    {
        //        return 16777215L;
        //    }

        //}

        public long GenerateId(bool flipMaterialWithSorting = false)
        {
            this.flipMaterialWithSorting = flipMaterialWithSorting;

            if (flipMaterialWithSorting)
            {
                id = fullscreenLayer << 62
                    | viewPort << 59
                    | viewportLayer << 56
                    | translucency << 54
                    | extra << 48
                    | materialid << 24 
                    | depthSorting 
                    ;
            }
            else
            {
                id = fullscreenLayer << 62
                    | viewPort << 59
                    | viewportLayer << 56
                    | translucency << 54
                    | extra << 48
                    | depthSorting << 34
                    | materialid 
                    ;                
            }
            return id;
        }        
    }
}

The render Queue Element (The Command — DUMMY)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EngineTestes.RQueue
{
    public abstract class RenderQueueElement
    {
        public RenderQueueId Id
        {
            get;
            private set;
        }

        public abstract void InitMaterial();
        public abstract void Draw();
        public abstract void EndMaterial();

    }

    public class RenderQueueElementComparer : IComparer
    {

        #region IComparer Members

        public int Compare(RenderQueueElement x, RenderQueueElement y)
        {
            if (x.Id.CachedId > y.Id.CachedId)
            {
                return 1;
            }
            else if (x.Id.CachedId == y.Id.CachedId)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
        #endregion
    }

}

The Render Queue Processing (DUMMY DUMMY DUMMY)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EngineTestes.RQueue
{
    public class RenderQueueProcessor
    {
        RenderQueueElementComparer renderQueueElementComparer = new RenderQueueElementComparer();
        List elements = new List();

        public void Process()
        {
            if (elements.Count == 0)
                return;

            elements.Sort(renderQueueElementComparer);
            RenderQueueElement last = elements[0];
            last.InitMaterial();
            last.Draw();

            for (int i = 1; i < elements.Count; i++)
            {
                var el = elements[i];
                if (el.Id.materialid != last.Id.materialid)
                {
                    last.EndMaterial();
                    el.InitMaterial();
                }
                el.Draw();
                last = el;
            }
            last.EndMaterial();

            elements.Clear();
        }
    }
}

Remember: Design the ID of the of the Render Queue the best way you can =P, this is the heart of the system =P

Reference:

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

, , ,

  1. #1 by look at more info on 16 de julho de 2017 - 11:50 am

    here are some hyperlinks to websites that we link to due to the fact we think they are worth visiting

  2. #2 by coreorf on 16 de julho de 2017 - 11:55 am

     東京都北区の女性(62)は目の病気で視野が狭く、見えても文字を拡大しなければならないため、PC使用時は読み上げソフトが頼みだ。 ASUSの分類も「2in1ノートパソコン」となっていてタブレットとは区別しています。 [url=http://www.office2016jpjp.net/officeprofessionalplus2010-c-2_18.html]microsoft excel 価格[/url]
    マイクロソフト、HoloLensの予約を12/2より開始/et_tomio/48957815.html。   (ここでは「誕生日」)③ 「誕生日」で検索したのでバースディカードなどが沢山表示されます。
    [url=http://www.serialkeys.org/microsoft-office/office-professional-plus-2013]office2013 メディア 購入[/url] 暫く放置してWindowsモジュールインストーラー/TrustedInstaller.exeが落ち着くのを待ってから電源/スリープ設定を元に戻して終了。  どちらのキー配列でも、ちゃんと使うことは可能。
    [url=http://softpcjpjp.com/?mode=cate&cbid=2087964&csid=0]Adobe Acrobat 11 pro[/url]
    ステッカーはWinXPのがくっついてたので、Windows7のをヤフオクで買って交換した。 メッシは今年1月、5度目となる男子年間最優秀選手に選ばれた。 [url=http://www.serialkeys.org/microsoft-office/office-professional-plus-2016]microsoft excel 価格[/url]
    推奨環境外では正常に動作しない場合があるため、推奨環境からご利用ください。  そんな基調講演で,Intelは何をアピールして,何を目指しているのが明らかになったのか。
    [url=http://www.office2016jpjp.net/]office2016 personal 価格[/url] ◆4:JetStream → Edgeが勝利続いてのテストもJavaScriptのベンチマークで、今度はApple製の「JetStream 1.1」を使用。   Windows.isoとなっているか確認。
    [url=http://softpcjpjp.com/?mode=cate&cbid=2087964&csid=0]Adobe Acrobat 11 pro[/url]

  3. #3 by Christian Louboutin Replica on 16 de julho de 2017 - 12:03 pm

    We have bought your brand out of bracelet a few instances. Every one is extremely adorable, done well, cannot tarnish and significant based on that definitely one you purchase as well as just who we bring this in order to.

  4. #4 by 2017 Christian Louboutin Outlet Online on 16 de julho de 2017 - 12:04 pm

    Our product is at such a very good cost I do not believed the grade is quite exceptional. It is perfect. Our mother will likely really like that regarding Xmas day after she starts gifts therefore looks like I spent even more, however pricing ended up being really very good!!

  5. #5 by Bat dong san Nha Viet on 16 de julho de 2017 - 12:21 pm

    Hmm is anyone else encountering problems with the pictures on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.

  6. #6 by patagonia jackets outlet on 16 de julho de 2017 - 12:49 pm

    After spending several hours online at last We have located an individual that seriously does know what they’re preaching about appreciate it a whole lot fantastic posting
    patagonia jackets outlet

  7. #7 by diy zip line on 16 de julho de 2017 - 12:59 pm

    730368 488914As a result you will call for ultra powerful online enterprise tips to maintain operating in acquiring into matters proper your incredible web-based function. MLM 524970

  8. #8 by http://www.horne.co.uk/store.aspx on 16 de julho de 2017 - 1:04 pm

    Our system had been with such a ideal amount I never ever consideration ones top quality would be and so excellent. It’s awesome. This particular mother might love they upon Xmas early morning where she opens gifts therefore appearances as though I invested far more, however rates was just great!!

  9. #9 by uk red bottoms shoes on 16 de julho de 2017 - 1:05 pm

    I have purchased this brand name concerning bracelet a few period. Every single a person is extremely adorable, created very well, does not tarnish and/or significant based on which kind of an individual you buy plus which a person present that it at.

  10. #10 by have a peek at this web-site on 16 de julho de 2017 - 1:05 pm

    the time to read or stop by the subject material or internet sites we’ve linked to beneath the

  11. #11 by Career And Jobs on 16 de julho de 2017 - 1:14 pm

    I really like your writing style, excellent information, appreciate it for posting :D. “Silence is more musical than any song.” by Christina G. Rossetti.

  12. #12 by hermes bracelet replica on 16 de julho de 2017 - 1:28 pm

    We have purchased it brand name regarding bracelet some times. Every single one is ultra attractive, has made very well, cannot tarnish and/or meaningful depending on which one you buy and also that people render this at.

  13. #13 by cheap nike roshe run on 16 de julho de 2017 - 1:29 pm

    That goods was in such a ideal rate I do not attention the actual top quality would be and so excellent. It’s perfect. It mother will prefer that it at Christmas time early morning once she opens up present therefore appearance like I devoted a lot more, however rates is only great!!

  14. #14 by togel online on 16 de julho de 2017 - 1:46 pm

    Hey there. I discovered your site by the use of Google while searching for a related subject, your site came up. It seems good. I have bookmarked it in my google bookmarks to come back then.

  15. #15 by navigate to this site on 16 de julho de 2017 - 1:50 pm

    we came across a cool internet site that you simply could love. Take a search in case you want

  16. #16 by Nha ban TPHCM on 16 de julho de 2017 - 2:15 pm

    Good info. Lucky me I reach on your website by accident, I bookmarked it.

  17. #17 by Bat dong san Nha Viet on 16 de julho de 2017 - 2:29 pm

    You got a very fantastic website, Gladiolus I found it through yahoo.

  18. #18 by Bat dong san TPHCM on 16 de julho de 2017 - 2:29 pm

    Very nice article and right to the point. I am not sure if this is truly the best place to ask but do you guys have any thoughts on where to hire some professional writers? Thanks in advance :)

  19. #19 by buy chanel bags on 16 de julho de 2017 - 2:36 pm

    Good thorough ideas here.Id like to recommend checking out things like cheese. What are your thoughts?
    buy chanel bags

  20. #20 by Travis Zajac Jersey on 16 de julho de 2017 - 3:55 pm

    Preconceived notions about what selling is or isnt and what people may or may not be thinking all adds to the sales reluctance mix.This is today’s entry. Almost no one on Twitter cares about your business opportunity.I can envision another warehouse and another and eventually we would own all the junk in town. They thought it was cool that I cared about them to do something to brighten the mood of the class.

  21. #21 by Visit Website on 16 de julho de 2017 - 4:23 pm

    The facts mentioned within the write-up are a number of the very best obtainable

  22. #22 by Poradnia Zdrowia Psychicznego Gorzow Wlkp Walczaka on 16 de julho de 2017 - 4:25 pm

    I really appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thank you again!

  23. #23 by cartier juste un clou replica on 16 de julho de 2017 - 4:57 pm

    We have purchased it brand name regarding bracelet various circumstances. Every a person is super attractive, has made well, cannot tarnish as well as meaningful depending on which an individual you buy then which people award they and.

  24. #24 by a knockout post on 16 de julho de 2017 - 5:07 pm

    Here are a number of the internet sites we suggest for our visitors

  25. #25 by http://www.sundbirsta.com/hermesbelts.asp on 16 de julho de 2017 - 5:21 pm

    That device ended up being with that great pricing I by no means thought on excellence is quite exceptional. It’s stunning. Your mom is going to love it concerning Xmas early morning once she starts present and it appearance as though I invested a lot more, still cost ended up being simply awesome!!

  26. #26 by Wholesale Jerseys China on 16 de julho de 2017 - 5:26 pm

    This can effortlessly be carried out all at 1 time. It is reliable to listed to the information the organization gives., giving you the option to back or lay bets.Knowing Tennis Enhances Your LifeKnowing Tennis Enhances Your LifeFebruary 12, 2013 | Author: Davis Pete | Posted in Recreation and SportsKnowing tennis can commence at various ages. Keeping it straight and turned out, pull forward slowly, and when you can’t go any further, hold your lower abdominals and let your upper torso bend over.
    Wholesale Jerseys China

  27. #27 by click this on 16 de julho de 2017 - 5:28 pm

    that would be the finish of this write-up. Right here you will uncover some web pages that we feel youll appreciate, just click the links over

  28. #28 by Cheap China Jerseys on 16 de julho de 2017 - 5:39 pm

    You need to ensure that you’re promoting your do business from home organization wherever possible. Amateur at Pumpkin Ridge, won by Tiger Woods.Free pre-foreclosure listings are extremely hard to find. Apply a coating of federal government and put your hands under the warmth lamp regarding 1 to 2 min’s until your hands become dry. Your weight will effect the stretch, breathe deeply a few times, and then come back up to a straight position.

  29. #29 by Tyson Covone on 16 de julho de 2017 - 5:43 pm

    It’s a pity you don’t have a donate button! I’d certainly donate to this superb blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will talk about this site with my Facebook group. Talk soon!

  30. #30 by Cheap Jerseys From China on 16 de julho de 2017 - 5:54 pm

    About an hour later the price had indeed come in and was now closer to the bookmakers’ prices at 4.”Everybody Wants To Follow This Method To Find Great Automobile Insurance RatesEverybody Wants To Follow This Method To Find Great Automobile Insurance RatesJune 5, 2012 | Author: Sally Rowe | Posted in InsuranceVehicle insurance is dictated in a lot each state and can be a necessity to drive legally. They also ask help from their friends and relatives who have hired technicians before. To keep to my promise of staying unbiased you should take into consideration the competitive player, who easily uses more than 80% mental capacity than physical. In 1994, Woods was a national star, and he jointly entered for the only time the SCGA Amateur and State Amateur.
    Cheap Jerseys From China

  31. #31 by hermes h belt replica on 16 de julho de 2017 - 5:56 pm

    The product was at such a very good expense I did not idea the actual excellence is hence exceptional. It’s perfect. Your mother will appreciate that it regarding Holiday morning whenever she opens up present therefore seems as though I invested significantly more, then again rates had been simply great!!

  32. #32 by why not try these out on 16 de julho de 2017 - 6:02 pm

    that will be the finish of this article. Right here you will locate some web-sites that we feel youll enjoy, just click the hyperlinks over

  33. #33 by Fashion on 16 de julho de 2017 - 6:03 pm

    I got what you intend, thanks for putting up.Woh I am pleased to find this website through google. “You must pray that the way be long, full of adventures and experiences.” by Constantine Peter Cavafy.

  34. #34 by red bottom shoes replica on 16 de julho de 2017 - 6:09 pm

    This goods had been with such a great price tag I never believed on premium is so excellent. It’s pretty. This one mom can prefer they on Christmas day after she starts gifts therefore seems as though I spent far more, however rates is only ideal!!

  35. #35 by http://www.bazaaralosra.com/wp-black.php on 16 de julho de 2017 - 7:01 pm

    This goods ended up being in such a great price I never ever thought some sort of grade is quite exceptional. It’s striking. The mama does like this regarding Holiday early morning where she opens up gift and it styles as though I spent alot more, and yet prices ended up being only awesome!!

  36. #36 by Moncler Sale on 16 de julho de 2017 - 7:01 pm

    I have purchased our brand regarding bracelet some days. Every one is super attractive, made very well, cannot tarnish additionally meaningful based on that single you purchase furthermore who one promote that it to.

  37. #37 by see page on 16 de julho de 2017 - 7:15 pm

    always a significant fan of linking to bloggers that I love but dont get lots of link really like from

  38. #38 by Fashion on 16 de julho de 2017 - 7:27 pm

    Hello, i think that i saw you visited my blog so i came to “return the favor”.I am trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!!

  39. #39 by timberland outlet online on 16 de julho de 2017 - 8:09 pm

    Simply want to say your article is as amazing.The clearness in your post is just spectacular and i can assume youre an expert on this subject.Fine with your permission allow me to grab your feed to keep up to date with forthcoming post.Thanks a million and please carry on the enjoyable work
    timberland outlet online

  40. #40 by http://www.paradis-haver.dk/laboutin.asp on 16 de julho de 2017 - 8:10 pm

    This supplement had been in that awesome price tag I never ever thought all excellence would be quite excellent. It’s striking. Our mama will certainly really like that on top of Christmas morning where she opens gift and it styles as though I invested far more, although prices is really ideal!!

  41. #41 by Wholesale NFL Jerseys Free Shipping on 16 de julho de 2017 - 8:43 pm

    Happy Playing and Learning!Want to find out more about tennis lessons, then visit Davis Pete’s site on how to select a qualified and experienced tennis coach for your needs.What you ought to know about UV gels, nail gels and also permanent nails?Banducci Staelens   Submitted 2014-03-25 13:32:33 Most women want to wear permanent nails as it gives a gorgeous and shimmering look in which catches face. Coal, natural gas and other clean ways to produce energy will not be around for forever. Likewise apply for a third time and allow the UV gels to dried out for Four to six minutes because at this time it can take longer time.You might discover tennis and be a mentor to others.
    Wholesale NFL Jerseys Free Shipping

  42. #42 by over at this website on 16 de julho de 2017 - 10:23 pm

    the time to study or go to the content material or web-sites we have linked to beneath the

  43. #43 by http://www.alvtank.se/redbottom.asp on 16 de julho de 2017 - 10:32 pm

    I have purchased our brand regarding bracelet a number of instances. Every one is super cute, made well, cannot tarnish and also significant depending on that a person you buy and/or whom your give they towards.

  44. #44 by Wholesale Jerseys China on 16 de julho de 2017 - 10:40 pm

    The prevalence of golf carts is a blessing and a curse, he said. The goal is to save money and we encourage you to try and save as much as possible, but you also want the best coverage as described in step one. This showed the impact that the introduction of REPs had to the consumption of power and the preferences Texans had. I would suggest choosing some sort of coverage for the vehicle if it has a market value over $4000. The nearby agencies in most instances represent 5-10 carriers every obtaining you a great deal of quotes in no time at all!Now that you have found the best site for cheap car insurance, it is time to fill out the quote form.
    Wholesale Jerseys China

  45. #45 by arcteryx for sale on 16 de julho de 2017 - 10:57 pm

    […] had a comment on my post Never Pray For Patience, and I wanted to share her comment and my response: I watched Evan Almighty, and will not […]
    arcteryx for sale

(não será publicado)