High-Level Shader Language Explained

Cg/HLSL
Screenshot Caption:A scene containing several different 2D HLSL shaders. Distortion of the statue is achieved purely physically, while the texture of the rectangular frame beside it is based on color intensity. The square in the background has been transformed and rotated. The partial transparency and reflection of the water in the foreground are added by a shader applied finally to the entire scene.
Paradigms:-->
Family:shading language
Designers:-->
Developer:nVIDIA, Microsoft
Dialects:Cg, HLSL, Playstation Shading Language, ReShadeFX, Slang
Influenced By:C, RenderMan Shading Language
Influenced:GLSL

The High-Level Shader Language[1] or High-Level Shading Language[2] (HLSL) is a proprietary shading language developed by Microsoft for the Direct3D 9 API to augment the shader assembly language, and went on to become the required shading language for the unified shader model of Direct3D 10 and higher. It was developed alongside the Cg (short for C for Graphics) shading language from Nvidia. Early versions of the two languages were considered identical, only marketed differently.[3]

Although Cg and HLSL share the same core syntax, some features of C were modified and new data types were added to make Cg/HLSL more suitable for programming graphics processing units.[4] [5]

Two main branches of the Cg/HLSL language exist: the Nvidia Cg compiler (cgc) which outputs DirectX or OpenGL and the Microsoft HLSL which outputs DirectX shaders in bytecode format.[6] [7] Nvidia's cgc was deprecated in 2012, with no additional development or support available.[8]

HLSL shaders can enable many special effects in both 2D and 3D computer graphics.

D3D11.3 and D3D12 introduced Shader Model 5.1[10] and later 6.0.[11]

Background

Due to technical advances in graphics hardware, some areas of 3D graphics programming have become quite complex. To simplify the process, new features were added to graphics cards, including the ability to modify their rendering pipelines using vertex and pixel shaders.

In the beginning, vertex and pixel shaders were programmed at a very low level with only the assembly language of the graphics processing unit. Although using the assembly language gave the programmer complete control over code and flexibility, it was fairly hard to use. A portable, higher level language for programming the GPU was needed, so Cg was created to overcome these problems and make shader development easier.

Some of the benefits of using Cg/HLSL over assembly are:

Language

Data types

Cg/HLSL has six basic data types. Some of them are the same as in C, while others are especially added for GPU programming. These types are:

Cg also features vector and matrix data types that are based on the basic data types, such as float3 and float4x4. Such data types are quite common when dealing with 3D graphics programming. Cg also has struct and array data types, which work in a similar way to their C equivalents.

Operators

Cg supports a wide range of operators, including the common arithmetic operators from C, the equivalent arithmetic operators for vector and matrix data types, and the common logical operators.

Preprocessor

Cg implements many C preprocessor directives and its macro expansion system. It implements .[12]

HLSL features

Environment

Cg programs are built for different shader profiles that stand for GPUs with different capabilities.[13] These profiles decide, among others, how many instructions can be in each shader, how many registers are available, and what kind of resources a shader can use. Even if a program is correct, it might be too complex to work on a profile.[12]

As the number of profile and shader types cropped up, Microsoft has switched to use the term "Shader Model" to group a set of profiles found in a generation of GPUs.[14] Cg supports some of the newer profiles up to Shader Model 5.0 as well as translation to glsl or hlsl.[13]

GPUs listed are the hardware that first supported the given specifications. Manufacturers generally support all lower shader models through drivers. Note that games may claim to require a certain DirectX version, but don't necessarily require a GPU conforming to the full specification of that version, as developers can use a higher DirectX API version to target lower-Direct3D-spec hardware; for instance DirectX 9 exposes features of DirectX7-level hardware that DirectX7 did not, targeting their fixed-function T&L pipeline.

Pixel shader comparison

