Shaders

Shaders can be thought of as a sequence of programming codes that tells a computer how an image should be drawn. Different shaders govern different properties of an image. For example, Vertex Shaders give properties such as position, color, and UV coordinates for individual vertices. Another important purpose of vertex shaders is to transform vertices with 3D coordinates into the 2D screen space for display. Pixel shaders processes pixels to provide color, z-depth, and alpha value information. Geometry shader is responsible for processing data at the level of a primitive (triangle, line, and vertex).

Data information from an image is passed from one shader to the next for processing before they are finally output through a frame buffer.

Shaders are also used to incorporate post-processing effects such as Volumetric Lighting, HDR, and Bloom effects to accentuate images in a game.

The language which shaders are programmed in depends on the target environment. For Direct3D, the official language is HLSL. For OpenGL, the official shading language is OpenGL Shading Language (GLSL).

Since most shaders are coded for a GPU, major GPU makers Nvidia and AMD have also tried developing their own languages that can output for both OpenGL and Direct3D shaders. Nvidia developed Cg (deprecated now after version 3.1 in 2012) and AMD developed Mantle (used in some games, such as Battlefield 4, that were released in 2014 and seems to be gaining popularity among developers). Apple has also recently released its own shading language known as Metal Shading Language for iOS 8 in September 2014 to increase the performance benefits for iOS. Kronos has also announced a next generation graphics API based on OpenGL known as Vulkan in early 2015, which appears to be strongly supported by member companies such as Valve Corporation.

The following image is taken from a Direct3D 11 graphics pipeline on MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ff476882(v=vs.85).aspx). It shows the programmable stages, which data can flow through to generate real-time graphics for our game, known as the rendering pipeline state representation.

Shaders

The information here is taken from Microsoft MSDN page. You can use the Direct3D 11API to configure all of the stages. Stages such as vertex, hull, domain, geometry, and pixel-shader (those with the rounded rectangular blocks), are programmable using HLSL. The ability to configure this pipeline programmatically makes it flexible for the game graphics rendering.

What each stage does is explained as follows:

Stage

Function

Input-assembler

This stage supplies data (in the form of triangles, lines, and points) to the pipeline.

Vertex-shader

This stage processes vertices such as undergoing transformations, skinning, and lighting. The number of vertices does not change after undergoing this stage.

Geometry-shader

This stage processes entire geometry primitives such as triangles, lines, and a single vertex for a point.

Stream-output

This stage serves to stream primitive data from the pipeline to memory while on its way to the rasterizer.

Rasterizer

This clips primitives and prepare the primitives for the pixel-shader.

Pixel-shader

Pixel manipulation is done here. Each pixel in the primitive is processed here, for example, pixel color.

Output-merger

This stage combines the various output data (pixel-shader values, depth, and stencil information) with the contents of the render target and depth/stencil buffers to generate the final pipeline result.

Hull-shader, tessellator, and domain-shader

These tessellation stages convert higher-order surfaces to triangles to prepare for rendering.

To help you better visualize what happens in each of the stages, the following image shows a very good illustration of a simplified rendering pipeline for vertices only. The image is taken from an old Cg tutorial. Note that different APIs have different pipelines but rely on similar basic concepts in rendering (source: http://goanna.cs.rmit.edu.au/~gl/teaching/rtr&3dgp/notes/pipeline.html).

Shaders

Example flow of how graphics is displayed:

  • The CPU sends instructions (compiled shading language programs) and geometry data to the graphics processing unit, located on the graphics card.
  • The data is passed through into the vertex shader where vertices are transformed.
  • If the geometry shader is active in the GPU, the geometry changes are performed in the scene.
  • If a tessellation shader is active in the GPU, the geometries in the scene can be subdivided. The calculated geometry is triangulated (subdivided into triangles).
  • Triangles are broken down into fragments. Fragment quads are modified according to the fragment shader.
  • To create the feel of depth, the z buffer value is set for the fragments and then sent to the frame buffer for displaying.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset