“As the ‘Red Book’ is known to be the gold standard for OpenGL, the ‘Orange Book’ is considered to be the gold standard for the OpenGL Shading Language. With Randi’s extensive knowledge of OpenGL and GLSL, you can be assured you will be learning from a graphics industry veteran. Within the pages of the second edition you can find topics from beginning shader development to advanced topics such as the spherical harmonic lighting model and more.”
—David Tommeraasen, CEO/Programmer, Plasma Software
“This will be the definitive guide for OpenGL shaders; no other book goes into this detail. Rost has done an excellent job at setting the stage for shader development, what the purpose is, how to do it, and how it all fits together. The book includes great examples and details, as well as good additional coverage of 2.0 changes!”
—Jeffery Galinovsky, Director of Emerging Market, Platform Development, Intel Corporation
“The coverage in this new edition of the book is pitched just right to help many new shader-writers get started, but with enough deep information for the ‘old hands.’”
—Marc Olano, Assistant Professor, University of Maryland
“This is a really great book on GLSL—well written and organized, very accessible, and with good real-world examples and sample code. The topics flow naturally and easily, explanatory code fragments are inserted in very logical places to illustrate concepts, and, all in all, this book makes an excellent tutorial as well as a reference.”
—John Carey, Chief Technology Officer, C.O.R.E. Feature Animation
“OpenGL® Shading Language provides a timely, thorough, and entertaining introduction to the only OpenGL ARB-approved high-level shading language in existence. Whether an expert or a novice, there are gems to be discovered throughout the book, and the reference pages will be your constant companion as you dig into the depths of the shading APIs. From algorithms to APIs, this book has you covered.”
—Bob Kuehne, CEO, Blue Newt Software
“Computer graphics and rendering technologies just took a giant leap forward with hardware vendors rapidly adopting the new OpenGL Shading Language. This book presents a detailed treatment of these exciting technologies in a way that is extremely helpful for visualization and game developers.”
—Andy McGovern, Founder, Virtual Geographics, Inc.
“The OpenGL Shading Language is at the epicenter of the programmable graphics revolution, and Randi Rost has been at the center of the development of this significant new industry standard. If you need the inside track on how to use the OpenGL Shading Language to unleash new visual effects and unlock the supercomputer hiding inside the new generation of graphics hardware, then this is the book for you.”
—Neil Trevett, Senior Vice President, Market Development, 3Dlabs
--This text refers to an alternate
For just about as long as there has been graphics hardware, there has been programmable graphics hardware. Over the years, building flexibility into graphics hardware designs has been a necessary way of life for hardware developers. Graphics APIs continue to evolve, and because a hardware design can take two years or more from start to finish, the only way to guarantee a hardware product that can support the then-current graphics APIs at its release is to build in some degree of programmability from the very beginning.
Until recently, the realm of programming graphics hardware belonged to just a few people, mainly researchers and graphics hardware driver developers. Research into programmable graphics hardware has been taking place for many years, but the point of this research has not been to produce viable hardware and software for application developers and end users. The graphics hardware driver developers have focused on the immediate task of providing support for the important graphics APIs of the time: PHIGS, PEX, Iris GL, OpenGL, Direct3D, and so on. Until recently, none of these APIs exposed the programmability of the underlying hardware, so application developers have been forced into using the fixed functionality provided by traditional graphics APIs.
Hardware companies have not exposed the programmable underpinnings of their products because there is a high cost of educating and supporting customers to use low-level, device-specific interfaces and because these interfaces typically change quite radically with each new generation of graphics hardware. Application developers who use such a device-specific interface to a piece of graphics hardware face the daunting task of updating their software for each new generation of hardware that comes along. And forget about supporting the application on hardware from multiple vendors!
As we moved into the 21st century, some of these fundamental tenets about graphics hardware were challenged. Application developers pushed the envelope as never before and demanded a variety of new features in hardware in order to create more and more sophisticated onscreen effects. As a result, new graphics hardware designs became more programmable than ever before. Standard graphics APIs were challenged to keep up with the pace of hardware innovation. For OpenGL, the result was a spate of extensions to the core API as hardware vendors struggled to support a range of interesting new features that their customers were demanding.
The creation of a standard, cross-platform high level shading language for commercially available graphics hardware was a watershed event for the graphics industry. A paradigm shift occurred, one that took us from the world of rigid, fixed functionality graphics hardware and graphics APIs to a brave new world where the visual processing unit, or VPU (i.e., graphics hardware), is as important as the central processing unit, or CPU. The VPU is optimized for processing dynamic media such as 3D graphics and video. Highly parallel processing of floating point data is the primary task for VPUs, and the flexibility of the VPU will mean that it can also be used to process data other than a stream of traditional graphics commands. Applications can take advantage of the capabilities of both the CPU and the VPU, utilizing the strengths of each to perform the task at hand optimally.
This book describes how graphics hardware programmability is exposed through a high-level language in the leading cross-platform 3D graphics API: OpenGL. This language, the OpenGL Shading Language, allows applications to take total control over the most important stages of the graphics processing pipeline. No longer restricted to the graphics rendering algorithms and formulas chosen by hardware designers and frozen in silicon, software developers are beginning to use this programmability to create stunning effects in real-time.
The primary audience for this book is application programmers that are interested in writing shaders. This book is intended to be used as both a tutorial and a reference book by people interested in learning to write shaders with the OpenGL Shading Language. Some will use the book in one fashion and some in the other. It is hoped that the organization will be amenable to both uses. It is not expected that most people will read the book in sequential order from back to front.
Readers do not need previous knowledge of OpenGL in order to absorb the material in this book, but it is very helpful. A brief review of OpenGL is included, but this book does not attempt to be a tutorial or reference book for OpenGL. Anyone attempting to develop an OpenGL application that uses shaders should be armed with OpenGL programming documentation in addition to this book.
Computer graphics has a mathematical basis, therefore some knowledge of algebra and trigonometry will help readers understand and appreciate some of the details presented. With the advent of programmable graphics hardware, key parts of the graphics processing pipeline are once again under the control of software developers. In order to develop shaders successfully in this environment, it is imperative that developers understand the mathematical basis of computer graphics.
About This Book
This book has three main parts. Chapters 1 through 8 are aimed at teaching the reader about the OpenGL Shading Language and how to use it. This part of the book covers details of the language and details of the OpenGL commands that are used to create and manipulate shaders. In order to provide the reader with a basis for writing shaders, Chapters 9 through 20 contain a gallery of shader examples and some explanation of the underlying algorithms. This part of the book is intended to be used as a basis for the reader's shader development and as a springboard to inspire new ideas. Finally, Chapter 21 contains a comparison with other notable commercial shading languages, and Appendices A, B, and C contain reference material for the language and the API entry points that support it.
The chapters are arranged to suit the needs of the reader who is least familiar with OpenGL and shading languages. Certain chapters can be skipped by readers who are more familiar with both topics. This book is designed to have somewhat compartmentalized chapters in order to allow such usage.
Chapter 1 contains a review of the fundamentals of the OpenGL API. Readers already familiar with OpenGL may skip to Chapter 2.
Chapter 2 provides an introduction to the OpenGL Shading Language and the OpenGL entry points that have been added to support it. If you want to know what the OpenGL Shading Language is all about and you have time to read only one chapter of this book, this is the one to read.
Chapter 3 thoroughly describes the OpenGL Shading Language. This material is organized to present the details of a programming language. This section will be useful as a reference section after readers have developed a general understanding of the language.
Chapter 4 discusses how the newly defined programmable parts of the rendering pipeline interact with each other and with OpenGL's fixed functionality. This discussion includes descriptions of the built-in variables defined in the OpenGL Shading Language.
Chapter 5 describes the built-in functions that are part of the OpenGL Shading Language. This section will also be useful as a reference section after readers have developed an understanding of the language.
Chapter 6 presents and discusses a fairly simple shader example. People who learn best by diving in and studying a real example will benefit from the discussion in this chapter.
Chapter 7 describes the entry points that have been added to OpenGL to support the creation and manipulation of shaders. This material will need to be understood by application programmers who want to use shaders in their application.
Chapter 8 presents some general advice on shader development and describes the shader development process. It also describes tools that are currently available to aid the shader development process.
Chapter 9 begins a series of chapters that present and discuss shaders with a common characteristic. In this chapter, shaders that duplicate some of the fixed functionality of the OpenGL pipeline are presented.
Chapter 10 presents a few shaders that are based on the capability to store data in and retrieve data from texture maps.
Chapter 11 is devoted to shaders that are procedural in nature (effects are computed algorithmically rather than being based on information stored in textures).
Chapter 12 presents several alternative lighting models that can be implemented using OpenGL shaders.
Chapter 13 discusses algorithms and shaders for producing shadows.
Chapter 14 delves into the details of shaders that implement more realistic surface characteristics, including refraction, diffraction, and more realistic reflection.
Chapter 15 describes noise and the effects that can be achieved by using it properly.
Chapter 16 contains examples of how shaders can be used to create rendering effects that vary over time.
Chapter 17 contains a discussion of the aliasing problem and how shaders can be written to reduce the effects of aliasing.
Chapter 18 illustrates shaders that are used to achieve effects other than photorealism. Such effects include technical illustration, sketching or hatching effects, and other stylized rendering.
Chapter 19 presents several shaders that are used to modify images as they are being drawn with OpenGL.
Chapter 20 describes some of the techniques and algorithms used in a complex OpenGL demo application that makes extensive use of the OpenGL Shading Language.
Chapter 21 compares the OpenGL Shading Language with other notable commercial shading languages.
Appendix A contains the language grammar that more clearly specifies the OpenGL Shading Language.
Appendix B contains reference pages for the API entry points that are related to the OpenGL Shading Language.
Appendix C is a quick reference for the OpenGL Shading Language
About the Shader Examples
The shaders contained in this book are primarily short programs designed to illustrate the capabilities of the OpenGL Shading Language. None of the example shaders should be presumed to illustrate the "best" way of achieving a particular effect. (Indeed, the "best" way to implement certain effects may have yet to be discovered through the power and flexibility of programmable graphics hardware.) Performance improvements for each shader are possible for any given hardware target. For most of the shaders, image quality may be improved by taking greater care to reduce or eliminate causes of aliasing.
The source code for these shaders is written in a way that I believe represents a reasonable trade-off between source code clarity, portability, and performance. Use them to learn the OpenGL Shading Language, and improve upon them for use in your own projects.
All of the images produced for this book were done either on the first graphics accelerator to provide support for the OpenGL Shading Language, the 3Dlabs Wildcat VP, or its successor, the 3Dlabs Wildcat Realizm. I have taken as much care as possible to present shaders that are done "the right way" for the OpenGL Shading Language rather than those with idiosyncracies due to being developed on the very early implementations of the OpenGL Shading Language.If you find any errors, please report them to me (randi@3dshaders.