Programmation GPU avec Cg et GLSL

Slides:



Advertisements
Présentations similaires
Premier programme en C :
Advertisements

Les fonctions A quoi ça sert ?
Carsten Dachsbacher Christian Vogelgsang Marc Stamminger
A RECUPERER EN ENTRANT Le polycopié de Caml Partie 1
Introduction Langage très répandu Noyau Linux VLC … Des avantages indéniables mais aussi des contraintes ! Ceci nest quun rapide tour.
Plan Rappels La chaîne d’affichage Evolutions Fonctionnement de l’œil
DirectX Guillaume Randon Microsoft Services
Unstructured Lumigraph Rendering
The Reyes Image Rendering Architecture
Technique des Surfels Surfels: Surface Elements as Rendering Primitives SIGGRAPH 2000 H.Pfiste, J.van Baar, M.Zwicker, M.Gross.
Ray Tracing Acceleration Techniques A Survey of Ray Tracing Acceleration Techniques James Arvo et David Kirk Ray Tracing on Programmable Graphics Hardware.
C++ 6ème cours Patrick Reuter maître de conférences
La classe String Attention ce n’est pas un type de base. Il s'agit d'une classe défini dans l’API Java (Dans le package java.lang) String s="aaa"; // s.
Cours n° 8 Conception et Programmation à Objets
IMAGIS-GRAVIR / IMAG Rendu de forêts en temps-réel iMAGIS / GRAVIR Franck Sénégas DEA IVR Tuteur: Fabrice Neyret.
la programmation en langage C
C.
Introduction : Compilation et Traduction
Projet 4info.
1 Visualisation avec OpenGL Jérémie Allard 4 Novembre 2002.
Programmation des logiciels infographiques interactifs 2D et 3D, et le OpenGL.
INF-1019 Programmation en temps réel
Introduction à la programmation (420-PK2-SL) cours 12 Gestion des applications Technologie de linformation (LEA.BW)
Les méthodes en java Une méthode est un regroupement d’instructions ayant pour but de faire un traitement bien précis. Une méthode pour être utilisée.
Bibliothèque standard du C++
RESUMES Module II1 SOMMAIRE CYCLE 1 : Saisir – Afficher – Données
Transformations de visualisation en OpenGL
Gestion des événements liés à la fenêtre et aux périphériques dentrée.
Structures de données IFT-2000
RAPIDE RENDU ARTISTIQUE D UNE IMAGE RÉALISTE P IERRE M ANNEBACK S ÉBASTIEN N OËL S ÉBASTIEN W AUQUIER.
Images synthètiques de haute qualité
Contrôle de types Les types en programmation Expressions de types Un contrôleur de types Equivalence de types Conversions de types Généricité.
IFT3730 : Infographie 3D Systèmes et modèles graphiques Pierre Poulin, Derek Nowrouzezahrai Hiver 2013 DIRO, Université de Montréal.
IFT2740 : Outils de l'infographie 3D Systèmes et modèles graphiques
IFT3730 : Infographie 3D Plan de cours
IFT 6800 Atelier en Technologies d’information
LIFI-Java 2004 Séance du Jeudi 9 sept. Cours 1. La notion de langage Décrire une tâche à effectuer –programme Écrire à un haut niveau –facile pour lutilisateur.
Chapitre 4 : Morphologie Mathématique
COURS DE PROGRAMMATION ORIENTEE OBJET :
L’essentiel du langage C
Animateur : Med HAIJOUBI
Traitement d’images sur carte 3D
1 Modèles de particules Reeves, W. T., "Particle Systems - A technique for Modeling a Class of Fuzzy Objects," SIGGRAPH 83, Reeves, W. T., and.
Les images de synthèses Duprat Anatole. Les images de synthèses consistent en la création assistée par ordinateur, d'images numériques. Il existe différentes.
Un survol du language C.
1 Structures des données. 2  Le tableau permettait de désigner sous un seul nom un ensemble de valeurs de même type, chacune d'entre elles étant repérée.
Journées d'études Faible Tension Faible Consommation 14, 15, 16 mai Gwenolé CORRE, Nathalie JULIEN, Eric SENN, Eric MARTIN LESTER, Université de.
Arbres binaires et tables de hachage
Tutorat en bio-informatique
Introduction aux Cartes Graphiques
Les classes et les objets Les données finales class A { … private final int n = 20 ; // la valeur de n est définie dans sa déclaration … } class A { public.
Microcontrôleurs PIC. 1ère séance Présentation du PIC16F876 Outils de programmation du PIC Le langage C Exemples d’applications simples 2ème séance Présentation.
OpenGL Shading Language Introduction. But de ce cours  Non-buts  Spécialistes GLSL  Experts en GLSL  Exploration du pipeline graphique  Séparation.
Créez des applications Silverlight 3 David Rousset Relations Techniques avec les développeurs Microsoft France
Credits : Joëlle Thollot, Hector Briceño, Edmond Boyer
Classe 1 CSI2572 Autres modificateurs de déclaration de variables: & volatile & register & static & auto & extern & const volatile Indique au compilateur.
1 Programmation en C++ Marianne Morris. 2 Intro générale à la programmation On a déjà étudié le langage assembleur Langage de bas niveau Meilleur que.
Cours 4 (14 octobre) Héritage. Chapitre III Héritage.
Architecture des ordinateurs, Environnement Numérique de Travail
Conception de Programmes - IUT de Paris - 1ère année Quelques éléments du langage C++ Les références La surcharge de fonctions Les fonctions «
INF8505: processeurs embarqués configurables Département de génie informatique et génie logiciel ASIP Design and Synthesis for Non Linear Filtering in.
Architecture et Fonctionnement d’un GPU Géométrie Rendu.
Introduction à SVG Scalable Vector Graphic. Informations ● Plus d'informations ici (draft, tutoriaux, outils... ): –
PRO-1027 Programmation Scientifique en C
IFT3355: Infographie Plan © Victor Ostromoukhov Dép. I.R.O. Université de Montréal.
Éclairage, ombrage Fiat lux !.
Introduction au langage C
Informatique 2A Langage C 2 è séance. Objectifs de la séance 2 Début de la modularité avec les fonctions Création d’une première bibliothèque.
Synthèse d’images Antoine Bouthors
Les Processeurs Graphiques Programmables
Transcription de la présentation:

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 ? IUT de Reims [ olivier.nocent@univ-reims.fr ]

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 ]

