Shader (realtime, logical)
Encyclopedia
A shader is essentially a computer program executed on a special environment. This article specifically covers realtime shaders which are shaders meant to execute on consumer-level GPU
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.
Although shaders were introduced for graphics related tasks which still hold a major part of their applications, shaders can also be used for more generic computation, just as generic programs can be used to compute arbitrary data. As the computational power of GPUs continue to rise faster than conventional CPU
Central processing unit
The central processing unit is the portion of a computer system that carries out the instructions of a computer program, to perform the basic arithmetical, logical, and input/output operations of the system. The CPU plays a role somewhat analogous to the brain in the computer. The term has been in...

s, the interest in shader programming attracts more and more attention. This requires rethinking algorithms or problems to fit the stream processing
Stream processing
Stream processing is a computer programming paradigm, related to SIMD , that allows some applications to more easily exploit a limited form of parallel processing...

 paradigm.

The goal of this article is to provide a look at the most important concepts concerning shaders in most important APIs 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...

. The reader is assumed to be proficient with 3D graphics, a graphics API and fourth generation shading pipelines.

Shaders alone control a large part of the working of a programmable graphics pipeline and thus the final appearance of an object. However, they are not the only entities involved in defining an accurate behaviour. The resources being used, as well as settings of other pipeline stages, still has a great influence upon the final result.

Generic shader

A generic shader replaces a specific stage of the shading pipeline with a user-defined program to be executed on need - thereafter, kernel. Shaders generally run in parallel with limited inter-communication between different executions - thereafter instances - usually limited to simplified first-derivative computation and cache optimizations.
Being simply a sequence of operations, kernels are defined using special programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s tailored to match the needs for explicit parallelization and efficiency. Various shading languages have been designed for this purpose.

Depending on the stage being replaced, a shader fetches specific data while its output is handed to successive stages. Input data is typically read-only and can be categorized in two main types:
  • uniform input does hold constant between different kernel instances of the same draw call. The application can set these uniform values with ease between different draw calls, but there is no way to change a uniform value on a per-instance basis. Uniform values can be loaded by calling specific API functions.
    • samplers are special uniforms meant to be used to access textures. Typically, sampler identifiers themselves specify a texture sampling unit in the pipeline (to be used for texture-lookups operations) which is then bound to a texture. Samplers are usually employed by kernels similarly to objects. The intended usage model presents some differences depending on the shading language being used.
  • varying input is typically the result of a previous computational stage, sometimes bound to some special, context-dependent semantics. For example, vertex positions are typical varying inputs for vertex shaders (named attributes in this context), pixel texture coordinates are typical varying inputs to pixel shaders.


The output is ideologically always varying (although two instances may actually output the same value). Fourth generation shading pipelines allow to control how output interpolation is performed when primitives are rasterized and pixel shader's varying input is generated.

Vertex shader

A vertex shader replaces part of the geometry stage of a graphics pipeline. Vertex shaders consume vertices filled by the Input Assembly stage by applying the specified kernel "for each vertex". The result, which usually include an affine transform, is then fetched by the next state - the Primitive Assembly stage. A vertex shader always produces a single transformed "vertex" and runs on a vertex processor.

Producing vertex position for further rasterization is the typical task of the vertex shaderMore friendly, EXT_geometry_shader4 issue (12) explains that that this is now optional not only because of Geometry Shaders, but Stream-Out as well..

Note the current meaning of "vertex" may or may not match the intuitive idea of a vertex
Vertex (geometry)
In geometry, a vertex is a special kind of point that describes the corners or intersections of geometric shapes.-Of an angle:...

. In general, it is better to think at a "vertex" as the basic input data set. This is especially important for generic processing, in which a vertex may hold attribute which does not map to any "geometrical" meaning.

Although vertex shaders were the first hardware accelerated shader type with a high degree of flexibility (see GeForce3
GeForce3
The GeForce 3 is the third-generation of NVIDIA's GeForce graphics processing units. Introduced in March 2001, it advanced the GeForce architecture by adding programmable pixel and vertex shaders, multi-sampling full-scene anti-aliasing and improved the overall efficiency of the rendering...

, Radeon R200
Radeon R200
The Radeon R200 is the second generation of Radeon graphics chips from ATI Technologies. The architecture features 3D acceleration based upon Microsoft Direct3D 8.1 and OpenGL 1.3, a major improvement in features and performance compared to the preceding Radeon R100 design. The GPU also includes 2D...

), their feature set was considerably different from other stages for a long time. Even if the exposed instruction set can be considered unified, the performance characteristics of vertex processing units can be considerably different from other execution units. Historically, branching has been considerably more efficient and flexible on vertex processors. Similarly, dynamic array indexing was possible only on vertex processors up to fourth generation pipelines.

Geometry shader

Geometry shaders replace a part of the geometry stage subsequent to Primitive Assembly stage and prior to Rasterization. Differently from other shader types, which replaced well-known tasks, the notion of a geometry shader have been only recently introduced to realtime systems so they currently don't map to anything possible before. Additionally, the problem being solved is conceptually very different so a generic geometry shader will be considerably different from a typical shader (both vertex and fragment).

Pixel shader

Pixel shaders determine (or contribute to the determination of) the color of a pixel.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK