The Cg Tutorial - Chapter 1. Introduction. The Cg Tutorial. The Cg Tutorial is now available, right here, online. You can purchase a beautifully printed version of this book, and others in the series, at a 3. Inform. IT and Addison- Wesley.
- This change log includes the entire Granny development history. If you'd like to be notified when there's a new version of Granny, you can follow us at twitter.com/granny3d.
- Fullscreen AntiAliasing. Need a tutorial that will make your OpenGL applications look better? A big problem we all run into is aliasing. That is, the square edged 'jaggies' that exist on diagonal lines in relation to the.
- OpenGL Vertex buffer object (VBO) to store vertex array data with high-performance transfer rate such as positions, normals, tex coords, etc.
- OpenGL; Original author(s) Silicon Graphics: Developer(s) Khronos Group (formerly ARB) Initial release: January 1992; 24 years ago (1992-01) Stable release.
In this tutorial shader programming using GLSL 1.2 will be covered. Although many parts are now considered deprecated, the essence of GLSL has remained constant. Besides, this tutorial will continue to be online because. Intel HD Graphics Driver v10.18.10.3345 Available for Windows, New OpenGL 4.1 / 4.2 / 4.3 Extensions.
Introduction. Chapter 1. Introduction. This chapter has the following four sections. 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 thereby more easily target Open. GL, Direct. X, 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 Open. GL API and Microsoft's High- Level Shading Language (HLSL) for Direct. X 9. 0. Cg stands for . 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. Much of this chapter is background that provides valuable context for understanding Cg and using it effectively. On the other hand, you may find Cg is easier to learn by doing. Feel free to skip to Chapter 2 at any time if you feel more comfortable just diving into the tutorial.
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, in Section 1. 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- 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 . 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 3. D scene, your corresponding vertex or fragment Cg program executes. Section 1. 3 explains Cg's data- flow model further. 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 3. D 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 3. D 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. General- purpose 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 Section 1. The Performance Rationale for Cg. To sustain the illusion of interactivity, a 3. D application needs to maintain an animation rate of 1. Generally, we consider 6.
The computer's display may have a million or more pixels that require redrawing. For 3. D 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 3. D applications can require hundreds of millions of pixel updates per second. Along with the required pixel processing, 3.
D 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 3. D applications and games demand. This means a developer can write a 3. D 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 (described in Section 1. 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 to 2. 00. 1, 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. 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 Direct. X 8 vertex and pixel shaders and some Open. GL 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. Example 1- 1. A Snippet of Assembly Language Code. 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 . Because Cg specializes in transforming vertices and fragments, it does not currently include many of the complex features required for massive software engineering tasks.