Unity Shader Tutorial: An intro to Compute Shaders

Are you ready to turn this up to 11? We are going to look at some real “Triple A” business now. The world of compute shaders. So what are these mysterious  creatures that you probably don’t know exist in Unity?

To be honest, I had completely forgot they were there and I was looking at a fur tutorial (that doesn’t actually seem to work by the way and was also a really dirty way of doing it ) and then remembered my mate had said you could probably do grass and fur in one. I think he actually meant geometry shaders, but compute shaders peeked my interest.

However, after digging around the net it turns out that info surrounding them when it comes to Unity seems quite scarce.

Let’s start from the top!

What is a compute shader, and why should I care?

In Microsoft’s fancy terms, “a compute shader is a programmable shader  stage that expands Microsoft Direct3D11 beyond graphics programming” and “a compute shader provides high-speed general purpose computing and takes advantage of the large numbers of parallel processors on the GPU”.

In simple terms, a compute shader is a program that runs on the graphics card that does stuff outside of the normal rendering pipeline.

So you are probably thinking “OK, I kind of get it, you can run some logic and put some work onto the graphics card, but why would I want to do that?” Well these shaders are really good at maths and parallelization, i.e. they are really good at performing tasks where you are doing a lot of the same thing. In other words, they are really good at tasks that involve applying the same set of calcualtions to every element in a given data set.

This is probably a kind of crappy exlanation, so lets wind the clock back a bit to when I was just gracing the planet with my presence. The 90s. It was a beautiful time with games like Doom, Final Fantasy 7, The Legend of Zelda: Ocarina of Time, Crash Bandicoot, Tekken 3… do I need to go on? Essentially lots of 3D games and PCs started going out with graphics cards. Stuff like this bad boy.

What a rush indeed! Getting that sweet 6MB of power all up in your grill. Anyway OpenGL and DirectX appeared and the magic of the programmable pipeline emerged. Developers just send geometry down to the graphics card and OpenGL/Direct X would figure it out. However, the pipeline was pretty rigid, and thus to make more interesting effects and push the boundaries it had to became more flexible. This led onto shaders, where devs could write their own programs to perform certain parts of the pipeline and make things look like the wizard’s tits.

This the opened up a lot of possibilities and this new system mean that the new pipline could deal with a lot of different types of algorithms and now the GPU can do stuff like crazy multi-threaded physics, etc.

What this means now is we can do crazy stuff like Nvidia’s Hair works.

You on board now? If not, just know it is cool and you feel like a Game Development Maverick when you do it.

Basically, you can potentially harness the GPU to do none graphicsy stuff if you so desire and gain MOAR POWER.

Sod it, lets jump in!

That’s the attitude I want!

Before you start though you need a WINDOWS machine. Macs don’t have it. And to be honest they are kinda crappy for big boy game development like this anyway 😛

Create a compute shader in Unity.

The first thing you will notice is that this is not CG. This is a Direct X 11 style HLSL bad boy. Yeah fasten your seat belts boys and girls.

// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain

// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;

void CSMain (uint3 id : SV_DispatchThreadID)
     // TODO: insert actual code here!

     Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0);

So the above is what you get if you just create one from scratch.

The #pragma kernel CSMain is the first thing we see in our shader. Kind of like the other shaders, this is us telling the program where our entry point is, in this case CSMain. A compute shader can have many a function and you can call a specific funciton from a script. More on that later.

The next bit is a RWTexture2D<float4> Result

Again, like our other shaders, this is just a variable declaration. However, as we aren’t using mesh data, we have to say what the shader will read and write to. In this case we have a RWTexture2D a read/write texture 2d object that the program is gonna use. Take a look at MSDN for reference:


Finally, the last super different thing is the numthreads which is the the number of thread groups that are spawned by our shader. GPUs love the parallel processing business and create threads that run simultaneously.  This line is specifying the dimensions of the thread groups. These specify how the threads that are created are organised and in this case we are saying that we want to create 64 threads. Take a look at msdn for refrence:


The size of your thread groups will be determined by a lot of factors and probably most notably your target hardware. For example, the PS4 may have  a different optimum size compared to the Xbox One.

The rest is kind of bog standard code. The kernel function determines what pixel it should be working on based on the uint3 ID of the thread running the function and writes some data to the result buffer.

Cool. We have our first compute shader! But how do we actually run this warlock? It doesn’t run on mesh data so we can’t attach it to a mesh. We need to grab it from a script.

But hold up, before we start lets change up our script that Unity spat out and make the compute shader do something different.

// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain

RWStructuredBuffer<int> buffer1;
void CSMain (uint3 id : SV_DispatchThreadID)
    buffer1[id.x] = mul(id.x, 2.0);