Pixel shader version1.01.11.21.3[15] 1.4 2.0[16] 2.0a[17] 2.0b[18] 3.0[19] 4.0[20] 4.1[21] 5.0[22]
Dependent texture limit44 46 8 Unlimited 8 Unlimited Unlimited
Texture instruction limit44 46 * 2 32 Unlimited Unlimited Unlimited Unlimited
Arithmetic instruction limit8888 * 264UnlimitedUnlimitedUnlimitedUnlimited
Position register
Instruction slots88 + 4 8 + 4(8 + 6) * 2 64 + 32 512 512 ≥ 512 ≥ 65536
Executed instructions88 + 4 8 + 4(8 + 6) * 2 64 + 32 512 512 65536 Unlimited
Texture indirections44 44 4 Unlimited 4 Unlimited Unlimited
Interpolated registers2 + 42 + 4 2 + 42 + 6 2 + 8 2 + 8 2 + 8 10 32
Instruction predication
Index input registers
Temp registers22 + 4 3 + 46 12 to 32 22 32 32 4096
Constant registers88 88 32 32 32 224 16×4096
Arbitrary swizzling
Gradient instructions
Loop count register
Face register (2-sided lighting)
Dynamic flow control (24) (64)
Bitwise Operators
Native Integers

"32 + 64" for Executed Instructions means "32 texture instructions and 64 arithmetic instructions."

Vertex shader comparison

Vertex shader version1.01.1[23] !2.0 2.0a 3.0 4.0
4.1
5.0
  1. of instruction slots
128128 256 256 ≥ 512 ≥ 65536
Max # of instructions executed128128 1024 65536 65536 Unlimited
Instruction predication
Temp registers1212 12 16 32 4096
  1. constant registers
≥ 96≥ 96 ≥ 256 256 ≥ 256 16×4096
Address register
Static flow control
Dynamic flow control
Dynamic flow control depth24 24 64
Vertex texture fetch
  1. of texture samplers
4 128
Geometry instancing support
Bitwise operators
Native integers

The standard library

As in C, Cg/HLSL features a set of functions for common tasks in GPU programming. Some of the functions have equivalents in C, like the mathematical functions abs and sin, while others are specialized in GPU programming tasks, like the texture mapping functions tex1D and tex2D.

The Cg/HLSL runtime library

Cg/HLSL programs are merely vertex and pixel shaders, and they need supporting programs that handle the rest of the rendering process. Cg can be used with two graphics APIs: OpenGL or DirectX. Each has its own set of Cg functions to communicate with the Cg program, like setting the current Cg shader, passing parameters, and such tasks. (HLSL only targets DirectX.)

In addition to being able to compile Cg source to assembly code, the Cg runtime also has the ability to compile shaders during execution of the supporting program. This allows the runtime to compile the shader using the latest optimizations available for hardware that the program is currently executing on. However, this technique requires that the source code for the shader be available in plain text to the compiler, allowing the user of the program to access the source-code for the shader. Some developers view this as a major drawback of this technique.

To avoid exposing the source code of the shader, and still maintain some of the hardware specific optimizations, the concept of profiles was developed. Shaders can be compiled to suit different graphics hardware platforms (according to profiles). When executing the supporting program, the best/most optimized shader is loaded according to its profile. For instance there might be a profile for a graphics card that supports complex pixel shaders, and another profile for one that supports only minimal pixel shaders. By creating a pixel shader for each of these profiles a supporting program enlarges the number of supported hardware platforms without sacrificing picture quality on powerful systems.'

Compilers and dialects

The Cg dialect has only ever had one compiler, in the form of Nvidia's Cg toolkit.

Microsoft has released two compilers for HLSL. The original compiler was the closed-source FXC (Effect Compiler), supported until 2015. It was deprecated in favor of the open-source LLVM-based DXC (DirectXShaderCompiler) with support for newer HLSL features.[24] Both compilers generate bytecode: while the older FXC used DXBC, DXC now uses DXIL. DXC can also emit SPIR-V bytecode.[25]

The Khronos Group has also written a LLVM-based HLSL compiler, in the form of a frontend for glslang, their GLSL-to-SPIR_V compiler. Support for SPIR-V means that the shaders can be cross-platform, no longer limiting them to a DirectX stack.[26] This task was previously performed by source-level converters like HLSL2GLSL, but the resulting code is often bloated.[27]

Derived languages

The PlayStation Shader Language (PSSL) is based on Cg/HLSL.[28]

The ReshadeFX shading language is also based on Cg/HLSL. Shaders written in ReshadeFX are compiled to OpenGL, DX, or Vulkan and injected into games to act as post-processing filters.[29]

