- Shader
A shader in the field of
computer graphics is a set of software instructions, which is used by the graphic resources primarily to perform rendering effects. Shaders are used to allow a 3D application designer to program thegraphics processing unit (GPU) "programmable pipeline", which has mostly superseded the older "fixed-function pipeline", allowing more flexibility in making use of advanced GPU programmability features.Introduction
From a technical view a "shader" is a part of the renderer, which is responsible for calculating the color of an object.
As
Graphics Processing Unit s evolved, major graphics software libraries such asOpenGL andDirect3D began to exhibit enhanced ability to program these new GPUs by defining special shading functions in their API. Changes were introduced to reflect shader capabilities in the platform independent graphics libraryOpenGL version 1.5, and in the proprietaryDirect3D version 8.Types of shaders
The
Direct3D andOpenGL graphic libraries use three types of shaders.*
Vertex shader s are run once for each vertex given to the graphics processor. The purpose is to transform each vertex's 3D position in virtual space to the 2D coordinate at which it appears on the screen (as well as a depth value for theZ-buffer ). Vertex shaders can manipulate properties such as position, color, and texture coordinate, but cannot create new vertices. The output of the vertex shader goes to the next stage in the pipeline, which is either a geometry shader if present or therasterizer otherwise.
*Geometry shader s can add and remove vertices from a mesh. Geometry shaders can be used to generate geometry procedurally or to add volumetric detail to existing meshes that would be too costly to process on the CPU. Direct3D 10 added geometry shader support to the Direct3D API. OpenGL supports geometry shaders through the use of an extension, though it may be incorporated into the standard itself with version 3.1. If geometry shaders are being used, the output is then sent to the rasterizer.
*Pixel shader s, also known as fragment shaders, calculate the color of individual pixels. The input to this stage comes from the rasterizer, which fills in the polygons being sent through the graphics pipeline. Pixel shaders are typically used for scene lighting and related effects such asbump mapping and color toning. (Direct3D uses the term "pixel shader," while OpenGL uses the term "fragment shader." The latter is arguably more correct, as there is not a one-to-one relationship between calls to the pixel shader and pixels on the screen. The most common reason for this is that pixel shaders are often called many times per pixel for every object that is in the corresponding space, even if it is occluded; the Z-buffer sorts this out later.)The
Unified shader model unifies the three aforementioned shaders inOpenGL andDirect3D 10 . See [http://www.nvidia.com/object/geforce_8600_8500_faq.html NVIDIA faqs] .As these shader types are processed within the GPU pipeline, the following gives an example how they are embedded in the pipeline:
Simplified graphic processing unit pipeline
* The CPU sends instructions (compiled
shading language programs) and geometry data to the graphics processing unit, located on the graphics card.
* Within the vertex shader, the geometry is transformed and lighting calculations are performed.
* If a geometry shader is in the graphic processing unit, some changes of the geometries in the scene are performed.
* The calculated geometry is triangulated (subdivided into triangles).
* Triangles are transformed intopixel quad s (one pixel quad is a 2 × 2 pixel primitive).Parallel processing
Shaders are written to apply transformations to a large set of elements at a time, for example, to each pixel in an area of the screen, or for every vertex of a model. This is well suited to parallel processing, and most modern GPUs have a multi-core design to facilitate this, vastly improving efficiency of processing.
Programming shaders
Since version 1.5,
OpenGL has had a C-like Shader-Language available to it, calledOpenGL Shading Language , or GLSL. There are also interfaces for the Cg shader language, developed byNvidia , which is syntactically somewhat similar to GLSL.In Direct3D, shaders are programmed with High Level Shader Language, or HLSL, but the types and complexity of shader programs allowed differ depending on what version of Direct3D is used.
The following table shows the relations between Direct3D versions:
See also
*
List of common shading algorithms External links
* [http://appsrv.cse.cuhk.edu.hk/~ymxie/Geometry_Shader Geometry shader tutorial]
* [http://wiki.directxers.com/index.php?title=Tutorial_5:_Geometry_Shader Direct3D Geometry shader tutorial code]
*nVidia releases a [http://www.nvidia.com/object/IO_37226.html new programming environment] and language compiler specifically for writing science applications that run on the shader units of its graphics cards. Also see [http://developer.nvidia.com/object/cuda.html developer's home page] .
* [http://www.msi.unilim.fr/~porquet/glexts/GL_EXT_geometry_shader4.txt.html OpenGL geometry shader extension]Further reading
* [http://www.lighthouse3d.com/opengl/glsl/ GLSL] : "OpenGL Shading Language @ Lighthouse 3D - GLSL Tutorial"
*Steve Upstill : "The RenderMan Companion: A Programmer's Guide to Realistic Computer Graphics", Addison-Wesley, ISBN 0-201-50868-0
*David S. Ebert ,F. Kenton Musgrave ,Darwyn Peachey ,Ken Perlin ,Steven Worley : "Texturing and modeling: a procedural approach", AP Professional, ISBN 0-12-228730-4.Ken Perlin is the author ofPerlin noise , an important procedural texturing primitive.
*Randima Fernando ,Mark Kilgard . "The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics," Addison-Wesley Professional, ISBN 0-321-19496-9
*Randi J. Rost : "OpenGL Shading Language", Addison-Wesley Professional, ISBN 0-321-19789-5
* [http://www.riemers.net/eng/Tutorials/DirectX/Csharp/series3.php Riemer's DirectX & HLSL Tutorial] : "HLSL Tutorial using DirectX with lots of sample code"
*GPGPU : "general purpose GPU"
*MSDN : [http://msdn2.microsoft.com/en-us/library/bb205123.aspx Pipeline Stages (Direct3D 10)]References
# Search [http://oss.sgi.com/projects/ogl-sample/registry/ARB/shader_objects.txt ARB_shader_objects] for the issue "32) Can you explain how uniform loading works?". This is an example of how a complex data structure must be broken in basic data elements.
# Required machinery has been introduced in OpenGL by [http://oss.sgi.com/projects/ogl-sample/registry/ARB/multitexture.txt ARB_multitexture] but this specification is no more available since its integration in core OpenGL 1.2.
# Search again [http://oss.sgi.com/projects/ogl-sample/registry/ARB/shader_objects.txt ARB_shader_objects] for the issue "25) How are samplers used to access textures?". You may also want to check out "Subsection 2.14.4 Samplers".
Wikimedia Foundation. 2010.