<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Michael Short on Medium]]></title>
        <description><![CDATA[Stories by Michael Short on Medium]]></description>
        <link>https://medium.com/@shorty?source=rss-e5c131d8c2df------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cOVvj3E3rQ9BL63y</url>
            <title>Stories by Michael Short on Medium</title>
            <link>https://medium.com/@shorty?source=rss-e5c131d8c2df------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 12:16:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shorty/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Jenkins Pipelines]]></title>
            <link>https://medium.com/spaceapetech/jenkins-pipelines-25ebc1980600?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/25ebc1980600</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[build]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[pipeline]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Wed, 19 Jun 2019 10:59:16 GMT</pubDate>
            <atom:updated>2019-06-19T10:59:16.943Z</atom:updated>
            <content:encoded><![CDATA[<h3>Jenkins Pipelines Shared Libraries</h3><h3>Introduction</h3><p>In the past I’ve spoken about our shared libraries at Space Ape (<a href="https://medium.com/spaceapetech/sharing-unity-code-the-journey-bd81a0016e4a">Part 1</a> &amp; <a href="https://medium.com/spaceapetech/shared-unity-code-the-implementation-a5f2b94f2173">Part 2</a>). TL/DR, we have a LOT of shared libraries, and they are used across all of our games, from our live games like <a href="http://www.transformersearthwars.com/">Transformers: Earth Wars</a> and <a href="https://fastlanertr.com/">Fastlane</a> through to our unreleased prototype projects.</p><h3>Freestyle Jobs</h3><p>At Space Ape we use <a href="https://jenkins.io/">Jenkins</a> to manage and release builds for everything (client, server, Devops and shared libraries). All of our shared libraries are built and released using traditional Jenkins Freestyle Jobs. However we came to the point where the use of Freestyle Jobs was giving us some friction.</p><p>For those that are unfamiliar, Freestyle Jobs are the traditional, predefined method of creating builds in Jenkins. Jobs are created by adding a set of steps. Jenkins then runs through each of these steps sequentially. Steps must be supported by Jenkins itself, or via a Jenkins plug-in. A lot of the time we tend to just add a shell stage, and then write shell script in job itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b_F7SQAEnGpGqM0ewNq1Lw.png" /><figcaption>Jenkins Freestyle Job setup</figcaption></figure><p>Each of our shared library Freestyle Jobs are independent of one another. So say you want to roll out a new feature in the build pipeline, we would have to go through 60+ jobs! Or you need to build an older version of a library, but the job has since changed and doesn’t support the old build setup. Then there’s also the fact that people change each of these jobs independently, to the point where they are doing vastly different things. Some jobs would add release tags to Git, others would build documentation etc. We needed to consolidate all of this, and move it into one easy to manage place.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IwRwuzdv-TVIDb4yLHa8Ig.jpeg" /></figure><h3>Declarative vs Scripted</h3><p>Before we begin there are two types of pipeline supported in Jenkins. Declarative and scripted.</p><p>Declarative pipelines are ideal for straight forward build systems as they have a very simple and pre-defined structure. Traditionally people wrote scripted pipelines. Scripted pipelines are written in Groovy. Scripted pipelines give you a massive amount of control and power, you can define and manipulate the entire build flow via code. So they are ideal for more complex build systems. At Space Ape we use scripted pipelines.</p><h3>First Attempt Jenkins Pipeline</h3><p>The first thing we had to do was look through all of our existing Freestyle Jobs and define all the common steps that we would need. Once we had this we started with one of our simplest libraries and started writing a Jenkins pipeline for it.</p><p>Pipelines are written in <a href="http://www.groovy-lang.org/">Groovy</a>. Start by placing a file at the root of your repository called “Jenkinsfile” — no file extension. When you build your project, Jenkins will grab this file and execute the code inside it.</p><p>The build is executed on an agent — a built node. It’s super simple to define an agent in a pipeline, all you do is provide a label, then all code within the curly braces will be executed on that node.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8059793e554684d4d5037330686816c0/href">https://medium.com/media/8059793e554684d4d5037330686816c0/href</a></iframe><p>Jenkins pipelines are comprised of stages. A stage is simply a block of work. It makes sense to group related work into stages, here is a diagram of our shared code stages from Jenkins:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ps7r7vAAJ1oSLUZAefa4Xw.png" /><figcaption>Space Ape’s shared library Jenkins Pipeline</figcaption></figure><p>All the stages within a node will be executed in sequence. Again, defining a stage is really simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5df3f494fc98c797d8756a1b338385e/href">https://medium.com/media/a5df3f494fc98c797d8756a1b338385e/href</a></iframe><p>Out of the box, and on top of Groovy, Jenkins adds a number of basic <a href="https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/">helper functions</a>. There’s also a <a href="https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/">Pipelines Utility plug-in</a> which contains a lot of useful functions, e.g.</p><ul><li>read/writeJson()</li><li>un/zip()</li><li>pwd()</li><li>sh()</li></ul><p>Once we defined the first pipeline, we needed to start porting our other shared libraries over to use it. Our first instinct was to put the Jenkinsfile in a separate repository and then check it out for each project, but then we came across Shared Libraries.</p><h3>Adding a New Pipeline Job</h3><p>In Jenkins Blue Ocean select “New Pipeline”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*szvwSJh96pRHUNqlhWhTLA.png" /><figcaption>Select “New Pipeline”</figcaption></figure><p>Now select where your hosting service</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*1XkomULDJen9c3QD9pk-bA.png" /><figcaption>Hosting services</figcaption></figure><p>And the organisation within that hosting service</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*uCokozWOgJRL4mXgRUnT3w.png" /><figcaption>Organisations</figcaption></figure><p>And finally select the repository that holds the project you want to add</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*QQOgnpQCWpEsiKob0CCJPw.png" /><figcaption>Select your repo</figcaption></figure><p>Jenkins will then look through each of your branches and find the Jenkinsfiles within those branches. It also integrates really nicely with pull requests. Jenkins will automatically pick up new pull requests and build them for you, it then reports the status of the build back to your SCM system — we use GitHub and the result is displayed in the pull request itself!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BDfSUz6Hgo8cV9W-THQLdA.png" /><figcaption>Pull requests in Blue Ocean</figcaption></figure><p>On top of this there is Blue Ocean also offers a beautiful UI for your Unit Tests</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kulPu_lC8yCDxdoCLJnBtQ.png" /><figcaption>Unit Test viewer in Blue Ocean</figcaption></figure><h3>Shared Library Structure</h3><p><a href="https://jenkins.io/doc/book/pipeline/shared-libraries/">Jenkins Shared Libraries</a> allow you to define and share pipeline code between projects. This is exactly what we needed. To create a shared library, create a new repository with the following structure:</p><pre>(root)<br>+- src                     # Groovy source files<br>|   +- org<br>|       +- foo<br>|           +- Bar.groovy  # for org.foo.Bar class<br>+- vars<br>|   +- foo.groovy          # for global &#39;foo&#39; variable<br>+- resources               # resource files<br>|   +- org<br>|       +- foo<br>|           +- bar.json    # static helper data for org.foo.Bar</pre><ul><li>vars — this is where you put all of your global functions, the ones you will call from your project’s Jenkinsfile. For us, this is where the vast majority of our code lives</li><li>src — This is a regular Java-style source code directory. All source code and classes in here can be imported into your pipeline using an import statement.</li><li>resources — Any supporting resources for your library will go here. As an example, we build documentation for all of our shared libraries. This documentation is then uploaded to S3. We also build docsets for <a href="https://blog.kapeli.com/dash-for-ios-android-windows-or-linux">Dash</a> and <a href="https://velocity.silverlakesoftware.com/">Velocity</a>. The template files used to build the documentation lives here.</li></ul><h3>Adding Shared Libraries to Jenkins</h3><p>To add your shared library to Jenkins, head to Manage Jenkins-&gt;Configure System and look for “Global Pipeline Libraries”. Here you can give your shared library a name (which will be used in future to import your library into your Jenkinsfiles), a default version (for us this is “master”) and also let Jenkins know where your shared library lives.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9EARMaYqF12pXziNCDx-rg.png" /></figure><h3>Using Shared Libraries</h3><p>It’s really simple to import a shared library into your project’s Jenkinsfile.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5dd81be461566dbe6cab64812959a3aa/href">https://medium.com/media/5dd81be461566dbe6cab64812959a3aa/href</a></iframe><p>Note the underscore at the end is not a typo. It imports everything from the library into your Jenkinsfile. Now you can use the code in your shared library as though it were in your project. If you need to import multiple libraries into your pipeline you simply provide the Library function with a list of libraries to import</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/acb99c8277e80657c4e601ee5e567123/href">https://medium.com/media/acb99c8277e80657c4e601ee5e567123/href</a></iframe><h3>Moving Code Into a Shared Library</h3><p>People shouldn’t have to spend hours setting up a new build in Jenkins. Therefore we decided to put almost all of our build code into the shared pipeline. Here’s an example Jenkinsfile from one of our projects:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28df4cff42864b9f87730245dee02f53/href">https://medium.com/media/28df4cff42864b9f87730245dee02f53/href</a></iframe><p>sharedCodePipeline is a global function that lives in our shared library. This function takes a Map of parameters and then creates the build pipeline for you. We have kept the pipeline as flexible as possible, if a project requires it, stages can be replaced or injected and additional parameters can be easily added.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da13d75ef0de4370cbb6324c14017ac2/href">https://medium.com/media/da13d75ef0de4370cbb6324c14017ac2/href</a></iframe><p>You might notice we have a number of functions that we call within our pipeline. As a quick example, this is how we install packages from Nuget using the installNugetPackages function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e912fa74430dcca0a1fa01c8b86cafe/href">https://medium.com/media/2e912fa74430dcca0a1fa01c8b86cafe/href</a></iframe><p>As I mentioned above, we defined our stages. 99% of the time our shared libraries will use the predefined structure. But we also allow people to override stages very easily. Say we need to checkout an additional repository at the start of our build</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a645e77d6853309227b33395117c169c/href">https://medium.com/media/a645e77d6853309227b33395117c169c/href</a></iframe><h3>Summary</h3><p>We’re now in a place where its really easy to add new builds, add new features to our build pipeline, fix bugs in the build pipeline etc. On top of that we’ve managed to consolidate all of our steps, so all of our projects run unit tests, build documentation, run library dependency checks etc. The Jenkins Pipeline learning curve is steep, and development and debugging can be slow as you need to kick off a build, wait for it to fail, fix the issue and then start again. It can be frustrating but once your pipeline is up and running it’s so much more flexible and useable than Freestyle Jobs.</p><p>If you have any questions or need any help, hit me up on <a href="https://twitter.com/mjshorty">Twitter</a> as documentation and knowledge on Jenkins Pipelines can still be quite hard to come across online.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25ebc1980600" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/jenkins-pipelines-25ebc1980600">Jenkins Pipelines</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Attaining Maximum Performance with Automated Tests]]></title>
            <link>https://medium.com/spaceapetech/attaining-maximum-performance-with-automated-tests-b758fb9bdce8?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/b758fb9bdce8</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[profiling]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Thu, 21 Mar 2019 17:07:49 GMT</pubDate>
            <atom:updated>2019-03-22T10:17:14.589Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LwXxPNPqXVCGw8-FxxXExw.jpeg" /></figure><h3>Performance</h3><h4>Why is it important?</h4><p>At Space Ape Games we take performance very seriously. Over the last year the studio has made a huge progress in this field. Performance is king. Our players deserve the best games. The best games are fun to play, look amazing, maintain a steady frame rate and don’t leave you with 10% battery charge after each session. If we take a look at one of our older games, Transformers: Earth Wars we can see there is a correlation between frame rate and day 30 retention</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*ZFTtr1KfPY9Nw7rOJr34sQ.png" /><figcaption>Day 30 retention vs Average FPS</figcaption></figure><p>If our players are enjoying our games, they are going to play them for longer as they are going to have a better experience.</p><h4>How do we track performance stats?</h4><p>We are a mobile only studio. We make games for iOS and Android. There are a relatively small number of iOS devices to optimize for, but we tend to support everything from the iPhone 5S and onward. However when you factor in Android, on which we target everything from the Samsung Galaxy S5 and up, the number of handsets and tablets we support skyrockets. There’s no way we have the bandwidth to test each of our games on every one of these devices.</p><p>In this pie chart we can see a breakdown of the devices that Transformers: Earth Wars is installed on. Each game differs, but we can assume that</p><ul><li>Low = Samsung Galaxy S5/iPhone 5S</li><li>Medium = Samsung Galaxy S7/iPhone 7</li><li>High = Samsung Galaxy S9/iPhone X</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*Gu8v1iI3kUFmrH9fdKshKQ.png" /><figcaption>The range of devices on which our games run. Its a pretty even split.</figcaption></figure><p>We track performance metrics in all of our games. These metrics are completely anonymous. They allow us to ensure our games are running at peak performance, and if not we can do something about it so that our players are getting the best possible experience. As users play our games, these stats are sent back to us. For more information on how this works, check out my <a href="https://medium.com/spaceapetech/realtime-mobile-performance-logging-1e319277d970">Realtime Mobile Performance Logging</a> blog post. Essentially it allows us to make correlations between performance metrics and hardware, operating systems, graphics API’s etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y3uWgjW9uh6n0vwEAueO7Q.png" /><figcaption>Average frame rate across Android and iOS devices over the past 30 days</figcaption></figure><h4>How do we fix performance regression?</h4><p>Tracking these performance metrics allows us to fix issues as we find them, however this is a very reactive approach to the problem. Our users would have to experience our game running poorly before we even know about it. Ideally we want to be able to catch performance regressions before we ship a new build.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*MWrboKhHEK5E9xSnrdlTpw.png" /><figcaption>Overdraw in one of our games</figcaption></figure><p>Our senior tech artist, Stoyan Dimitrov, created a monthly game report to help the teams identify problems during development. These reports highlighted issues such as overdraw, draw call counts and shader complexity. At the end of the report he would give some advice on what each of our games teams should do to remedy any issues he found. Unfortunately as we started more and more exciting prototypes, Stoyan was unable to dedicate enough time to each team.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FCzFrmqhji27f2PjdKth6g.png" /><figcaption>A subsection of a Stoyan Report</figcaption></figure><p>This is a much more proactive approach. We find and fix issues before builds are released. But it took up way too much of Stoyan’s time.</p><h3>Collaborating with Arm</h3><h4>Workflow</h4><p>Our old workflow would look something a little like this. We would identify a slowdown, someone would take the time to profile the game and try to identify the issue. The first step is to identify if the game is CPU or GPU bound. From here we need to find out why we are CPU (e.g. we’re submitting to many draw calls to the graphics API) or GPU (e.g. we’re shading to many pixels) bound. For example, say we are GPU bound, are we blowing up one of our shader pipelines (vertex or fragment), then are we maxing out the ALU’s, are we stalling on texture reads? This can be a very time consuming task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SfSONdMXfk3l0x-xuKZD3w.png" /><figcaption>Old workflow</figcaption></figure><p>As we often use <a href="https://developer.arm.com/products/software-development-tools/arm-development-studio/components/streamline-performance-analyzer">Streamline</a> and <a href="https://developer.arm.com/products/software-development-tools/graphics-development-tools/mali-graphics-debugger">Mali Graphics Debugger</a> to identify these issues we decided to reached out to <a href="https://www.arm.com/">Arm</a>. After some discussion we decided to collaborate on a prototype profiling tool. Our aim was to produce Stoyan reports automatically. This would essentially change our workflow to look more like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aKBbeXUzsEEQBIG24NKcfQ.png" /><figcaption>Desired workflow</figcaption></figure><h4>Unity Integration</h4><p>The first step is to integrate Arm’s tooling into our games. We use Unity across all games in our studio. If you look inside your Mali Graphics Debugger install folder there will be a /target/android/arm/unrooted/ folder. Copy this directory into your /Assets/Plugins/ folder within your Unity project.</p><p>If you’re using Unity 2018.2 or newer a new native interface was introduced allowing us to pass Unity profiler annotations directly to external profilers such as Streamline. We actually <a href="https://github.com/spaceapegames/streamline-native-profiler">open sourced the Unity profiler integration</a> so you can simply drop it into your project.</p><p>Now you should be able to run your game on an Android device and attach the Mali Graphics Debugger or Streamline with detailed profiler annotations.</p><h4>Streamline, Gator and the Performance Advisor</h4><p>Before we dive in, lets take a quick look at how Streamline works. At the base layer our hardware and software expose counters and events. These events are collected by a proxy tool known as Gator. Gator gathers all of these metrics and composites them into a single capture. This capture is then opened in Streamline where we can view all of the counters and events in graph form.</p><p>Streamline can be quite intimidating to use. It’s very low level. We were desperate for a tool that would give us a high level overview of performance within our games that the entire team could understand and use. Arm were more than happy to prototype this tool. Below we can see the basic flow that was required to output data in a simple report format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KXDjBRebzPv142rjwaUn5Q.png" /><figcaption>The flow required to generate a performance report.</figcaption></figure><p>Arm created what’s now known as the Analysis Engine. This engine gathers all data created from the Gator capture and analyses it, making sense of the data so that it can provide insightful information and suggestions via a report. The report is a simple HTML document that can be opened by anyone. We then worked with Arm to take this and build it into our CI system. Each night we would build one of our games, run it on an Android device, capture the performance data with Gator and automatically generate a report that the team can then see each morning when they sit down with their coffee.</p><h4>CI Integration</h4><p>The Continuous Integration setup was a little trickier than we anticipated (at Space Ape we use <a href="https://jenkins.io/">Jenkins</a>). Capturing data on Android using Gator is currently a very manual process. Working with Arm we managed to create a prototype automation system. The plan going forward is to develop this into an out of the box solution.</p><p>We developed a shell script that runs through the following steps</p><ol><li>Uninstall any old build from the device and install the new APK.</li><li>Run the newly installed APK on the device.</li><li>Capture data via Gator.</li><li>Transfer this capture data back to the build box.</li><li>Analyse this data and produce a Performance Report.</li></ol><p>There’s a lot to cover here, so I’ve shared a script below that we developed with a lot of help from Arm. Like I say this is a prototype, but going forward it will help us to develop a really simple, out of the box solution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/525d4694e6115c6483f0fedde426a613/href">https://medium.com/media/525d4694e6115c6483f0fedde426a613/href</a></iframe><h4>Devices</h4><p>Because the Android platform is so fragmented we found it really difficult to identify devices that would give us all of the counters and events that we needed out of the box (i.e. a non rooted device). In the end we found that these 3 devices were the best at providing reliable results.</p><ul><li>Oppo R15</li><li>Huawei Mate 10 Pro</li><li>Samsung Galaxy S9</li></ul><p>Going forward it would be awesome to see Google or the hardware vendors collaborate on this and standardizing the platform a little to help out developers.</p><h4>Performance Advisor Reports</h4><p>So we finally have our reports! We have a overall summary of the capture at the top. It shows us the percentage of time where our game is bound by either VSync, CPU, GPU vertex shading, GPU Fragment Shading along with an average frame rate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/831/1*3CjT44dcZOIiGd6Ffp4Xlg.png" /><figcaption>Arm’s Performance Advisor frame summary</figcaption></figure><p>Underneath this we have a more in depth graph. This graph shows the average frame rate over the entire capture. The level of overdraw in your game. It also colour codes the capture so you can see at any point in the capture where you are bound. We also worked with Arm to add regions. This allows us to categorise parts of the capture. So we can see clearly if were on a loading screen, character select screen, in game etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*Aq8qUbFqg_fq7mR_G1MhGQ.png" /><figcaption>Arm’s Performance Advisor frame analysis</figcaption></figure><p>The Performance Advisor then gives us a region by region overview. Allowing us to see where each region was bound, and its average frame rate. Using <a href="https://github.com/spaceapegames/streamline-native-profiler">our plugin</a>, you can define a region in your game by calling StreamlinePlugin.BeginRegion(&quot;myRegion&quot;); and StreamlinePlugin.EndRegion(&quot;myRegion&quot;);</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*jC-yf0PKXjThUbvu16J98g.png" /><figcaption>Arm’s Performance Advisor region analysis</figcaption></figure><h4>The Future</h4><p>We’ve been working closely with Arm to help drive the development of this tool. It’s been awesome working with them. We really feel as though they have valued our feedback and they’ve delivered an awesome prototype that’s allowed our game teams to take control of performance. The data is so accessible that everyone from the game team, QA, product, art and dev, can find what they need. We’re hoping to further collaborate with Arm and take the Performance Advisor tool from prototype to shippable. Arm have discussed taking this even further to provide automated frame analysis, like what you see inside MGD or Streamline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uNJwt9CAut3NHxacgBhdMA.png" /><figcaption>Automated Frame Analysis</figcaption></figure><p>For anyone that’s interested, we’re holding a <a href="https://schedule.gdconf.com/session/attaining-maximum-performance-with-automated-tests-presented-by-space-ape-games-and-arm/865276">talk with Arm at GDC</a> this year, so if you’re there drop by and say hello or come speak to someone at the Expo (South Hall, #1049). If you’re not lucky enough to be at GDC then you can always download the slides afterwards. If you’re interested in becoming a beta tester and providing feedback for the Performance Advisor suite, <a href="https://twitter.com/mjshorty">reach out to me</a> and I can pass your details along to someone at Arm or sign up <a href="https://developer.arm.com/products/software-development-tools/arm-mobile-studio/components/performance-advisor">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b758fb9bdce8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/attaining-maximum-performance-with-automated-tests-b758fb9bdce8">Attaining Maximum Performance with Automated Tests</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Realtime Mobile Performance Logging]]></title>
            <link>https://medium.com/spaceapetech/realtime-mobile-performance-logging-1e319277d970?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e319277d970</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Thu, 15 Nov 2018 16:09:50 GMT</pubDate>
            <atom:updated>2018-11-15T16:09:50.850Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>When I worked in console development it was relatively easy to discover and record performance metrics. Although there are multiple iterations of console hardware, the number of variants was still pretty low. For example XBox One and XBox One X, or PlayStation 4 and PlayStation 4 Pro. So you can run your game, fire up a profiler and know that your title will perform the same on this dev unit console as it will on every other console out there.</p><p>In the mobile world this is completely different. The closest we have to this fixed platform hardware is Apple’s iOS devices. Even then there are tens of hardware variations. Devices ranging from the original iPhone with 128MB of RAM, a Power VR GPU running at 103 MHz and a 412 MHz 32 bit processor up to the recently released iPhone Xs with 4GB of RAM, a custom Apple quad core GPU clocking in at 1.1GHz and a custom Apple A12 6 core processor running at 2.49GHz. Check out <a href="https://en.wikipedia.org/wiki/List_of_iOS_devices#iPhone">Wikipedia</a> for more information on these device specs.</p><p>The ability of these devices, and those in between vary massively. And then you start to factor in Android devices. In 2015 it was <a href="https://venturebeat.com/2015/08/05/fragmentation-report-there-are-now-24093-distinct-android-devices-up-78-from-last-year/">reported</a> that there were over 24,000 different Android devices, and there was a 28% increase on the previous year. During development its impossible to account for performance on all of these devices.</p><h3>Solution</h3><p>So how do we monitor and record performance statistics across these device? We don’t have the capacity to sit down, connect each and every device to our laptops and then profile the game. Instead we get our players to help us out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tFxMSgSV1Zi0VMe9CszQmA.png" /><figcaption>Profiling in XCode</figcaption></figure><p>During development each game will define their minimum, average and high spec devices for both Android and iOS. For example:</p><p>Minimum</p><ul><li>iPhone 5s</li><li>Samsung Galaxy S5</li></ul><p>Average</p><ul><li>iPhone 8</li><li>Samsung Galaxy S7</li></ul><p>High End</p><ul><li>iPhone Xs</li><li>Samsung Galaxy s9</li></ul><p>We then use these devices as our core test bed — we test our game on many more devices than this, but for simplicity these are our target devices — much like you have target hardware configurations on PC. We regularly run our games on these devices, log statistics and profiler runs and then fix any issues that occur.</p><p>But what of the thousands of other devices out there that we cant possibly test on? We have a performance tracking and logging system built into our games. Each team decides what they want to record and when. Some teams chose to record statistics during UI, some during the core gameplay etc. An example of some of the data that we record:</p><ul><li>Average frame time</li><li>Average frame rate</li><li>Memory consumption</li><li>Battery drain</li><li>Load times</li><li>CPU frequencies</li></ul><p>Alongside these figures we also log the anonymised device information information, such as</p><ul><li>CPU clock speed</li><li>CPU core count</li><li>CPU model</li><li>GPU clock speed</li><li>GPU model</li><li>Graphics API (Open GL ES 2.0, 3.0, Vulkan, Metal etc)</li><li>Available memory</li><li>Operating System</li></ul><p>We then send all of this data to our back end data store (thanks DevOps!). We use a platform called Elasticsearch to index and catalogue all this information. When then use another platform called Kibana. Kibana is a visualisation tool for all this data. We are able to log and compare performance statistics across different builds, CPUs, GPUs, Graphics APIs, OS, Unity version etc. It can be visualised in many different ways, bar, line, pie etc, and then segmented further. We have entire dashboards that show the current status of our games.</p><h3>Examples</h3><p>Disclaimer, all of the following graphs are from a pre soft launch game and performance isn’t always top priority in titles at this stage of development. At this stage of development we are focused on gameplay and ensuring what we are developing is fun.</p><p>Here we are able to see the average frame rate for each level of one of our games, across all devices. If we then spot an issue we can then drill down further.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*18ApXcQ8hTUBKwccku7BLQ.png" /><figcaption>Average frame rate for each level in our game</figcaption></figure><p>Now we can see the average frame rate across various GPUs. When we notice a particular device is struggling we can grab it from the device drawer and profile it manually. If we don’t have that device to hand we can find another device with a similar GPU.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RNHJ79lfQmeiKQqxf_vcCA.png" /><figcaption>Average frame rate for each GPU</figcaption></figure><p>A more useful graph might be average frame rate per game build for our target devices. This way we can track the overall game performance from release to release and make sure that there isn’t a downward trend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eLy8DQduFOU0ay8HHKTmcQ.png" /><figcaption>Average frame rate for each build, for each of our target devices</figcaption></figure><p>When people are playing mobile games they tend to do so in short stints of time. They don’t want to spent minutes waiting for a level to load. Therefore its essential to keep loading times to a minimum. Here we can see the load time in seconds of each level in our game. If something becomes unacceptable we can see and address it quickly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EMPQM-qT3hK6U7XP1asOnA.png" /><figcaption>Load time (seconds) for each level</figcaption></figure><p>Finally people aren’t going to play your game if it drains too much battery power. If they fire up your game for 5 minutes and then notice that their battery is sitting at 30% they aren’t going to play again. We’re able to track battery consumption per level. Unusually high battery consumption is a sign of high processing cost or high bandwidth usage. If we have a particular level that’s consuming a lot of battery power, we can investigate further and address the issue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*g5xFe59H4r5ssTtY7ceaXw.png" /><figcaption>Battery drain for each game level</figcaption></figure><h3>Usage</h3><p>I made it as easy as I could for our dev teams to record and visualise data. Our performance logging system is shared via Nuget, and comes with everything game teams need to start recording data. They can even submit custom data per recording or per event.</p><p>You simply create a test runner and add whatever recorders and writers you want:</p><pre>var gameData = new Dictionary&lt;string, string&gt;()<br>{<br>    {&quot;build&quot;, AppConfig.BUILD_NUMBER},<br>    {&quot;environment&quot;, AppConfig.ENVIRONMENT},<br>    {&quot;game&quot;, &quot;MyGameName&quot;}<br>}<br>var runner = PerformanceTesting.RunnerComponent.Create(gameData);</pre><pre>runner.Add(new FrameRateRecorder(), new FrameRateElasticWriter());<br>runner.Add(new BatteryLevelRecorder(), new BatteryLevelElasticWriter());<br>runner.Add(new CpuRecorder(), new CpuElasticWriter());<br>runner.Add(new MemoryRecorder(), new MemoryElasticWriter());</pre><p>Then you start and stop events in your title, when you want:</p><pre>runner.StartEvent(&quot;perfTest1&quot;);<br>runner.StopEvent(&quot;perfTest1&quot;);</pre><p>And when you’re finished, write the data out:</p><pre>runner.Write();</pre><p>On top of that the DevOps team helped me setup a shared performance logging data store and visualisation platform. Anyone in the studio can view performance data for any game by logging onto a single website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6IBgnthfljahqO93iQp5Ug.png" /><figcaption>One of our games’ performance dashboards</figcaption></figure><h3>Conclusion</h3><p>In mobile game development, trying to record performance data across all your target devices is almost impossible. There are just too many variations to consider. Make it easy for teams to collect and view performance data and in turn they will put it to the forefront of their development. Your games, and studio, will benefit tremendously.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e319277d970" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/realtime-mobile-performance-logging-1e319277d970">Realtime Mobile Performance Logging</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Physically Based Shading on Mobile]]></title>
            <link>https://medium.com/spaceapetech/physically-based-shading-on-mobile-d7d4e90bb4bd?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7d4e90bb4bd</guid>
            <category><![CDATA[rendering]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[graphics]]></category>
            <category><![CDATA[lighting]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Tue, 30 Oct 2018 13:40:49 GMT</pubDate>
            <atom:updated>2018-10-30T13:47:52.792Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>About a year or so ago we noticed more and more that our art team weren’t happy with how our games were looking on device. They wanted, and we wanted, to push our game visuals above and beyond anything we’d previously been capable of doing. So, over the past year or so we developed an Uber Shader, using the <a href="https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html">Unity shader variants</a> toolchain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ve6r8p-eRsD5DSsPgdud5g.png" /><figcaption>A scene rendererd with our Uber Shader</figcaption></figure><p>The Uber Shader was intended to be a one stop shop for every surface we have in our games. It’s a PBR inspired shader, in that it doesn&#39;t follow the PBR rule set, but used it for guidance. For those that haven’t come across PBR yet, I can recommend Trent Reeds post, <a href="http://www.trentreed.net/blog/physically-based-shading-and-image-based-lighting/">Physically Based Shading and Image Based Lighting</a>. As it was adopted throughout the studio, more and more features were requested by the art team and the shader grew into what we know it as today. It supports a whole host of features, including, but not limited to:</p><ul><li>Detail maps</li><li>Texture blending</li><li>Reflections</li><li>Pseudo sub surface scattering</li><li>Fresnel</li><li>Normal maps</li><li>Metallic surfaces</li><li>Linear &amp; gamma space workflows</li></ul><h4>Issues</h4><p>Because there are so many shader variants, compile times can be astronomical. When you include all of our variants, shader LODs and shader passes, shader compile times can be upwards of 30 minutes. This is a killer for rapid iteration time.</p><p>We have a custom shader inspector. The inspector enables and disables the shader features when certain textures are set, or when toggles are enabled. We noticed that the inspector was getting overly complex and our initial thought was to modularize it, separating features out so that the artists can find things easier. But it quickly became apparent that things had gotten a bit too complicated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*cYgdoEycFthFH1ZxIdIx_w.png" /><figcaption>The Uber Shader Inspector</figcaption></figure><p>We gave the art team so much flexibility that it became almost impossible for them to balance materials in game. They were spending too much time creating and configuring materials. What’s more, our materials look completely different throughout our toolchain (Maya, Substance Painter and Unity) meaning that the artists had to configure surfaces three times, with vastly different settings.</p><h4>Substance Painter</h4><p>Our art team use, and love, <a href="https://www.allegorithmic.com/products/substance-painter">Substance Painter</a>. For those that don’t know, it allows you to create and blend materials in a very visual and artistic manner. It’s used by hundreds of studios around the world. They also love the way in which their materials look inside Painter. Investigating a bit further we immediately noticed that Substance has a PBR pipeline. Ideally, the materials the art team produce in Substance should look identical, or as close to identical as possible, in Unity, on mobile devices.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*14Itbbaxr5PUn2Bw4XNoew.png" /><figcaption>Substance Painter</figcaption></figure><h4>Research</h4><p>There’s a lot of information out there when it comes to physically based rendering and shading. It’s been widely adopted over the past 5 or so years. I set about reading papers, watching talks and clicking through slides. Collating as much information as humanly possible. The first thing that came to light was that almost all of the PBR information and research out there we designed for desktop and console hardware. However, we want to be able to achieve the same visuals on mobile. We had our work cut out! As a side note, I’ve added a lot of links to the research I found at the bottom of this post.</p><h3>Decisions</h3><h4>Disney PBR Principles</h4><p>PBR was first brought into the limelight when Disney published their paper titled <a href="https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_slides_v2.pdf">Physically-Based Shading at Disney</a>. They talk about the way they transitioned to PBR when creating Wreck-It Ralph in 2012. As well as defining their lighting models, they also defined a number of rules which you should use to create and establish a PBR pipeline:</p><ol><li>Intuitive rather than physical parameters should be used.</li><li>There should be as few parameters as possible.</li><li>Parameters should be zero to one over their plausible range.</li><li>Parameters should be allowed to push beyond where it makes sense.</li><li>All combinations of parameters should be plausible.</li></ol><h4>Workflow</h4><p>There are two main workflows that people follow when developing PBR pipelines:</p><ol><li><strong>Specular<br></strong>Comprises of 7 channels:<br>Albedo (RGB)<br>Specular (RGB)<br>Smoothness [0, 1]</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*hS4VRPWM-KTZXTrUs5Lm7w.png" /><figcaption>Specular workflow controls</figcaption></figure><ol><li><strong>Metallic</strong><br>Comprises of 5 channels:<br>Base Colour (RGB)<br>Roughness [0, 1]<br>Metallic [0, 1]</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*ap7JL0fqW2jOKUHghnd_wQ.png" /><figcaption>Metallic workflow controls</figcaption></figure><p>For dielectric materials, the specular highlight colour is a greyscale value. So using the specular model you’re wasting two channels in the specular texture. For metallic surfaces, the diffuse colour is almost black, so again in the specular model your wasting 3 channels in the albedo texture. Considering this, the fact that were on mobile and the fact that Disney&#39;s 2nd principle states that “There should be as few parameters as possible” we chose to go exclusively with the metallic workflow. Bungee (Destiny 2), Unreal Engine, Frostbite Engine and Unity all support the metallic workflow (Unity also supports the specular workflow).</p><h4>ALU vs LUT</h4><p>Our Uber Shader toolchain bakes specular and diffuse lighting results into a look-up texture. We then sample this texture using N.L for diffuse and [N.H, smoothness] for the specular highlight. This is really cheap and lets us use many expensive lighting models. However we noticed that the highlights would sometimes not be sharp enough, we lost some data due to texture and data compression. We also know that going forward, supporting multiple lights isn’t feasible. If we have four lights affecting a single surface, that would result in eight texture fetches from the lighting alone.</p><h4>Lighting Model</h4><p>The most famous specular lighting model is quite possibly the Blinn model. It’s been around for decades. Moving to PBR people have adopted a normalized Blinn Phong lighting model. But we wanted to do better. Most console and desktop titles today go with the GGX specular model, it looks a lot more realistic when compared with a real world specular highlight. Unfortunately, this realism comes with a lot of extra cost! I did a lot of research and came across a paper published by Unity at SIGGRAPH 2015 titled <a href="https://community.arm.com/cfs-file/__key/communityserver-blogs-components-weblogfiles/00-00-00-20-66/siggraph2015_2D00_mmg_2D00_renaldas_2D00_slides.pdf">Optimizing PBR</a>. It’s a great read and sets out the formula we adopted, it’s a very close approximation that actually runs on mobile devices.</p><h3>Implementation</h3><p>Here is a (simplified) outline of the fragment shader we developed for the ForwardBase lighting pass. All of our input colours and colour based textures are gamma corrected, so we are working in linear space. In the end, we write the final colour back to the frame buffer in gamma space.</p><p>We store smoothness, metallic and ambient occlusion parameters in a linear texture. We also store an emissive colour mask and a texture blend mask in a second texture, though we may look to squeeze this all into one texture at the expense of some ALU unpacking overhead. We prefetch as much texture data as possible inside the sampleMaterialTexture() and initSurface() function calls. We also pre calculate a lot of reusable surface information and store it in the SurfaceData structure, things like:</p><ul><li>Roughness</li><li>Normals</li><li>Diffuse &amp; Specular terms</li><li>Reflection probe samples</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b340feda204896bc0ae362a70c3899e1/href">https://medium.com/media/b340feda204896bc0ae362a70c3899e1/href</a></iframe><p>Our BRDF is broken down into quite a few helper functions, there’s really to much to share, but here is the main part of it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b882f177d4638cc7ed4d63230f3793aa/href">https://medium.com/media/b882f177d4638cc7ed4d63230f3793aa/href</a></iframe><p>We combine our environmental lighting like so. We prefetch our cubemap data at the beginning of our shader (in the initSurface() function call) to prevent stalls — you can read about this a little further down.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/772acdbe980bd81dcc1ea24de7038380/href">https://medium.com/media/772acdbe980bd81dcc1ea24de7038380/href</a></iframe><h3>Profiling</h3><p>It’s definitely worth profiling your shaders, the best tool for this is XCode’s GPU Debugger — it shows you line by line costs of your shader. We had a stall in the PBR shader that was consuming 40% of the entire shader time! This stall came from sampling the environment cube map, and then decoding the value immediately. Sampling the cube map at the beginning of our shader, then decoding it much later in the shader allowed us to take that stall down to just 3% of the total shader time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-1Cf8R328fvhbG0P4mLAxA.png" /><figcaption>XCode GPU Debugger — costs per line of shader code</figcaption></figure><h3>Conclusion</h3><p>We were able to develop a prototype that looks very similar across our tool chain and runs smoothly on our target platforms (Android and iOS).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ki4kTmUq0lg2qDo8A2qRbw.png" /><figcaption>Left to right: Substance Painter, Unity Windows, Unity iOS. The biggest discrepancy between iOS and the other platforms comes from the LDR reflection probes.</figcaption></figure><h4>Performance: ALU vs Texture Fetch</h4><p>We wanted to prove that an ALU lighting approach made more sense for us, especially when adding more and more lights to the scene. The table below shows ALU instruction count and texture fetch counts for each of our 3 implementations, for one, two and four lights.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BNSXs_MEy6ipiI6W-UbwNw.png" /></figure><p>We can clearly see that the ALU based approach uses more ALU instructions and fewer texture fetches. This is more appropriate for our target platform. Bandwidth is super expensive on mobile! Over the years mobile GPUs have gotten more and more powerful, they are able to process lots of complex maths, however adding additional texture reads to a shader can cause the GPU to stall quite significantly.</p><h4>Performance: ALU vs sRGB Gamma to Linear Conversion</h4><p>Mobile devices supporting Open GL ES 3.0 and above provide support for sRGB texture reads. The GPU texture sampler is able to convert texture data from gamma to linear space. For earlier devices we have to convert between gamma and linear space manually:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f9ea3a133b2a55ff2c33039b5c196e4/href">https://medium.com/media/8f9ea3a133b2a55ff2c33039b5c196e4/href</a></iframe><p>Before committing to either we need to find out the cost of each. Using XCode GPU Debugger I found that, rendering a whole bunch of spheres, the ALU conversion took 3.05ms, whereas the sRGB method took 2.79ms. If you can drop support for OpenGL ES 2.0, or maybe just work in gamma space on these devices 😢, then adopt the sRGB method.</p><h4>Next Steps</h4><p>So far all of this work has come from our own trials and research. We’re still in the prototype stage and were finalizing our final feature set that we will take forward into the final production pipeline. We’ve made the shader code as modular and flexible as possible. Transitioning to scriptable render pipelines and ultimately deferred shading is our target.</p><h3>References</h3><ul><li><a href="http://www.alexandre-pestana.com/physically-based-shading-metallic-specular-workflows/">Physically Based Shading, Metallic and Specular workflows</a></li><li><a href="https://community.arm.com/cfs-file/__key/communityserver-blogs-components-weblogfiles/00-00-00-20-66/siggraph2015_2D00_mmg_2D00_renaldas_2D00_slides.pdf">Optimizing PBR for Mobiles</a></li><li><a href="https://www.gdcvault.com/play/1025238/Shading-of-Spellsouls-Achieving-AAA">Shading of Spellsouls</a></li><li><a href="https://www.gdcvault.com/play/1025290/Translating-Art-into-Technology-Physically">Physically Based Shading in Destiny 2</a></li><li><a href="https://spaceapegames.atlassian.net/wiki/spaces/AL/pages/969539636/Physically+Based+Shading">Moving Frostbite to PBR</a></li><li><a href="https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_slides_v2.pdf">Physically Based Shading at Disney</a></li><li><a href="http://www.trentreed.net/blog/physically-based-shading-and-image-based-lighting/">Physically Based Shading and Image Based Lighting</a></li><li><a href="http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html">sRGB Approximations for HLSL</a></li><li><a href="https://blog.selfshadow.com/publications/s2012-shading-course/hill/s2012_pbs_importance.pdf">Importance of Physically Based Shading</a></li><li><a href="https://blog.selfshadow.com/publications/s2012-shading-course/hoffman/s2012_pbs_physics_math_slides.pdf">Physics and Maths of Physically Based Shading</a></li><li><a href="https://seblagarde.wordpress.com/2011/08/17/hello-world/">Adopting a Physically Based Pipeline</a></li><li><a href="https://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_slides.pdf">Real Shading in Unreal Engine 4</a></li><li><a href="https://github.com/Unity-Technologies/ScriptableRenderPipeline">Unity’s Scriptable Render Pipeline</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7d4e90bb4bd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/physically-based-shading-on-mobile-d7d4e90bb4bd">Physically Based Shading on Mobile</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimising our shadows in Unity]]></title>
            <link>https://medium.com/spaceapetech/optimising-our-shadows-in-unity-c26e691d9d0b?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/c26e691d9d0b</guid>
            <category><![CDATA[graphics]]></category>
            <category><![CDATA[rendering]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[shaders]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Tue, 25 Sep 2018 14:27:51 GMT</pubDate>
            <atom:updated>2018-09-25T14:27:51.885Z</atom:updated>
            <content:encoded><![CDATA[<p>We have a projected shadows system that we use in a few of our games. Very much like a shadow map, it involves rendering objects from the perspective of a light and then projecting the shadows from that light onto the scene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*zmvrd3Ne87siASfj" /></figure><p>On some of our games, the fully fledged Unity shadow mapping solution is overkill — we don’t want to render dynamic shadows for everything, only smaller objects in the scene. We also want more control over how we filter our shadows — how we blur them to make them softer.</p><p>During a recent profiling session on one of our games, we noticed that generating one of these shadow maps was taking up approximately 12% of the total frame time. So I went about investigating it and looking into what we could do to reduce this cost and at the same time reduce the amount of memory the system was consuming.</p><h3>Optimisations</h3><p>My first step was to fire up my preferred profiling tools for both Android (<a href="https://renderdoc.org/">RenderDoc</a>) and iOS (<a href="https://developer.apple.com/xcode/">XCode</a>). RenderDoc is a free to use profiler and debugger that can connect to a host Android device and capture frame traces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/0*oAywPgDzfw9C0Wxk" /><figcaption>RenderDoc</figcaption></figure><p>XCode is to the go-to development app on MacOS, you can capture a GPU frame at any time by selecting the option from the debug menu.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*xsqjqNS__b2AKD7oVp_xBw.png" /><figcaption>XCode Frame Debugger</figcaption></figure><h3>Making the most of the space we have</h3><p>Using the render target viewer on both platforms I spotted that the contents of the shadow map render target was only occupying a small section of the entire texture. I would estimate that over 50% of the pixels in the render target were unoccupied — what a waste of space!</p><p>We use our projected shadows with directional lights, orthographic projection modes tend to be easier to control and tweak. You will lose any perspective but for us, this isn’t an issue. Swapping the projection mode over to orthographic as well as better positioning of the light source allowed us to make better use of the available render target space.</p><p>In the end, we were able to reduce the resolution of our shadow map texture from 128×128 to 64×64 — that’s 1/4 of the original size. One of the biggest bottlenecks in mobile devices is bandwidth — mobile devices have small busses. Moving 75% less data down the bus is a big saving. This along with shading 75% fewer fragments is a huge win (ignore the colour for the minute — I changed the way we store the shadow map in the render texture).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*q9-Qn1BiIB9JcQKY" /><figcaption>128x128 shadow map</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/150/0*5AYb-yNbQeI_DA_E" /><figcaption>64x64 shadow map</figcaption></figure><h3>MSAA</h3><p>As we are using such a small render target when objects start moving within the render target you will notice a lot of aliasing. Due to the way in which mobile GPUs work, MSAA is very cheap. Mobile GPU’s use a tile-based architecture. All of the anti-aliasing work is done on chip and all additional memory is on tile memory. Enabling 4x MSAA with a smaller render texture gave us much better results with only a tiny increase in processing cost.</p><h3>Render Target Formats</h3><p>I spotted that our shadow map render texture was using an <a href="http://docs.unity3d.com/ScriptReference/TextureFormat.RGBA32.html">R8G8B8A8</a> format. Only two of the channels were being used. The first (R) was being used to store the shadow itself, and the second channel (G) was being used to store a linear fall off. Our artists requested that the intensity of our shadows fall off with distance.</p><p>Looking further into it, we didn’t actually need to store both pieces of information here. We only needed the shadow value or the falloff value depending on what was enabled for this shadow projector. I changed the render target format to a single 8-bit channel format (<a href="http://docs.unity3d.com/ScriptReference/RenderTextureFormat.R8.html">R8</a>). This cut our texture size down by another 1/4. Again this reduces our bandwidth massively,</p><h3>Blur Method</h3><p>After we populate our shadow map render texture we blur it. This reduces any artefacts we see from using smaller textures as well as giving the impression of soft shadows. We were using a 3×3 box blur — that’s 9 texture samples per pixel. What’s more we weren’t taking advantage of bilinear filtering with a half pixel offset. I quickly added the option to only sample the surrounding corner pixels along with a half pixel offset, this reduced our sample count from 9 to 5 (we still sample the centre pixel).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/0*4KMFvXZPWKD-HOGL" /><figcaption>Hard Shadows</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/0*bVCIQTyBKJKMfGuc" /><figcaption>5 Tap Soft Shadows</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/0*OP5ASE0_8_slujTB" /><figcaption>9 Tap Soft Shadows</figcaption></figure><p>You sample a texel from a texture using a texture coordinate. With point filtering enabled, sampling between two texels will result in only one of the texels being sampled. With bilinear filtering enabled the GPU will actually linearly blend between the two texels and return you the average of the two texels. So if we add an additional half pixel offset, we can essentially sample two pixels for the price of one.</p><h3>Reducing ALU Instructions</h3><p>Unity doesn’t support the common border texture wrapping mode. Therefore we had to add a bit of logic to our blur shader that checks to see if the current texel is a border texel, and if so keep it clear. This prevents shadows from smearing across the receiving surface. The shader code was using the step intrinsic to calculate if the current texel was a border texel. The step intrinsic is kind of like an if statement, I managed to rework this bit of code to use floor instead, this alone reduced the ALU count from 13–9. This doesn’t sound like much, but when you are doing this for each pixel in a render texture, it all adds up. This wasn’t the only saving we made here, but its an example of what we did.</p><p>When writing your shader, head to the inspector in Unity. From here, with a shader selected, you can select the “Compile and show code” button. This will compile your shader code and open it in a text editor. At the top of you compiled shader code you can see how many ALU and Tex instructions your shader uses.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8138aa1d4b03adbb563eabce74677731/href">https://medium.com/media/8138aa1d4b03adbb563eabce74677731/href</a></iframe><p>For even more information you can download and use the <a href="https://developer.arm.com/products/software-development-tools/graphics-development-tools/mali-offline-compiler">Mali Offline Shader Compiler</a>. Simply copy the compiled shader code — the bit in between #if VERTEX or #if FRAGMENT — and save it to a .vert or .frag file. From here you can run it through the compiler and it will show you the shader statistics</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f5ee5ad3bba1b349008b87d57cb533c/href">https://medium.com/media/2f5ee5ad3bba1b349008b87d57cb533c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/0*aZ7-Rp_JfS6havvk" /></figure><p>Above you can see that the 5 tap blur shader uses</p><ul><li>2 ALU (arithmetic logic unit) instructions</li><li>3 Load/Store instructions</li><li>5 Texture instructions</li></ul><h3>OnRenderImage</h3><p>After the end of the blur pass, I noticed that there was an additional Blit — copying the blurred texture into another render target! I started digging into this and noticed that, even though we specified that our blurred render texture is of R8 format, it was R8G8B8A8! It turns out that this is a bug with Unity. OnRenderImage is passed a 32-bit destination texture, then the value of this is copied out to the final target format. This wasn’t acceptable so I changed our pipeline. We now allocate our render textures manually and perform the blur in OnPostRender.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e24c150c46e8b34e3e5348fc313007f4/href">https://medium.com/media/e24c150c46e8b34e3e5348fc313007f4/href</a></iframe><h3>Depth Buffer</h3><p>This one is a bit weird — I’m not sure if I like it or not, I’m pretty sure I don’t. If you’re desperate to save memory you can disable the depth buffer. But this means that you are going to get a tonne more overdraw. However if you know what’s going into the shadow map render target and you know there isn’t a lot of overdraw, then this might be an option for you. The only way you’re going to tell is if you profile it, and before you even do this, make sure you’re really desperate for that additional few kilobytes.</p><h3>Performance Metrics</h3><p>Here we can see the cost of rendering a single shadow map (the example above). These readings were taken using XCode GPU Frame Debugger on an iPhone 6s. As you can see, the cost of rendering this shadow map is less than 50% of the original cost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/0*1xrSGdG-VT9muI01" /><figcaption>Profiling results — before and after optimisations</figcaption></figure><p>Thanks to reducing the size of our render targets, using a smaller texture format, eliminating the unnecessary Blit and (optionally) not using a depth buffer our memory consumption went from 320kb down to 8kb! Using a 16-bit depth buffer doubles that to 16kb. Either way, that’s A LOT of saved bandwidth.</p><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*0aTB_shx3EXSybFG" /></figure><p>In the best case scenario, we were able to reduce our memory consumption (and bandwidth usage) by over 40x. We were also able to reduce the overall cost of our shadow system by just over 50%! To any of the art team that may be reading this — it doesn’t mean we can have twice as many shadows 😀 All in all, I spent about 2–3 days profiling, optimizing and changing things up and it was definitely worth it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c26e691d9d0b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/optimising-our-shadows-in-unity-c26e691d9d0b">Optimising our shadows in Unity</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Crowd Rendering on mobile with Unity]]></title>
            <link>https://medium.com/spaceapetech/crowd-rendering-on-mobile-with-unity-b14745126a2a?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/b14745126a2a</guid>
            <category><![CDATA[crowd]]></category>
            <category><![CDATA[shaders]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[rendering]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Tue, 18 Sep 2018 10:52:52 GMT</pubDate>
            <atom:updated>2018-09-25T14:25:52.898Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the artists came to me one day and asked “What’s the best way to render a crowd in our game?”. Being a mobile-first studio, it’s not an easy solution. Back in my console days, we could consider instancing and GPU simulations, but our game needs to support OpenGL ES 2.0 and run on devices with a lot less bandwidth and power. Crowd systems inherently consume a lot of processing power, you’ve got to animate thousands of people and each of those people need to look different.</p><p>We took a look at what options were available to use. We could simply render a lot of skinned meshes but this is going to be expensive both on CPU and GPU as we need to skin each mesh and then submit it to the GPU. We could use the sprite system in Unity to render a billboarded crowd, but as the camera angles change the sprites would have to be re-sorted. After a while, we realised we needed to come up with a custom solution.</p><h3>Technique</h3><p>Our crowds were going to be displayed at quite a distance from the camera, on devices with small screens. Therefore fidelity was not so much of a concern. Rendering thousands of 3D skinned meshes on mobile wasn’t really an option, we chose to stick to 2D crowds.</p><h3>Placement</h3><p>We need crowd placement to be quick and easy. We don’t want our art team spending hours painfully placing GameObjects inside scenes to signify where a person should spawn. Ideally, an artist should be able to define a region or area where they want people to spawn and when they hit a button it all comes to life.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/0*Lw7IlnuY3bhw2Ely" /></figure><p>We gave the artists the ability to spawn crowds inside bounding boxes, around a sphere and at points in a mesh. We found that 95% of the time the art team would choose to spawn crowds using a bounding box.</p><h3>Randomisation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*CRefYeR8mOAXHAWj" /></figure><p>One of the biggest challenges with crowd rendering is having enough variation within the crowd so that it looks believable. This means people in the crowd will need different animations, coloured clothes, coloured skin, hairstyles etc. And those characters that are duplicated will require offset animations so that they look less like clones. You soon realise that people don’t focus on one person in the crowd, they focus on the crowd as a whole. This means that as long as there are enough variation and movement in there it looks pretty convincing.</p><p>We allow the artists to randomise:</p><ul><li>Sprites</li><li>Position</li><li>Rotation</li><li>Colour</li><li>Animation offsets</li><li>Scale</li><li>Movement</li></ul><h3>Batching</h3><p>Our games still target older Android devices that only support OpenGL ES 2.0. In order to reduce CPU overhead from issuing too many draw calls, we knew that we would have to batch as many people in the crowd as possible. For this reason, we made the decision that every person within a crowd region would be batched together into one draw call, but this obviously introduces a problem…</p><h3>Sorting</h3><p>As soon as you batch everything together you lose any ability to sort individual people within the crowd. So we had to come up with a flexible sorting solution for the artists. We ended up allowing the art team to sort characters in the group along a specific axis (e.g. along the z-axis) or by distance from an object. The latter proved to be the most used option.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/051deaea22a5dd8406d241b672e8ba8d/href">https://medium.com/media/051deaea22a5dd8406d241b672e8ba8d/href</a></iframe><p>Our crowds were used within a stadium, and our camera is always in the centre of the stadium, looking out towards the crowd. Therefore we are able to sort the members of each crowd group by their distance from the centre of the stadium. Every so often you may spot one character rendering in front of another, but again our crowds are so far from the camera that the chances of you seeing this are very, very slim.</p><h3>Billboarding</h3><p>We do all of our billboarding within the vertex shader. We generate 4 vertices for each crowd member, each of the verts is located at the centre of the rectangle. We bake a scale into the vertex data and then this scale is used along with the uv’s to offset the vertex from the centre and align it to the camera.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ad57f3557de8d2230e623fedab9ce26/href">https://medium.com/media/2ad57f3557de8d2230e623fedab9ce26/href</a></iframe><p>You can see that the uv’s are a float3, not the usual float2. The first 2 components of the vector are standard uv texture coordinates and the 3rd component is the scale of the billboard.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49200c414b3dd410d4e8c7d081ab52aa/href">https://medium.com/media/49200c414b3dd410d4e8c7d081ab52aa/href</a></iframe><h3>Lighting</h3><p>The artists weren’t happy that the crowd didn’t blend nicely with the rest of the scene, they looked flat and a bit out of place. Therefore we developed a bit of code that would bake data from the light probes in the scene into each vertex in the crowd. All of our crowd’s meshes are generated offline, then loaded at runtime.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccbcb899fcbbdb2a3deec1edd40e430d/href">https://medium.com/media/ccbcb899fcbbdb2a3deec1edd40e430d/href</a></iframe><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*pZKixeB5QKhWCjSs" /></figure><p>In the end, we ended up creating a crowd system that fit our needs exactly. We had to cut some corners in terms of visuals to meet the demands of our target platforms. But we managed to do so and our solution had virtually no impact on performance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b14745126a2a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/crowd-rendering-on-mobile-with-unity-b14745126a2a">Crowd Rendering on mobile with Unity</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Motion Blur for mobile devices in Unity]]></title>
            <link>https://medium.com/spaceapetech/motion-blur-for-mobile-devices-in-unity-656c8047508?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/656c8047508</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[shaders]]></category>
            <category><![CDATA[techart]]></category>
            <category><![CDATA[rendering]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Thu, 06 Sep 2018 15:30:04 GMT</pubDate>
            <atom:updated>2018-09-25T14:16:11.061Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://en.wikipedia.org/wiki/Motion_blur">Wikipedia</a> defines motion blur as:</p><blockquote><strong><em>Motion blur</em></strong><em> is the apparent streaking of moving objects in a </em><a href="https://en.wikipedia.org/wiki/Photograph"><em>photograph</em></a><em> or a sequence of frames, such as a </em><a href="https://en.wikipedia.org/wiki/Film"><em>film</em></a><em> or </em><a href="https://en.wikipedia.org/wiki/Animation"><em>animation</em></a><em>. It results when the image being recorded changes during the recording of a single exposure, due to rapid movement or </em><a href="https://en.wikipedia.org/wiki/Long-exposure_photography"><em>long exposure</em></a><em>.</em></blockquote><p>When we capture an image with a camera, the shutter opens, the image is captured by the sensor and then the shutter closes again. The longer the shutter is open, the more light the sensor can capture. However, leaving the shutter open for longer also means that the image being captured can change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ntz9bOHAMwV2AS3y--vEjQ.jpeg" /></figure><p>Imagine we are trying to capture an image of a car speeding along a race tack. If the shutter stays open for a whole second, the car will have shot past the camera and the entire image will be a blur. Now, if we were to open the shutter for a fraction of that time, say 1/500th of a second, chances are we will be able to capture the image with no blurring at all.</p><p>Motion blur is a side effect of leaving the shutter open for a long period of time. In games it can be desirable to simulate this effect. It can add a sense of speed and motion to our scenes. Depending on the genre of the game this can add a whole other level of realism to the game. Genres that may benefit from this effect include racing, first person shooter and third person shooter to name a few.</p><h3>Pipeline Overview</h3><p>We wanted to develop a motion blur effect for one of our games, a racing game. There are a number of different implementations currently available.</p><h3>Frame Blur</h3><p>The simplest method of simulating motion blur is to take the previous frame’s render target and interpolate between that and the current frame’s render target. When programmable shaders first came about, this is how it was done. It’s really simple and easy to implement and it doesn’t require any changes to the existing render pipeline. However it isn’t very realistic and you don’t have the ability to blur different objects in the scene at different scales.</p><h3>Position Reconstruction</h3><p>A step up from frame blurring is position reconstruction. In this method we render the scene as we normally would. Then we sample the depth buffer for each pixel in the render target and reconstruct the screen space position. Using the previous frames transformation matrices we then calculate the previous screen space position of that pixel. We can then calculate the direction and distance, in screen space, and blur that pixel. This method assumes that everything in the scene is static. It expects that the world space position of the pixel in the frame buffer does not change. Therefore, it is great for simulating motion from the camera, but it’s not ideal if you want to simulate finer grained motion from dynamic objects in your scene.</p><h3>Velocity Buffer</h3><p>If you really need to handle dynamic objects, then this is the solution for you. It’s also the most expensive of the three. Here we need to render each object in your scene twice, once to output the normal scene render target and again to create a velocity buffer (usually a R16G16 render target). You could circumvent the second draw call by binding multiple render targets if you wish.</p><p>When we create our velocity buffer, we transform each object we render from object space by the current and the previous world-view-projection matrix. Doing this we are able to take into account world space changes aswell. We then calculate the change in screen space and store this vector in the velocity buffer.</p><h3>Implementation</h3><p>We decided to implement the Position Reconstruction method.</p><ul><li>Frame blurring wasn’t an option — this method was too old school and didn’t offer enough realism.</li><li>The camera in our game follows the players vehicle which is constantly moving, so even though we can’t simulate world space transformations we should still get a convincing effect.</li><li>We didn’t want to incur the additional draw call cost of populating velocity buffer.</li><li>We didn’t want to incur the additional bandwidth overhead of populating the velocity buffer.</li><li>We didn’t want to consume the additional memory required to store the velocity buffer.</li></ul><h3>Code</h3><p>We start by rendering our scene as we usually would. As a post processing step we then read the depth of each pixel in the scene in our shader:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34a21e2401f56325125c89f09aee2eca/href">https://medium.com/media/34a21e2401f56325125c89f09aee2eca/href</a></iframe><p>Then we will reconstruct the screen space position from the depth:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69717f769f5103f1fdc239399df52e79/href">https://medium.com/media/69717f769f5103f1fdc239399df52e79/href</a></iframe><p>In C# we pass a transformation matrix into our shader. This matrix will transform the current screen space position as follows:</p><ol><li>Camera Space</li><li>World Space</li><li>Previous frames Camera Space</li><li>Previous frames Screen Space</li></ol><p>This is all done in a simple multiplication:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51a8a95ec49b03a86d698ee15b0c249f/href">https://medium.com/media/51a8a95ec49b03a86d698ee15b0c249f/href</a></iframe><p>To calculate this transformation matrix we do the following in C#</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/125735c6083397e49f3481b77152c849/href">https://medium.com/media/125735c6083397e49f3481b77152c849/href</a></iframe><p>We can now calculate the direction and distance between the two screen space vectors. We then use the distance as a scale and sample the render target along the direction vector.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f73df548f745f102fabb1b6593de4b36/href">https://medium.com/media/f73df548f745f102fabb1b6593de4b36/href</a></iframe><h3>Controlling the Motion Blur</h3><p>Once we got all this on-screen, we quickly decided that there was just too much blurring going on. We want most of the scene to be blurred, but the artists wanted vehicles and drives to be crisp. In order to achieve this, with as little pipeline impact as possible, we decided to use the alpha channel to mask out areas of the scene that we didn’t want to blur. We then multiplied this mask by the blur vector to effectively make the blur vector [0, 0].</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc21a0f417c578fc70051f50af23ce4a/href">https://medium.com/media/dc21a0f417c578fc70051f50af23ce4a/href</a></iframe><p>To add to this we also found that objects in the distance shouldn’t blur as much as those in the foreground. To achieve this we simply scaled the blur vector by the linear eye (view space) depth, calculated from the depth buffer (LinearEyeDepth) is a helper function inside the Unity cginc headers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0aff82c93d6152c18802a156e7030996/href">https://medium.com/media/0aff82c93d6152c18802a156e7030996/href</a></iframe><h3>Conclusion</h3><p>Out of the box, Unity will support motion blur by generating a velocity buffer for you, but for our requirements this was overkill. We always need to keep in mind that we are a mobile studio, so we need to take performance into account every step of the way. The method we implemented has its tradeoffs, we had to add distance based scaling to prevent objects in the distance blurring too much. However, it gave us a convincing effect due to the fact that our camera is constantly moving. If you have any questions or feedback, feel free to drop me a message on <a href="https://twitter.com/mjshorty">Twitter</a> or leave a comment below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=656c8047508" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/motion-blur-for-mobile-devices-in-unity-656c8047508">Motion Blur for mobile devices in Unity</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Shared Unity Code: The Implementation]]></title>
            <link>https://medium.com/spaceapetech/shared-unity-code-the-implementation-a5f2b94f2173?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5f2b94f2173</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[shared-code]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[nuget]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Thu, 05 Jul 2018 14:10:57 GMT</pubDate>
            <atom:updated>2018-09-25T14:24:35.641Z</atom:updated>
            <content:encoded><![CDATA[<p>If you haven’t already read it, take a look at the first in this two parter. It covers the journey we took and why we decided to give Nuget a shot (<a href="https://medium.com/spaceapetech/sharing-unity-code-the-journey-bd81a0016e4a">Shared Unity Code: The Journey</a>).</p><h3>How we store NuGet packages</h3><p>Our server team use a packaging system called Maven.</p><p><em>We actually experimented with sharing code using Maven too, but it was overly complex for our needs and didn’t quite fit what we were looking for.</em></p><p>We have a lot of shared server code, and this code lives in a Maven repository hosted on a service called<a href="http://artifactory.jfrog.io/"> Artifactory</a>. As it turns out Artifactory supported Nuget too, so we were able to use this to host our private Nuget repository!</p><p>For testing purposes you can easily create a private repo on an internal server, an AWS node or even just a new folder on someone’s computer on a shared network — if you do this, just be sure that its backed up! All of these options are covered in <a href="https://docs.microsoft.com/en-us/nuget/hosting-packages/overview">this article</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_BSX61CxShyqW7oT7Kgc8Q.jpeg" /></figure><h3>Setting up NuGet package</h3><p>Once you’ve got somewhere to store your packages, it’s time to get people uploading and downloading them. You need to first install Nuget on your system.</p><p>On Windows I’d recommend using<a href="https://chocolatey.org/"> Chocolatey</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48b53b9516d1588c1a6305ff43721fbe/href">https://medium.com/media/48b53b9516d1588c1a6305ff43721fbe/href</a></iframe><p>On MacOS I’d use<a href="https://brew.sh/"> Homebrew</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f393ac7a292c4a12d71354256d1cb6e0/href">https://medium.com/media/f393ac7a292c4a12d71354256d1cb6e0/href</a></iframe><p>You’re also going to let Nuget know about your private server, you can do this by firing up the terminal/command prompt and running the following commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6adafc779f18e5490e6b6cc279019b1f/href">https://medium.com/media/6adafc779f18e5490e6b6cc279019b1f/href</a></iframe><p>If your using a folder on your local machine, you’d do something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/baf4d88703d8cad535ce55ae236b1d26/href">https://medium.com/media/baf4d88703d8cad535ce55ae236b1d26/href</a></iframe><p>If you have setup a username, password and/or API key to your server you will also need to let Nuget know about this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/350ce50288e93643ace07d881e5f1079/href">https://medium.com/media/350ce50288e93643ace07d881e5f1079/href</a></iframe><h3>Consuming Packages</h3><p>Once you’ve done this you can start downloading packages from your private server! Doing this is simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a439a7e1a98a60c01ccc00331e5dc0bd/href">https://medium.com/media/a439a7e1a98a60c01ccc00331e5dc0bd/href</a></iframe><p>However this isn’t ideal on a project with multiple developers.</p><p>Each package is versioned, and packages have dependencies on other packages that are also versioned. Therefore we use a configuration file, per project, to specify what packages we are going to use. Here is an example packages.config file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ad60a79034a19f7b9dd0c9f6779b844/href">https://medium.com/media/3ad60a79034a19f7b9dd0c9f6779b844/href</a></iframe><p>Each entry in this file specifies a package name, a version number and a target (the .Net framework version). You can find all ther reference material for the config file<a href="https://docs.microsoft.com/en-us/nuget/reference/packages-config"> here</a>. When we have a packages.config file we can then use it to install our packages:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e1e16d074c1775ad4e843cf6d8cfabb/href">https://medium.com/media/4e1e16d074c1775ad4e843cf6d8cfabb/href</a></iframe><p>This will go through every package in your config file and download it to your computer.</p><h3>Releasing Packages</h3><p>In order to release a package you have to create a <em>Nuspec </em>file. This file will define all the information about your package, including:</p><ul><li>Package Name</li><li>Developer</li><li>URL</li><li>Package Version</li><li>Dependencies</li><li>Files</li></ul><p>It’s pretty important to keep this file up to date, if you change a dependency or update one of the dependencies to a newer release, update your <em>Nuspec </em>file!</p><p>A <em>Nuspec </em>file is actually just another XML document, here is an example of one of ours:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42f893ab158c86133297fb25e7dd7e0d/href">https://medium.com/media/42f893ab158c86133297fb25e7dd7e0d/href</a></iframe><p>As you can see there’s quite a lot of information in there, but once you’ve set this file up, it wont change much. For all the details on Nuspec files, you can head over to the<a href="https://docs.microsoft.com/en-us/nuget/reference/nuspec"> reference page</a>. So, now you have your project set up, its building, the tests are passing and you have your <em>Nuspec </em>file ready to go. Jump back into the terminal and run the following commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/603b685e66795294bbf49138551cb928/href">https://medium.com/media/603b685e66795294bbf49138551cb928/href</a></iframe><p>The first command will package up all the files into a <em>nupkg</em> (which is actually just a .zip file, you can change the extension to .zip and unzip it to see what’s inside it) file and the second will push it up to your repository.</p><p>People can now use your NuGet package!</p><p><em>We actually build and release all of our modules via Jenkins.</em></p><p>For the entire Nuget Command Line Interface reference, head over to<a href="https://docs.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference"> here</a>.</p><h3>Multiple Teams, Multiple Versions of Unity</h3><p>You may have noticed that we hijack the target field to distribute different DLL’s compiled against different versions of Unity in a single package. This is quite unconventional and is something we’ve only recently started trying, but it seems to be working ok so far.</p><p>It means a single package can contain a DLL compiled against Unity 5.6 and Unity 2017, but using the target field we specify that our game is only interested in the Unity 2017 DLL.</p><p>This allows us to easily support multiple teams with the same shared modules.</p><h3>Building Multiple DLLs</h3><p>In order to build our modules against different versions of Unity we change the C# project file so that it has multiple configurations — one for each version of Unity that we are building against.</p><p>Each configuration then has the required preprocessor defines assigned to them (e.g. UNITY_5_6_OR_NEWER, UNITY_2017_1_OR_NEWER etc). And each configuration links to the correct Unity DLL’s.</p><p>We can then use the exact same source code in a test Unity project to quickly track down any bugs or issues we might encounter.</p><h3>Unity and NuGet</h3><p>When you create prefabs in Unity and assign a custom script to that prefab, the GUID of that script is stored within the prefab. So if the GUID of the script ever changes, your prefab will lose the script reference.</p><p>So next time you release your package, and you’re super excited to get everyone to update, they do so, and all their prefabs break. Why?</p><p>Nuget places the version number in the name of the DLL <em>(eg SpaceApe.PerformanceTesting.1.0.0.dll)</em></p><p>When you update your package, the version number will change (which changes the filename too!)</p><p>Unity calculates the GUID of a script within a DLL by taking</p><ul><li>the name of the DLL</li><li>the name of the script</li><li>And then hashing them together</li></ul><p>Therefore when we install packages we use a special –excludeversion option that doesn’t put the package version number onto the DLL and we don’t delete the DLL’s meta file, eg:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66c07b7ebd7b28b06517ad639fe78314/href">https://medium.com/media/66c07b7ebd7b28b06517ad639fe78314/href</a></iframe><h3>Where we are now</h3><p>So as it stands everything is working really well for us.</p><ul><li>We can share code between teams</li><li>We can support multiple versions of Unity</li><li>Our code is shipping with more and more tests</li><li>A lot of our modules are well documented and the documentation is automatically updated on each release</li></ul><p>This all leads to one thing, our dev teams are able to get on with what matters — making that genre defining hit.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5f2b94f2173" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/shared-unity-code-the-implementation-a5f2b94f2173">Shared Unity Code: The Implementation</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sharing Unity Code: The Journey]]></title>
            <link>https://medium.com/spaceapetech/sharing-unity-code-the-journey-bd81a0016e4a?source=rss-e5c131d8c2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd81a0016e4a</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[nuget]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Michael Short]]></dc:creator>
            <pubDate>Thu, 31 May 2018 10:06:16 GMT</pubDate>
            <atom:updated>2018-09-25T14:24:01.635Z</atom:updated>
            <content:encoded><![CDATA[<p>At Space Ape we have a lot of shared code which we use across all of our projects. As of now, we have 57 shared code modules; these range from messaging systems, logging and our in-game console, to crowd rendering and asset processing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hlLc5qSPzPShJWlqeiPk4Q.jpeg" /></figure><h3>Why Share Code?</h3><ul><li>We don’t need to spend <strong>time</strong> reinventing the wheel.</li><li>We have the <strong>flexibility</strong> to take the shared modules we require.</li><li>We’re <strong>confident</strong> that this code comes with a suite of unit and integration tests.</li><li>We know it’s been <strong>proven</strong> in practical use across previous projects.</li><li>We are all <strong>familiar</strong> with the code, making project start-up times much faster.</li></ul><p>Sharing code allows developers to focus more on the final product, in our case the games we create, because they spend less time worrying about low-level implementation details and non functional requirements.</p><h3>The Journey</h3><p>All of this shared code didn’t just happen overnight. We’ve tried and failed quite a few times with our release process, distribution and collaboration. <strong>As a result of failing and learning from these failures, we’re now able to easily share our code, and developers from various projects are all contributing to it.</strong></p><p>The process is by no means perfect — we’re still working out the kinks — but it’s improved our workflow a lot, and maybe it will work for you too.</p><h3>At the Beginning, there was only Ctrl+C, Ctrl+V</h3><p>When Space Ape first started we were all focused on one project, so there was no need to share code. We were a start-up and our primary objective was to ship a product.</p><p>When the time came to start our second project, some of the code was copied over from the first project and we remove any project dependencies, but we had a code base to start with. Our first title was still being developed, bugs were being fixed, and features were being added.</p><p>Whilst all of this was happening the two code bases diverged quite a bit. If a bug was fixed in one of our games, there’s a good chance that fix didn’t make it into the other game — the same goes for features and improvements. As each project went on, the ‘shared’ code was modified to fit each project, and in the end sharing code between projects was more hassle than it’s worth.</p><h3>Context of the Problem</h3><p>Roll forward a few years. We now have a few established games, Samurai Siege, Rival Kingdoms, Transformers: Earth Wars and Fastlane, and we’ve entered into a partnership with Supercell.</p><p>Unsurprisingly, our company goals have changed a bit. We’re no longer a start-up in the true sense of the phrase. Our goal is to create a genre defining mobile hit, and in doing so we are moving away from our build and battle heritage.</p><p>We are branching out into many new genres, so we need to iterate quickly. We can’t predict that every new game idea will be a success. We need to try new things rapidly, and learn as quickly as possible.</p><p>Having a solid foundation of shared code would help us to iterate faster. In order to do this we would have to look at how we could share code between projects, with as little pain and slowdown as possible. When it comes to sharing code, <strong>the biggest obstacle is not writing the code itself, it’s the tooling and practices around releasing and distributing it.</strong></p><h3>Enter Git Submodules</h3><p><a href="https://git-scm.com/book/en/v2/Git-Tools-Submodules">Git Submodules</a> are like a repository within a repository. You can continue to work on your code base and once you’ve finished a feature or fixed a bug, you can check it in. You just push your shared code up to one repository, and your project’s code to another.</p><p>This seemed ideal at first! We were already using Git across our studio so everyone was familiar with it. But we soon ran into problems.</p><p>As the source code is there for you to edit freely, teams would obviously change shared code, check it in and then when the other team pulled changes, their code wouldn’t compile! This sounds a little lazy and reckless, but this issue stems from the fact that there is no boundary between what is shared code and what isn’t. From a team’s point-of-view, they are just changing code in one big solution. The ideal solution here is to expose a simple yet well-defined API to the game teams.</p><p>So once this became an issue, each team decided to branch the shared modules off the master branch, and we were back to square one. Two diverging code paths, never merged together.</p><p>Further to this, we found that anyone who’s not a developer (artists, animators etc) can have quite a hard time using submodules. The tooling around submodules isn’t straight forward. Often we would update a submodule but someone wouldn’t pull changes for that submodule, so project and shared code would get out of sync.</p><h3>Maven</h3><p>Our server developers use <a href="https://maven.apache.org/">Maven</a> to manage and release packages. Maven is a tool developed for the Java ecosystem. When you are ready to release your project, Maven will take all of the information within a <a href="https://maven.apache.org/pom.html">pom file</a> and then package up your code so that it can be shared with others.</p><p>Because of all the features offered by Maven, and the fact that it’s not a native .Net tool chain, it often felt more complicated than it needed to be. Out of the box it comes with things like build life-cycle management. But at the end of the day all we were really interested in was dependency management, versioning and packaging; and that came with a lot of overhead. We ended up creating custom build steps to install our packages which made our build and release process even more complicated. As it wasn’t natively supported (or developed for) either Unity or .Net we felt that there must be a better solution.</p><h3>Unity Packages</h3><p>Because we are using Unity, the next technology that came to mind was Unity Packages, just like you see on the Asset Store. It was really easy to integrate. However, the whole release process and package storage was quite unregulated. There’s no real package versioning support and no dependency management. You also need additional tooling to uninstall a package as there’s no defined package structure, so we would have to clean up the old package before installing the new one.</p><p>Finally, Unity packages traditionally contained source code. We wanted to stop teams making changes to source code within these shared modules and improve compile times. This meant we needed to use Dynamic Link Libraries. DLL’s also allow us to easily develop shared code modules that depend on other modules, without having to make sure that the source code for the dependency was the correct version and compiled in the first place. Whats more using DLL’s would also lead us to faster compile times.</p><p>So we looked elsewhere, and found:</p><h3>NuGet</h3><p>If you’ve not come across Nuget before, it’s a package management system designed specifically for the .Net framework and it supports dependency management. There are currently over 110,000 packages on the<a href="http://nuget.org/"> public repository,</a> some of which we were already using. However this repository is public, and a lot of our code isn’t for public release, so we couldn’t just go ahead and push our packages up to this public repository.</p><p>Before we could make a start there was quite a bit of work involved in setting up a whole development and release process around Nuget, not to mention setting up our own Nuget package server and getting everything to work nicely with Unity. In my <a href="https://medium.com/spaceapetech/shared-unity-code-the-implementation-a5f2b94f2173">next blog post</a> I’m going to take you through everything, from start to finish.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd81a0016e4a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spaceapetech/sharing-unity-code-the-journey-bd81a0016e4a">Sharing Unity Code: The Journey</a> was originally published in <a href="https://medium.com/spaceapetech">spaceapetech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>