Khronos has since evolved glslang into slang, a shading language and compiler that is mostly source-code-compatible with HLSL. Slang can compile to textual shading languages (GLSL, MSL, CUDA, WGSL), bytecodes (D3D11, D3D12, Vulkan SPIR-V) as well as the CPU. It also has a GLSL-compatibility mode.[30]

Applications and games that use Cg or HLSL

See also

Further reading

External links

Notes and References

  1. Web site: Writing HLSL Shaders in Direct3D 9 . . February 22, 2021.
  2. Web site: High-level shader language (HLSL) . . February 22, 2021.
  3. Web site: Fusion Industries :: Cg and HLSL FAQ ::. https://web.archive.org/web/20120824051248/http://www.fusionindustries.com/default.asp?page=cg-hlsl-faq. dead. August 24, 2012. August 24, 2012.
  4. Web site: Fusion Industries :: Cg and HLSL FAQ ::. https://web.archive.org/web/20120824051248/http://www.fusionindustries.com/default.asp?page=cg-hlsl-faq. dead. 24 August 2012. 24 August 2012.
  5. Web site: The Cg Tutorial - Chapter 1. Introduction . developer.download.nvidia.cn.
  6. Web site: Writing HLSL Shaders in Direct3D 9 (Windows). msdn.microsoft.com. 24 May 2021 .
  7. Web site: Cg FAQ. NVIDIA DesignWorks. 8 March 2011. 25 May 2017.
  8. Web site: Cg Toolkit | NVIDIA Developer. 8 March 2011.
  9. Web site: January 2008 . Cg 2.0 Release Notes . nvidia.com.
  10. Web site: Shader Model 5.1 Objects . . February 22, 2021.
  11. Web site: HLSL Shader Model 6.0 . . February 22, 2021.
  12. [Mark Kilgard|Mark J. Kilgard]
  13. Web site: Cg Profile Documentation . Nvidia developer.
  14. Web site: Shader Models vs Shader Profiles - Win32 apps . docs.microsoft.com . 30 June 2021 . en-us.
  15. Web site: Pixel Shader Differences . . August 19, 2020 . February 22, 2021.
  16. Web site: Introduction to the DirectX 9 High-Level Shader Language . Peeper . Craig . Mitchell . Jason L. . July 2003 . . February 22, 2021.
  17. Web site: NVIDIA Introduces GeForce FX (NV30) . https://web.archive.org/web/20130610035912/http://www.anandtech.com/show/1034/4 . dead . June 10, 2013 . Shimpi . Anand Lal . Anand Lal Shimpi . . February 22, 2021.
  18. Web site: ATI Radeon X800 Pro and XT Platinum Edition: R420 Arrives . https://web.archive.org/web/20120928192354/http://www.anandtech.com/show/1314/4 . dead . September 28, 2012 . Derek . Wilson . . February 22, 2021.
  19. Shader Model 3.0, Ashu Rege, NVIDIA Developer Technology Group, 2004.
  20. The Direct3D 10 System, David Blythe, Microsoft Corporation, 2006.
  21. Web site: Registers - ps_4_1 . . August 23, 2019 . February 22, 2021.
  22. Web site: Registers - ps_5_0 . . August 23, 2019 . February 22, 2021.
  23. Web site: Vertex Shader Differences . . August 19, 2020 . February 22, 2021.
  24. Web site: Porting from FXC to DXC . GitHub . en.
  25. Web site: microsoft/DirectXShaderCompiler: This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang. . Microsoft . 21 October 2020.
  26. Web site: glslang: Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator. . The Khronos Group . 21 October 2020.
  27. Matt Turner. .
  28. Web site: Stenson . Richard . Ho . Chris . PlayStation Shading Language for PS4 . GDC Europe 2013.
  29. Web site: ReShade FX shading language. GitHub. 15 February 2022.
  30. Web site: shader-slang/slang . The Slang Shading Language . 8 December 2025.
  31. Web site: Maya Cg Plug-in | NVIDIA.
  32. Web site: LightWave - 11.6 Features Overview.
  33. Web site: Unity - Manual: Writing Shaders.