본문 바로가기
College Study/GLSL

[GLSL] What is a shader?

by 2den 2022. 1. 5.
728x90

 

 


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.

 

A shader is a function that calculates the position and color of a pixel to be displayed on the screen. (쉐이더란 화면에 출력할 픽셀의 위치와 색상을 계산하는 함수이다.) Then why are shaders fast?

 

Every single pixel on the screen needs to be computed, and in 3D games geometries and perspectives need to be calculated as well.

CPU
GPU

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? ... 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. ... the more parallel hardware you have, the bigger the stream it can manage. 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.

 

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.

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.

 

Shaders are classified into vertex shaders and fragment shaders. In this course, I'm going to study fragment shaders, a.k.a. pixel shaders.

 

728x90

'College Study > GLSL' 카테고리의 다른 글

[GLSL] Smoothstep  (0) 2022.01.05
[GLSL] Fragment Coordinate (gl_FragCoord)  (0) 2022.01.05
[GLSL] Uniform  (0) 2022.01.05
[GLSL] Hello World  (0) 2022.01.05
[GLSL] Let's Dive in to the Shader!  (0) 2022.01.05

댓글