2. GLSL Overview

GLSL has various dialects, and there are different types of shaders.Here, we will explain all of them.

2.1 Dialects

In the case of GLSL, different platforms use different dialects.

OpenGL Core

Shaders for OpenGL Core are intended for use on desktops with OpenGL versions 3.3 to 4.6.

This is the dialect we will focus on in this tutorial series.Feel free to check the shader files included in the GLSL tester downloaded in the previous chapter.

OpenGL ES and WebGL

Shaders for OpenGL ES are intended for use on mobile devices with OpenGL ES 3.0.

This dialect is only slightly different from that of OpenGL Core.The main difference lies in the header section.

OpenGL Core:

#version 450 core

in vec2 vUv;
out vec4 out_color;

OpenGL ES:

#version 300 core
precision highp float;

varying vec2 vUv;

WebGL:

#version 100
precision highp float;

varying vec2 vUv;

Other differences include using varying for communication from vertex to fragment shaders, or using gl_FragColor instead of a custom variable (in our case, out_color).Unlike OpenGL Core, OpenGL ES and WebGL require defining precision qualifiers (highpmediumplowp) for floating-point numbers and vectors.

Vulkan

This is a modified version of OpenGL Core 4.5 and later, including Vulkan-specific extensions.However, unlike the above, Vulkan requires GLSL shaders to be compiled to SPIR-V before use.Interestingly, once compiled, tools are available to convert back to GLSL, HLSL, or MSL.This makes learning the other two major shading languages much easier.

OpenGL Core:

#version 450 core

in vec2 vUv;
out vec4 out_color;

Vulkan:

#version 450

layout(location = 0) in vec2 vUv;
layout(location = 0) out vec4 out_color;

Nintendo Systems

Unfortunately, due to NDAs, I cannot discuss this in detail, but in my experience, shaders used in Vulkan worked on the Nintendo Switch without modification.

2.2 Types of Shaders

There are several types of shaders.Among them, vertex shaders and fragment shaders are mandatory, while the rest are used as needed.

To understand why vertex and fragment shaders are necessary, we need to show an image of the graphics pipeline used in Vulkan:

The graphics pipeline will be explained in more detail in the upcoming Vulkan tutorial series.

Vertex Shader

Vertex shaders process each vertex of a 3D model, transforming its position from model space to screen space (applying model, view, and projection transformations).They can also compute per-vertex attributes such as normals, colors, and texture coordinates for use in later stages.

This allows for transforming 3D coordinates, computing per-vertex lighting, and passing texture coordinates to the fragment shader.

Fragment Shader

Fragment shaders process each pixel (or fragment) generated by rasterization, determining its final color and depth.They handle tasks such as texturing, per-pixel lighting, and post-processing effects.

This allows for applying textures, computing lighting effects, blending colors, and applying fog.

Geometry Shader

Geometry shaders operate on entire primitives (triangles, lines) after the vertex shader, enabling modification, creation, or destruction of geometry.They can generate new vertices or change the topology of primitives.

This allows for creating particles, extruding silhouettes, generating shadow volumes, and tessellating simple geometry into more complex shapes.

Tessellation Control Shader

Tessellation control shaders are part of the tessellation pipeline.They define how a primitive (e.g., a patch) is subdivided into smaller primitives.They set the tessellation level and pass data to the tessellation evaluation shader.

This allows for controlling the level of detail for curved surfaces (e.g., Bézier patches) or terrain rendering.

Note: Tessellation was introduced in OpenGL 4.0, so older games may not support it.

Tessellation Evaluation Shader

Tessellation evaluation shaders evaluate the tessellated geometry generated in the tessellation stage, determining the final positions and attributes of new vertices based on the tessellation pattern.

This allows for defining the shape of subdivided surfaces (e.g., smooth curves or detailed terrain).

Compute Shader

Compute shaders are general-purpose shaders not tied to the graphics pipeline, used for arbitrary computations on the GPU (e.g., physics simulations, image processing, machine learning tasks).

This allows for parallel processing of tasks such as particle simulations, fluid dynamics, or post-processing effects like blur.

Note: Compute shaders were introduced in OpenGL 4.3, making them a relatively new type of shader.