Shaders
The Origins of Shaders
While some parts of 3D applications are more efficiently run on classic processors (CPUs), the rendering part can be run by a dedicated processor with much better results: the GPU or Graphic Processing Unit. Historically, the technology appeared in the late 1980′s. It was introduced by Pixar through Renderman – a standard way to describe a 3D scene with Shaders. The hardware part (the GPU itself) is the real-time 3D industry’s effort to leverage on this technology. It was meant to relieve the CPU from computing most common 3D tasks (flat shading, gouraud shading, perspective, texture mapping and some blending operations). Many of those chipsets made the success of SGI workstations, whose technology quickly trickled down to the game industry via 3D card manufacturers like 3dfx, NVidia and ATI.
Until recently, the main drawback of these inexpensive 3D cards was that rendering algorithms were limited to the hard-coded functions (called Fixed-Functions – used in the Fixed-Pipeline) implemented by card manufacturers. This constraint was one of the reasons many 3D games looked and felt all alike.
The increase in technological power has allowed 3D card manufacturers to build a Programmable-Pipeline within their chipset, bringing back flexibility in real-time rendering algorithms. This effectively means that evolved rendering techniques that were once exclusive to precomputed images are now possible in real-time.
What is a Shader?
A Shader is a piece of code that is executed directly by the GPU. it describes the ways a surface is going to be rendered.
A Shader can contain three very distinct parts:
- States – This portion defines the states of Fixed-Pipeline variables like the material’s colors, material’s texture or blending options, and also allows some predefined 3D transformations, as well as 2D texture coordinate transformations (e.g. environment mapping).
- States are the basic way to describe a material. States are not executed for each vertex, nor are they for each pixel, but set only once per material/object,
- Programmers need to understand States in order to fully master Shaders.
- Vertex Shader Instructions – This portion of code defines the operations
to execute on each vertices of the faces being rendered. This part is optional,
and if omitted, the GPU will only use the States to define vertices’ default attributes
(like vertex color, vertex normal or texture coordinate), - Pixel Shader Instructions – This portion of code defines the operations
to execute on each pixels of the faces being rendered. This part is also
optional, and if omitted, the GPU will only use the Render States to define
pixels’ default attributes (like pixel color, pixel blending, or depth value), - Geometry Shader Instructions – On recent hardware, Geometry Shaders are available. Only DX10-compliant hardware can run this type of shaders. This portion of code defines the operations to execute per primitive. It allows the addition of vertices on-the-fly; this is useful to generate geometry procedurally.
Note: No DX10 rasterizer has been implemented in 3DVIA Studio yet
Shader Languages
Shaders were first written using a specific assembly language. It was widely used first on the XBox (through DirectX 8). This approach proved to be a very
unintuitive way of dealing with rendering algorithms.
Together, nVidia and Microsoft sought to remedy this situation by creating
a higher level language that could be compiled into GPU assembly code. The language has a C-like syntax. nVidia realeased his version of the language first, and it is called Cg (C for Graphics) . Microsoft waited until DirectX
9‘s release to integrate a very similar language (called HLSL for High Level Shader Language) in their Effect Framework – already used in DirectX 8 to describe shader
effects and their assembly instructions. The combination of the DirectX 9 Effect Framework with this
C-like language (HLSL) is nowadays often abridge HLSL.
OpenGL has its own shader language, called GLSL. While HLSL and Cg look a lot alike, GLSL is very different on a syntax stand-point. Because it doesn’t come with an integrated effect framework comparable to the DX9 FX Framework, it is regarded as harder-to-use alternative by lots of people in the video game industry.
HLSL has been retained as the integrated shader language within 3DVIA Studio’s
DirectX rasterizer.
You can find more specific information about Shaders Inside 3DVIA Studio.