Programmable Graphics Hardware Alessandro Martinelli alessandro.martinelli@unipv.it 6 November 2011 Rendering Pipeline (6): Programmable Graphics Hardware Rendering Architecture First Rendering Pipeline Second Pipeline: Illumination and Shading Third Pipeline: Coordinates Transforms Fourth Pipeline: Texturing Fifth Pipeline: Memory Structures Programmable Graphics Hardware Computer Graphics
Vertex e Fragment Shader Rendering Pipeline (6): Programmable Graphics Hardware Rendering Architecture First Rendering Pipeline Second Pipeline: Illumination and Shading Third Pipeline: Coordinates Transforms Fourth Pipeline: Texturing Fifth Pipeline: Memory Structures Programmable Graphics Hardware Vertex and Fragment Shaders Shading Languages Examples Geometry Shading, Redirection and Unified Shading A. Martinelli Pipeline 2/12/2009 2 / 19
Vertex e Fragment Shader Rendering Pipeline (OpenGL 1.x Model) With shaders, the Rendering Pipeline... Vertici ymax (x,c,z,txos[])min (x,c,z,txos[])max y Modulo di Digitalizzazione ymin Frammenti( (x,y),(c,z,txos[]) ) Gestione Primitive Clipping, Viewport Transform A. Martinelli Pipeline 2/12/2009 3 / 19
Vertex e Fragment Shader Rendering Pipeline (OpenGL 2.0+ Model)...got changed: Vertex Pipeline and Fragment Pipeline got replaced by progammable graphics hardware components, which are used to customize how vertices and fragments are elaborated. ymax (x,c,z,txos[])min (x,c,z,txos[])max y Modulo di Digitalizzazione ymin Frammenti( (x,y),(c,z,txos[]) ) Gestione Primitive Clipping, Viewport Transform A. Martinelli Pipeline 2/12/2009 4 / 19
Vertex e Fragment Shader New Units Vertex Shader replaces fixed vertex-based functionalities: texture coordinates management, transforms, lighting Fragment Shader replaces fixed fragment-based functionalities: all color mixing methods for multi-texturing, and fog. This units may be implemented by software developers: so it is possible to customize the way in which vertices and fragments are managed. The languages we will use to do so are called Shading Langauges All the GPU supporting this model, will have more units working in parallel and running your vertex or fragment shader on more vertices and fragments at a time. A. Martinelli Pipeline 2/12/2009 5 / 19
Vertex e Fragment Shader Vale la considerazione già fatta per la pipeline standard: il numero dei frammenti è mediamente molto più grande del numero dei vertici che descrivono i triangoli. Questo comporta la necessità di disporre di un numero superiore di unità di fragment shading che di vertex shading, esattamente come presentanto nella slide precedente. A. Martinelli Pipeline 2/12/2009 6 / 19
Vertex e Fragment Shader Some details... Vertex Shader It takes as input all data related to a vertex you send to the pipeline. It gives as output output still vertex infos, the ones used by the primitives manager and clipping modules. It can only change vertex Data. It cannot generate new vertices. It cannot delete vertices. Fragment Shader It takes as input a fragment produces by rasterization hardware. It gives as output a fragment, with the data required by buffers manager. It cannot generate new fragments, but It may delete (discard) fragments. A. Martinelli Pipeline 2/12/2009 6 / 19
Shading Languages Rendering Pipeline (6): Programmable Graphics Hardware Rendering Architecture First Rendering Pipeline Second Pipeline: Illumination and Shading Third Pipeline: Coordinates Transforms Fourth Pipeline: Texturing Fifth Pipeline: Memory Structures Programmable Graphics Hardware Vertex and Fragment Shaders Shading Languages Examples Geometry Shading, Redirection and Unified Shading A. Martinelli Pipeline 2/12/2009 7 / 19
Shading Languages Shading Languages There are some available shading languages: HLSL ( High Level Shading Language ): Microsoft Direct3D Shading Language. GLSL ( Graphics Library Shading Language): OpenGL Shading Language CG: very well known NVidia specific Shading Language. Shading languages are vectorial languages: They define operations on vectors with 1,2 3 or 4 elements. Sums, products and subtractions are always component by component on vectors with same length. You can use control structures like if or for. But: they may bring to performance issues on some hardware. The only safe control is a for cycling a fixed number of times. (Like for(int i=0;i<4;i++)), because you will have a compiler unwrapping your cycle. A. Martinelli Pipeline 2/12/2009 8 / 19
Shading Languages GLSL Nowadays, You will find GLSL in almost every OpenGL based platform. OpenGL Versions: main differences OpenGL 2.0 will still have the Fixed Pipeline. So, in that case, GLSL will allow the access to all the State Variables used by Fixed Pipelines. For example, you can read Fixed-Pipeline Lights Parameters in your shader. With OpenGL ES 2.0/2.1 and WebGL you have not a Fixed Pipeline. So the OpenGL API is missing important functions used to set State Variables and that State Variables are not available in GLSL A. Martinelli Pipeline 2/12/2009 9 / 19
Shading Languages GLSL: Variables Shading Languages variables are very significant: Predefined Language Variables Uniform Variables Varying Variables Of course, these languages will have temporary variables as every other languages. A. Martinelli Pipeline 2/12/2009 10 / 19
Shading Languages Predefined Language Variables Predefined Language Variables are used to access fixed pipeline data. For example: gl Vertex is the input position for a vertex in a Vertex Pipeline(should only be read). gl Position is the output position (unit volume position) for a vertex in a Vertex Pipeline(should only be written). gl Color is the input fragment color in a Fragment Shader (should only be read). gl ModelViewProjectionMatrix is the result of all transforms, a 4x4 which is the product of the Projection with the Modelview (should only be read). A. Martinelli Pipeline 2/12/2009 11 / 19
Shading Languages Varying Variables Generic Vertex Properties assigned by your Vertex Pipeline, which should be interpolated by rasterization Hardware because you want a value for them on each fragment. You can see them both in Fragment and in Vertex Shader. They can only be written on the Vertex Side. They can only be read on the Fragment Side. A Paradox in the Libraries At the time being, varying variables are deprecated in OpenGL 4.0. They are replaced with different solutions. But WebGL and ES versions still will be based on varying. A. Martinelli Pipeline 2/12/2009 12 / 19
Shading Languages Uniform Variables They are like standard State Variables. They are used to assign settings/parameters to Shaders. They indeed should be seen as Customized State Variables. They can only be read both by Vertex and Fragment Shaders. They will be assigned an identifier. With gluniform and the identifier, we will be able to assign values to Uniforms. A. Martinelli Pipeline 2/12/2009 13 / 19
Examples Rendering Pipeline (6): Programmable Graphics Hardware Rendering Architecture First Rendering Pipeline Second Pipeline: Illumination and Shading Third Pipeline: Coordinates Transforms Fourth Pipeline: Texturing Fifth Pipeline: Memory Structures Programmable Graphics Hardware Vertex and Fragment Shaders Shading Languages Examples Geometry Shading, Redirection and Unified Shading A. Martinelli Pipeline 2/12/2009 14 / 19
Examples An Example GLSL Vertex Shader: void main(void){ gl Position = gl ModelViewProjectionMatrix gl Vertex; gl FrontColor = gl Color; gl TexCoord[0] = gl MultiTexCoord0; } GLSL Fragment Shader: void main(void){ gl FragColor = gl Color vec4(1,0.5,0.5,1); } A. Martinelli Pipeline 2/12/2009 15 / 19
Geometry Shading, Redirection and Unified Shading Rendering Pipeline (6): Programmable Graphics Hardware Rendering Architecture First Rendering Pipeline Second Pipeline: Illumination and Shading Third Pipeline: Coordinates Transforms Fourth Pipeline: Texturing Fifth Pipeline: Memory Structures Programmable Graphics Hardware Vertex and Fragment Shaders Shading Languages Examples Geometry Shading, Redirection and Unified Shading A. Martinelli Pipeline 2/12/2009 16 / 19
Geometry Shading, Redirection and Unified Shading GPU: Towards the State of Art (1/3) There are some important additional elements in modern GPUs. Redirection: it is possible to move part of data stream traversing the Pipeline and store it in GPU memory. For example, we can move vertices elaborated by a Vertex Shader and store them in a Vertex Buffer Object. Unified Shading Model: we have a big set of programmable units, instead of having specific units for each Shader Type. Usually it is allowed (with specific libraries like OpenCL) to access that units apart from the rendering process, in order to use them to execute other parallel programming tasks. Geometry Shaders: an additional shader which can change the structure of primitives. It is allowed to generate geometries and vertices, and that s making it having serious performance issues. Primitives Shader: in order to make things more complex, OpenGL 4.0 allows the definition of customized primitives, with the introduction of Primitives Shaders. This should be seen as a workaround to the issues related to Geometry Shaders. A. Martinelli Pipeline 2/12/2009 17 / 19
Geometry Shading, Redirection and Unified Shading GPU: Towards the State of Art (2/3) Gestione Primitive Clipping, Viewport Transform Modulo di Digitalizzazione Buffer Manager A. Martinelli Pipeline 2/12/2009 18 / 19
Geometry Shading, Redirection and Unified Shading Esiste una unica unità di calcolo parallelo con un numero molto grande di unità programmabili al suo interno. I vertici che arrivano alla pipeline vengono passati ad una unità di gestione dei vertex shader; questa unità chiede all unità di calcolo parallelo di avere alcuni processori allocati all esecuzione di un programma di vertex shading. I vertici elaborati tornano all unità di gestione dei vertex shader, che li manda alla solita unità di gestione delle primitive. L unità di gestione delle primitive passa i dati delle primitive ad una unità di Gestione dei Geometry Shader; questa unità è in grado di applicare programmi che data una primitiva sono in grado di produrre un numero grande di primitive, benchè allo stato dell arte l output di questi moduli è bloccato ad un massimo 1Kb. Per svolgere queste operazioni di elaborazione delle primitive, il modulo ha bisogno che l unità di calcolo parallelo gli allochi delle unità programmabili. Le primitive elaborate seguono il solito processo che prevede: clipping, viewport mapping e digitalizzazione. I frammenti vengono processati da una unità di gestione dei fragment shader, che utilizza le unità programmabili generiche per l implementazione. I frammenti elaborati sono passati al solito all unità di gestione dei buffer. Le unità programmabili hanno libero accesso alla gestione delle texture. Le unità programmabili possono salvare uno stream di dati in posizioni di memoria della scheda grafica. Le unità programmabili possono essere utilizzate, attraverso appositi linguaggi, anche da applicazioni esterne che non svolgono attività grafica o che svolgono attività grafica di supporto. A. Martinelli Pipeline 2/12/2009 19 / 19
Geometry Shading, Redirection and Unified Shading GPU: Towards the State of Art (3/3) Some very important considerations: Geometry Shaders are bad. It has been proven that Geometry Shaders can be replaced with a good use of Vertex Shaders Primitive Shaders are better, but they are really new (the first time in OpenGL standard specification was 2010), and available in few platforms. Redirection may bring to bad performance on some platforms. It should be used carefully. Embedded Systems (and WebGL) will be slow in introducing new Shading functionalities, which are less significant. Something which is guaranteed: the Unified Shading Model is part of most of the GPU since 2006: Shading units are managed with a load-balancing mechanics (that is: at run time more or less units will execute Vertex or Fragments Shaders, depending upon the number of vertices and fragments) This will dynamically improve the performance of programs using shading languages. Another reliable fact nowadays is the availability of Instruments and Libraries to access Parallel Hardware in GPUs. A. Martinelli Pipeline 2/12/2009 19 / 19