What is a shader

更新时间: 2023-12-01 16:20:53

# What is a fragment shader?

In the previous chapter we described shaders as the equivalent of the Gutenberg press for graphics. Why? And more importantly: what's a shader?

If you already have experience making drawings with computers, you know that in that process you draw a circle, then a rectangle, a line, some triangles until you compose the image you want. That process is very similar to writing a letter or a book by hand - it is a set of instructions that do one task after another.

单词

  • instructions n. 指令;说明(instruction 的复数形式)

Shaders are also a set of instructions, but the instructions are executed all at once for every single pixel on the screen. That means the code you write has to behave differently depending on the position of the pixel on the screen. Like a type press, your program will work as a function that receives a position and returns a color, and when it's compiled it will run extraordinarily fast.

单词

  • executed v. 处决;实施;完成(动作);执行(execute 的过去式与过去分词)

  • extraordinarily adv. 极其,极端地;非常奇怪地,不寻常地

# Why are shaders fast?

To answer this, I present the wonders of parallel processing.

单词

  • parallel processing 并行处理

Imagine the CPU of your computer as a big industrial pipe, and every task as something that passes through it - like a factory line. Some tasks are bigger than others, which means they require more time and energy to deal with. We say they require more processing power. Because of the architecture of computers the jobs are forced to run in a series; each job has to be finished one at a time. Modern computers usually have groups of four processors that work like these pipes, completing tasks one after another to keep things running smoothly. Each pipe is also known as a thread.

单词

  • parallel processing adj. 工业的,产业的;有很多产业的,工业发达的;工业用的,用于工业的;(与)工业摇滚(有关)的;因勤劳而得到的
    n. 从事工业的公司;工业股票;工业工人

  • energy n. 能力,力气;精力,活力;能源;能,能量

  • architecture 架构

  • thread 线程

Video games and other graphic applications require a lot more processing power than other programs. Because of their graphic content they have to do huge numbers of pixel-by-pixel operations. Every single pixel on the screen needs to be computed, and in 3D games geometries and perspectives need to be calculated as well.

单词

  • operations n. 运营;运作;业务操作(operation 的复数)

Let's go back to our metaphor of the pipes and tasks. Each pixel on the screen represents a simple small task. Individually each pixel task isn't an issue for the CPU, but (and here is the problem) the tiny task has to be done to each pixel on the screen! That means in an old 800x600 screen, 480,000 pixels have to processed per frame which means 14,400,000 calculations per second! Yes! That’s a problem big enough to overload a microprocessor. In a modern 2880x1800 retina display running at 60 frames per second that calculation adds up to 311,040,000 calculations per second. How do graphics engineers solve this problem?

单词

  • metaphor n. 隐喻,暗喻;象征,标志 网络 隐喻 /暗喻 /比喻 /比喻说话

  • represents v. 代表(represent 的三单形式);表现,表示;描绘

  • individually adv. 分别地,单独地,各别地;独特地,有个性地;个人地,以个人身份地 网络 个别地 /单独地 /单个地

  • tiny adj. 极小的,微小的

  • microprocessor n. [计] 微处理器

  • retina display
    视网膜显示屏

This is when parallel processing becomes a good solution. Instead of having a couple of big and powerful microprocessors, or pipes, it is smarter to have lots of tiny microprocessors running in parallel at the same time. That’s what a Graphic Processor Unit (GPU) is.

Picture the tiny microprocessors as a table of pipes, and the data of each pixel as a ping pong ball. 14,400,000 ping pong balls a second can obstruct almost any pipe. But a table of 800x600 tiny pipes receiving 30 waves of 480,000 pixels a second can be handled smoothly. This works the same at higher resolutions - the more parallel hardware you have, the bigger the stream it can manage.

单词

  • obstruct v. 堵塞,阻塞(洞口、通道、道路等);遮住,挡住(视线);阻碍,妨碍

  • resolutions n. 决议(resolution 的复数);解决;决心;[物]分辨率

Another “super power” of the GPU is special math functions accelerated via hardware, so complicated math operations are resolved directly by the microchips instead of by software. That means extra fast trigonometrical and matrix operations - as fast as electricity can go.

难句

  • Another “super power” of the GPU is special math functions accelerated via hardware GPU的另一个“超能力”是通过硬件加速的特殊数学功能

  • microchips n. 微芯片(microchip 的复数形式)

# What is GLSL?

GLSL stands for openGL Shading Language, which is the specific standard of shader programs you'll see in the following chapters. There are other types of shaders depending on hardware and Operating Systems. Here we will work with the openGL specs regulated by Khronos Group. Understanding the history of OpenGL can be helpful for understanding most of its weird conventions, for that I recommend taking a look at: openglbook.com/chapter-0-preface-what-is-opengl.html

单词

  • conventions n. [法]惯例;会议;[计]约定(convention 的复数)

# Why are Shaders famously painful?

As Uncle Ben said “with great power comes great responsibility,” and parallel computation follows this rule; the powerful architectural design of the GPU comes with its own constraints and restrictions.

单词

  • constraints n. [数]约束;限制;约束条件(constraint 的复数形式)

  • restrictions n. 限制;限制条件(restriction 的复数)

In order to run in parallel every pipe, or thread, has to be independent from every other thread. We say the threads are blind to what the rest of the threads are doing. This restriction implies that all data must flow in the same direction. So it’s impossible to check the result of another thread, modify the input data, or pass the outcome of a thread into another thread. Allowing thread-to-thread communications puts the integrity of the data at risk.

单词

  • independent adj. 自治的,独立的;自立的,自力更生的;公正的,不受他人影响的;分离的,单独的;私营的;无党派的;有主见的,有独立见解的
    n. 独立自主者,独立团体;无党派政治家;公理会教友

  • outcome n. 结果,效果

  • integrity n. 正直,诚实;完整,完全;职业操守;(电子数据的)集成度

Also the GPU keeps the parallel micro-processor (the pipes) constantly busy; as soon as they get free they receive new information to process. It's impossible for a thread to know what it was doing in the previous moment. It could be drawing a button from the UI of the operating system, then rendering a portion of sky in a game, then displaying the text of an email. Each thread is not just blind but also memoryless. Besides the abstraction required to code a general function that changes the result pixel by pixel depending on its position, the blind and memoryless constraints make shaders not very popular among beginning programmers.

单词

  • abstraction n. 抽象;提取;抽象概念;空想;心不在焉
    网络 抽象化 /抽象 /抽象概念 /出神

  • outcome n. 结果,效果

  • integrity n. 正直,诚实;完整,完全;职业操守;(电子数据的)集成度

Don't worry! In the following chapters, we will learn step-by-step how to go from simple to advanced shading computations. If you are reading this with a modern browser, you will appreciate playing with the interactive examples. So let's not delay the fun any longer and press Next >> to jump into the code!