OpenGL Shading Language
lastmod 2024-04-30created 2014-05-0313 edits
GLSL (OpenGL Shading Language) is a high-level shading language based on the C language. It is also known as GLslang. Similar to HLSL, it was created by the OpenGL ARB (Architecture Review Board) so that developers can directly control the graphics pipeline without using assembly language or hardware-specific languages.
GLSL has profiles, so developers can also directly convert code written in Cg.
Vertex Shader
The Vertex Shader defines where vertices made of 3D coordinates are drawn on the 2D screen. The gl_Position variable is predefined and stores the value pointing to the position of a point on the screen.
void main(void) {
const vec4 vertices[3] =
vec4[3](vec4( 0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4( 0.25, 0.25, 0.5, 1.0));
// 화면에 3개의 점을 그린다.
gl_Position = vertices[gl_VertexID];
}
Geometry Shader
Between the vertex shader and the fragment shader there is an optional shader stage called the geometry shader. A geometry shader takes as input a set of vertices that form a single primitive, for example a point or a triangle. The geometry shader can transform these vertices as it sees fit before sending them to the next shader stage. What makes the geometry shader interesting, however, is that it can convert the vertices into completely different primitives, generating more vertices than were originally given.
Example:
#version 330 core
layout (points) in;
layout (line_strip, max_vertices = 2) out;
void main() {
gl_Position = gl_in[0].gl_Position + vec4(-0.1, 0.0, 0.0, 0.0);
EmitVertex();
gl_Position = gl_in[0].gl_Position + vec4( 0.1, 0.0, 0.0, 0.0);
EmitVertex();
EndPrimitive();
}
Fragment Shader
The Fragment Shader defines what color each pixel is drawn in. It returns a color buffer, and objects not represented in the color buffer are all rendered black. gl_FragColor is the variable used as the function's return value and defines the color shown on the screen.
#ifdef GL_ES
precision mediump float;
#endif
void main() {
// Vertex Shader 에 의해 정의된 영역을 빨간색으로 칠한다.
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}
Versions
GLSL versions have evolved alongside specific versions of the OpenGL API. It is only with OpenGL versions 3.3 and above that the GLSL and OpenGL major and minor version numbers match. These versions for GLSL and OpenGL are related in the following table:
| GLSL Version | OpenGL Version | Date | Shader Preprocessor |
| 1.10.59 1 | 2.0 | April 2004 | #version 110 |
| 1.20.82 | 2.1 | September 2006 | #version 120 |
| 1.30.103 | 3.0 | August 2008 | #version 130 |
| 1.40.084 | 3.1 | March 2009 | #version 140 |
| 1.50.115 | 3.2 | August 2009 | #version 150 |
| 3.30.66 | 3.3 | February 2010 | #version 330 |
| 4.00.97 | 4.0 | March 2010 | #version 400 |
| 4.10.68 | 4.1 | July 2010 | #version 410 |
| 4.20.119 | 4.2 | August 2011 | #version 420 |
| 4.30.810 | 4.3 | August 2012 | #version 430 |
| 4.4011 | 4.4 | July 2013 | #version 440 |
Favorite site
- OpenGL Shading Language
- Wikipedia (en) GLSL
- Wikipedia (en) OpenGL Shading Language
- Summary of OpenGL shader creation functions
- Stackoverflow: Finding supported GLSL version
- OpenGL ES 2.0 shader pipeline compared with Direct3D
- OpenGL Shader concepts | Hyeonwoo's dev notes
Guide
- GLSL Version Check
- GLSL Shader 5 - Setup/Overview
- Converting YUV420 format colors to RGB using an OpenGL Shader