Fragment Shader

What is Fragment Shader (fragment shader)?

If you have used computer drawing experience, you know that in this process you need to draw a circle, then a rectangle, a line, some triangles... until you draw the image you want. This process is much like writing a letter or a book by hand-it's a series of instructions that you need to complete one by one.

Shaders are also a series of instructions, but these instructions will be issued to every pixel on the screen at the same time. In other words, your code must perform different operations based on different positions of the pixels on the screen. Just like movable type printing, your program is like a function (function), input position information, output color information, when it is compiled, it will run at a fairly fast speed.

Why are shaders running so fast?

In order to answer this question, I have to introduce the magic of parallel processing.

Imagine your CPU is a large industrial pipeline, and then every task is something that passes through this pipeline - just like a production pipeline. Some tasks are bigger than others, which means it takes more time and energy to process. We just say it requires more processing power. Because of the computer's own architecture, these tasks need to be serialized; that is, they must be completed sequentially one at a time. Modern computers usually have a set of four processors that operate like this pipeline, processing these tasks one after another, so that the computer runs smoothly. Each pipeline is usually called a thread.
Insert picture description here
Video games and other graphics applications require much higher processing power than other programs. Because their graphic content needs to manipulate countless pixels. Think about it, every pixel on the screen needs to be calculated, and geometry and perspective also need to be calculated in a 3D game.

Let's go back to the metaphor from the beginning about pipelines and tasks. Each pixel on the screen represents the simplest task. It is easy for the CPU to complete the task of any pixel alone, so the problem is that every pixel on the screen needs to solve such a small task! In other words, even for an old screen (resolution 800x600), it needs to process 480000 pixels per frame, that is, 14.440,000 calculations per second! Yes, this is a big problem for microprocessors! For a modern 2800x1800 retina screen, running 60 frames per second requires 311040000 calculations per second. How did graphics engineers solve this problem?

Insert picture description here
At this time, parallel processing is the best solution. Compared to using three or five powerful microprocessors (or "pipes") to process this information, it is much better to use a lot of small microprocessors to perform parallel calculations. This is the origin of the Graphic Processor Unit (GPU: Graphic Processor Unit).

Insert picture description here
Imagine a bunch of small microprocessors arranged in a plane picture, suppose the data of each pixel is a ping pong ball. 14.440,000 ping-pong balls can block almost any pipe in one second. But an 800x600 pipe wall can be smoothly completed by receiving 30 waves of 480,000 pixels of information per second. This is also true at higher resolutions-the more parallel processors, the larger the data stream that can be processed.

Another GPU magic is that special math functions can be accelerated by hardware. Very complex mathematical operations can be solved directly by the microchip without the need for software. This means that there can be faster triangle and matrix operations-as fast as current.

What is GLSL?

GLSL stands for openGL Shading Language, openGL Shading Language, which is the specific standard that the programs you see in the next chapters follow. Depending on the hardware and operating system, there are other shaders (shaders). Here we will follow the rules of Khronos Group. Knowing the history of OpenGL will help you understand most strange conventions, so I suggest you read openglbook.com/chapter-0-preface-what-is-opengl.html.

Why is Shaders notoriously hard to learn?

Just like the famous saying in Spider-Man, the greater the ability, the greater the responsibility, and the same is true for parallel computing; GPU's powerful architecture design also has its limitations and shortcomings.

In order to make many pipelines run in parallel, each thread must be independent of the others. We call these threads "blindly watching" the operations performed by other threads. This restriction will cause all data to flow in the same direction. Therefore, it is impossible to check the output results of other threads, modify the input data, or input the output results of one thread to another thread. If data flow from thread to thread is allowed, all data will be threatened.

And the GPU keeps all parallel microprocessors (pipes) always busy; as long as they are free, they will receive new information. It is impossible for a thread to know what it was doing at the moment. It may be drawing a button on the operating system interface, then rendering a part of the sky in the game, and then displaying some text in an email. Each thread is not only "blind-sighted", but also "memoryless". At the same time, it requires writing a general rule to output different results in turn according to different positions of the pixels. This abstraction, and the limitations of blindness and memorylessness make shaders not very popular among new programmers.

But don’t worry! In the following chapters, we will learn the shading language step by step, from the shallower to the deeper. If you are reading this tutorial with a reliable browser, you will enjoy reading and playing with the examples in the book. Well, don't waste any more time, go and play! Click Next >> Start the shader journey!

Guess you like

Origin blog.csdn.net/weixin_38616850/article/details/107895618