Slashdot Mirror


The Cg Tutorial

Martin Ecker writes "NVIDIA's book The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics, published by Addison-Wesley is a book that many 3D graphics programmers have been waiting for. Finally a book is available that introduces NVIDIA's high-level shading language Cg (short for 'C for Graphics') and the concepts involved with writing shader programs for programmable graphics pipeline architectures to the interested reader." If you are such an interested reader, you'll find the rest of Ecker's review below. The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics author Randima Fernando, Mark J. Kilgard pages 384 publisher Addison-Wesley Publishing rating 8 reviewer Martin Ecker ISBN 0321194969 summary An excellent introduction to the high-level shading language Cg (C for Graphics) and its uses in real-time 3D graphics.

The first half of the book teaches the basic language constructs of the Cg shading language and shows how to use them in concrete example shaders, whereas the second half concentrates on more advanced techniques that can be achieved on today's programmable GPUs with Cg, such as environment or bump mapping. Even these more advanced techniques are explained in a clear and easy-to-understand manner, but the authors do not neglect to present the mathematics behind the techniques in detail. Especially the more serious 3D programmer will appreciate this fact. The explanation of texture space bump mapping must be the easiest-to-understand explanation of the technique I have read to date, which alone makes it worth to have this book on my shelf. At this point it is important to note that the book does not discuss the Cg runtime which is used by applications to compile and upload shaders to the GPU. The book focuses exclusively on the Cg language itself. So if you're already familiar with Cg and want to learn how to use the Cg runtime, this book is not for you and you should rather read the freely available Cg Users Manual.

The book contains many diagrams and figures to illustrate the discussed equations and show the rendered images produced by the presented shaders. Note that most figures in the book are in black and white which sometimes leads to funny situations, such as in chapter 2.4.3 where the resulting image of a shader that renders a green triangle is shown. Since the figure is not in color the triangle that is supposed to be solid green ends up being solid gray. However, in the middle of the book there are sixteen pages with color plates that depict most of the important color images and also show some additional images of various applications, NVIDIA demos, and shaders written for Cg shader contests at www.cgshaders.org.

Accompanying the book on CD-ROM is an application framework that allows you to modify, compile, and run all the example shaders in the book without having to worry about setting up a 3D graphics API, such as OpenGL or Direct3D. The application framework uses configuration files to load meshes and textures and set up the graphics pipeline appropriately for the shaders. This way the Cg shaders can be examined and modified in isolation with the results being immediately visible in the render window of the application. Thanks to this framework application even readers that are not yet familiar with a 3D graphics API or even 3D artists interested in programmable shading on modern GPUs can begin to learn Cg and experiment with real-time shaders.

A final note for programmers using Direct3D 9: The high-level shading language included with the latest version of Direct3D, simply called HLSL for High-Level Shader Language, is syntactically equivalent to Cg. Everything written in the book about Cg equally applies to HLSL. Thus, the book is also an excellent guide for programmers that only intend to work with HLSL.

This book truly is the definitive guide for all beginners with the Cg language, and also more advanced 3D programmers will find the chapters about vertex skinning, environment mapping, bump mapping, and other advanced techniques interesting. Once you've started writing shaders in Cg you will never want to go back to writing them in low-level assembly shading languages ever again.

You can purchase The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics from bn.com. The book's official website has additional information and ordering options besides. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

