OpenGL Shading Language
Because of its orange cover, the book is also called the "Orange Book," and together with its siblings, the classic "Red Book" (aka OpenGL Programming Guide) and the "Blue Book" (aka OpenGL Reference Manual (see this earlier review), it is a member of the OpenGL family of books from Addison-Wesley. Although it has a short overview of the basic features of OpenGL, it is intended for an audience that is already somewhat familiar with OpenGL and with 3D graphics programming in general. The interested reader should probably have read the "Red Book" or at least have a good understanding of how to use the OpenGL graphics API before attempting to tackle this book.
Rost, as well as the co-authors on some of the chapters, John M. Kessenich and Barthold Lichtenbelt, all employees of the graphics hardware vendor 3Dlabs, were driving forces behind the inception of the OpenGL Shading Language. They are also core contributors to the final language specification as well as the OpenGL extensions that provide the framework for this new shading language. So the information in the book actually comes from the people that created the language, which is a definite plus.
The book consists of 17 chapters and two appendices which can be roughly categorized into four major parts: An introduction to the basics of OpenGL and GPU programmability; a description of the OpenGL Shading Language and the associated OpenGL extensions; a number of chapters that show the shading language in action; and finally a reference section on the language grammar and the entry points introduced by the new OpenGL extensions. Each chapter of the book has numerous interesting references to get further information on the presented topics. I can only recommend taking a closer look at some of them.
The first two chapters of the book describe the basics of the OpenGL graphics API, followed by an overview of the new programmable processors in the graphics pipeline and an overview of the shading language used to program them. The introductory chapter on OpenGL basics is very well written and worth the read even for more experienced OpenGL programmers. However, as mentioned above, the reader should have enough expertise in using OpenGL to be able to understand the more advanced parts of the book. The introductory chapter won't be enough in my opinion.
The third chapter, written by John Kessenich - one of the main authors of the OpenGL shading language specification - presents the language definition. This chapter is where the basic data types as well as the available control structures are described in detail. For people interested in writing a compiler for the OpenGL Shading Language, Appendix A also contains the entire language grammar in BNF.
Chapter four moves on to describe the programmable graphics pipeline, which was first introduced in the second chapter, in more detail. The programmable vertex and fragment processors and their interaction with OpenGL's fixed functionality are presented. In chapter five, the description of the shading language concludes with the available built-in functions. Chapter six offers the first simple example that shows the shading language in action - a shader to procedurally create a brick texture.
Until this point, the book doesn't talk much about how to integrate shaders into the host program running on the CPU. New OpenGL extensions were created for this purpose, in particular GL_ARB_shader_objects, GL_ARB_vertex_shader, and GL_ARB_fragment_shader. Chapter seven contains detailed descriptions of the entry points provided by these new extensions. Among other things, it describes how shader objects are created, compiled, and then linked to form shader programs that can then be used to render objects. Appendix B also has a reference section on the new entry points similar in style to the "Blue Book." Chapter seven concludes the dry, technical part of the book that introduced both the shading language and the necessary infrastructure to use it from a host program running on the CPU.
The remaining chapters delve into the really interesting topic: shader development. These chapters offer multiple ideas on what can be done with shaders and how to effectively use them in graphics programming. Standard techniques, such as bump mapping, procedurally creating textures, using noise, and others, are presented. Chapter nine deserves special mention because it presents shaders that mimic the behavior of the OpenGL fixed-function pipeline. Many developers new to shader programming are faced with re-implementing certain features offered by OpenGL's fixed functionality. This chapter addresses this.
Chapter fourteen also deserves mention. Shaders that procedurally create textures usually suffer from aliasing artifacts. This chapter shows a number of anti-aliasing techniques to diminish these artifacts. In my opinion, this important topic has not received the attention it deserves -- it's good to see such a chapter in this book.
Closing this section of the book, chapters fifteen and sixteen describe some interesting non-photorealistic shaders and shaders for doing image processing. (For more ideas on what can be done with shaders I also recommend the book "GPU Gems", which I have read and reviewed some time ago.
The final chapter of the book (chapter seventeen) is a language comparison with other high-level shading languages such as the RenderMan Shading Language, SGI's Interactive Shading Language from the OpenGL Shader package, Microsoft's HLSL, and NVIDIA's Cg. Although I am quite familiar with most of these languages, I found this chapter to be an interesting read because it attempts to look at the languages objectively, listing advantages and disadvantages of the various approaches.
The book contains many diagrams and images, all in black and white, except for 16 pages containing 30 color plates in the middle of the book. Most of the images are not overly "flashy" but do give a practical idea of the types of rendered images a particular shader can produce.
There is also a website for the book where you can find an errata list and download a sample chapter (chapter six). As mentioned above, this chapter develops a simple brick shader to show the basic features of the shading language. The website also has all the shaders presented in the book available for download. Because the book does not come with a CD-ROM this is the only means of getting shaders code without having to type them. At the time of this review, the site appeared to be in a transitional state.
Rost's OpenGL Shading Language succeeds at giving a good introduction to shader programming with the OpenGL Shading Language. Not only does it provide the necessary technical instruction to allow the reader to write his/her own shaders as well as integrate them with the host program, it also demonstrates a number of practical applications for shaders and tries to encourage exploring the new dimension of real-time graphics programming opened up by the OpenGL Shading Language. Since there is no other book currently available on this topic, it is hard to say whether the "Orange Book" will stand the test of time and actually become the reference book on the OpenGL Shading Language, but I believe it will.
Ecker has been involved in real-time graphics programming for more than 9 years and works as a arcade game developer. He also works on a graphics-related open source project called XEngine. You can purchase OpenGL Shading Language from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, carefully read the book review guidelines, then visit the submission page.
I'm no Microsoft apologist, but there's one architectural decision that I foresee to be the potential cause of major problems: the fact that each IHV's driver is responsible for the high-level compilation of the shading language, rather than having a common runtime do it, as with DirectX HLSL, where it is compiled into a intermediate binary token format, after which is then passed on to the driver and turned into a vendor-specific and optimized binary format.
The core competencies of graphics IHVs are generally not in compiler writing -- writing a good optimizing compiler is still a "black art" significantly more difficult than writing a hardware driver, and it will be very annoying if compiler bugs show up on some vendors' drivers and some don't, forcing developers to work around them -- or different compilers optimize things differently.
At least with DirectX, there's guaranteed to exist one common compiler that's written by a company with years of experience in optimizing compilers.
Of course, the philosophy of OpenGL is counter to DirectX in that there's no one Big Company controlling it all, but
at the very least, there needs to be some standard token bytecode defined and standarized by the ARB, and a reference compiler design, as well as a compliance suite to verify compiler correctness and language compliance.
-- Samir Gupta, Ph. D. Head, New Technology Research Group, Nintendo Co. Ltd., Kyoto, Japan.
Lest we forget, the most important thing after the widget set is the documentation on how to use it...
I spoke to a friend who is a developer for visualization applications. He feels that with Open GL 1.5 and some of the new ratified standards from the OpenGL ARB, OpenGL is back on par with Direct X 9.0.
Anyone have other insights ?
The most important design issue... is the fact that Linux is supposed to be fun... -- Linus Torvalds at the First Dutch International Symposium on Linux
No one will be able to say this isn't "News for Nerds".
Just wait till some crappy band steals your nic.
I found the Orange Book to be a very good introduction to GLSL, and I think it'll make for a good reference as I develop stuff. It does a good job of explaining how GLSL fits into the entire GL pipeline, which is cool because I don't know of any other books on OpenGL shader development that do this (It was hard to write a book on GL shaders before, and while GLSL is still ARB extensions, it's pretty clear that it's really targeted towards the core, while ARBvp1/ARBfp1 really seemed to just be placeholders until something really robust--GLSL--could come along).
Although it has a short overview of the basic features of OpenGL, it is intended for an audience that is already somewhat familiar with OpenGL and with 3D graphics programming in general
Boy, I hope someone looking to learn OpenGL at least knows enough about 3D graphics to realize this isn't likely the right book to start learning OpenGL with. That said, the Red and Blue books are indispensable. I certainly hope this "Orange" book lives up to the family reputation.
Anyone know if Doom 3 uses these newly ratified extension? I would imagine it does - but I of course have no facts to back this up.
I saw on the listing of seminars/tutorials at WWDC a few weeks ago that Apple was presenting a session on OpenGL Shading Language, yet I didn't even know it was supported on OS X. Is it currently supported in Panther? Will it be supported in Tiger? Will there be an IDE for writing shaders, etc?
http://www.opengl.org/documentation/oglsl.html
Save youselves a bob or two.
-S
Hahah... good one.
I'm guessing whoever modded this down doesn't know what they're talking about.
But for what it's worth, the parent raises some interesting points, although I disagree that a bytecode/pseudo asm route is a better method.
In reality, the ASM-like bytecode used probably bears little resemblance to the machine code instruction sets actually used by the various cards. Forcing vendors to use such a low level instruction set allows very little room for optimisation, whereas allowing vendors to write their own compilers gives much better scope to improve the code at the machine code according to their hardware's instruction sets and limitations.
There may be a few teething problems at first (witness the arguments about nvidia's GLSL compliance on OpenGL.org), but I'm convinced its a better method overall.
We used the red book for a graphics class in college. After browsing the book for a couple of evenings I was able to write a simple OpenGL app to draw, texturemap, light, and animate a heightfield made of triangle strips. In fact, I spent less time writing the OGL code than figuring out how to get GTK to throw up a drawing window.
Yes, it's that good.
...because I think OpenGL would have been relegated to academia and top-end engineering applications at most if he hadn't used OpenGL for his Quake engines.
Why do I care about this? Because M$ only produces/copies acceptable technologies when heavy pressure is applied to them or if they are playing catch-up. Look at IE for example. I speculate that DirectX would have dominated as the graphics API but would have followed a similar fate to IE if it hadn't been for OpenGL racing along with decent drivers all because of this thing called Quake.
Thinking short-term as a developer, I would have loved if the world had one standard, even for shaders, but I take off my hat to Darwin and accept a little hardship implementing for two or more APIs to let them evolve each other.
Here is brief overview of shading, in case you don't know.
Shading is the process by which a renderer assigns color values to pixels on the screen. There are currently three popular rendering methods: Rasterization, REYES, and Ray-tracing. Rasterization simply projects planar polygons onto a 2D plane and discreetizes them to pixels. REYES is slightly more complicated in that it takes mathmatically defined patches, slices and dices them into micro-polygons which are then rasterized. Ray-tracing point-samples the scene by tracing rays through it.
Rasterization is the method of rendering that is implemented on your commodity graphics accelerator. It is commonly considered the most adept at handling real-time graphics. In the past, the type of shading that you could perform in real-time was rather limited. What people did was take a dial and switch approach to shading. Users would tell their graphics API (like OpenGL) what features they wanted to use and pass in parameters. That was basically it. Unfortunately, the more bells and whistles you add to the rendering pipeline, the more unweildly your graphics API becomes. This is because, users deal with the most shading algorithm possible, no matter how simple the task.
Hence, the programmable pipeline is born. Instead, of adjusting dials and switches, you just write little procedures telling the renderer exactly what you want it to do at a certain stage of rendering pipeline. There is some history behind this approach coming from REYES renderer. Renderman is the shading language that tells Pixar's REYES renderer what to do. It is popular, and it is pretty standard in the graphics industry. People have even implemented Ray-tracers capable of using the same shaders that were used for the REYES renderer. But, in all this is pretty inefficent. Shaders are intimately tied to the rendering method that you are using. Therefore, a new shading language needed to be developed.
In all, the shading languages will look like Renderman, which in turn looks like C. Real-time shading languages differ in their specifics. But, in rasterization there are two procedures you can write. A vertex shader which allows the procedure to manipulate points of your planar polygon in some fashion, and a fragment shader which is invoked after the discreetization to actually color the pixels. Cg and HLSL are actually the same shading language. OpenGL Shading language (glslang) is an alternative. Probably the biggest difference at this point is HLSL/Cg dictates a particular instruction set architecture on the hardware. GLslang doesn't, but it requires that your graphics driver has a compiler that compiles the shading language to something the graphics card can use.
In all, I think the money is on HLSL/Cg to win. It has been out considerably longer, and I think it has already picked up developer mind-share. I also think that it makes things considerably easier for graphic driver writers although it might be more limiting. However, we probably won't feel the pains of it being limiting in the next 5 years, and by then the battle will be over.
As for those that mention rendermonkey. Rendermonkey isn't a shading language, it is a suite of tools that help you produce shaders. It uses HLSL/Cg or a general graphics assembly language underneath. It is pretty independent of the whole shading language war.
That said, this book might be good to pick up independent of the actual language that it discusses just because it goes over important issues that you face when writing these shaders.
Facts are meaningless. You could use facts to prove anything that's even remotely true! -Homer Simpson
I think the compilation by the IHVs is a very good idea. It means that the driver can optimise in the best way for the card.
If you remember figures starting coming out about very poor performance with nVidia hardware and Half-Life 2 a while back. If HL2 had been OpenGL, nVidia could have tweaked their driver to workaround the suboptimal way Valve was driving their hardware, presumably to the benefit of the frame rate. ATI could have done the same thing.
It's free... but the Orange Book covers much more. I just bought the Orange Book because of that promotion on the front page (35% off). $60 is a little rich. :D
Cg/HLSL are definitely older and hence have picked up developer mind share, but being the first few high-level realtime shading languages, they have design flaws. GLSL is based on an excellent design and if your API of preference is OpenGL, then GLSL is what you will want to use.
Probably the most eagerly awaited feature for OpenGL developers, everything in the OpenGL state is directly accessible using built-in variables in the shader and need not passed explicitly from the program before executing the shaders.
'varying' variables are another cool design feature, where you can pass data from the vertex shader to the fragment shader directly.
GLSL is based on excellent design and it is only a matter of time before OpenGL developers switch to it. I, for one, have switched to GLSL. nVidia's latest SDK (7.0) already has many GLSL demos and their 0.60 drivers for FX and 6800 already support GLSL.
--
The memory management on the PowerPC can be used to frighten small children. -Linus
PRMan implements the REYES algorithm, based in buckets, micropolygons and other things you can learn by reading any of the book about it. Pixie also implements a REYES like method. BTW, REYES can be expanded as Renders Everything You Ever Saw.
There is nothing worse than an API designed by such a glacial commitee.
Funny you should mention ARB being "glacial," because if Microsoft were broken up or bankrupted then DirectX would not survive. Given that OpenGL is a platform-independent freedom standard, it would survive in the societies and communities if ANY of its first hosting organelles of persons were to disband. (*puts on Dummy::Bush hat*) If OpenGL is for freedom, that makes any person a Terrorist if they speak or write any negatory or against OpenGL. (happily removes Dummy::Bush hat*)
I'm an Alpha computer user, and look at that crazy x86 computer architecture; do you think everyone would drop dead if Intel pulled its lower lip over its face and swallowed itself up in bankrupty? Hello^H, No!
OpenGL is not glacial as you in contrast to your declaration, its moreso a Pykrete whilst Microsoft and its DirectX even Direct3D is the liquid sewer muck that evolves countless genera of virus exploits to this day.
Sincerily,
The Alpha Troll
You should actually write this up in wikipedia. It's quite a good write up on the subject.
Thanks!
Since the app sends the shader source code to the driver, it would be easy to see all the shaders an app is using. I remember Randi saying that this is one of the issues they are having to deal with. Microsoft doesn't have this problem because the shaders are compiled before shipping.
while not an issue for me, it will be for many companies planning on competitively deploying shaders in their apps.
-metric