
Write functional and performant shaders is the basic skill for GPU developer no matter what framework or engine he or she uses. But there are not much information about it on the web especially if we talk about Metal. Though shading languages are pretty similar and hours spent on ShaderToy can give you some inspiration and knowledge about OpenGL shaders and you can easily convert them to Metal I personally feel a huge lack of structured information about shader writing. So in this article and others under the title Shaders Explained I’ll go through different shaders in detail, teach you some shader technics and explain my motivation under each line of code. Hope that it will make you and me better shader programmers.
For the first article I chose a pretty basic gradient shader which I implemented for one of my personal projects. Gradients are at the very heart of GPU programming and every shader tutorial starts with the triangle colored with gradient from red, green and blue. But we won’t build yet another Hello Triangle tutorial. Instead I’ll show you how to make gradient shader to fill texture with arbitrary gradient stops with different locations and colors for three basic gradient types. Additionally we’ll add support for rotation. Actually we will implement the most part of CAGradientLayer
or SwiftUI Gradient from scratch with broad focus on its GPU side.

When you build a Swift Package Xcode will compile all your .swift
files to single executable file and process all other resources which includes Assets catalogue, Core Data files, Storyboards and many more including all .metal
files. All these resources will be put in package Bundle and you have access to them through Bundle.module
if you add them to resources
array in package’s target.
But it will work only for library
product type. If you want to build a Swift executable
product resources won’t be processed or copied to Bundle because there is no actual Bundle there. So Swift executable is limited to only .swift
files so you can’t build executable which will run you Metal shaders. Let’s try to fix it!