DirectX - First Shader



This chapter gives an overview of Shader which is written in HLSL. Integrating the shader with respect to DirectX is the core concept which is covered in this chapter. Types of shaders include −

  • Vertex Shader
  • Pixel Shader

A Shader is a small code snippet that completely runs on the graphics processor and is therefore considered very fast. DirectX supports shaders and its implementation since version 8.0. The first support started with Vertex and Pixel Shaders written in Assembler language.

In DirectX 9, High Level Shader Language (HLSL) was later added, which helped programming in C-like language. From version 10, it includes features of Geometry Shader and DirectX 11 extended to the implementation of Compute Shader.

After DirectX10, Shader is considered as the primary focus of DirectX as it includes Fixed Rendering Pipeline which was removed from the scratch. In other words, if a user likes to have an appealing result using DirectX, user will automatically use them. It is possible to create beautiful surface effects, animations and other things which can be seen within the mentioned surface area.

This chapter will focus on vertex shader and the next chapter on pixel shader.

Vertex Shader

The Vertex Shader is designed as per one vertex which takes it as an input, processes it, and then returns the value to DirectX Pipeline. The Vertex Shader is considered as the first Shader for the mentioned Pipeline and gets the data in combination with input and assembler. Mainly vertex shader helps in transforming and illuminating the calculations for special effects which are considered possible.

Below is the structure of a simple Vertex Shader −

Vertex Shader

Like in C programming language, the vertex data can be packaged together in a particular structure. DirectX receives the data mapping identifiers called Semantics, so that DirectX knows which data can be processed and which should be assigned to the required variables. The Semantic starting with SV is referred as System Value Semantic, while the one provided with system declaration is always taken through the DirectX pipeline. The input and output data can be considered different on a Vertex Shader in comparison to all shaders.

In the snapshot mentioned above, normal and texture coordinates are passed to the Shader and after processing in the Shader, the vertex position is treated with respect to world coordinates which is returned in addition to the transformed data. It includes its own data types in the DirectX with reference to the DirectX SDK which can be considered mandatory.

The following points should be kept in mind before proceeding with it −

  • Variables must be passed with respect to the shader with resource objects.

  • There are several types of objects which can be included namely: cBuffer, tBuffer, Texture2D and Texture2DArray.

  • The position of each vertex is transformed as the Vertex Shader with the combination of world matrix.

  • The meaning of the normal and the position in world coordinates becomes crystal clear when user tries to implement Pixel Shader.

Advertisements