Versions, Profiles & Preprocessor

Must be the first line (only comments/whitespace before it):

5.1 The version directive

Must be the first line (only comments/whitespace before it):

Without #version, GLSL 1.10 is assumed — always specify it.

#version 460 core          // OpenGL 4.6 core profile
#version 330 core          // OpenGL 3.3 core — a widely supported baseline
#version 300 es            // OpenGL ES 3.0 / WebGL 2
#version 100               // GLSL ES 1.00 / WebGL 1 (old syntax: attribute/varying, gl_FragColor)
#version 450               // typical for Vulkan (with GL_KHR_vulkan_glsl semantics)

5.2 Old vs modern syntax

Legacy (GLSL ≤ 1.20 / ES 1.00) Modern (≥ 1.30 / ES 3.00) attribute vec3 pos; in vec3 pos; (vertex) varying vec2 uv; out (vertex) / in (fragment) gl_FragColor =...; out vec4 fragColor; user-declared texture2D(s, uv) texture(s, uv) textureCube(s, dir) texture(s, dir) built-in matrices (gl_ModelViewMatrix)

5.3 Preprocessor

Predefined macros: __VERSION__, __LINE__, __FILE__, GL_ES (in ES), GL_core_profile, VULKAN (when compiling for Vulkan).

There is no #include in core GLSL; engines implement it by string concatenation, or use GL_GOOGLE_include_directive (glslang) / GL_ARB_shading_language_include.

#define PI 3.14159265359
#define SQUARE(x) ((x) * (x))
#define USE_SHADOWS 1

#if USE_SHADOWS
    float shadow = computeShadow();
#else
    float shadow = 1.0;
#endif

#ifdef GL_ES
precision mediump float;
#endif

#ifndef MAX_LIGHTS
#define MAX_LIGHTS 8
#endif

#undef SQUARE
#error "Unsupported configuration"
#pragma optimize(on)
#pragma debug(off)
#line 100
#extension GL_ARB_shading_language_include : enable    // enable an extension
#extension GL_EXT_nonuniform_qualifier : require

5.4 Comments

// single-line comment
/* multi-line
   comment */