Advanced

While GLSL allows you to define functions in shaders, the call flow of those functions was always static. To dynamically select between multiple functions, you either created two distinct shaders, or used an if-statement to make a run-time selection, like demonstrated in Example 2.5.

Example 2.5. Static Shader Control Flow

#version 330 corevoid func_1() { ...  }void func_2() { ...  }uniform int func;void main(){     if (func == 1)         func_1();     else          func_2();}

Shader subroutines are conceptually similar to function pointers in C for implementing dynamic subroutine selection. In your shader, you specify a subroutine type and use that type when declaring the set of subroutines eligible for dynamic use. Then, you choose ...

Get OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 4.3, Eighth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.