Wednesday, February 1, 2017

Stingray Renderer Walkthrough #1: Overview

Stingray Renderer Walkthrough #1: Overview

Introduction

When we started writing Bitsquid back in mid 2009 all platforms we intended to run on were already multi-core architectures. This and the fact that we had some prior experience trying to get our last engine to run efficiently on the PS3 answered the question how not to architecture an efficient renderer that scales to many cores. We knew we needed more than functional parallelism, we wanted data-parallelism.

To solve that we divide the CPU view of a rendered frame into three stages:

  1. Culling - Filter out visible renderable objects with respect to a camera from a potentially huge set of different type of objects (meshes, particle systems, lights, etc).
  2. Render - Iterate over the filtered result from Culling and “record” an intermediate representation of draw calls/state switches to a command buffer.
  3. Dispatch - Take result from Render and translate that into actual render API calls (D3D, OGL, Metal, GNM, etc).

As you can see each stage pipes its result into the next. Rendering is typically very simple in that sense; we tend to have a one way flow of our data: [[user input or time affects state, state propagates into changes of the renderable objects (transforms, shader constants, etc), figure out what need to be rendered, iterate over that and finally generate render API calls. Rinse & Repeat :]]

If we ignore the problem of ordering the final API calls in the rendering backend it’s fairly easy to see how we can achieve data parallelism in this scenario. Just fork at each stage splitting the workload into a n-chunks (where n is however many worker threads you can throw at it). When all workers are done for a stage take the result and pipe into the next stage.

In essence this is how all rendering in Stingray works. Obviously I’ve glanced over some rather important and challenging details but as you will see they are not too hard to solve if you have good control over your data flows and are picky about when mutation of the data happens.

Design Philosophies & Concepts

The rendering code in Stingray tends to be heavily influenced by Data Oriented Programming principles. When designing new systems our biggest efforts usually goes into structuring our data efficiently and thinking about its flow through the systems, more so than writing the actual code that transforms the data from one form to another.

To achieve data-parallelism throughout the rendering code the first thing to realize is that we have to be very picky about when mutation of the renderable objects happens. Multiple worker threads will run over our objects and its not unlikely that more than one thread visits the same object at the same time, hence we must not mutate the state of our objects in its render function. Therefore all of our render() functions are const.

To further guard ourselves from the outer world (i.e gameplay, physics, etc) the renderer operates in complete isolation from the game logics. It has its own representation of the data it needs, and only the data relevant for rendering. While the gameplay logics usually wants to reason about high-level concepts such as game entities (which basically groups a number of meshes, particle systems, lights, etc together), we on the rendering side don’t really care about that. We are much more interested in just having an array of all renderable objects in a game world, in a memory layout that makes it efficient to access.

Another nice thing with decoupling the representation of the renderable objects from the game objects is that it allows us to run simulation in parallel with rendering (functional parallelism). So while simulation is updating frame n the renderer is processing frame n-1. Some of you might argue that overlaying rendering on top of simulation doesn’t give any performance improvements if the work in all systems is nicely parallelized. In reality though this isn’t really the case. We still have systems that don’t go wide, or have certain sections where they need to do synchronous processing (last generation graphics APIs: e.g DX11, OpenGL are good examples). This creates bubbles in the frame slowing us down.

By overlaying simulation and rendering we get a form of bubble filling among the worker threads which in most cases gives a big enough speed improvement to justify the added complexity that comes from this architecture. More specifically:

  1. Double buffering of state - since the simulation might mutate the state of an object for frame n at the same time as the renderer is processing frame n-1 any mutable state needs to be double buffered.
  2. Life scope tracking of immutable data - while immutable/read only state such as static vertex and index buffers are safe to read by both simulation and renderer we still need to be careful not pulling the rug under the renderers feet by freeing anything still being in use by the renderer.

Here’s a conceptual graph showing the benefits of overlaying simulation and rendering:

So basically what we got here is two “controller threads”: simulation and render both offloading work to the worker threads. In the case that a controller thread is blocked waiting for some work to finish it will assist the worker threads striving to never sit idle. One thing to note is that to prevent frames from stacking up, we never allow the simulation thread to run more than one frame ahead of the render thread.

As a comparison here’s the same workload with simulation and rendering running in sequence.

As you can see we get significantly more idle time (bubbles) on the worker threads due to certain parts of both the simulation and rendering not being able to go wide.

Next up

I think this pretty much covers the high level view of the core rendering architecture in Stingray. Now lets go into some more detail.

Since Andreas Asplund recently covered both how we handle propagation of state from simulation to the renderer (we call this “State reflection” in Stingray): http://bitsquid.blogspot.se/2016/09/state-reflection.html as well as how our view frustum culling system(s) works: http://bitsquid.blogspot.se/2016/10/the-implementation-of-frustum-culling.html I won’t be covering that in this series.