11 of 111 comments (clear)

  1. Re:Sounds interesting, but by Urkki · · Score: 3, Informative

    The shader programs are totally different anyway, they're not x86 (or whatever) assembly, they run in the graphics chip and have their own specialized assembly language suited for the stuff they do. So it can't be "just an extra library", it needs to be a new compiler and all that. The regular C-library part (which would be done through DirectX anyway) would be just for copying the shader programs to the graphics chip. But I'm not an expert, somebody correct me if I'm wrong.

  2. Re:Sounds interesting, but by Molt · · Score: 5, Informative

    Cg is a very specific language which runs on the graphics card itself, and is only used for pixel and vertex shader programming. It's always used in conjunction with one (Or even more) higher level libraries.
    Firstly you have the application-level library (SDL, for example), this handles the stuff like opening windows, the user interaction. This is also the bit that's often written specifically for the game.
    At the next level we have the 3d library, normally OpenGL or DirectX. This handles most of the actual graphics work itself, such as displaying your polygon meshes, applying standard lighting effects, and so forth.
    Finally we hit the shader level. It's here that Cg comes into it's own, with special snippets of Cg code to get the reflections on the water to look just right and ripple as the character walks through, or to make the velvet curtains actually have the distinctive sheen. Special effects work only.
    It is worth noting that Direct X does have it's own way of doing shaders now, and OpenGL does have a specification for them but last time I looked no one had this implemented.
    Hope this makes sense.

    --
    404 Not Found: No such file or resource as '.sig'
  3. Re:YEAH BUT DOES IT RUN LINUX? by Anonymous Coward · · Score: 1, Informative

    Yes it runs on Linux. There are RPMs and TGZs at http://developer.nvidia.com/view.asp?IO=cg_toolkit

  4. good book by Horny+Smurf · · Score: 5, Informative
    I got a copy last month, and I've only read a few chapters, and skimmed some others, but it looks liek a good book.


    Don't let the title foo you -- it contains high level descriptions of the algorithms as well as the mathematical concepts. They cover some advanced realtime techniques that older books don't (since the processing power wasn't there even 4 years ago), but also discuss optimizing for low-end systems.


    I do recommend this book if you ahve any interest in graphic programming (whether you use Cg or not). If you use it with Coputer Graphics (3rd edition), you should have access to pretty much all graphic algorithms. (at least until TAOP volume 7: Computer Graphics is written :)

  5. Re:NVIDIA by br0ck · · Score: 3, Informative

    This overview by Tom's Hardware of HLSLs says that Nvidia is pushing for Cg to be hardware independant and used by all video card vendors (see the "Which HLSL?" page). The article also explains exactly what HLSLs are and why ATI and Nvidia have created the respective languages Rendermonkey and Cg.

  6. Re:Sounds interesting, but by SmirkingRevenge · · Score: 2, Informative

    C/C++ is a high level language. It's intended for writing code for the more macro level (moving objects around a scene, setting lighting sources, texturing, etc).

    Cg on the other hand is used for writing what are known as procedural shaders. Shaders determine what an object/particle will look like, _procedural_ shaders can change what a polygon will look like based on any number of criteria (not the lease of which is time).

    So if I'm going to texture a wall with wood, it makes sense to proceduraly generate the wood. If I'm making a flickering fire, a procedural fire texture will look a zillion times better. To do these sorts of things you need to operate VERY quickly, and very exactly, a great example of a fire shader is a perlin noise in 4-d fire from the CgShaders site: http://www.cgshaders.org/shaders/show.php?id=39

    The above will blow you away.

  7. Cg and OpenGL by dmouritsendk · · Score: 2, Informative

    First, i dont understand why some people think its a bad idea nvidia are doing Cg. One of the goals of Cg is easier crossplatform development.. thats a noble cause if any =D

    Secondly, my bet is OpenGLs shading language and Cg will eventually merge.

    check: this

    and notice this part:

    Compatibility with DX is very important, but they're willing to weigh advantages of changes vs. cost. Cg and the proposed "OpenGL 2.0" language are similar but not identical; both look like C, and both target multiple underlying execution units. Interfaces, communication between nodes, and accessing state across nodes are different. It's very late, but not too late to contemplate merging the two languages.


  8. Re:Other books/sources by magic · · Score: 5, Informative
    Why not a good GL wrapper for CG, does one exist? How about some good GL samples, period? Can anyone help here?


    I released 75,000 lines of C++ code for supporting OpenGL game development on Windows and Linux as the G3D library. It is under the BSD license. The next release includes support for the shaders that are compiled by Cg-- you can grab it from the SourceForge CVS site.


    G3D includes some small OpenGL demos (~200 lines), wrappers for the nasty parts of OpenGL, and wrappers for objects like textures and vertex shaders.


    -m

  9. Re:GCC and ANSI C standards by Hortensia+Patel · · Score: 2, Informative

    The current development version of GCC (3.3) *does* have support for automatic vectorization (i.e. using SIMD instructions where appropriate). I'm not sure whether you need to help it out by flagging decls with GCC-specific attributes, but it's definitely there.

    As others have said, Cg is not an extension of C, and GCC will never and should never support it.

  10. Re:Do they still make "demos"? by Anonymous Coward · · Score: 1, Informative

    scene.org

    That'll take you in. It's a little difficult to google "demo."

  11. Re:Cg for NVIDIA only? by Dr.+Sp0ng · · Score: 4, Informative

    orgive my ignorance, but is Cg made for NVIDIA only? Or is it even optimized for NVIDIA chips?

    It's not optimized for anything. When you compile a Cg program (either offline or at runtime - the Cg compiler is very fast!) you specify a "profile" for it to use. Some of the currently-supported profiles are arbvp1 (which outputs code for OpenGL's ARB_vertex_program extension), vs_1_1 (DirectX 8 vertex shader), vp20 (NVIDIA's NV_vertex_program OpenGL extension), vp_2_0/vp_2_x (DirectX 9 vertex shader), vp30 (NVIDIA's NV_vertex_program2 OpenGL extension), ps_1_1/ps_1_2/ps_1_3 (DirectX 8 pixel shader), fp20 (OpenGL NV_texture_shader and NV_register_combiners extensions), arbfp1 (OpenGL's ARB_fragment_program extension - vendor-independent for older cards), ps_2_0/ps_2_x (DirectX 9 pixel shader, vendor-independent for 4th generation GPUs), and fp30 (NV_fragment_program OpenGL extension, for 4th-gen NVIDIA GPUs).

    So these profiles are optimized for their target platforms, and yes, currently NVIDIA chips are better supported. However, vendors can write profiles for their chips without NVIDIA's support, so for example, ATI could write a profile for the Radeon 9800 and it would work fine. However, ATI has already written support for DX9 shaders, so the vs_2_x/ps_2_x targets would work fine for that (or vs_1_x/ps_1_x for the 8500 generation).

    Don't listen to the Slashbots here - I am a professional game developer, and Cg is a godsend (and I'm even developing mostly using ATI cards). Since runtime compilation is so fast, Cg programs can be compiled when the game is played and the exact hardware being used is known. I don't imagine I have to go into more detail as to why that's a fantastic thing.