Shader
Encyclopedia
In the field of computer graphics
, a shader is a computer program
that is used primarily to calculate rendering
effects on graphics hardware with a high degree of flexibility. Shaders are used to program the graphics processing unit
(GPU) programmable rendering pipeline, which has mostly superseded the fixed-function pipeline that allowed only common geometry transformation and pixel-shading functions; with shaders, customized effects can be used.
with their RenderMan
software. RenderMan was introduced in 1989, but it wasn't until the 1995 release of Pixar's movie Toy Story
that the general public was introduced to the power of RenderMan and shaders. Such computer-generated imagery
(CGI) became more and more popular in movies and television.
As graphics processing unit
s evolved, major graphics software libraries such as OpenGL
and Direct3D
began to support shaders. The first shader-capable GPUs only supported pixel shading, but vertex shaders were quickly introduced once developers realized the power of shaders. Geometry shaders were recently introduced with Direct3D 10 and OpenGL 3.2, and they are not widely supported as of 2011 except in high-end video cards.
or a pixel
. Vertex shaders describe the traits (position, texture coordinates
, colors, etc.) of a vertex, while pixel shaders describe the traits (color, z-depth
and alpha
value) of a pixel. A vertex shader is called for each vertex in a primitive
(possibly after tessellation
) – thus; one vertex in, one (updated) vertex out. Each vertex is then rendered as a series of pixels onto a surface (block of memory) that will eventually be sent to the screen.
Shaders replace a section of video hardware typically called the Fixed Function Pipeline (FFP) – so-called because it performs lighting and texture mapping in a hard-coded manner. Shaders provide a programmable alternative to this hard-coded approach.
The graphic pipeline uses these steps in order to transform three dimensional (and/or two dimensional) data into useful two dimensional data for displaying. In general, this is a large pixel matrix or "frame buffer".
shaders, compute color
and other attributes of each pixel
. Pixel shaders range from always outputting the same color, to applying a lighting value, to doing bump mapping
, shadows, specular highlight
s, translucency and other phenomena. They can alter the depth of the pixel (for Z-buffering
), or output more than one color if multiple render targets are active. A pixel shader alone cannot produce very complex effects, because it operates only on a single pixel, without knowledge of a scene's geometry.
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 the Z-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 the rasterizer otherwise.
.
Geometry shader programs are executed after vertex shaders. They take as input a whole primitive, possibly with adjacency information. For example, when operating on triangles, the three vertices are the geometry shader's input. The shader can then emit zero or more primitives, which are rasterized and their fragments ultimately passed to a pixel shader.
Typical uses of a geometry shader include point sprite generation, geometry tessellation
, shadow volume
extrusion, and single pass rendering to a cube map. A typical real world example of the benefits of geometry shaders would be automatic mesh complexity modification. A series of line strips representing control points for a curve are passed to the geometry shader and depending on the complexity required the shader can automatically generate extra lines each of which provides a better approximation of a curve.
, and most modern GPUs have multiple shader pipeline
s to facilitate this, vastly improving computation throughput.
shading language is OpenGL Shading Language, and the official Direct3D shading language is High Level Shader Language. However, Cg is a third-party shading language developed by Nvidia
that outputs both OpenGL and Direct3D shaders.
Computer graphics
Computer graphics are graphics created using computers and, more generally, the representation and manipulation of image data by a computer with help from specialized software and hardware....
, a shader is a computer program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
that is used primarily to calculate rendering
Rendering (computer graphics)
Rendering is the process of generating an image from a model , by means of computer programs. A scene file contains objects in a strictly defined language or data structure; it would contain geometry, viewpoint, texture, lighting, and shading information as a description of the virtual scene...
effects on graphics hardware with a high degree of flexibility. Shaders are used to program the graphics processing unit
Graphics processing unit
A graphics processing unit or GPU is a specialized circuit designed to rapidly manipulate and alter memory in such a way so as to accelerate the building of images in a frame buffer intended for output to a display...
(GPU) programmable rendering pipeline, which has mostly superseded the fixed-function pipeline that allowed only common geometry transformation and pixel-shading functions; with shaders, customized effects can be used.
History
The term "shader" was coined by PixarPixar
Pixar Animation Studios, pronounced , is an American computer animation film studio based in Emeryville, California. The studio has earned 26 Academy Awards, seven Golden Globes, and three Grammy Awards, among many other awards and acknowledgments. Its films have made over $6.3 billion worldwide...
with their RenderMan
PhotoRealistic RenderMan
PhotoRealistic RenderMan, or PRMan for short, is a proprietary photorealistic RenderMan-compliant renderer.It primarily uses the Reyes algorithm but is also fully capable of doing ray tracing and global illumination....
software. RenderMan was introduced in 1989, but it wasn't until the 1995 release of Pixar's movie Toy Story
Toy Story
Toy Story is a 1995 American computer-animated film released by Walt Disney Pictures. It is Pixar's first feature film as well as the first ever feature film to be made entirely with CGI. The film was directed by John Lasseter and featuring the voices of Tom Hanks and Tim Allen...
that the general public was introduced to the power of RenderMan and shaders. Such computer-generated imagery
Computer-generated imagery
Computer-generated imagery is the application of the field of computer graphics or, more specifically, 3D computer graphics to special effects in art, video games, films, television programs, commercials, simulators and simulation generally, and printed media...
(CGI) became more and more popular in movies and television.
As graphics processing unit
Graphics processing unit
A graphics processing unit or GPU is a specialized circuit designed to rapidly manipulate and alter memory in such a way so as to accelerate the building of images in a frame buffer intended for output to a display...
s evolved, major graphics software libraries such as OpenGL
OpenGL
OpenGL is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL...
and Direct3D
Direct3D
Direct3D is part of Microsoft's DirectX application programming interface . Direct3D is available for Microsoft Windows operating systems , and for other platforms through the open source software Wine. It is the base for the graphics API on the Xbox and Xbox 360 console systems...
began to support shaders. The first shader-capable GPUs only supported pixel shading, but vertex shaders were quickly introduced once developers realized the power of shaders. Geometry shaders were recently introduced with Direct3D 10 and OpenGL 3.2, and they are not widely supported as of 2011 except in high-end video cards.
Technology overview
Shaders are simple programs that describe the traits of either a vertexVertex (computer graphics)
A vertex in computer graphics is a data structure that describes a point in 2D or 3D space. Display objects are composed of arrays of flat surfaces and vertices define the location and other attributes of the corners of the surfaces.-Application to object models:In computer graphics, objects are...
or a pixel
Pixel
In digital imaging, a pixel, or pel, is a single point in a raster image, or the smallest addressable screen element in a display device; it is the smallest unit of picture that can be represented or controlled....
. Vertex shaders describe the traits (position, texture coordinates
Texture mapping
Texture mapping is a method for adding detail, surface texture , or color to a computer-generated graphic or 3D model. Its application to 3D graphics was pioneered by Dr Edwin Catmull in his Ph.D. thesis of 1974.-Texture mapping:...
, colors, etc.) of a vertex, while pixel shaders describe the traits (color, z-depth
Z-buffering
In computer graphics, z-buffering is the management of image depth coordinates in three-dimensional graphics, usually done in hardware, sometimes in software. It is one solution to the visibility problem, which is the problem of deciding which elements of a rendered scene are visible, and which...
and alpha
Alpha compositing
In computer graphics, alpha compositing is the process of combining an image with a background to create the appearance of partial or full transparency. It is often useful to render image elements in separate passes, and then combine the resulting multiple 2D images into a single, final image in a...
value) of a pixel. A vertex shader is called for each vertex in a primitive
Geometric primitive
The term geometric primitive in computer graphics and CAD systems is used in various senses, with the common meaning of the simplest geometric objects that the system can handle . Sometimes the subroutines that draw the corresponding objects are called "geometric primitives" as well...
(possibly after tessellation
Tessellation
A tessellation or tiling of the plane is a pattern of plane figures that fills the plane with no overlaps and no gaps. One may also speak of tessellations of parts of the plane or of other surfaces. Generalizations to higher dimensions are also possible. Tessellations frequently appeared in the art...
) – thus; one vertex in, one (updated) vertex out. Each vertex is then rendered as a series of pixels onto a surface (block of memory) that will eventually be sent to the screen.
Shaders replace a section of video hardware typically called the Fixed Function Pipeline (FFP) – so-called because it performs lighting and texture mapping in a hard-coded manner. Shaders provide a programmable alternative to this hard-coded approach.
Simplified graphic processing unit pipeline
- The CPU sends instructions (compiled shading languageShading languageA shading language is a special programming language adapted to map on shader programming. Those kind of languages usually have special data types like color and normal...
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 broken down into pixel quads (one pixel quad is a 2 × 2 pixel primitive).
The graphic pipeline uses these steps in order to transform three dimensional (and/or two dimensional) data into useful two dimensional data for displaying. In general, this is a large pixel matrix or "frame buffer".
Types of shaders
There are three types of shaders in common use. While older graphics cards utilize separate processing units for each shader type, newer cards feature unified shaders which are capable of executing any type of shader. This allows graphics cards to make more efficient use of processing power.Pixel shaders
Pixel shaders, also known as fragmentFragment (computer graphics)
In computer graphics, a fragment is the data necessary to generate a single pixel's worth of a drawing primitive in the frame buffer.This data may include, but is not limited to:* raster position* depth...
shaders, compute color
Color
Color or colour is the visual perceptual property corresponding in humans to the categories called red, green, blue and others. Color derives from the spectrum of light interacting in the eye with the spectral sensitivities of the light receptors...
and other attributes of each pixel
Pixel
In digital imaging, a pixel, or pel, is a single point in a raster image, or the smallest addressable screen element in a display device; it is the smallest unit of picture that can be represented or controlled....
. Pixel shaders range from always outputting the same color, to applying a lighting value, to doing bump mapping
Bump mapping
Bump mapping is a technique in computer graphics for simulating bumps and wrinkles on the surface of an object. This is achieved by perturbing the surface normals of the object and using the perturbed normal during lighting calculations. The result is an apparently bumpy surface rather than a...
, shadows, specular highlight
Specular highlight
A specular highlight is the bright spot of light that appears on shiny objects when illuminated . Specular highlights are important in 3D computer graphics, as they provide a strong visual cue for the shape of an object and its location with respect to light sources in the scene.-Microfacets:The...
s, translucency and other phenomena. They can alter the depth of the pixel (for Z-buffering
Z-buffering
In computer graphics, z-buffering is the management of image depth coordinates in three-dimensional graphics, usually done in hardware, sometimes in software. It is one solution to the visibility problem, which is the problem of deciding which elements of a rendered scene are visible, and which...
), or output more than one color if multiple render targets are active. A pixel shader alone cannot produce very complex effects, because it operates only on a single pixel, without knowledge of a scene's geometry.
Vertex shaders
Vertex shaders are run once for each vertexVertex (computer graphics)
A vertex in computer graphics is a data structure that describes a point in 2D or 3D space. Display objects are composed of arrays of flat surfaces and vertices define the location and other attributes of the corners of the surfaces.-Application to object models:In computer graphics, objects are...
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 the Z-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 the rasterizer otherwise.
Geometry shaders
Geometry shaders are a relatively new type of shader, introduced Direct3D 10 and OpenGL 3.2. This type of shader can generate new graphics primitives, such as points, lines, and triangles, from those primitives that were sent to the beginning of the graphics pipelineGraphics pipeline
In 3D computer graphics, the terms graphics pipeline or rendering pipeline most commonly refers to the current state of the art method of rasterization-based rendering as supported by commodity graphics hardware. The graphics pipeline typically accepts some representation of a three-dimensional...
.
Geometry shader programs are executed after vertex shaders. They take as input a whole primitive, possibly with adjacency information. For example, when operating on triangles, the three vertices are the geometry shader's input. The shader can then emit zero or more primitives, which are rasterized and their fragments ultimately passed to a pixel shader.
Typical uses of a geometry shader include point sprite generation, geometry tessellation
Tessellation
A tessellation or tiling of the plane is a pattern of plane figures that fills the plane with no overlaps and no gaps. One may also speak of tessellations of parts of the plane or of other surfaces. Generalizations to higher dimensions are also possible. Tessellations frequently appeared in the art...
, shadow volume
Shadow volume
Shadow volume is a technique used in 3D computer graphics to add shadows to a rendered scene. They were first proposed by Frank Crow in 1977 as the geometry describing the 3D shape of the region occluded from a light source...
extrusion, and single pass rendering to a cube map. A typical real world example of the benefits of geometry shaders would be automatic mesh complexity modification. A series of line strips representing control points for a curve are passed to the geometry shader and depending on the complexity required the shader can automatically generate extra lines each of which provides a better approximation of a curve.
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 processingParallel computing
Parallel computing is a form of computation in which many calculations are carried out simultaneously, operating on the principle that large problems can often be divided into smaller ones, which are then solved concurrently . There are several different forms of parallel computing: bit-level,...
, and most modern GPUs have multiple shader pipeline
Graphics pipeline
In 3D computer graphics, the terms graphics pipeline or rendering pipeline most commonly refers to the current state of the art method of rasterization-based rendering as supported by commodity graphics hardware. The graphics pipeline typically accepts some representation of a three-dimensional...
s to facilitate this, vastly improving computation throughput.
Programming shaders
The language in which shaders are programmed depends on the target environment. The official OpenGL and OpenGL ESOpenGL ES
OpenGL for Embedded Systems is a subset of the OpenGL 3D graphics application programming interface designed for embedded systems such as mobile phones, PDAs, and video game consoles. OpenGL ES is managed by the not-for-profit technology consortium, the Khronos Group, Inc.- Versions :Several...
shading language is OpenGL Shading Language, and the official Direct3D shading language is High Level Shader Language. However, Cg is a third-party shading language developed by Nvidia
NVIDIA
Nvidia is an American global technology company based in Santa Clara, California. Nvidia is best known for its graphics processors . Nvidia and chief rival AMD Graphics Techonologies have dominated the high performance GPU market, pushing other manufacturers to smaller, niche roles...
that outputs both OpenGL and Direct3D shaders.
See also
- List of common shading algorithms
- GLSLGLSLOpenGL Shading Language , is a high-level shading language based on the syntax of the C programming language...
– Shading language developed for use with OpenGL - HLSL – Shading language developed for use with the Direct3D API
Further reading
- 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 PerlinKen PerlinKen Perlin is a professor in the Department of Computer Science at New York University, founding director of the Media Research Lab at NYU, and the Director of the Games for Learning Institute. His research interests include graphics, animation, multimedia, and science education...
, Steven Worley: Texturing and modeling: a procedural approach, AP Professional, ISBN 0-12-228730-4. Ken PerlinKen PerlinKen Perlin is a professor in the Department of Computer Science at New York University, founding director of the Media Research Lab at NYU, and the Director of the Games for Learning Institute. His research interests include graphics, animation, multimedia, and science education...
is the author of Perlin noisePerlin noisePerlin noise is a computer-generated visual effect developed by Ken Perlin, who won an Academy Award for its use in the motion picture Tron...
, an important procedural texturing primitive. - Randima Fernando, Mark KilgardMark KilgardMark J. Kilgard is a graphics software engineer working at Nvidia.Prior to joining Nvidia, Mark Kilgard worked at Compaq and Silicon Graphics. While at Silicon Graphics, he authored the OpenGL Utility Toolkit, better known as GLUT, to make it easy to write OpenGL-based 3D examples and demos. The...
. The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics, Addison-Wesley Professional, ISBN 0-321-19496-9 - Randi J. RostRandi J. RostRandi J. Rost is a computer graphics professional and frequent contributor to graphics standards. He was an early participant in the personal computer industry, creating a game called King Cribbage for the Apple II computer in 1981 and publishing numerous instructional and review articles in trade...
: OpenGL Shading Language, Addison-Wesley Professional, ISBN 0-321-19789-5 - Riemer's DirectX & HLSL Tutorial: HLSL Tutorial using DirectX with lots of sample code
- HLSL Tutorials: HLSL tutorials on shader structure and how to implement multiple types of lighting.
- GPGPUGPGPUGeneral-purpose computing on graphics processing units is the technique of using a GPU, which typically handles computation only for computer graphics, to perform computation in applications traditionally handled by the CPU...
: general purpose GPU - MSDN: Pipeline Stages (Direct3D 10)