1984 : «Shade Trees» par Rob Cook (Lucasfilm Ltd.) Final Color Copper Color Weight of Ambient Components Ambient Weight of Specular Components Specular Normal Viewer Surface Roughness 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, …) IUT de Reims [ olivier.nocent@univ-reims.fr ]

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 ]

Arbre généalogique des « Shading Languages » C (AT&T, 1970) Langage généraliste Shading Language API graphique Langage GPU 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 ]

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

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

Graphics Pipeline Application OpenGL DirectX Uniform parameters (Matrices, light positions, blend factors, …) Vertex operations Transform Lighting Fragment operations Texture mapping Primitive Assembly Rasterization & Interpolation Frame-Buffer Tests Textures Vertex indices 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. IUT de Reims [ olivier.nocent@univ-reims.fr ]

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

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

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

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

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

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

Quelques chiffres… Génération Année Gamme Transistors Fill rate Polygon rate 1ère Fin 1998 RIVA TNT 7 M 50 M/s 6 M/s Début 1999 RIVA TNT2 9 M 75 M/s 9 M/s 2ème Fin 1999 GeForce 256 23 M 120 M/s 15 M/s Début 2000 GeForce 2 25 M 200 M/s 25 M/s 3ème Début 2001 GeForce 3 57 M 800 M/s 30 M/s Début 2002 GeForce 4 Ti 63 M 1200 M/s 60 M/s 4ème Début 2003 GeForce FX 125 M 2000 M/s NB: le microprocesseur Pentium 4 d’Intel contient 55 M de transistors. IUT de Reims [ olivier.nocent@univ-reims.fr ]

