Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
GLSL Essentials
GLSL Essentials

GLSL Essentials: If you're involved in graphics programming, you need to know about shaders, and this is the book to do it. A hands-on guide to the OpenGL Shading Language, it walks you through the absolute basics to advanced techniques.

eBook
€8.99 €22.99
Paperback
€28.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

GLSL Essentials

Chapter 2. GLSL Basics

OpenGL Shading Language is based on ANSI C. Many features of the C programming language have been incorporated to GLSL, while the ones that go against performance or language simplicity have been removed.

All GLSL shader types (vertex, fragment, geometry, and compute) use the same language, so you will benefit a lot from this chapter. All you will learn in this chapter will serve for every following chapter. In this chapter you will learn the basics of the language and the common elements between each shader type. Specifically, we will talk about the following topics:

  • Language basics

  • Shader input/ouput variables

Because GLSL is very close to the C language, I won't expose a complete and exhaustive list of each language element. I'll focus only on the differences between GLSL and C, and the biggest difference of them all, which will definitely lighten up a lot of readers, is that GLSL does not have pointers.

The Language


When you start coding a shader, you have to keep in mind one important thing: which GLSL version you are going to code for. This is usually a minimum requirements decision for your application. GLSL version is always bound to a specific OpenGL version, so in order to choose a GLSL version that supports the features we need, we are also tied to the minimum OpenGL version that supports that GLSL version.

At the moment of writing this book, I had to decide which version of GLSL to use. As we will talk about the compute shaders, we need to go to the minimum version that support them in a native way (not through extensions), and that's GLSL Version 4.30.6.

The official GLSL specification could be a good reference resource, once you have finished reading this book. You will be able to find the full list of functions in the tables located in the specification. The 4.30.6 GLSL specification can be downloaded directly from the OpenGL official site: https://www.opengl.org/registry/doc/GLSLangSpec...

Language basics


Before we begin, I expect you to have a basic understanding and proficiency of C. OpenGL is available in different programming languages such as Java, Python, or C#. However, I will be concentrating on the C/GLSL concepts.

Instructions

The instructions always end with a semicolon, and there could be more than one per line:

c = cross(a, b);
vec4 g; g = vec4(1, 0, 1, 1);

A block of instructions is created by putting them in brackets. All variables declared inside a block will be destroyed when the block finishes. If two variables have the same name—one declared outside the block (also called scope) and another declared inside the block—by default, the inner variable is the one which will be referenced:

float a = 1.0;
float b = 2.0;
{
  float a = 4.0;
  float c = a + 1.0; // c -> 4.0 + 1.0
}
b = b + c; // wrong statement. Variable c does not exist here

Tabulations or whitespaces don't change the semantics of the language. You can use them to format the code at your wish.

Basic types...

Shader input and output variables


Until now, we've been speaking about the language itself; how it's similar to the C programming language, but focusing specially on the differences. That was only the language part. Now it's time to see some of the functional parts of GLSL and cover an important topic: the inputs and outputs of the shaders.

Uniform variables

Suppose, you want to create a shader that makes use of a simple light. Well, shaders don't know about lights or any other high-level concept. Shaders only know about math and programming. So, if the language doesn't have support for lights, how would I use the light's position or color in a shader?

You need to pass those variables from your application to your shader and perform the lighting calculations inside the shader.

A parameter that is passed from the application to the shaders is called a uniform variable. Those variables are always read-only (constant), global to the shaders that form the current executable program.

The procedure...

Summary


We have learned about all the tools that we need to start reading about specific shaders. Although, this chapter contains plenty of new definitions and concepts, it contains the minimum (but solid) base to start developing shaders.

We learned about language grammar, key C-GLSL differences, basic language structures, shaders' native vectorial types and operations, and shaders' inputs and outputs. Now, in the next chapter, we will face our first true challenge: vertex shaders.

Left arrow icon Right arrow icon

Key benefits

  • Learn about shaders in a step-by-step, interactive manner
  • Create stunning visual effects using vertex and fragment shaders
  • Simplify your CPU code and improve your overall performance with instanced drawing through the use of geometry shaders

Description

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.

Who is this book for?

If you want upgrade your skills, or are new to shader programming and want to learn about graphic programming, this book is for you. If you want a clearer idea of shader programming, or simply want to upgrade from fixed pipeline systems to state-of-the-art shader programming and are familiar with any C-based language, then this book will show you what you need to know.