Instead I will jump straight into how creating and destroying GPU resources works, and from there go through all the building blocks needed to implement the second stage Render mentioned above.

24 comments:

  1. Good one you explain. all building and civil related work to must use for the pre engineering process is very well done to introduce. Majorly access material is boom lift rental for access and tools used for core work. Thanks for the opportunity. and Thanks for post and creadited to Sendhamarai Engineering.

    ReplyDelete
  2. Wow, Great information and this is very useful for us.

    Aluminium scaffolding hire

    ReplyDelete
  3. Our sap scm mentors are sap production network the board (sap scm) ensured specialists and experienced working experts with hands on ongoing numerous SAP SCM ventures information. We have planned our sap scm course substance and prospectus dependent on understudies necessity to accomplish everybody's profession objective. In our sap scm training program, you will learn request the board, Supply Network Planning Heuristic, SNP Run Using Capable, SNP Configuration, Master Data and Transaction Data in SNP, Demand Planning, Interactive Planning, Safety Stock Planning, sap scm continuous venture and sap store network the board (sap scm) placement training.

    For More Info:- SAP SCM Course in Gurgaon

    ReplyDelete
  4. Thanks for the info. Please keep sharing the good stuff. I'm a first time visitor of your website and I love the information people share here. I'll bookmark your site and keep visit regularly. I was looking for Best Hindi Shayari Collection and I found this..

    Best Hindi Shayari Collection Best Hindi Shayari Collection
    Love Shayari Love Shayari
    Sad Shayari Sad Shayari
    Romantic Shayari Romantic Shayari
    Urdu Shayari Urdu Shayari
    Friendship Shayari Friendship Shayari
    New Shayari New Shayari
    Motivational Shayari Motivational Shayari
    Best Shayari Best Shayari
    Shayari Images to download Shayari Images to download

    ReplyDelete
  5. Very nice blog post, it is informative and i subscribed for all its future post. there are some useful links, i think i must share here:
    www.mcafee.com/activate
    mcafee.com/activate

    ReplyDelete
  6. What a resourceful piece of information thanks you for sharing. When it becomes hard to manage your resources, you can check this. I am very happy to read your post. I'm also sharing my nice stuff to you guys please go through it and take a review.

    outsource digital marketing services
    outsource website development
    top digital marketing agencies in india
    virtual assistant websites
    web design and development india
    web design and development india

    ReplyDelete
  7. Detroit Metropolitan Wayne County Airport- Choose Detroit Metropolitan Wayne County Airport to start your journey. Enjoy discounts and deals, book with us.

    ReplyDelete
  8. You can get more offers through Lufthansa Airlines Phone Number , which mainly looks for any details that you usually really like your airline. Want to experience with Lufthansa Airlines Phone Number is your personal travel agent who books your plan.

    ReplyDelete
  9. Get easy and quick reservations for American Airlines Cancellation . So there is no need to wait in lines now when all your booking can be done on the call of our travel expert.

    ReplyDelete
  10. Book your flight tickets effortlessly and also check-in through the website by visiting our “ Qatar Airways Contact Number ” website. You can contact us anytime. Our experts are always ready to assist you anytime.

    ReplyDelete
  11. If you wish to cancel your ticket that was purchased from American Airlines, you can contact American Airlines Cancellation through an online refund request by communicating customer support.

    ReplyDelete
  12. You just have to call Southwest Airlines Customer Service for flight booking our services. Get ready to have a joyful and comfortable journey with us.

    ReplyDelete
  13. Etihad Airways helps you to get luxurious facilities in low-cost. You can avail of all amazing facilities by connecting with us. Just dial Etihad Airways Contact Number and resolve all your travel-related issues here.

    ReplyDelete
  14. Feel free to get in touch with us via British Airways Toll Free Number to get the great deals and offers on your flight tickets and get all your problems and questions resolved in no time. We have a team of experts who are well-trained and always ready with support of your queries.

    ReplyDelete
  15. Plan a journey and contact our travel specialists on United Airlines Phone Number. helpline. With us, nothing is unattainable for our esteemed customers. United Phone Number is our toll-free service which takes care of all your booking needs when you want to move across to your destination for business or leisure purposes.

    ReplyDelete
  16. Call Etihad Airways Phone Number for your cheapest airfares across destinations. With a journey in your mind, Etihad Phone Number helpdesk is your best help.

    ReplyDelete
  17. If you are looking for reliable and affordable services then visit Spirit Airlines Contact Number . Here you can get best deals and offers on your flight tickets. So, go now book your flight tickets at low-cost. Now you can fulfil your vacation dreams in your budget.

    ReplyDelete