Crash Course in HLSL

What does HLSL stand for? Why was it created? How does an HLSL effect file look like? What can you do with HLSL? What do float4x4, TEXCOORD0 or compile ps_3_0 mean? The answer to the first question is simple: HLSL means High Level Shader Language. This answer by itself might raise a few questions. The answers for these and a few other questions will be found in this article. You will first learn about the history of HLSL and why it came to existence. After that, you will see the basic structure of an HLSL effect file, and learn about the different elements of the language. Finally, after looking over the language’s basics, you will see the template effect file that XNA gives to us.

History of HLSL

In more than one way, HLSL is for GPU programming as C is for CPU programming. You can use them both well without caring much about what was before them. However, just like with C, knowledge about what was before it will bring better understanding of what makes it special, and similar to computer science students who learn about CPU history, next you will see a brief history of GPUs.

As you probably know already, a GPU (Graphics Processing Unit) is the component inside your PC or console which has a single purpose: process and display computer graphics. The ancestors of modern GPUs are graphics chips present in 80’s PCs that has special circuits dedicated to combining two or more bitmaps into one image. However, the history of interest starts with the release of the first 3D hardware accelerator, in 1996. This was the 3dfx Voodoo card, which was a separate hardware component that had 3D functions implemented into it. The 3D functionality was soon integrated with the classic 2D video cards, to produce a single chip, the grandfather of current GPUs. The most popular video cards in that generation were the Voodoo, TNT, GeForce and Radeon series.

Another step towards current GPUs was the appearance of hardware support for Transform and Lighting, which meant that the video card could handle transformation of geometry in 3D space, clipping and lighting of the polygons, all in hardware. At this moment in time, all 3D capabilities were implemented as fixed-functions (thus called the Fixed Function Pipeline), which meant that you could only use the transformations and lighting models provided by the card. This caused many games of that time to look very similar.

To further enhance the realism and flexibility of computer graphics, the video card manufacturers could take one of two possible paths. One would have been to simply add more and more fixed-function functionality, to cover the many features that programmers desired. This would have needed an exponential growth in the number of circuits on the board, as well as the number of variables and states in the APIs. The second option, which is fortunately what was chosen, was to allow small programs to be written, which were to be executed for each vertex, or for each pixel that is processed by the video cards. This would give a lot of flexibility to developers to process graphics in any way they wanted (in the limits of the hardware, of course).

These small programs are called shaders, and were introduced in DirectX 8.0. A set of specifications, called Shader Model 1 detailed their possible functionality. In this version of Direct3D, programmers had to write shaders in a language similar to assembly and you can see an example of a vertex shader written in assembly language below.

vs_1_1
dcl_position v0
dcl_color v0
m4x4 oPos, v0, c0
mov oD0, v1

Shaders written in assembly were hard to read, understand and maintain. Besides this, Shader Model 1 allowed for very few instructions, and limited functionality. And just like CPU programming was blessed with higher level programming languages to make programming easier, DirectX 9.0 brought Shader Model 2.0, and together with it the High Level Shading Language.

HLSL is the language used to write shaders for GPUs in DirectX. It is syntactically similar to C, but has its own data types and program structure. It makes the graphics programmer’s life easier by allowing the elements of high level programming languages, such as named variables, functions, expressions, statements, standard and user-defined data types, and preprocessor directives. This makes it easier to write, read, understand and debug.

Before we dive into HLSL syntax and structure, you need to look at the graphics-processing pipeline.

  • Dennis Brandis
    #1 written by Dennis Brandis 3 years ago

    On page 4:
    float4 PixelShaderFunction(float2 TexCoords : TEXCOORD0) : COLOR0
    {
    return tex2D(TextureSampler, input.TexCoord);
    }

    may be it should be:
    return tex2D(TextureSampler, TexCoord);

  • Catalin Zima
    #2 written by Catalin Zima 3 years ago

    I fixed it now. Thanks!

  • Hassan Aly Selim
    #3 written by Hassan Aly Selim 3 years ago

    Thanks alot for this HLSL Crash Course =)
    Now I can start writing my own Custom Shaders in XNA !

  • Enio
    #4 written by Enio 3 years ago

    Thanks for your helpful explanations. Which book would be more suitable for a beginner who needs to learn everything from scratch (shaders, algebra, algorithms, and physical)? Thanks!

  • Enio
    #5 written by Enio 3 years ago

    Why not develop the second part of the course with several practical examples (Blur, DOF, Sepia, etc.)?

  • Devrunner
    #6 written by Devrunner 2 years ago

    You’re the best.

  • ZWabbit
    #7 written by ZWabbit 2 years ago

    I’ve seen both () and used when doing Texture = (something) in declaring samplers. Is there an actual difference between the two?

  • ZWabbit
    #8 written by ZWabbit 2 years ago

    Uh, that was supposed to two braces, that some reason didn’t show up.

  • ZWabbit
    #9 written by ZWabbit 2 years ago

    Sorry, again that didn’t work, I think they’re treating them as html delimiters and I’m not quite sure how to escape them. Basically the less than and greater than symbols wrapping something.

  • Roy Triesscheijn
    #10 written by Roy Triesscheijn 2 years ago


    ZWabbit:

    Sorry, again that didn’t work, I think they’re treating them as html delimiters and I’m not quite sure how to escape them. Basically the less than and greater than symbols wrapping something.

    Hey Zwabbit, they don’t defer in meaning :) .

    Btw CatalinZima, great tutorial. Been reading this and the deferred rendering tuts. Really freshens up my shader knowledge, I forgot almost everything that I learned when following riemers’ tuts.

  • Tom
    #11 written by Tom 2 years ago

    very nice introduction,
    cleared the stuff up. thanks!

  • Peter
    #12 written by Peter 2 years ago

    Thanks a lot.
    Great introduction.

  • pvns
    #13 written by pvns 9 months ago

    Hi,
    Thanks for sharing awesome information.
    Can you give some simple source codes for HLSL and C++?

  • Admiral Ronton
    #14 written by Admiral Ronton 5 months ago

    That was amazing, thank you SO MUCH for this clear and concise introduction to HLSL! I feel much better equipped to decipher the MS DirectX examples now. =]

  • Seyed Ahmad Parkhid
    #15 written by Seyed Ahmad Parkhid 4 months ago

    nice explanation ….

  • You may use these HTML tags: <a> <abbr> <acronym> <b> <blockquote> <cite> <code> <del> <em> <i> <q> <strike> <strong>

  • Comment Feed for this Post
Go to Top