What you will learn

  • Use vertex shaders to dynamically displace or deform a mesh on the fly
  • Colorize your pixels unleashing the power of fragment shaders
  • Learn the basics of the Phong Illumination model to add emphasis to your scenes
  • Combine textures to make your scene more realistic
  • Save CPU and GPU cycles by performing instanced drawing
  • Save bandwidth by generating geometry on the fly
  • Learn about GPU Generic programming concepts
  • Convert algorithms from CPU to GPU to increase performance

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 26, 2013
Length: 116 pages
Edition : 1st
Language : English
ISBN-13 : 9781849698016
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Dec 26, 2013
Length: 116 pages
Edition : 1st
Language : English
ISBN-13 : 9781849698016
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 116.97
GLSL Essentials
€28.99
OpenGL 4 Shading Language Cookbook, Second Edition
€45.99
OpenGL Development Cookbook
€41.99
Total 116.97 Stars icon
Banner background image

Table of Contents

6 Chapters
The Graphics Rendering Pipeline Chevron down icon Chevron up icon
GLSL Basics Chevron down icon Chevron up icon
Vertex Shaders Chevron down icon Chevron up icon
Fragment Shaders Chevron down icon Chevron up icon
Geometry Shaders Chevron down icon Chevron up icon
Compute Shaders Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
(3 Ratings)
5 star 0%
4 star 0%
3 star 33.3%
2 star 33.3%
1 star 33.3%
Michael Dwan Feb 23, 2014
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
GLSL Essentials is a small, but dense, book on OpenGL Shading Language 4.3. The book itself is divided quite logically with the first chapter unremarkably offering an obligatory introduction to the rendering pipeline.Chapter 2 is where you first begin to find some value in this small book. Here, you will be introduced to GLSL 4.30, of course, were it not for the many evolutions of GLSL over the years this chapter would be mostly unnecessary, however, it is nice to have as a reference if you're coming from older versions of GLSL. In general, it would have been nice to have annotation for constructs indicating in what version of GLSL they were introduced, but otherwise, this is a fine reference.Chapters 3 and 4 cover vertex and fragment shaders, and for the most part these stages have not changed drastically since earlier versions of GLSL, GLSL Essentials does an admirable job of introducing these two shader stages without assuming prior experience using GLSL, I expect this to be a welcome prelude for those newer to OpenGL and GLSL.Chapter 5 and Chapter 6 which cover geometry and compute shaders, respectively, were the most interesting sections, in my opinion. Although like chapters 3 and 4, the coverage is quick and high level they both do a good job of introducing the key benefits of these shader stages which, to a beginner especially, is not necessarily clear just reading GLSL documentation. After reading these chapters you will have enough insight to understand some of the possibilities these stages expose and that is valuable.All the shader stage chapters are punctuated nicely with example code illustrating the discussion, however, the examples are generally quite simple, and the discussions are, for the most part, elementary. This is not meant to slight the content of the book, it lives up to its claims, in that it covers the essential topics in just enough detail to get you started, but its definitely geared to the uninitiated.Some of the example code is poorly written/edited. For instance, there is, inexplicably, several loops iterating with floating point counters, and then worse, the floating point value is used to index an array. I can’t imagine the reasoning here, but it speaks poorly of the text, which otherwise is not bad.Overall I can recommend the book as an introduction to the topic, provided you have the hardware necessary to run the code you are writing. Those hoping for a more in depth exploration of GLSL would definitely do well to look elsewhere. The book does lose points for being poorly edited and also for the exclusion of tessellation shaders, an exclusion for which the author offers no explanation.
Amazon Verified review Amazon
TauriRed Aug 28, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I was hoping for much when I opened book with such title. This is very small descriptions of existing shader types and that's all.No Why's or If's on shaders.
Amazon Verified review Amazon
DC Bateman Feb 12, 2014
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I was looking forward to this book, the chapter headings looked promising and with a low page count (about 90) I was expecting a concise introduction to GLSL. Any hopes were short lived, however.Apart from the chapter headings this is a confused mess of book. Written and, worse, poorly edited by non-native English speakers, with confusing, distracting, incorrect grammar which makes the already poor descriptions even more confusing and difficult to follow.Chapter headings aside, there is no clear progression and important steps/points are missed in both explanations and code samples. Key concepts -- fixed and programmable pipelines, their relationship to each other, and the flow of data through the programmable pipeline -- are poorly described.The code samples are incomplete:for instance no values are given for the projection and model-view matrices -- "fill with proper values" is the sum total of the information given to aid the reader.Apart from a dozen or so lines of C, the code samples are all GLSL. I was unable to find any source code online so it appears that the reader is expected to write the host code themselves.Some specific shortcomings:* "floats" are used in "for" loops, apart from being an unnecessary waste of GPU space they can lead to off-by-one errors.* The description of matrices as an array of vectors is out-and-out wrong.GLSL Essentials might have been a good book, all the necessary parts are there, but they are confusingly realized, so this book is sorely lacking as an introduction. While some basic concepts are over-explained (e.g. comments) others are simply taken for granted (e.g. variable location) and left unexplained.While it aims to cover the essentials, it does so very poorly and with the basic mistakes it makes it is difficult to trust anything in this book (I was constantly referring to the internet to confirm what I read).A very poor book and most definitely not essential.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.