Encore quelques chiffres… Comparaison des performances CPU et GPU réalisée par Mark Harris [1]. Processeur GFLOPS* Débit Pentium 4 3 GHz 6 (théorique) 5.96 Go/s GeForce FX 5900 20 (observée) 25.6 Go/s GeForce 6800 Ultra 40 (observée) 35.2 Go/s * 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 ]

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

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

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

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

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

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

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

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

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 ]

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 ]

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 ]

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 ]

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 ]

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 ]

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

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

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

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

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

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

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

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

« Semantics » Vertex Program Fragment Program POSITION POSITION NORMAL glVertex3f(…) Vertex Program 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 ]

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

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

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

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

Acte II : compilation et chargement du « shader ». Program C/C++ Program main Vertex Program main Fragment Program main CPU GPU Vertex Processor Fragment Processor 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); IUT de Reims [ olivier.nocent@univ-reims.fr ]

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

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

« Built-in » variables Vertex Program Fragment Program gl_Vertex glVertex3f Vertex Program 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 ]

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 ]

GLOOSE en action !! IUT de Reims [ olivier.nocent@univ-reims.fr ] #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"); IUT de Reims [ olivier.nocent@univ-reims.fr ]

« Un code source vaut dix mille mots » (vieux proverbe informatique) Epilogue « Un code source vaut dix mille mots » (vieux proverbe informatique) IUT de Reims [ olivier.nocent@univ-reims.fr ]

P ’ = RotationZ( twist x ||P|| ) x P Twisting P ’ = RotationZ( twist x ||P|| ) x P twist = 0 twist = 3 IUT de Reims [ olivier.nocent@univ-reims.fr ]

