Introduction
Unity 2018 is finally here and with it comes several important new additions to your favorite toolset. The Job System, Progressive Lightmapper, and of course Scriptable Render Pipelines (SRP), just to name a few.
Not only is Unity offering two maintained Scriptable Render Pipelines through their Package Manager and public GitHub, but they’ve also made available the means of developing completely custom-tailored Pipelines yourself. Since I specialize in Rendering here at Schell Games, that last bit is really exciting. In this post, I’d like to talk about what SRP is and why this feature is going to be important for you and your Unity games moving forward.
What Are SRP, Exactly?
Scriptable Render Pipelines are the new way to implement custom rendering in Unity. It is a collection of C# scripts and Shaders that gives you, a Developer, unprecedented control of how your game gets rendered. But moreover, SRP is about choice. When you build a specific type of game that has specific needs, you will inevitably make important choices. When it comes to rendering, these choices, more often than not, have the largest impact on performance.
For instance, a cinematic, photo-realistic, Third Person Action game targeting PC & Console at 30 frames per second (FPS) has a very different set of constraints than a Mobile VR game that needs to run at a solid 60 FPS on a phone. This is where SRP truly shines and where the built-in Renderer provided by Unity has suffered in the past.
In a way, SRP is a reboot of how we can think of rendering when it comes to Unity.
If you are targeting hardware with limited resources, you can significantly reduce overhead caused by a one-size-fits-all renderer and focus on the bits that are the most important to your game. If you don’t rely heavily on precise per-pixel lighting effects, you don’t need to pay the performance cost for setting them up in the first place. This frees up resources to be spent on making your game look and play the best that it can!
Building a Scriptable Render Pipeline: At a Glance
During the Public Beta phase of 2018.1, I decided to write my own SRP from scratch as an exercise to gain a better understanding of what this new system can do. I’ve assembled some notes on what it takes and how it works.
The Render Pipeline Asset
Everything starts here. You build an Asset that serves as a hub for your Renderer’s global settings. This Asset is what gets assigned to your Project’s Graphics Settings in order to enable the SRP feature.
The Render Loop
The Render Pipeline Asset will generate a Render Pipeline instance when the Unity Engine requests it. This is where your Render loop is executed. The Render loop receives a Scriptable Render Context, and all of the Cameras that will render this frame. In the Editor, this introduces some interesting cases for things like Inspector Previews and Thumbnails because they all run on the same loop!
Scriptable Render Context
The context object is where you’ll build up your render commands. This is where you will issue your commands to Draw Renderers, Shadows, and executing Graphics Commands.
Culling
You’ll only want to render things the Player will be able to see, and Unity makes this easy with CullResults. You can specify custom Scriptable Culling Parameters, or simply grab them for the currently rendering Camera. This takes any Level of Detail (LOD) and Occlusion Culling data that may have been set up into account as well. The resulting data will be a collection of visible Lights and Renderers for you to do with what you please.
Drawing
Rendering is still done in Passes, just like it has always been. But in the case of SRP, you can build as many custom Passes as you want. This allows for further filtering of your visible Renderers to be drawn as you see fit. There are other means of filtering this data as well, which exist across the Material and Renderer levels. This provides a robust API for customizing your Renderer, and has some added performance benefits when organizing your loop. For instance, organizing draw order by Shader complexity can save precious milliseconds on Tile-Deferred GPUs.
Putting It Together
All together, a given SRP can be as simple or as complex as your target deems necessary. And this flexibility is the true power of the feature. You can organize your render loop to be precisely what is needed and optimized for your target hardware without concern for any extra bloat that the Legacy Renderer provides. This combined with Unity’s new SRP Batcher gives you performance by default, which is always a plus.
The Benefits of SRP Moving Forward
As I’ve mentioned above, SRP is about choice. When you get to the point in your project that you’re making decisions about Rendering, you probably know what type of game you’re going to make. Here are some things to consider:
- Target Hardware. This is going to dictate what you’re ultimately capable of doing, so it should be the first thing to be considered. Bleeding-edge Graphics techniques require more resources and often Graphics hardware features that aren’t widely supported.
- Game Type. A Mobile Match 3 game probably doesn’t need a Renderer capable of unlimited real-time per-pixel Lights. Dialing back the Graphics will not only provide a smooth experience, but will prolong battery life on your Players’ Mobile devices!
- Time and Effort. The more complex the Renderer, the more time and effort you will spend building and maintaining its features. Be sure to weigh this against what the feature will provide your Content Creators, even if you’re a single person team.
Previously, before the introduction of SRP, extending Unity’s Renderer to achieve custom effects was often limited, difficult, and sometimes downright infuriating. If a particular hook into the Renderer wasn’t readily available, it could result in hours of work or worse if a bug was blocking you. Since SRP minimizes the core ‘black box’ rendering code, the margin for error where show-stopping bugs can ruin your day has been greatly reduced. This is an invaluable benefit.
Finally, the addition of ShaderGraph, which is only supported through SRP is going to be life-changing for Content Creators. Artists and Designers will be able to author custom Shaders without having to learn how to program. This puts the power in the hands of the ones who can do the most with it. Your Content Teams not only want or need this, they deserve it.
How Do I Get Started?
Getting started is easy if you just want to pull in one of Unity’s provided SRP, and there are a couple avenues you can go right from within Unity.
Project Templates
If you’re creating a project from scratch, you can start with one of the new Project Templates from your Create Project Menu. It’s important to note here that these Templates may not necessarily bring in the ‘latest and greatest’ version of the SRP Package.
The Package Manager
If you’re looking to upgrade an existing project and want to test if SRP is right for you, just open up the Package Manager and install the SRP package of your choice.
Next, you’ll need to generate a ScriptableRenderPipeline asset in your Project, and assign it to the appropriate slot at the top of your Graphics Settings Window, et voila! Now you’re cooking with SRP!
I should mention here that at this point, many of the Shaders used by Unity previously will simply stop working. No worries there if you’re upgrading your project, there is a Material Converter to port built-in Materials to SRP. This won’t work with any custom or third-party and Asset Store packages by default, though, so some work may still be ahead of you.
Straight From the Source
If you’re at all like me, you’re going to want more control than Package Manager provides. In which case, get the direct source of SRP from the GitHub Project. In this case, you’ll need to clone the Repo into your Project’s directory and likely checkout a branch that corresponds with the version of Unity you’re on. At the point that I’m writing this, I am personally synced to the 2018.2 Branch.
Closing Thoughts
I am obviously very excited about the future of rendering in Unity and what the community will be able to achieve with this new and powerful tool. I am looking forward to using it to empower our Artists and help create amazing visual content here at Schell Games!
If you’re curious about the inner workings of SRP, I have a post on my personal Blog where I’ve kept running notes while developing one from scratch, complete with source code!