1 / 27

Cg ShadingTutorial (Open GL)

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 ”.

trilby
Download Presentation

Cg ShadingTutorial (Open GL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Overview • What is Cg? • How to Install/Run Cg programs • Anatomy of a Cg Shader. • Cg Examples • RenderTexture

  3. 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

  4. What is Cg used for?

  5. What does Cg look like?

  6. 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).

  7. 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.

  8. 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).

  9. 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

  10. How can I use Cg? • Your application must call the Cg Runtime to invoke the Cg programs and pass the appropriate parameters.

  11. 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.

  12. Cg Runtime Example

  13. Anatomy of a Cg Vertex Program

  14. Anatomy of a Cg Fragment Program

  15. 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.

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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”

  24. Uses a fragment program to read normal and color information from texture maps and perform standard "fixed function" plastic lighting. Normal Mapping

  25. 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/

  26. 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).

  27. 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.

More Related