twisting.cg [Cg] IUT de Reims [ olivier.nocent@univ-reims.fr ] struct myvertin { float4 Position : POSITION; float4 Color : COLOR; }; struct myvertout { myvertout main(myvertin IN, uniform float twisting, uniform float4x4 ModelViewProjectionMatrix) { myvertout OUT; float angle = twisting*length(IN.Position); float cosLength, sinLength; sincos(angle, sinLength, cosLength); OUT.Position.x = cosLength*IN.Position.x - sinLength*IN.Position.y; OUT.Position.y = sinLength*IN.Position.x + cosLength*IN.Position.y; OUT.Position.z = 0.0f; OUT.Position.w = 1.0f; OUT.Position = mul(ModelViewProjectionMatrix, OUT.Position); OUT.Color = IN.Color; return OUT; } IUT de Reims [ olivier.nocent@univ-reims.fr ]

twisting.vert [GLSL] uniform float twisting; void main(void) { float angle = twisting*length(gl_Vertex); float cosLength = cos(angle); float sinLength = sin(angle); gl_Position.x = cosLength*gl_Vertex.x - sinLength*gl_Vertex.y; gl_Position.y = sinLength*gl_Vertex.x + cosLength*gl_Vertex.y; gl_Position.z = 0.0; gl_Position.w = 1.0; gl_Position = gl_ModelViewProjectionMatrix * gl_Position; gl_FrontColor = gl_Color; } IUT de Reims [ olivier.nocent@univ-reims.fr ]

Simple Phong shading (OpenGL ou DirectX) V N Color = AmbientColor + DiffuseColor x ( N . L ) + SpecularColor x ( N . H )n H = ( V + L )/2 (half vector) IUT de Reims [ olivier.nocent@univ-reims.fr ]

True Phong shading L R V N Color = AmbientColor + DiffuseColor x ( N . L ) + SpecularColor x ( R . V )n R : Reflected light vector IUT de Reims [ olivier.nocent@univ-reims.fr ]

vp_per_vertex_true_phong.cg [Cg] struct myvertin { float4 Position : POSITION; float4 Normal : NORMAL; }; struct myvertout { float4 Position : POSITION; float4 Color : COLOR; myvertout main(myvertin IN, uniform float4x4 ProjectionMatrix, uniform float4x4 ModelViewMatrix, uniform float4x4 ModelViewMatrixIT, uniform float4 LightPosition, uniform float4 DiffuseMaterial, uniform float4 SpecularMaterial) { myvertout OUT; OUT.Position = mul(ModelViewMatrix, IN.Position); // Vertex position in View space. float3 N = normalize(mul(ModelViewMatrixIT, IN.Normal).xyz); // Vertex normal in View space. float3 L = normalize((LightPosition - OUT.Position).xyz); // Light vector in View space. OUT.Color = DiffuseMaterial * 0.4; // Ambient component. float diffuse = dot(N, L); if (diffuse>0) { OUT.Color += DiffuseMaterial * diffuse; // Diffuse component. float3 R = reflect(-L, N); // Reflected light vector in View space. float3 V = normalize(-OUT.Position.xyz); // Reflected light vector in View space. float specular = dot(V, R); if (specular>0) OUT.Color += SpecularMaterial * pow(specular, 40); // Specular component. } OUT.Position = mul(ProjectionMatrix, OUT.Position); // Vertex position in Screen space. return OUT; IUT de Reims [ olivier.nocent@univ-reims.fr ]

per_vertex_true_phong.vert [GLSL] uniform vec4 LightPosition; uniform vec4 DiffuseMaterial; uniform vec4 SpecularMaterial; void main() { gl_Position = gl_ModelViewMatrix * gl_Vertex; // Vertex position in View space. vec3 N = normalize(gl_NormalMatrix * gl_Normal); // Vertex normal in View space. vec3 L = normalize((LightPosition - gl_Position).xyz); // Light vector in View space. gl_FrontColor = DiffuseMaterial * 0.4; // Ambient component. float diffuse = dot(N, L); if (diffuse>0) { gl_FrontColor += DiffuseMaterial * diffuse; // Diffuse component. vec3 R = reflect(-L, N); // Reflected light vector in View space. vec3 V = normalize(-gl_Position .xyz); // View vector in View space. float specular = dot(V, R); if (specular>0) gl_FrontColor += SpecularMaterial * pow(specular, 40); // Specular component. } gl_Position = gl_ProjectionMatrix * gl_Position; // Vertex position in Screen space. IUT de Reims [ olivier.nocent@univ-reims.fr ]

Per vertex true phong shading (1) IUT de Reims [ olivier.nocent@univ-reims.fr ]

Per vertex true phong shading (2) IUT de Reims [ olivier.nocent@univ-reims.fr ]

Comparaison des deux modèles. IUT de Reims [ olivier.nocent@univ-reims.fr ]

vp_per_pixel_true_phong.cg [Cg] struct myvertin { float4 Position : POSITION; float4 Normal : NORMAL; }; struct myvertout { float4 ProjPosition : POSITION; float4 Position : TEXCOORD0; float4 Normal : TEXCOORD1; myvertout main(myvertin IN, uniform float4x4 ModelViewProjectionMatrix, uniform float4x4 ModelViewMatrix, uniform float4x4 ModelViewMatrixIT) { myvertout OUT; // Vertex position in Screen space. OUT.ProjPosition = mul(ModelViewProjectionMatrix, IN.Position); // Vertex position in View space. OUT.Position = mul(ModelViewMatrix, IN.Position); // Vertex normal in View space. OUT.Normal = mul(ModelViewMatrixIT, IN.Normal); return OUT; } IUT de Reims [ olivier.nocent@univ-reims.fr ]

fp_per_pixel_true_phong.cg [Cg] struct myfragin { float4 Position : TEXCOORD0; float4 Normal : TEXCOORD1; }; struct myfragout { float4 Color : COLOR; myfragout main(myfragin IN, uniform float4 LightPosition, uniform float4 DiffuseMaterial, uniform float4 SpecularMaterial) { myfragout OUT; float3 N = normalize(IN.Normal.xyz); float3 L = normalize((LightPosition - IN.Position).xyz); OUT.Color = DiffuseMaterial * 0.4; float diffuse = dot(N, L); if (diffuse>0) { OUT.Color += DiffuseMaterial * diffuse; float3 R = reflect(-L, N); float3 V = normalize(-IN.Position.xyz); float specular = dot(V, R); If (specular>0) OUT.Color += SpecularMaterial * pow(specular, 40); } return OUT; IUT de Reims [ olivier.nocent@univ-reims.fr ]

per_pixel_true_phong.vert [GLSL] void main() { // Vertex position in Screen space. gl_Position = ftransform(); // Vertex position in View space. gl_TexCoord[0] = gl_ModelViewMatrix * gl_Vertex; // Vertex normal in View space. gl_TexCoord[1] = vec4(gl_NormalMatrix * gl_Normal, 0); } IUT de Reims [ olivier.nocent@univ-reims.fr ]

per_pixel_true_phong.frag [GLSL] uniform vec4 LightPosition; // Light position in View space. uniform vec4 DiffuseMaterial; // Material. uniform vec4 SpecularMaterial; void main() { // Vertex normal in View space. vec3 N = normalize(gl_NormalMatrix * gl_TexCoord[1].xyz); // Light vector in View space. vec3 L = normalize((LightPosition - gl_ModelViewMatrix * gl_TexCoord[0]).xyz); // Ambient component. gl_FragColor = DiffuseMaterial * 0.4; float diffuse = dot(N, L); if (diffuse>0) { // Diffuse component. gl_FragColor += DiffuseMaterial * diffuse; // Reflected light vector in View space. vec3 R = reflect(-L, N); // View vector in View space. vec3 V = normalize(-gl_TexCoord[0].xyz); // Specular component. float specular = dot(V, R); if (specular>0) gl_FragColor += SpecularMaterial * specular; } IUT de Reims [ olivier.nocent@univ-reims.fr ]

Toon Shading IUT de Reims [ olivier.nocent@univ-reims.fr ]

Toon Shading C C N.L N.L IUT de Reims [ olivier.nocent@univ-reims.fr ]

vp_toon_shading.cg [Cg] struct myvertin { float4 Position : POSITION; float4 Normal : NORMAL; }; struct myvertout { float4 Position : POSITION; float DiffuseComponent : TEXCOORD0; float SpecularComponent : TEXCOORD1; float Edge : TEXCOORD2; myvertout main(myvertin IN, uniform float4 LightPosition, uniform float4x4 ModelViewMatrix, uniform float4x4 ModelViewMatrixIT, uniform float4x4 ProjectionMatrix) { myvertout OUT; OUT.Position = mul(ModelViewMatrix, IN.Position); // Output vertex position in View space. float3 N = normalize(mul(ModelViewMatrixIT, IN.Normal).xyz); // Vertex normal in View space. float3 L = normalize(LightPosition.xyz - OUT.Position.xyz); // Light vector in View space. float3 V = normalize(-OUT.Position.xyz); // View vector in View space. OUT.Edge = max(dot(N, V), 0); // Perform edge detection OUT.DiffuseComponent = dot(N, L); // Compute diffuse component. if (OUT.DiffuseComponent > 0) { float3 H = normalize(L + V); // Half vector in View space. OUT.SpecularComponent = pow(max(dot(N, H), 0), 40); } else { OUT.DiffuseComponent = 0; OUT.SpecularComponent = 0; } OUT.Position = mul(ProjectionMatrix, OUT.Position); // Output vertex position in Screen space. return OUT; IUT de Reims [ olivier.nocent@univ-reims.fr ]

fp_toon_shading.cg [Cg] struct myfragin { float DiffuseComponent : TEXCOORD0; float SpecularComponent : TEXCOORD1; float Edge : TEXCOORD2; }; struct myfragout { float4 Color : COLOR; myfragout main(myfragin IN) { myfragout OUT; if (IN.Edge < 0.2) OUT.Color = float4(0, 0, 0, 1); else { if (IN.DiffuseComponent < 0.2) IN.DiffuseComponent = 0.3; else IN.DiffuseComponent = 1.0; if (IN.SpecularComponent < 0.5) IN.SpecularComponent = 0.0; IN.SpecularComponent = 1.0; OUT.Color = float4(1, 0, 0, 1)*IN.DiffuseComponent + float4(1, 1, 1, 1)*IN.SpecularComponent; } return OUT; IUT de Reims [ olivier.nocent@univ-reims.fr ]

toon_shading.vert [GLSL] uniform vec4 LightPosition; void main() { gl_Position = gl_ModelViewMatrix * gl_Vertex; // Vertex position in View space. vec3 N = normalize(gl_NormalMatrix * gl_Normal); // Vertex normal in View space. vec3 L = normalize((LightPosition - gl_Position).xyz); // Light vector in View space. vec3 V = normalize(-gl_Position.xyz); // View vector in View space. gl_TexCoord[0].x = max(dot(N, V), 0.0); // Perform edge detection. gl_TexCoord[0].y = dot(N, L); // Diffuse component. if (gl_TexCoord[0].y>0) { vec3 H = normalize(L + V); // Half vector in View space. gl_TexCoord[0].z = pow(max(dot(N, H), 0.0), 40); } else { gl_TexCoord[0].y = 0.0; gl_TexCoord[0].z = 0.0; gl_Position = gl_ProjectionMatrix * gl_Position; // Vertex position in Screen space. IUT de Reims [ olivier.nocent@univ-reims.fr ]

toon_shading.frag [GLSL] void main() { float diffuse, specular; if (gl_TexCoord[0].x< 0.1) gl_FragColor = vec4(0, 0, 0, 1); else { // Compute final color with fixed diffuse and specular materials. if (gl_TexCoord[0].y< 0.2) diffuse = 0.3; else diffuse = 1.0; if (gl_TexCoord[0].z<0.5) specular = 0.0; specular = 1.0; gl_FragColor = vec4(1, 0, 0, 1)*diffuse + vec4(1, 1, 1, 1)*specular; } IUT de Reims [ olivier.nocent@univ-reims.fr ]

Rendu Raytracing High Dynamic Range Rendering IUT de Reims [ olivier.nocent@univ-reims.fr ]

Simulation physique Simulation de fluides Animation de vêtements IUT de Reims [ olivier.nocent@univ-reims.fr ]

Traitement Numérique des Images Calcul d’histogrammes Détection de contours IUT de Reims [ olivier.nocent@univ-reims.fr ]

GLIP: Image Processing with OpenGL Objectif de GLIP Utilisation du processeur graphique pour le traitement numérique des images. Nécessité d’adapter les algorithmes classiques du traitement des images aux contraintes de la programmation GPU. Un processeur graphique manipule des données géométriques uniquement (triangles, lignes, points). Gestion des images ? Un processeur graphique génère des résultats sous la forme d’une image 2D (rendu de la géométrie 3D selon un point de vue donné). Calcul d’histogramme, calcul de moments ? IUT de Reims [ olivier.nocent@univ-reims.fr ]

Principe général du TI sur GPU (1) (0,0) (w,h) Texture (Image en entrée) Forme géométrique (Quad) (0,0) (w,h) GPU // // Threshold operator ... Color = texRECT(image, Position); float l = min(min(Color.r,Color.g), Color.b); if (l>=threshold) OUT.Color = float4(1,1,1,1); else OUT.Color = float4(0,0,0,1); Résultat du rendu (Image en sortie) Fragment shader (Traitement) [2] «GPU Image Processing» par Frank Jargstorff (NVIDIA Corporation) présenté à SIGGRAPH 2004. IUT de Reims [ olivier.nocent@univ-reims.fr ]

Principe général du TI sur GPU (2) Passe 1 Flou Passe 2 Seuillage [2] «GPU Image Processing» par Frank Jargstorff (NVIDIA Corporation) présenté à SIGGRAPH 2004. IUT de Reims [ olivier.nocent@univ-reims.fr ]

Principe général du TI sur GPU (3) Etapes clés Dessin de rectangles Utilisation du texture mapping (images de taille quelconque via l’extension GL_ARB_texture_rectangle) Compilation, chargement et exécution de shaders Conversion du résultat du rendu en texture Le but de la bibliothèque GLIP est de libérer le programmeur de ces étapes ‘bas niveau’ en les encapsulant dans des classes C++. IUT de Reims [ olivier.nocent@univ-reims.fr ]

Architecture logicielle de GLIP Image + width() + height() + download() + upload() + getHue() + setHue() ColorizeFilter + process() UnaryFilter ThresholdFilter + getThreshold() + setThreshold() MeanFilter + getHalfSize() + setHalfSize() SeparableUnaryFilter + process() SeparableMeanFilter + getHalfSize() + setHalfSize() + process() BinaryFilter AddFilter SubFilter IUT de Reims [ olivier.nocent@univ-reims.fr ]

Analogie avec le modèle flot/noyau (stream/kernel) Modèle flot/noyau : un même traitement (noyau) appliqué à chaque élément du flot d’entrée (image source) pour générer un flot de sortie de même taille (image destination). Approche adaptée aux traitements locaux : Un pixel : seuillage, changement d’espace couleur (RGB, HSV, …) Un voisinage de pixel : Convolution (flou gaussien, gradient, …) Morphologie mathématique (érosion, dilatation, ouverture, fermeture, …) Limitation d’une approche basée flot/noyau : Calcul d’histogramme, de moments Filtres récursifs IUT de Reims [ olivier.nocent@univ-reims.fr ]

Gestion de la mémoire vidéo (1) Comment convertir l’image de sortie du calcul précédent en image d’entrée pour le calcul suivant ? Passe 1 ? Flou Passe 2 Seuillage IUT de Reims [ olivier.nocent@univ-reims.fr ]

Gestion de la mémoire vidéo (2) Il existe actuellement 3 stratégies différentes [4] : Copie du Frame Buffer vers une texture à l’aide de la fonction glCopyTexSubImage2D. Solution multi plateforme mais lente. Utilisation des Pixel Buffers avec les extensions WGL_ARB_pbuffer et WGL_ARB_render_texture. Solution plus rapide mais plus coûteuse en mémoire (contextes de rendu multiples) et disponible uniquement sous Windows. Utilisation des Frame Buffer Object avec l’extension GL_EXT_framebuffer_object. Solution optimale mais encore expérimentale : disponible uniquement sur les pilotes bêta pour Windows de NVIDIA (76.50) [4] «The OpenGL Frame Buffer Object Extension» par Simon Green (NVIDIA Corporation) présenté à Game Developer Conference 2005 IUT de Reims [ olivier.nocent@univ-reims.fr ]

Principe des Frame Buffer Object Pouvoir créer un nouveau Frame Buffer (Color Buffers, Depth Buffer et Stencil Buffer) en y attachant des images : Des textures pour chaque Color Buffer. Des Render Buffer pour les Depth Buffer et Stencil Buffer. Texture 0 Frame Buffer Object Texture 1 Color Attachment 0 Color Attachment 1 Render Buffer 0 … Render Buffer 1 Color Attachment n Stencil Attachment Depth Attachment IUT de Reims [ olivier.nocent@univ-reims.fr ]

Comparatif « Copie vers une texture » vs. FBO Dimensions glCopyTexSubImage2D FPS* Frame Buffer Object FPS Ratio 256x256 (1 Mo) 14 1720 122.8 800x600 (7,5 Mo) 2 260 130.8 * FPS : Frames per second Configuration Processeur AMD Athlon 64 bits 1 Go de RAM GeForce 6600 GT sur port PCI Express 16x IUT de Reims [ olivier.nocent@univ-reims.fr ]

Utilisation de filtres séparables Exemple de l’érosion : calcul du minimum des luminances dans un voisinage carré de taille 3. Version non séparable : 8 5 3 8 5 3 3x3 9 6 2 9 1 2 4 8 1 4 8 1 Version séparable : 8 5 3 8 3 3 8 5 3 3 3 9 6 2 9 2 2 9 1 2 4 8 1 4 1 1 4 8 1 IUT de Reims [ olivier.nocent@univ-reims.fr ]

Utilisation de filtres séparables Image couleur 256256 Taille du noyau n Version non sép. FPS [ n 2 ] Version séparable FPS [ 2n ] Ratio 3 305 [9] 422 [6] 1.38 7 70 [49] 225 [14] 3.21 15 16 [225] 117 [30] 7.31 29 4 [841] 63 [58] 15.75 73 Ø [5329] 26 [146] 255 Ø [65025] 8 [510] IUT de Reims [ olivier.nocent@univ-reims.fr ]

Utilisation de filtres séparables Image couleur 800600 Taille du noyau n Version non sép. FPS [ n 2 ] Version séparable FPS [ 2n ] Ratio 3 41 [9] 60 [6] 1.46 7 9 [49] 30 [14] 3.33 15 2 [225] 15 [30] 29 0 [841] 8 [58] 16 73 Ø [5329] 3 [146] 255 Ø [65025] 1 [510] IUT de Reims [ olivier.nocent@univ-reims.fr ]

Calcul d’histogramme sur CPU Principe : calcul de la table de fréquences des luminances (256 niveaux de gris) dans une image. image : adresse de début de l’image size : nombre de pixels de l’image h : histogramme (tableau de 256 cases) unsigned long h[256]; unsigned char* ptr = image; for (int n=size; n--;) H[*(ptr++)]++; IUT de Reims [ olivier.nocent@univ-reims.fr ]

Calcul d’histogramme sur GPU (1) Accès en écriture « aléatoire » for (int n=size; n--;) H[*(ptr++)]++; Dans un fragment shader, l’emplacement d’écriture est imposé par le pipeline. Il faut donc intervenir plus tôt dans le pipeline, au niveau du vertex shader Par conséquent, transformation de l’image en modèle géométrique (tableau de sommets) IUT de Reims [ olivier.nocent@univ-reims.fr ]

Calcul d’histogramme sur GPU (2) Structure mémoire de l’image … 45 127 1 octet Structure mémoire du tableau de sommets … 45 127 4 octets IUT de Reims [ olivier.nocent@univ-reims.fr ]

Calcul d’histogramme sur GPU (3) Modifier le mode de mélange de couleurs (addition de couleurs) Pour chaque sommet du tableau (pixel de l’image) déplacer le sommet sur l’horizontale en fonction de sa coordonnée en x (luminance) et lui attribuer une couleur égale à (1,0,0,0). Grâce au mode de mélange additif, le résultat du rendu correspond à une ligne de largeur 256 dont chaque pixel contient la fréquence correspondante. IUT de Reims [ olivier.nocent@univ-reims.fr ]

Calcul d’histogramme sur GPU (4) Mode de mélange est limité a une précision de 16 bits en virgule flottante dont 9 bits pour la mantisse.  plus grande valeur entière est de 2048. Pour pallier au manque de précision, décomposer la structure géométrique en paquets de 2048 sommets et faire plusieurs histogrammes intermédiaires sur plusieurs lignes.  Nécessité d’une deuxième passe pour faire la somme sur chaque colonne. (fragment shader en précision 32 bits dont 23 bits pour la mantisse. Valeur maximale > 16 000 000). IUT de Reims [ olivier.nocent@univ-reims.fr ]