1 / 92

Programmation GPU avec Cg et GLSL

Programmation GPU avec Cg et GLSL. Olivier NOCENT olivier.nocent@univ-reims.fr. Introduction. « Cg (C for Graphics) is a high level shading language for programmable GPUs » ( Bill Mark, nVIDIA ) Qu’est ce que c’est ? Pour quoi faire ? Comment ça marche ?. High level shading language.

eden
Télécharger la présentation

Programmation GPU avec Cg et GLSL

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. Programmation GPUavec Cg et GLSL Olivier NOCENTolivier.nocent@univ-reims.fr

  2. Introduction «Cg (C for Graphics) is a high level shading language for programmable GPUs» (Bill Mark, nVIDIA) • Qu’est ce que c’est ? • Pour quoi faire ? • Comment ça marche ? IUT de Reims [ olivier.nocent@univ-reims.fr]

  3. High level shading language Qu’est ce que c’est ? Shading language: langage de description des interactions, simples ou complexes, entre la lumière et la matière. IUT de Reims [ olivier.nocent@univ-reims.fr]

  4. 1984 : «Shade Trees» par Rob Cook (Lucasfilm Ltd.) • Chaque feuille de l’arbre représente une valeur.(couleur, vecteur, coefficient, …) • Chaque nœud de l’arbre représente une opération.(addition, multiplication, produit scalaire, …) Final Color Copper Color Weight ofAmbientComponents Ambient Weight of Specular Components Specular Normal Viewer Surface Roughness IUT de Reims [ olivier.nocent@univ-reims.fr]

  5. Premiers « Shading Languages » Plus de souplesse dans la description des interactions Lumière/Matière. Prise en compte des aspects «dynamiques» (textures procédurales) 1988 : «Renderman» par Pat Hanrahan (Pixar) 2000 : «Interactive Shading Language» (SGI) 2001 : «Stanford Real-Time Shading Language» IUT de Reims [ olivier.nocent@univ-reims.fr]

  6. Langage généraliste Shading Language API graphique Langage GPU Arbre généalogique des « Shading Languages » C (AT&T, 1970) Shade Trees (Cook, 1984) C++ (AT&T, 1983) Renderman (Pixar, 1988) OpenGL (ARB, 1992) DirectX (Microsoft, 1995) Interactive Shading Language (SGI, 2000) Real-Time Shading Language (Stanford, 2001) C for Graphics (nVIDIA, 2002) HLSL (Microsoft, 2003) GL Shading Language (3Dlabs, 2004) IUT de Reims [ olivier.nocent@univ-reims.fr]

  7. Références bibliographiques • Shade TreesRobert Cook (SIGGRAPH 1984) • RenderMan : a Language for Shading and Lighting CalculationsPat Hanrahan and Jim Lawson (SIGGRAPH 1990) • A Shading Language on Graphics Hardware: The Pixel Flow Shading SystemMarc Olano and Anselmo Lastra (SIGGRAPH 1998) • A Real-Time Procedural Shading System for Programmable Graphics HardwareKekoa Proudfoot, Bill Mark and Svetoslav Tzvetkov (SIGGRAPH 2001) IUT de Reims [ olivier.nocent@univ-reims.fr]

  8. High level shading language for GPUs Pour quoi faire ? IUT de Reims [ olivier.nocent@univ-reims.fr]

  9. Graphics Pipeline Graphics pipeline: enchaînement d’opérations appliquées aux données géométriques afin de produire l’affichage d’une scène 3D. Application OpenGL DirectX Uniform parameters (Matrices, light positions, blend factors, …) Vertex operations Transform Lighting Fragment operations Texture mapping Rasterization & Interpolation Frame-Buffer Tests Primitive Assembly Textures Vertex indices IUT de Reims [ olivier.nocent@univ-reims.fr]

  10. Pixel OperationsTexture Mapping Vertex OperationsTransform & Lighting Primitive assembly Rasterization& Interpolation Graphics Pipeline Position Position Color Normal Texture Coords Texture Coords Color Color Texture Coords Depth IUT de Reims [ olivier.nocent@univ-reims.fr]

  11. 1998/1999 : la première génération. Rasterization + texture mapping (DirectX 6, OpenGL) • nVidia TNT2 • ATI Rage • 3dfx Voodoo3 Application OpenGL DirectX Uniform parameters (Matrices, light positions, blend factors, …) Vertex operations Transform Lighting Fragment operations Texture mapping Rasterization & Interpolation Frame-Buffer Tests Primitive Assembly Textures Vertex indices CPU GPU IUT de Reims [ olivier.nocent@univ-reims.fr]

  12. 1999/2000 : la deuxième génération. Transform & Lighting (DirectX 7, OpenGL) • nVidia GeForce 256 • ATI Radeon 7500 • S3 Savage3D Application OpenGL DirectX Uniform parameters (Matrices, light positions, blend factors, …) Vertex operations Transform Lighting Fragment operations Texture mapping Rasterization & Interpolation Frame-Buffer Tests Primitive Assembly Textures Vertex indices CPU GPU IUT de Reims [ olivier.nocent@univ-reims.fr]

  13. 2001/2002 : la troisième génération. Vertex programmability( DirectX 8, OpenGL NV_vertex_program ) • nVidia GeForce 3, GeForce 4 Ti • ATI Radeon 8500 Application OpenGL DirectX Uniform parameters (Matrices, light positions, blend factors, …) Custom Vertex Program Vertex operations Transform Lighting Fragment operations Texture mapping Rasterization & Interpolation Frame-Buffer Tests Primitive Assembly Textures Vertex indices CPU GPU IUT de Reims [ olivier.nocent@univ-reims.fr]

  14. 2003/? : la quatrième et dernière génération. Vertex & Pixel programmability(DirectX 9, OpenGL ARB_shader_object ) • nVidia GeForce FX, GeForce 6800 • ATI Radeon 9700 Application OpenGL DirectX Uniform parameters (Matrices, light positions, blend factors, …) Custom Fragment Program Custom Vertex Program Vertex operations Transform Lighting Fragment operations Texture mapping Rasterization & Interpolation Frame-Buffer Tests Primitive Assembly Textures Vertex indices CPU GPU IUT de Reims [ olivier.nocent@univ-reims.fr]

  15. Primitive assembly Rasterization& Interpolation Graphic Pipeline 3 appels au Vertex Program Position Position Vertex OperationsTransform & Lighting Color Normal Texture Coords Texture Coords n appels au Fragment Program Pixel OperationsTexture Mapping Color Color Texture Coords Depth IUT de Reims [ olivier.nocent@univ-reims.fr]

  16. Quelques chiffres… NB: le microprocesseur Pentium 4 d’Intel contient 55 M de transistors. IUT de Reims [ olivier.nocent@univ-reims.fr]

  17. Comparaison des performances CPU et GPU réalisée par Mark Harris [1]. Encore quelques chiffres… * GFLOPS : Giga floating point operations per second Loi de Moore (CPU) : Croissance sur 10 ans ~ 60× GPU : Croissance sur 10 ans > 1000× [1] «GPGPU: General-Purpose Computation on GPUs» par Mark Harris (NVIDIA Developer Technology Group) présenté à EuroGraphics’2004 IUT de Reims [ olivier.nocent@univ-reims.fr]

  18. 1995 SEGA Virtua Fighter 50.000 polygones/s 1 M píxels/s IUT de Reims [ olivier.nocent@univ-reims.fr]

  19. 2001 100 M polygones/s 1 G píxels/s IUT de Reims [ olivier.nocent@univ-reims.fr]

  20. Doom (1993) IUT de Reims [ olivier.nocent@univ-reims.fr]

  21. Doom II (1994) IUT de Reims [ olivier.nocent@univ-reims.fr]

  22. Doom III (2004) IUT de Reims [ olivier.nocent@univ-reims.fr]

  23. Doom III (2004) IUT de Reims [ olivier.nocent@univ-reims.fr]

  24. High level shading language for GPUs Comment ça marche ? IUT de Reims [ olivier.nocent@univ-reims.fr]

  25. Début 2004 Début 2003 Fin 2002 Chronologie DirectX 9HLSLHigh Level Shading Language OpenGL 1.5GLSLangOpenGL Shading Language IUT de Reims [ olivier.nocent@univ-reims.fr]

  26. Références bibliographiques The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics Randima Fernando, Mark J. Kilgard Addison-Wesley Professional edition February 26, 2003 384 pages ISBN: 0321194969 IUT de Reims [ olivier.nocent@univ-reims.fr]

  27. Références bibliographiques OpenGL(R) Shading Language Randi J. Rost Addison-Wesley Professional February 12, 2004) 608 pages ISBN: 0321197895 IUT de Reims [ olivier.nocent@univ-reims.fr]

  28. Références bibliographiques GPU Gems: Programming Techniques, Tips, and Tricks for Real-Time Graphics Randima Fernando Addison-Wesley Professional edition March 22, 2004 816 pages ISBN: 0321228324 IUT de Reims [ olivier.nocent@univ-reims.fr]

  29. Références bibliographiques GPU Gems 2 : Programming Techniques for High-Performance Graphics and General-Purpose Computation Matt Pharr, Randima Fernando Addison-Wesley Professional edition March 3, 2005 880 pages ISBN: 0321335597 IUT de Reims [ olivier.nocent@univ-reims.fr]

  30. Références bibliographiques Metaprogramming GPUs with Sh Michael McCool, Stefanus DuToit AK Peters, Ltd. July 29, 2004 307 pages ISBN: 1568812299 IUT de Reims [ olivier.nocent@univ-reims.fr]

  31. Sur le web Cg • http://developer.nvidia.com DirectX 9 HLSL • http://msdn.microsoft.com OpenGL Shading Language • http://www.opengl.org Programmation GPU • http://www.shadertech.com • http://www.gpgpu.org IUT de Reims [ olivier.nocent@univ-reims.fr]

  32. High level shading language for GPUs : C for Graphics IUT de Reims [ olivier.nocent@univ-reims.fr]

  33. Cg : principe général Cg shader Compilateur Cg (runtime) IUT de Reims [ olivier.nocent@univ-reims.fr]

  34. Cg : du code source au code exécutable OpenGL DirectX driver Graphics Hardware (GPU) Application DirectX Opengl Shader Object Assembly Program Shader Source Assembler Cg Compiler Cg API IUT de Reims [ olivier.nocent@univ-reims.fr]

  35. Acte I : présentation des acteurs. C/C++Program main CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  36. Acte II : contexte de communication entre le CPU et le GPU. CGcontext* Context = cgCreateContext(); Context C/C++Program main CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  37. Acte III : compilation et chargement du « shader ». VProgram = cgCreateProgramFromFile(Context,CG_SOURCE, "pixel_true_phong.cg",CG_PROFILE_VP30,"myvp",NULL); cgGLLoadProgram(VProgram); Context VProgram C/C++Program main VertexProgram myvp FragmentProgram myfp CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  38. Acte IV : association et passage de paramètres. LightPositionBind = cgGetNamedParameter(VProgram, "LightPosition"); cgGLSetParameter4f(LightPositionBind, x, y, z, w); Context VProgram C/C++Program main LightPosition VertexProgram myvp FragmentProgram myfp CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  39. Acte V : activation/désactivation du programme. cgGLEnableProfile(CG_PROFILE_VP30); cgGLBindProgram(VProgram); ... cgGLDisableProfile(CG_PROFILE_VP30); Context VProgram C/C++Program main LightPosition VertexProgram myvp FragmentProgram myfp CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  40. « Semantics » Vertex Program glVertex3f(…) POSITION POSITION glNormal3f(…) NORMAL COLOR glMultiTexCoord(0,…) TEXCOORD0 TEXCOORD0 glMultiTexCoord(1,…) TEXCOORD1 TEXCOORD1 Fragment Program COLOR COLOR TEXCOORD0 DEPTH TEXCOORD1 IUT de Reims [ olivier.nocent@univ-reims.fr]

  41. High level shading language for GPUs GLSL OpenGL Shading Language IUT de Reims [ olivier.nocent@univ-reims.fr]

  42. OpenGL Shading Language : principe général GLSL shader Compilateur GLSL (runtime) IUT de Reims [ olivier.nocent@univ-reims.fr]

  43. GLSL : du code source au code exécutable OpenGL driver Graphics Hardware (GPU) Application OpenGL only Shader Source Shader Object Program Object Linker OpenGL API Compiler IUT de Reims [ olivier.nocent@univ-reims.fr]

  44. Acte I : présentation des acteurs. C/C++Program main CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  45. Acte II : compilation et chargement du « shader ». Program VShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); glShaderSourceARB(VShader, 1, (const GLcharARB**) &VSource, NULL); glCompileShaderARB(VShader); FShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); glShaderSourceARB(FShader, 1, (const GLcharARB**) &FSource, NULL); glCompileShaderARB(FShader); Program = glCreateProgramObjectARB(); glAttachObjectARB(Program, VShader); glAttachObjectARB(Program, FShader); glLinkProgramARB(Program); C/C++Program main VertexProgram main FragmentProgram main CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  46. Acte III : association et passage de paramètres. GLint LightPositionloc = glGetUniformLocationARB(Program,"LightPosition"); glUniform4fARB(LightPositionloc , x, y, z, w); Program C/C++Program main LightPosition VertexProgram main FragmentProgram main CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  47. Acte IV : activation/désactivation du programme. glUseProgramObjectARB(Program); ... glUseProgramObjectARB(0); Program C/C++Program main LightPosition VertexProgram main FragmentProgram main CPU GPU Vertex Processor Fragment Processor IUT de Reims [ olivier.nocent@univ-reims.fr]

  48. « Built-in » variables Vertex Program glVertex3f gl_Vertex gl_Position glNormal3f gl_Normal gl_FrontColor glMultiTexCoord(0,…) gl_MultiTexCoord0 gl_MultiTexCoord0 glMultiTexCoord(1,…) gl_MultiTexCoord1 gl_MultiTexCoord1 Fragment Program gl_Color gl_FragColor gl_TexCoord[0] gl_FragDepth gl_TexCoord[1] IUT de Reims [ olivier.nocent@univ-reims.fr]

  49. GLOOSE: Object Oriented Shading Environment for OpenGL Un phare dans la brume des Shading Languages (Cg, GLSL) ShaderObject createFromVertexSourceFile(const char*) createFromFragmentSourceFile(const char*) createFromSourceFiles(const char*, const char*) enable() disable() setParameter1f(const char*, float) setParameter2f(const char*, float, float) setParameter3f(const char* ParameterName, float, float, float) ... CGGLShaderObject ... GLSLShaderObject ... IUT de Reims [ olivier.nocent@univ-reims.fr]

  50. GLOOSEen action !! #define CGGL //#define GLSL #include <cggl_shader_object.h> #include <glsl_shader_object.h> ShaderObject* Shader; void display() { ... Shader->enable(); Shader->setParameter1f("twisting", Twisting); ... Shader->disable(); ... } int main() { ... #ifdef CGGL Shader = new CGGLShaderObject(arb1); Shader->createFromVertexSourceFile("shaders/Cg/twisting.cg"); #endif #ifdef GLSL Shader = new GLSLShaderObject(); Shader->createFromVertexSourceFile("shaders/GLSL/twisting.vert"); #endif ... } IUT de Reims [ olivier.nocent@univ-reims.fr]

More Related