Book Image

GLSL Essentials

By : Jacobo Rodriguez
Book Image

GLSL Essentials

By: Jacobo Rodriguez

Overview of this book

Shader programming has been the largest revolution in graphics programming. OpenGL Shading Language (abbreviated: GLSL or GLslang), is a high-level shading language based on the syntax of the C programming language.With GLSL you can execute code on your GPU (aka graphics card). More sophisticated effects can be achieved with this technique.Therefore, knowing how OpenGL works and how each shader type interacts with each other, as well as how they are integrated into the system, is imperative for graphic programmers. This knowledge is crucial in order to be familiar with the mechanisms for rendering 3D objects. GLSL Essentials is the only book on the market that teaches you about shaders from the very beginning. It shows you how graphics programming has evolved, in order to understand why you need each stage in the Graphics Rendering Pipeline, and how to manage it in a simple but concise way. This book explains how shaders work in a step-by-step manner, with an explanation of how they interact with the application assets at each stage. This book will take you through the graphics pipeline and will describe each section in an interactive and clear way. You will learn how the OpenGL state machine works and all its relevant stages. Vertex shaders, fragment shaders, and geometry shaders will be covered, as well some use cases and an introduction to the math needed for lighting algorithms or transforms. Generic GPU programming (GPGPU) will also be covered. After reading GLSL Essentials you will be ready to generate any rendering effect you need.
Table of Contents (13 chapters)


If you are still in the old fixed pipeline days and your OpenGL knowledge is a little bit rusty, or if you want to jump from OpenGL|ES to a more complete and modern version of OpenGL, then this book is for you. Inside the pages of this book, you will find the details of the most recent OpenGL Shading Language Version: 4.3.

This book has been written looking forward in the OpenGL specification. No old functionalities or deprecated code will be found in this book, just the plain 4.3 Version.

What this book covers

Chapter 1, The Graphics Rendering Pipeline, starts with a brief introduction to the rendering pipeline and moves on to give an overview of the programmable stages.

Chapter 2, GLSL Basics, covers language basics, types, vector operations, flow control, preprocessor, and shader inputs and outputs.

Chapter 3, Vertex Shaders, looks at the vertex programmable stage, uniform variables and basic lighting, and vertex shader examples.

Chapter 4, Fragment Shaders, looks at the execution model, inputs and outputs, and examples.

Chapter 5, Geometry Shaders, looks at geometry shader structure, interface blocks, and examples.

Chapter 6, Compute Shaders, covers the execution model, GPGPU basics, render to texture, and basic raw computations.

What you need for this book

In order to run the samples from this book, you will need a text editor and a C/C++ compiler. For the text editor, a free editor such as Notepad++ or SciTE could be used, and for the C/C++ compiler, GCC/G++, or Visual Studio (the Express version is free) are the best choices. As we are addressing the latest OpenGL version, a graphics card that supports at least OpenGL 4.3 will be needed.

Who this book is for

This book is for people who have some experience or basic knowledge in computer graphics and who want to upgrade their knowledge to the latest OpenGL version. It is also for people who want to take the jump from the fixed pipeline to the programmable pipeline.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The buffer keyword denotes the type of the interface block."

A block of code is set as follows:

layout(std430, binding = 0) buffer InputBufferA{float inA[];};
layout(std430, binding = 1) buffer InputBufferB{float inB[];};
layout(std430, binding=2) buffer OutputBuffer{float outBuffer[];};

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

layout(std430, binding = 0) buffer InputBufferA{float inA[];};
layout(std430, binding = 1) buffer InputBufferB{float inB[];};
layout(std430, binding=2) buffer OutputBuffer{float outBuffer[];};

New terms and important words are shown in bold.


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from:


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.