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...