본문 바로가기
셰이더 (Shader)/The Book of Shaders (완)

[GLSL] 3 - Uniforms, gl_FragCoord

by Minkyu Lee 2023. 4. 14.

uniform

// uniform이란

Thread들은 서로에 대해 데이터를 공유할수 없더라도, CPU에서 인풋을 받을수 있다. 

이 인풋들은 모든 Thread들에 있어서 일정(uniform)하고 read only이다.

즉, 읽을순 있어도 변경할 수 없다.

 

이런 인풋들을 uniform이라고 한다.

float, vec2, vec3, vec4, mat2, mat3, mat4, sampler2D, samplerCube 등의 데이터 타입을 지원한다.

유니폼 값들은 보통 floating pont precision설정이 끝난 후 선언된다.

 

// uniform 선언 위치

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution; // Canvas size (width,height)
uniform vec2 u_mouse;      // mouse position in screen pixels
uniform float u_time;     // Time in seconds since load

 

// uniform 사용 예제

유니폼은 CPU와 GPU사이에 다리라고 봐도 좋을것이다.

 u_ 를 변수앞에 붙혀서, 유니폼이라고 명시한다는 점도 유의하기 바란다. 

 

아래 코드는 u_time을 sin 함수에 인자로 넣어 빨간색 값을 조절한 것이다.

#ifdef GL_ES
precision mediump float;
#endif

uniform float u_time;

void main() {
gl_FragColor = vec4(abs(sin(u_time)),0.0,0.0,1.0);
}

gl_FragCoord

gl_FragCoor는 pixel의 위치를 가지고 있는 값이다.

 

이값은 uniform값과는 다르다. 

각 쓰레드마다 값이 다른 varying타입이다.

 

GLSL의 기본 출력으로는 vec4 gl_FragColor가 있다.

기본 입력으로는 vec4 gl_FragCoord가 있다.

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main() {
vec2 st = gl_FragCoord.xy/u_resolution;
gl_FragColor = vec4(st.x,st.y,0.0,1.0);
}

 

위 코드는 좌표를 0~1로 노말라이즈한 것이다.

쉐이더 작업에서는 다양한 디버깅 기능을 사용할 수 없다.

그래서 값을 색에 대입해 예측하는 방법을 쓴다.



댓글