Reader small image

You're reading from  Become a Unity Shaders Guru

Product typeBook
Published inJul 2023
Reading LevelN/a
PublisherPackt
ISBN-139781837636747
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Mina Pêcheux
Mina Pêcheux
author image
Mina Pêcheux

Mina Pêcheux is a freelance content creator who has been passionate about game development since an early age. She is a graduate of the French Polytech School of Engineering in applied mathematics and computer science. After a couple of years of working as a data scientist and web developer in startups, she turned to freelancing and online instructional content creation to reconnect with what brightens her days: learning new things everyday, sharing with others and creating multi-field projects mixing science, technology, and art.
Read more about Mina Pêcheux

Right arrow

Exploring the Unity Compute Shaders and Procedural Drawing

Up until now, we have focused mainly on the most common application of shaders in video games: the rendering of objects in 3D scenes. We have seen various tools and techniques and studied complex shading tricks, but they all aimed at visualizing some geometry on our 2D screen.

However, shader scripting is a very interesting type of development that can be used to solve other problems, too. For example, because, at its core, it is about writing low-level code to run in parallel for many data points, the art of shader creation can be transposed to another fascinating topic: the efficient processing of expensive computational tasks.

In Unity, it is possible to greatly increase the speed of some large calculations by taking advantage of the power of shaders and offloading the work from the CPU to the GPU, using compute shaders. This, in turn, can lead to delightful advanced techniques in the field of procedural drawing.

...

Technical requirements

To try out the samples yourself, you will need to have Unity installed.

The Generating a grid of randomized cubes section is compatible with both the legacy and URPs, but the Applying a compute shader-based screen effect in URP section will require that you use Unity 2019 or later and set up a URP project. To do this, you can do either of the following:

  • Start with the common 3D template, and then upgrade it to use the URP render pipeline (see the Stepping up with the URP render pipeline section of Chapter 2)
  • Start with the new 3D URP template (see the Stepping up with the URP render pipeline section of Chapter 2 for guidance on how to download and pick this template)

You can also find all the code files for this chapter on GitHub, at https://github.com/PacktPublishing/Become-a-Unity-Shaders-Guru/tree/main/Assets/Chapter%2007.

Discovering compute shaders and compute buffers

Compute shaders are quite a unique tool that has many applications, such as the rendering of procedurally generated shapes (see Figure 7.1, a screenshot taken from https://www.youtube.com/watch?v=WWI07UQbJ9E) or the large calculations in a complex system.

Figure 7.1 – Render of the Mandelbrot Set using compute shaders

Figure 7.1 – Render of the Mandelbrot Set using compute shaders

In the following sections, we will see what compute shaders are exactly, why they can be useful, how they work hand in hand with compute buffers to properly share data between the CPU and the GPU, and how they are structured.

What are compute shaders?

In a nutshell, compute shaders are a specific type of shader script that are meant to offload processing from the CPU to the GPU.

The main idea is that because the GPU runs code in parallel, it can achieve better performance for computationally intensive tasks and large amounts of data – typically, heavy rendering algorithms...

Generating a grid of randomized cubes

Alright – we now know what compute shaders and compute buffers are. To truly understand how they can be used in a real-life scenario, however, let’s discuss a basic example where compute shaders can greatly improve performance: the generation of a grid of randomly positioned and colored cubes.

We will first see how this simple process can be performed with a naive approach that runs on the CPU to get familiar with the problem, and then explore the limitations of this implementation. Finally, we will transfer our logic over to the GPU by using a compute shader and see how it boosts our performance.

Writing a naive C# implementation

Before diving into GPU offloading and performance enhancement, it stands to reason that we must first establish a baseline – or, in other words, a reference to compare our “improved” version against. So, first, let’s create a simple implementation of our random color...

Applying a compute shader-based screen effect in URP

So far, we have seen how to use compute shaders to optimize calculations on pure data. We eventually used the information to modify cube visual properties, but it was a sort of “side-effect” to make it easier to see the results: in theory, we could have simply stuck to the compute part and measured the execution time to compare our CPU and GPU process performances.

Still, screen effects and postprocessing are big applications of compute shaders, so we definitely need to have a look at how we can use these shaders to directly render something on our screen.

To test things out, we are going to create a very simple compute shader that fills the screen with red pixels. The goal is thus to effectively end up with a fully red screen. I know, it is not a very exciting idea, but it will be a good way to understand the basics of using compute shaders in a URP project without too much distraction!

In the following sections...

Summary

In this chapter, we dived into the compute shaders and the compute buffers, and we explored how these tools can be used in a variety of situations to render demanding visuals or optimize heavy computations.

We first zoomed in on the base concepts and introduced how compute shaders allow us to offload processing from the CPU to the GPU by defining HLSL kernel functions for parallel execution and transferring data via compute buffers.

We then implemented a basic grid randomization process on the CPU and studied how refactoring our code to use the GPU improved the performance significantly by reducing the execution time, in particular, for large amounts of data.

Finally, we talked about another common application of compute shaders, namely the postprocessing and screen effects, and we saw how to set up a URP project to handle this special type of rendering, thanks to a new custom render feature.

Of course, compute shaders are a vast topic, and they have hundreds of...

Going further

If you’re curious about procedural drawing and compute shaders, here are a few interesting resources to check out or continue your journey with:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Become a Unity Shaders Guru
Published in: Jul 2023Publisher: PacktISBN-13: 9781837636747
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Mina Pêcheux

Mina Pêcheux is a freelance content creator who has been passionate about game development since an early age. She is a graduate of the French Polytech School of Engineering in applied mathematics and computer science. After a couple of years of working as a data scientist and web developer in startups, she turned to freelancing and online instructional content creation to reconnect with what brightens her days: learning new things everyday, sharing with others and creating multi-field projects mixing science, technology, and art.
Read more about Mina Pêcheux