270 likes | 584 Views
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 ”.
E N D
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”. • 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
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).
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.
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).
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
How can I use Cg? • Your application must call the Cg Runtime to invoke the Cg programs and pass the appropriate parameters.
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.
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.
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). Green 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); Color 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. Normal Vertex 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. Normal 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". Texture Fragment Sphere
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. “Plastic” Per-Vertex 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.) “Plastic” Per-Fragment Shading
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; Color Shaping using “lerp”
Uses a fragment program to read normal and color information from texture maps and perform standard "fixed function" plastic lighting. Normal Mapping
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/
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).
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.