Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
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.

Arrow left icon
Profile Icon Jacobo Rodriguez
Arrow right icon
€18.99 per month
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2 (3 Ratings)
Paperback Dec 2013 116 pages 1st Edition
eBook
€15.99 €22.99
Paperback
€28.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Jacobo Rodriguez
Arrow right icon
€18.99 per month
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2 (3 Ratings)
Paperback Dec 2013 116 pages 1st Edition
eBook
€15.99 €22.99
Paperback
€28.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€15.99 €22.99
Paperback
€28.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781849698009
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Dec 26, 2013
Length: 116 pages
Edition : 1st
Language : English
ISBN-13 : 9781849698009
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
OpenGL Development Cookbook
€41.99
OpenGL 4 Shading Language Cookbook, Second Edition
€45.99
GLSL Essentials
€28.99
Total 116.97 Stars icon

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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.