|
Features

Animation With Cg
What
is Cg? The Cg language makes it possible for you to control the shape,
appearance, and motion of objects drawn using programmable graphics
hardware. It marries programmatic control of these attributes with
the incredible speed and capabilities of today's graphics processors.
Never before have computer graphics practitioners, whether artists
or programmers, had so much control over the real-time images they
generate.
Cg
provides developers with a complete programming platform that is
easy to use and enables the fast creation of special effects and
real-time cinematic-quality experiences on multiple platforms. By
providing a new level of abstraction, Cg removes the need for developers
to program directly to the graphics hardware assembly language,
and 2 thereby more easily target OpenGL, DirectX, Windows, Linux,
Macintosh OS X, and console platforms such as the Xbox. Cg was developed
in close collaboration with Microsoft Corporation and is compatible
with both the OpenGL API and Microsoft's High-Level Shading Language
(HLSL) for DirectX 9.0.
Cg
stands for "C for graphics." The C programming language
is a popular, general purpose language invented in the 1970s. Because
of its popularity and clean design, C provided the basis for several
subsequent programming languages. For example, C++ and Java base
their syntax and structure largely on C. The Cg language bases itself
on C as well. If you are familiar with C or one of the many languages
derived from C, then Cg will be easy to learn.
On
the other hand, if you are not familiar with C or even programming
languages in general but you enjoy computer graphics and want to
learn something new, read on anyway. Cg programs tend to be short
and understandable.
A
Language for Programming Graphics Hardware
Cg
is different from C, C++, and Java because it is very specialized.
No one will ever write a spreadsheet or word processor in Cg. Instead,
Cg targets the ability to programmatically control the shape, appearance,
and motion of objects rendered using graphics hardware. Broadly,
this type of language is called a shading language. However, Cg
can do more than just shading. For example, Cg programs can perform
physical simulation, compositing, and other nonshading tasks.
Think
of a Cg program as a detailed recipe for how to render an object
by using programmable graphics hardware. For example, you can write
a Cg program to make a surface appear bumpy or to animate a virtual
character. Later you will learn more about the history of shading
languages and where Cg fits into this history.
Cg's
Data-Flow Model
In
addition to being specialized for graphics, Cg and other shading
languages are different from conventional programming languages
because they are based on a data- 3 flow computational model. In
such a model, computation occurs in response to data that flows
through a sequence of processing steps.
Cg
programs operate on vertices and fragments (think "pixels"
for now if you do not know what a fragment is) that are processed
when rendering an image. Think of a Cg program as a black box into
which vertices or fragments flow on one side, are somehow transformed,
and then flow out on the other side. However, the box is not really
a black box because you get to determine, by means of the Cg programs
you write, exactly what happens inside.
Every
time a vertex is processed or the rasterizer generates a fragment
while rendering a 3D scene, your corresponding vertex or fragment
Cg program executes.
Most
recent personal computers-and all recent game consoles-contain a
graphics processing unit (GPU) that is dedicated to graphics tasks
such as transforming and rasterizing 3D models. Your Cg programs
actually execute within the GPU of your computer.
GPU
Specialization and CPU Generalization
Whether
or not a personal computer or game console has a GPU, there must
be a CPU that runs the operating system and application programs.
CPUs are, by design, general purpose. CPUs execute applications
(for example, word processors and accounting packages) written in
general-purpose languages, such as C++ or Java.
Because
of the GPU's specialized design, it is much faster at graphics tasks,
such as rendering 3D scenes, than a general-purpose CPU would be.
New GPUs process tens of millions of vertices per second and rasterize
hundreds of millions or even billions of fragments per second. Future
GPUs will be even speedier. This is overwhelmingly faster than the
rate at which a CPU could process a similar number of vertices and
fragments. However, the GPU cannot execute the same arbitrary, general-purpose
programs that a CPU can.
The
specialized, high-performance nature of the GPU is why Cg exists.
Generalpurpose programming languages are too open-ended for the
specialized task of processing vertices and fragments. In contrast,
the Cg language is fully dedicated to this task. Cg also provides
an abstract execution model that matches the GPU's execution model.
You will learn about the unique execution model of GPUs in coming
sections.
The
Performance Rationale for Cg
To
sustain the illusion of interactivity, a 3D application needs to
maintain an animation rate of 15 or more images per second. Generally,
we consider 60 or more frames per second to be "real time,"
the rate at which interaction with applications appears to occur
instantaneously. The computer's display may have a million or more
pixels that require redrawing. For 3D scenes, the GPU typically
processes every pixel on the screen many times to account for how
objects occlude each other, or to improve the appearance of each
pixel. This means that real-time 3D applications can require hundreds
of millions of pixel updates per second. Along with the required
pixel processing, 3D models are composed of vertices that must be
transformed properly before they are assembled into polygons, lines,
and points that will be rasterized into pixels. This can require
transforming tens of millions of vertices per second.
Moreover,
this graphical processing happens in addition to the considerable
amount of effort required of the CPU to update the animation for
each new image. The reality is that we need both the CPU and the
GPU's specialized graphics-oriented capabilities. Both are required
to render scenes at the interactive rates and quality standards
that users of 3D applications and games demand. This means a developer
can write a 3D application or game in C++ and then use Cg to make
the most of the GPU's additional graphics horsepower.
Coexistence
with Conventional Languages
In
no way does Cg replace any existing general-purpose languages. Cg
is an auxiliary language, designed specifically for GPUs. Programs
written for the CPU in conventional languages such as C or C++ can
use the Cg runtime to load Cg programs for GPUs to execute. The
Cg runtime is a standard set of subroutines used to load, compile,
manipulate, and configure Cg programs for execution by the GPU.
Applications supply Cg programs to instruct GPUs on how to accomplish
the programmable rendering effects that would not otherwise be possible
on a CPU at the rendering rates a GPU is capable of achieving.
Cg
enables a specialized style of parallel processing. While your CPU
executes a conventional application, that application also orchestrates
the parallel processing of vertices and fragments on the GPU, by
programs written in Cg.
If
a real-time shading language is such a good idea, why didn't someone
invent Cg sooner? The answer has to do with the evolution of computer
graphics hardware. Prior 5 to 2001, most computer graphics hardware-certainly
the kind of inexpensive graphics hardware in PCs and game consoles-was
hard-wired to the specific tasks of vertex and fragment processing.
By "hard-wired," we mean that the algorithms were fixed
within the hardware, as opposed to being programmable in a way that
is accessible to graphics applications. Even though these hard-wired
graphics algorithms could be configured by graphics applications
in a variety of ways, the applications could not reprogram the hardware
to do tasks unanticipated by the designers of the hardware. Fortunately,
this situation has changed.
Graphics
hardware design has advanced, and vertex and fragment processing
units in recent GPUs are truly programmable. Before the advent of
programmable graphics hardware, there was no point in providing
a programming language for it. Now that such hardware is available,
there is a clear need to make it easier to program this hardware.
Cg makes it much easier to program GPUs in the same manner that
C made it much easier to program CPUs.
Before
Cg existed, addressing the programmable capabilities of the GPU
was possible only through low-level assembly language. The cryptic
instruction syntax and manual hardware register manipulation required
by assembly languages-such as DirectX 8 vertex and pixel shaders
and some OpenGL extensions-made it a painful task for most developers.
As GPU technology made longer and more complex assembly language
programs possible, the need for a high-level language became clear.
The extensive low-level programming that had been required to achieve
optimal performance could now be delegated to a compiler, which
optimizes the code output and handles tedious instruction scheduling.
Figure 1-1 is a small portion of a complex assembly language fragment
program used to represent skin. Clearly, it is hard to comprehend,
particularly with the specific references to hardware registers.
In
contrast, well-commented Cg code is more portable, more legible,
easier to debug, and easier to reuse. Cg gives you the advantages
of a high-level language such as C while delivering the performance
of low-level assembly code.
Other
Aspects of Cg
Cg
is a language for programming "in the small." That makes
it much simpler than a modern general-purpose language such as C++.
Because Cg specializes in transforming vertices and fragments, it
does not currently include many of the complex features required
for massive software engineering tasks. Unlike C++ and Java, Cg
does not support classes and other features used in object-oriented
programming. Current Cg 6 implementations do not provide pointers
or even memory allocation (though future implementations may, and
keywords are appropriately reserved). Cg has absolutely no support
for file input/output operations. By and large, these restrictions
are not permanent limitations in the language, but rather are indicative
of the capabilities of today's highest performance GPUs. As technology
advances to permit more general programmability on the GPU, you
can expect Cg to grow appropriately. Because Cg is closely based
on C, future updates to Cg are likely to adopt language features
from C and C++.
|
|
. . .
DEFINE LUMINANCE = {0.299, 0.587, 0.114, 0.0};
TEX H0, f[TEX0], TEX4, 2D;
TEX H1, f[TEX2], TEX5, CUBE;
DP3X H1.xyz, H1, LUMINANCE;
MULX H0.w, H0.w, LUMINANCE.w;
MULX H1.w, H1.x, H1.x;
MOVH H2, f[TEX3].wxyz;
MULX H1.w, H1.x, H1.w;
DP3X H0.xyz, H2.xzyw, H0;
MULX H0.xyz, H0, H1.w;
TEX H1, f[TEX0], TEX1, 2D;
TEX H3, f[TEX0], TEX3, 2D;
MULX H0.xyz, H0, H3;
MADX H1.w, H1.w, 0.5, 0.5;
MULX H1.xyz, H1, {0.15, 0.15, 1.0, 0.0};
MOVX H0.w, H1.w;
TEX H1, H1, TEX7, CUBE;
TEX H3, f[TEX3], TEX2, 1D;
MULX H3.w, H0.w, H2.w;
MULX H3.xyz, H3, H3.w;
. . .
|
 |
 |
 |
Figure 1-1. A Snippet of Assembly Language Code
|
Cg
provides arrays and structures. It has all the flow-control constructs
of a modern language: loops, conditionals, and function calls.
Cg
natively supports vectors and matrices because these data types
and related math operations are fundamental to graphics and most
graphics hardware directly supports vector data types. Cg has a
library of functions, called the Standard Library, that is well
suited for the kind of operations required for graphics. For example,
the Cg Standard Library includes a reflect function for computing
reflection vectors. Cg programs execute in relative isolation. This
means that the processing of a particular vertex or fragment has
no effect on other vertices or fragments processed at the same time.
There are no side effects to the execution of a Cg program. This
lack of interdependency among vertices and fragments makes Cg programs
extremely well suited for hardware execution by highly pipelined
and parallel hardware.
______________________________________________________
|