27
Cg ShadingTutorial (Open GL) CIS 700/010 GPU Programming and Architecture Instructor: Dr. Suresh Venkatasubramanian TA: Paul Kanyuk

Cg ShadingTutorial (Open GL)

  • Upload
    greta

  • View
    19

  • Download
    0

Embed Size (px)

DESCRIPTION

Cg ShadingTutorial (Open GL). CIS 700/010 GPU Programming and Architecture Instructor: Dr. Suresh Venkatasubramanian TA: Paul Kanyuk. Overview. What is Cg? How to Install/Run Cg programs Anatomy of a Cg Shader. Cg Examples RenderTexture. What is Cg?. Cg stands for “ C for Graphics ”. - PowerPoint PPT Presentation

Citation preview

Page 1: Cg ShadingTutorial (Open GL)

Cg ShadingTutorial (Open GL)

CIS 700/010GPU Programming and Architecture

Instructor: Dr. Suresh Venkatasubramanian

TA: Paul Kanyuk

Page 2: Cg ShadingTutorial (Open GL)

Overview

What is Cg?

How to Install/Run Cg programs

Anatomy of a Cg Shader.

Cg Examples

RenderTexture

Page 3: Cg ShadingTutorial (Open GL)

What is Cg?

Cg stands for “C for Graphics”.

Cg compiles to code that can be executed by a GPU’s programmable fragment or vertex processor

Cg is “theoretically” cross-platform, API independent and future-proof. In reality, porting Cg code from system to system is a pain. For this class, we will use Cg and OpenGL with Visual Studio on Windows

Page 4: Cg ShadingTutorial (Open GL)

What is Cg used for?

Page 5: Cg ShadingTutorial (Open GL)

What does Cg look like?

Page 6: Cg ShadingTutorial (Open GL)

Where do I get Cg? http://developer.nvidia.com/page/cg_main.html

Download the latest Cg Toolkit, which contains: Cg Compiler Cg Runtime Cg User’s Manual Cg Browser Sample Cg Shaders

*Be sure to let the installer setup the appropriate environment variables (PATH, CG_LIB_PATH, CG_INC_PATH).

Page 7: Cg ShadingTutorial (Open GL)

Will Cg work on my Machine?

How old is your graphics card? Anything over a year old is starting to push it.

Try running the Cg Browser, and see how many of the effects run. If none run, you’re in trouble. If some of the fancy ones don’t, have no fear, that could be Nvidia’s way of scaring ATI users.

Page 8: Cg ShadingTutorial (Open GL)

A Better Test Try building and running C:\Program Files\NVIDIA

Corporation\Cg\examples\runtime_ogl_vertex_fragment\ogl_example.dsw in Visual Studio.

If you get errors about vertex or fragment programs being unsupported, your gpu is probably too old. (be careful that the PATH includes the Cg bin directory, if not, these errors can come up as well).

Page 9: Cg ShadingTutorial (Open GL)

If all else fails, Emulate. If your hardware is just too old, you can always run Cg

programs via software emulation. This technique, however, is very very SLOW.

http://download.nvidia.com/developer/GLSL/NVemulate.exe

Page 10: Cg ShadingTutorial (Open GL)

How can I use Cg?

Your application must call the Cg Runtime to invoke the Cg programs and pass the appropriate parameters.

Page 11: Cg ShadingTutorial (Open GL)

Cg Runtime The Cg Runtime can be more challenging than

Cg itself.

For complete documentation on the Cg Runtime, see the Cg User’s Manual.

The best way to learn is by hacking and poking around C:\Program Files\NVIDIA Corporation\Cg\examples\runtime_ogl_vertex_fragment\ogl_example.dsw in Visual Studio.

Page 12: Cg ShadingTutorial (Open GL)

Cg Runtime Example

Page 13: Cg ShadingTutorial (Open GL)

Anatomy of a Cg Vertex Program

Page 14: Cg ShadingTutorial (Open GL)

Anatomy of a Cg Fragment Program

Page 15: Cg ShadingTutorial (Open GL)

Cg Examples The following examples demonstrate, in

building complexity, how to use interesting Cg programs with OpenGL and the Cg runtime.

Each example is derived from the Nvidia Cg OpenGL test scene.

Page 16: Cg ShadingTutorial (Open GL)

Green Sphere Uses a simple

vertex program to color a sphere solid green.

The fragment program just sets its color output to its color input (does nothing but pass the data along).

Page 17: Cg ShadingTutorial (Open GL)

Color Sphere Same as green

sphere, but the Cg Runtime is used to pass color in as a uniform parameter.

float myColor[4] = { 1, 0, 0,1 };

cgGLSetParameter4fv(cgGetNamedParameter(vertexProgram, "iColor"),myColor);

Page 18: Cg ShadingTutorial (Open GL)

Normal Vertex Sphere Uses a vertex

program to shade a sphere with rgb components equal to the normal components.

The normals are passed via a varying vertex parameter.

Page 19: Cg ShadingTutorial (Open GL)

Normal Fragment Sphere Uses a fragment program

to set the sphere color to the normalized interpolated vertex normal (demonstrating the benefit of fragment programs over vertex programs).

The normals are passed to the vertex program via a varying parameters, and are passed to the fragment program via texture coordinates.

Page 20: Cg ShadingTutorial (Open GL)

Texture Fragment Sphere Uses a fragment program

to map a texture of the world to a sphere.

The texture coordinates are passed to the vertex program via a varying parameter.

The texture is passed directly to the fragment program as a "sampler".

Page 21: Cg ShadingTutorial (Open GL)

“Plastic” Per-Vertex Shading Uses a vertex

program to calculate standard "fixed function" lighting (aka plastic).

Note the “faceted” nature of the shading, this will be addressed in the next example.

Page 22: Cg ShadingTutorial (Open GL)

“Plastic” Per-Fragment Shading Uses a fragment

program to perform standard "fixed-function" lighting (aka plastic).

Note that the per-fragment calculations perform a much more accurate rendering (But Slower, most shading uses a hybrid approach.)

Page 23: Cg ShadingTutorial (Open GL)

Color Shaping using “lerp” Uses a fragment program to

perform color shaping on the diffuse and specular components of the standard "fixed function" lighting.

Color shaping is achieved by lerp'ing between two colors based on the diffuse and specular float values.

float3 specular = lerp(Ks0,Ks1,specularLight) * lightColor * specularLight;

Page 24: Cg ShadingTutorial (Open GL)

Normal Mapping Uses a fragment program

to read normal and color information from texture maps and perform standard "fixed function" plastic lighting.

Page 25: Cg ShadingTutorial (Open GL)

RenderTexture C++ class for rendering the output of a

fragment program to a texture (facilitates multi-pass rendering).

You can download RenderTexture from http://sourceforge.net/projects/gpgpu

You will also need to install glew (openGL Extension Wrangler): http://glew.sourceforge.net/

Page 26: Cg ShadingTutorial (Open GL)

RenderTexture Example A rotating glutTorus

is rendered into a texture, and then read into a Cg fragment program bound to a rotating plane.

Note, depth as well as color can be stored with RenderTexture (at variable bit depths).

Page 27: Cg ShadingTutorial (Open GL)

That’s all for now, be sure to Practice!

One of the best ways to learn Cg is by hacking existing examples: The Nvidia OpenGL CgSphere Demo, and the GPGPU RenderTexture example.

HW1 will ask you to do just that. This presentation as well as the slides will be available on the course website.