Shaders and the Rendering Pipeline

정민용·2025년 7월 11일

Rendering Pipeline

  • 이미지를 화면에 렌더링하기 위한 과정

Rendering Pipeline Stages

  1. Vertex Specification
  2. Vertex Shader
  3. Tessellation
  4. Geometry Shader
  5. Vertex Post-Processing
  6. Primitive Assembly
  7. Rasterization
  8. Fragment Shader
  9. Per-Sample Operations

Vertex Specification

  • Vertex: 공간에서 좌표를 결정하는 점
  • Primitive: Vertices를 어떻게 해석해서 연결할지를 정의하는 규칙
  • Vertex Specification: 렌더링하고자 하는 정점들의 데이터를 설정하는 것
  • VAOs(Vertex Array Objects), VBOs(Vertex Buffer Objects)를 이용
    - VAO: Vertex가 가진 데이터(position, color, texture, normals, ...) 정의
    - VBO: 데이터 자체를 정의(color, texture, ...)
  • Attribute Pointer: 쉐이더가 어떻게 정점 데이터에 접근할 수 있는지 정의
  • VAO와 VBO를 통해 그래픽 카드에 모든 데이터를 저장할 수 있어 처리 속도에 이점이 생김

Example: Creating Triangle

void CreateTriangle()
{
	GLfloat vertices[] = {
    	-1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f, 1.0f, 0.0f
    };
    
    glGenVertexArrays(1, &VAO);	// Create VAO
    glBindVertexArray(VAO);		// Bind VAO
    
    glGenBuffers(1, &VBO);				// Create VBO
    glBindBuffer(GL_ARRAY_BUFFER, VBO);	// Bind VBO
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);
    
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);		// Unbind VAO
}

Vertex Shader

  • 각 정점에 대해 한 번씩 실행됨.
  • gl_Position 정의 필요

Simple Example

#version 330

layout (location = 0) in vec3 pos;

void main()
{
	gl_Position = vec4(pos, 1.0);
}

Tessellation

  • Data를 더 작은 primitive로 나누어 세부적인 문제를 해결할 때 용이함
  • 동적으로 높은 수준의 세부묘사를 더할 수 있음.

Geometry Shader

  • Vertex Shader는 Vertex를 처리하고, Geometry Shader는 Primitive를 처리함.
  • data, primitive type 변경 가능

Vertex Post-Processing

  • Transform Feedback
    - 매번 모든 Vertex, Geometry Stages를 거치는 것 대신 값들을 버퍼에 저장한 뒤 다음번에 사용하는 형식 이용
  • Clipping: 보이지 않는 Primitives에 대해 그리지 않음으로 처리 속도 향상
    - 좌표계를 clip-space에서 window space로 전환하는 작업 수행

Primitive Assembly

  • Vertex를 Primitive로 재정의
  • Face culling: 볼 수 없는 Primitives를 제거하여 불필요한 렌더링을 줄임

Rasterization

  • Primitive를 Fragment로 전환
  • 각 값들은 각 정점에 대한 상대적인 위치에 기반하여 보정받음

Fragment Shader

  • 해당 픽셀의 실제 색을 출력할 수 있음

Simple Example

#version 330

out vec4 colour;

void main()
{
	colour = vec4(1.0, 0.0, 0.0, 1.0);
}

Per-Sample Operations

  • 여러 test를 통과한 데이터만 Frame buffer에 작성됨.
    - Depth test: 그려지는 지점 앞에 무언가 있는지 확인하며, 만약 가려져서 보이지 않는다면 해당 대상은 그리지 않음.
    - Blending: 창문과 같은 투명하거나 반투명한 물체를 표현하기 위해, 새로 계산된 프래그먼트의 색상과 프레임버퍼에 이미 있는 색상을 공식에 따라 혼합.
  • buffer swap을 작성하여, 업데이트 된 Frame buffer를 전면부로 교체하여 화면에 표시

0개의 댓글