Introduction to Shaders
Shaders are an essential part of our workflow in LightningChart. They are used to achieve outstanding performance through delegation of the calculation process to Graphic Card. They also provide us with an ability to implement custom visualization algorithms in a very close to the hardware level.
GPU is an example of an extremely parallel hardware. Efficient utilization of its power requires parallel data computation. Computation Pipeline is a very good approach for this scenario. It can be used in a situation when parallel computation is applied to some specific problem. Then the case can be generalized and broken to steps with certain sets of valid input and output parameters. In such pipelines data travels from stage to stage sequentially. Rigid data flow allows performing automatic parallelization of the computation process efficiently.
There are programmable and unprogrammable parts of computation pipeline. Programmable steps are represented by some kind of pure functions which have access to the isolated part of the entire dataset provided as an input. In addition, they can access a set of the immutable objects which is supplied at an initialization step. Unprogrammable stages implement a part of logic which is generic for the pipeline. For example, they manage data distribution across computational units.
The pipeline approach is very common nowadays; for example, Apache Hadoop (MapReduce) uses it to perform Big Data analysis on computer clusters. They break computation into three steps: Map (sorting, filtering), Shuffle (automatic redistribution of a dataset according to Map results) and Reduce (summary operation). A combination of chained higher-order functions allows Apache Spark to build highly scalable, fault tolerant custom pipelines for real-time data processing. Graphic Pipeline represents an application of the concept to GPU-accelerated computer graphics.
Every Graphic Library has its own implementation of Graphic Pipeline. Their common feature is the fixed order of computational stages. The unprogrammable part of the pipeline is called fixed functions. Programmable parts are called Shaders. In LightningChart, we are using DirectX 9 and 11, so some of the terminology used further in the article can be specific for their implementation of Graphic Pipeline.
The computational pipeline of modern Graphic Library contains four types of Shaders: Vertex, Tessellation, Geometry and Pixel (Fragment). Shaders are usually implemented in specially designed C-like languages with a very simple syntax. The main aim of the languages is clear and efficient declaration of computations which are needed for realistic 3D space visualization.
Graphics Pipeline receives a collection of vertices as an input. For a successful invocation, it also needs to set options for fixed functions, code of correspondent shaders and constant values for them. An output of the pipeline processing is displayed on the screen or returned as a texture.