You can see we switched out the texture for a structured buffer. This is just an array of data consisting of a single data type, in this case it is an int. In the code you can see we are just taking the id of the thread and multiplying it by 2.

Cool lets write a new script.

using UnityEngine;
using System.Collections;

public class RunComputeShader : MonoBehaviour
    private ComputeShader _shader;

    void Start()
       ComputeBuffer buffer = new ComputeBuffer(4, sizeof(int));

       _shader.SetBuffer(0, "buffer1", buffer);

       _shader.Dispatch(0, 1, 1, 1);

        int[] data = new int[4];


        for (int i = 0; i < 4; i++)


Firstly we are creating a compute buffer the size of an int, a buffer that ComputeShader programs use to store arbitrary data and then we are using the SetBuffer to tell the shader to dump data in there. We use the dispatch function to run our shader and then grab the work the shader has done.

If you set up the above you should see in the debug window it print out some numbers. Yeah it did that on the Graphics card.

Alright fine, it wasn’t the most crazy thing in the world, but it is just showing you that work other than just rendering pretty images can be done.

Round up

This is a post to show you compute shaders are there. I am not saying go out and use them everywhere. The GPU can be used to do some cool multi threaded tasks, however a word to the wise. The tasks that the GPU can do are going to be limited and you really have to look at the problem you are trying to solve before you go down this path. If your game is gonna be super pretty in, you porbably want to be maxing out the gpu on that first before offloading stuff the cpu can do onto it. If your GPU is jsut idling though… maybe on like some lower poly strategy game, etc then maybe consider offloading some of the logic to the GPU using a compute shader.

Well until next time!

Unity Shader Tutorial: Surface Shader Custom lighting

Yes I know I said I wasn’t going to do a tutorial this weekend, but I was learning some shader stuff for my project and The Cel-Shading in Tales of Vesperia, Guilty Gear Xrd and other games are a big inspiration. I was following a gamasutra article on how to do this and I saw he was using a custom lighting function within a surface shader.

When working on some projects, you don’t always want to go for super realistic lighting. You may want to use a custom lighting model rather than just the built in one. Here is the shader code I am using, a slightly modified version of the one in the gamasutra article:


Take a look at the the first pragma line where we are defining the surface shader function and the lighting mode. However instead of using one of the built-in lighting models we are using a method we have defined. Scroll down the shader and you will see the method “LightingCelShadedUsingForwardRendering”. This is the custom lighting calculation we are using. This is a function which you can define in various ways that returns a half4.

The one thing that caught me out when creating this custom method is you need to prefix it with “Lighting” and then when you define which lighting method in the #pragma you are using you do this minus the lighting prefix. You are now using custom lighting in your shader! Neat huh?

It is worth taking a further look at the Unity docs to see other examples of how you can define your own lighting models


Unity Shader Tutorial: Talking to Shaders through C# scripts

Hey look, a picture of Final Fantasy XV. Probably the game I am most excited for because I enjoyed everything about the demo. I might even go back and play that again this evening.

Anyway, we have been talking about how we can write some shaders, but how can we hook them up to C# scripts to create cool image effects? Well, we can have a look at one in the Standard Assets. Firstly, open up your Unity project then go Assets->Import Package->Effects. Once you have that, open up ScreenOverlay.cs. You can attach this to a camera if you want to see what it does, but we should jump into the important bits.

Firstly, you will see at the top of the class there is a public variable for the Shader and a private one for the Material. If you keep going down the class, you will see there is a CheckShaderAndCreateMaterial inside the Check Resources method. If youa re in Visual Studio (and you should be because MonoDevelop is garbage), Right click and got to the definition of that function and you will find yourself in the PostEffectsBase, the base class for a fair few of the Post Processing Effects.

This particular function is Creating the Material. If I haven’t said it already by now, every Shader needs a Material, and as the Post Processing Effects work a tad bit differently to the whole “create material through inspector” then “select which shader to use with it”, the Post Processing effect needs to use this function to create a material. As you can see, if all is well, it creates a new material with the given shader.

That bit is important, but is not always needed, it is only needed here because of the way the Post Processing Effects work. If you head back to the ScreenOverlay.cs file and scroll down to the OnRenderImage function, you will see inside there there are functions being called on the Material called things like “SetVector” that take a string and then the value type that corresponds to their function.

Head back into Unity and search for the BlendModesOverlay shader.

Inside the shader you will see there are variable sin the shader like “half _Intensity”.  Now if you head back to ScreenOverlay.cs you can see the line “SetFloat(“_Intensity,” intensity”)”. Essentially we can set textures, floats, vectors, etc that we declare in shaders through C# code by using the functions. You can also grab values to. For full reference, head over to to the Unity doumentation.