Shader Fundamentals
This chapter introduces shaders and explains how to use them with OpenGL. Along the way, it describe the OpenGL Shading Language (commonly called GLSL), and details how shaders will influence your OpenGL applications.
Chapter Objectives
After reading this chapter, you’ll be able to do the following:
Identify the various types of shaders that OpenGL uses to create images.
Construct and compile shaders using the OpenGL Shading Language.
Pass data into shaders using a variety of mechanisms available in OpenGL.
Employ advanced GLSL shading capabilities to make shaders more reusable.
This chapter introduces shaders and explains how to use them with OpenGL. Along the way, we describe the OpenGL Shading Language (commonly called GLSL), and detail how shaders will influence your OpenGL applications.
This chapter contains the following major sections:
“Shaders and OpenGL” discusses programmable graphics shaders in the context of OpenGL applications.
“OpenGL’s Programmable Pipeline” details each stage of the OpenGL programmable pipeline.
“An Overview of the OpenGL Shading Language” introduces the OpenGL Shading Language.
“Interface Blocks” shows how to organize shader variables shared with the application or between stages.
“Compiling Shaders” describes the process of converting GLSL shaders into programmable shader programs usable in your OpenGL application.
“Shader Subroutines” discusses a method to increase the usability of shaders by allowing them to select execution routines without recompiling shaders.
“Separate Shader Objects” details how to composite elements from multiple shaders into a single, configurable graphics pipeline.
“SPIR-V” discusses how to set shaders compiled to the SPIR-V binary intermediate language.
Shaders and OpenGL
The modern OpenGL rendering pipeline relies very heavily on using shaders to process the data you pass to it. About the only rendering you can do with OpenGL without shaders is clearing a window, which should give you a feel for how important shaders are when using OpenGL.
Shaders, whether for OpenGL or any other graphics API, are usually written in a specialized programming language. For OpenGL, we use GLSL, the OpenGL Shading Language, which has been around since OpenGL Version 2.0 (and before as extensions). It has evolved along with OpenGL, usually being updated with each new version of OpenGL. While GLSL is a programming language specially designed for graphics, you’ll find it’s very similar to the C language, with a little C++ mixed in.
Shaders are so fundamental to the operation of OpenGL, it’s important to introduce them early and get you comfortable with writing them. Any OpenGL program will essentially be divided into two main parts; the part that’s running on the CPU, written in a language such as C++, and the part that’s running on the GPU, which is written in GLSL.
In this chapter, we describe how to write shaders, gradually introducing GLSL along the way, discuss compiling and integrating shaders into your application, and show how data in your application passes between the various shaders.