The Cg Runtime Cyril Zeller Cg Pipeline Graphics

  • Slides: 12
Download presentation
The Cg Runtime Cyril Zeller

The Cg Runtime Cyril Zeller

Cg Pipeline Graphics programs are written in Cg. . . and compiled to. .

Cg Pipeline Graphics programs are written in Cg. . . and compiled to. . . low-level assembly code. . . that runs on any GPU compatible with Direct. X or Open. GL Cg Runtime API

Compiling Offline At Development Time Cg program source code Cg Compiler Shader program assembly

Compiling Offline At Development Time Cg program source code Cg Compiler Shader program assembly code Shader Compiler (nvasm. exe, psa. exe) Shader program binary code // // Diffuse lighting // float d = dot(normalize(frag. N), normalize(frag. L)); if (d < 0) d = 0; c = d*tex 2 D(t, frag. uv)*diffuse; … … DP 3 RSQ MUL DP 3 MAX MUL TEX MUL … r 0. x, f[TEX 0]; r 0. x, r 0. x; r 0, r 0. x, f[TEX 0]; r 1. x, f[TEX 1]; r 1. x, r 1. x; r 1, r 1. x, f[TEX 1]; r 0, r 1; r 0. x, 1. 0; r 0, r 0. x, DIFFUSE; r 1, f[TEX 1], 0, 2 D; r 0, r 1; At Runtime At initialization: Load assembly or binary program For every frame: Load program parameters to hardware registers Set rendering state Load geometry Render

Compiling at Runtime At Development Time At Runtime At initialization: Compile and load Cg

Compiling at Runtime At Development Time At Runtime At initialization: Compile and load Cg program source code // // Diffuse lighting // float d = dot(normalize(frag. N), normalize(frag. L)); if (d < 0) d = 0; c = d*tex 2 D(t, frag. uv)*diffuse; … For every frame: Load program parameters with the Cg Runtime API Set rendering state Load geometry Render

Pros and Cons of Runtime Compilation Pros: Future compatibility: The application does not need

Pros and Cons of Runtime Compilation Pros: Future compatibility: The application does not need to change to benefit from future compilers (future optimizations, future hardware) Easy parameter management Cons: Loading takes more time because of compilation Cannot tweak the result of the compilation

Cg Runtime Architecture Core Cg Runtime: cg. Create. Program Direct 3 D Cg Runtime:

Cg Runtime Architecture Core Cg Runtime: cg. Create. Program Direct 3 D Cg Runtime: cg. D 3 D 9 Load. Program Open. GL Cg Runtime: cg. GLLoad. Program

Core Cg Runtime Does not make any 3 D API call Allows you to:

Core Cg Runtime Does not make any 3 D API call Allows you to: Create a context: cg. Create. Context() Compile a program for a given profile (vs_2_0, vs_2_x, ps_2_0, ps_2_x, arbvp 1, vs_1_1, ps_1_1, fp 20, etc. . . ): cg. Create. Program(), cg. Get. Program. String(), etc. . . Manage program parameters: Iterate through the parameters: cg. Get. First. Parameter(), cg. Get. Next. Parameter(), etc. . . Get parameter information: type, semantic, register, . . . Handle errors: cg. Get. Error(), cg. Set. Error. Callback(), etc. . .

Direct 3 D Cg Runtime: Minimal Interface Does not make any Direct 3 D

Direct 3 D Cg Runtime: Minimal Interface Does not make any Direct 3 D call Allows you to translate the information obtained through the Core Runtime to Direct 3 D data structures, so that you can: Create a Direct 3 D vertex declaration from the Cg program: cg. D 3 D 9 Get. Vertex. Declaration() Validate a Direct 3 D vertex declaration against the Cg program: cg. D 3 D 9 Validate. Vertex. Declaration() and create a Direct 3 D vertex or pixel shader from the Cg program

Direct 3 D Cg Runtime: Expanded Interface Makes the necessary Direct 3 D calls

Direct 3 D Cg Runtime: Expanded Interface Makes the necessary Direct 3 D calls for you Allows you to: Pass the Direct 3 D device: cg. D 3 D 9 Set. Device() Load a program into Direct 3 D: cg. D 3 D 9 Load. Program() Tell Direct 3 D to render with it: cg. D 3 D 9 Bind. Program() Set parameter values: cg. D 3 D 9 Set. Uniform(), cg. D 3 D 9 Set. Uniform. Array(), cg. D 3 D 9 Set. Texture(), etc. . . Output debug information by using the Cg Runtime debug DLL

Open. GL Cg Runtime Makes the necessary Open. GL calls for you Allows you

Open. GL Cg Runtime Makes the necessary Open. GL calls for you Allows you to: Load a program into Open. GL: cg. GLLoad. Program() Enable a profile: cg. GLEnable. Profile() Tell Open. GL to render with it: cg. GLBind. Program() Set parameter values: cg. GLSet. Parameter{1234}{fd}{v}(), cg. GLSet. Parameter. Array{1234}{fd}(), cg. GLSet. Texture. Parameter(), etc. . .

Learning the Runtime The documentation is in: Windows: C: Program FilesNVIDIA CorporationCgdocs Linux: /usr/share/doc/Cg

Learning the Runtime The documentation is in: Windows: C: Program FilesNVIDIA CorporationCgdocs Linux: /usr/share/doc/Cg It includes: A Using the Cg Runtime Library chapter from the Cg User’s Manual pages for every function Source code examples are in: Windows: C: Program FilesNVIDIA CorporationCgexamples Linux: /usr/local/Cg/examples They include self-contained Direct 3 D and Open. GL examples

Questions, comments, feedback? Cyril Zeller, czeller@nvidia. com For Cg support Go to developer. nvidia.

Questions, comments, feedback? Cyril Zeller, czeller@nvidia. com For Cg support Go to developer. nvidia. com/Cg Go to www. cgshaders. org Email cgsupport@nvidia. com