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 of the 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 means 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, using the strengths of each to optimally perform the task at hand.
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, lets applications 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 who want to write shaders. This book can 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. The organization is amenable to both uses and is based on the assumption that most people won't read the book in sequential order from back to front (but some intrepid readers of the first edition reported that they did just that!).
Readers do not need previous knowledge of OpenGL to absorb the material in this book, but such knowledge 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, so some knowledge of algebra, trigonometry, and calculus 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. To develop shaders successfully in this environment, developers must understand the mathematical basis of computer graphics.
About This Book
This book has three main parts. Chapters 1 through 8 teach 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 create and manipulate shaders. To supply 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 both the baseline for a reader's shader development and a springboard for inspiring new ideas. Finally, Chapter 21 compares other notable commercial shading languages, and Appendices A and B 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 has somewhat compartmentalized chapters in order to allow such usage.
- Chapter 1 reviews the fundamentals of the OpenGL API. Readers already familiar with OpenGL may skip to Chapter 2.
- Chapter 2 introduces 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 two chapters of this book, this chapter and Chapter 3 are the ones to read.
- Chapter 3 thoroughly describes the OpenGL Shading Language. This material is organized to present the details of a programming language. This section serves as a useful reference section for readers who 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 is a useful reference section for readers with 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. Application programmers who want to use shaders in their application must understand this material.
- 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; that is, effects are computed algorithmically rather than being based on information stored in textures.
- Chapter 12 presents several alternative lighting models that can be implemented with 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 with its proper use.
- Chapter 16 contains examples of how shaders can 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 achieve effects other than photorealism. Such effects include technical illustration, sketching or hatching effects, and other stylized rendering.
- Chapter 19 presents several shaders that modify images as they are being drawn with OpenGL.
- Chapter 20 describes some of the techniques and algorithms used in a complex OpenGL 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.
About the Shader Examples
- Finally, Glossary collects terms defined in the book, Further Reading gathers all the chapter references and adds more, and Index ends the book.
The shaders contained in this book are primarily short programs that 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 if greater care is taken 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 on them for use in your own projects.
All 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 idiosyncrasies from their development on the very early implementations of the OpenGL Shading Language. Electronic versions of most of these shaders are available through a link at this book's Web site at http://3dshaders.com.
I know that this book contains some errors, but I've done my best to keep them to a minimum. If you find any errors, please report them to me (email@example.com) and I will keep a running list on this book's Web site at http://3dshaders.com.
This book contains a number of typographical conventions to enhance readability and understanding.
- Italics are used for emphasis, document titles, and coordinate values such as x, y, and z.
- Bold serif is used for language keywords.
Italic sans serif is used for variables, parameter names, spatial dimensions, and matrix components.