Cg (short for C for Graphics) and
High-Level Shader Language
The High-Level Shader Language or High-Level Shading Language (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 f ...
(HLSL) are two names given to a high-level
shading language
A shading language is a graphics programming language adapted to programming shader effects (characterizing surfaces, volumes, and objects). Such language forms usually consist of special data types, like "vector", "matrix", "color" and "normal". ...
developed by
Nvidia
Nvidia CorporationOfficially written as NVIDIA and stylized in its logo as VIDIA with the lowercase "n" the same height as the uppercase "VIDIA"; formerly stylized as VIDIA with a large italicized lowercase "n" on products from the mid 1990s to ...
and
Microsoft
Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washing ...
for
programming shader
In computer graphics, a shader is a computer program that calculates the appropriate levels of light, darkness, and color during the rendering of a 3D scene - a process known as ''shading''. Shaders have evolved to perform a variety of spec ...
s. Cg/HLSL is based on the
C programming language
''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
and although they 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 unit
A graphics processing unit (GPU) is a specialized electronic circuit designed to manipulate and alter memory to accelerate the creation of images in a frame buffer intended for output to a display device. GPUs are used in embedded systems, mobi ...
s.
Two main branches of the Cg/HLSL language exist: the Nvidia Cg compiler (cgc) which outputs
DirectX
Microsoft DirectX is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. Originally, the names of these APIs all began with "Direct", ...
or
OpenGL
OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardwa ...
and the Microsoft HLSL which outputs DirectX shaders in bytecode format. Nvidia's cgc was
deprecated
In several fields, especially computing, deprecation is the discouragement of use of some terminology, feature, design, or practice, typically because it has been superseded or is no longer considered efficient or safe, without completely removing ...
in 2012, with no additional development or support available.
HLSL shaders can enable many
special effects
Special effects (often abbreviated as SFX, F/X or simply FX) are illusions or visual tricks used in the theatre, film, television, video game, amusement park and simulator industries to simulate the imagined events in a story or virtual wor ...
in both 2D and 3D
computer graphics
Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great de ...
. The Cg/HLSL language originally only included support for
vertex shader
In computer graphics, a shader is a computer program that calculates the appropriate levels of light, darkness, and color during the rendering of a 3D scene - a process known as ''shading''. Shaders have evolved to perform a variety of speci ...
s and
pixel shader
In computer graphics, a shader is a computer program that calculates the appropriate levels of light, darkness, and color during the rendering of a 3D scene - a process known as ''shading''. Shaders have evolved to perform a variety of spec ...
s, but other types of shaders were introduced gradually as well:
* DirectX 10 (Shader Model 4) and Cg 2.0 introduced
geometry shader
In computer graphics, a shader is a computer program that calculates the appropriate levels of light, darkness, and color during the rendering of a 3D scene - a process known as ''shading''. Shaders have evolved to perform a variety of speci ...
s.
* DirectX 11 (Shader Model 5) introduced
compute shader
In computing, a compute kernel is a routine compiled for high throughput accelerators (such as graphics processing units (GPUs), digital signal processors (DSPs) or field-programmable gate arrays (FPGAs)), separate from but used by a main progr ...
s (
GPGPU
General-purpose computing on graphics processing units (GPGPU, or less often GPGP) is the use of a graphics processing unit (GPU), which typically handles computation only for computer graphics, to perform computation in applications traditiona ...
) and
tessellation shader
In computer graphics, a shader is a computer program that calculates the appropriate levels of light, darkness, and color during the rendering of a 3D scene - a process known as ''shading''. Shaders have evolved to perform a variety of speci ...
s (hull and domain). The latter is present in Cg 3.1.
* DirectX 12 (Shader Model 6.3) introduced
ray tracing shaders (ray generation, intersection, bit /closest hit / miss).
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 over assembly are:
* High level code is easier to learn, program, read, and maintain than assembly code.
* Cg code is portable to a wide range of hardware and platforms, unlike assembly code, which usually depends on hardware and the platforms it's written for.
* The Cg compiler can optimize code and do lower level tasks automatically, which are hard to do and error prone in assembly.
Language
Data types
Cg 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:
*
float
Float may refer to:
Arts and entertainment Music Albums
* ''Float'' (Aesop Rock album), 2000
* ''Float'' (Flogging Molly album), 2008
* ''Float'' (Styles P album), 2013
Songs
* "Float" (Tim and the Glory Boys song), 2022
* "Float", by Bush ...
- a 32bit floating point number
*
half
One half ( : halves) is the irreducible fraction resulting from dividing one by two or the fraction resulting from dividing any number by its double. Multiplication by one half is equivalent to division by two, or "halving"; conversely, ...
- a 16bit floating point number
*
int - a 32bit integer
*
fixed
Fixed may refer to:
* ''Fixed'' (EP), EP by Nine Inch Nails
* ''Fixed'', an upcoming 2D adult animated film directed by Genndy Tartakovsky
* Fixed (typeface), a collection of monospace bitmap fonts that is distributed with the X Window System
* ...
- a 12bit fixed point number
*
bool - a boolean variable
* sampler* - represents a texture object
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 type
In computer science, array is a data type that represents a collection of ''elements'' (value (computer science), values or variable (computer science), variables), each selected by one or more indices (identifying keys) that can be computed at R ...
s, 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 operator
In logic, a logical connective (also called a logical operator, sentential connective, or sentential operator) is a logical constant. They can be used to connect logical formulas. For instance in the syntax of propositional logic, the binary co ...
s.
Functions and control structures
Cg shares the basic control structures with C, like if/else, while, and for. It also has a similar way of defining functions.
Semantics
Preprocessor
Cg implements many
C preprocessor
The C preprocessor is the macro preprocessor for the C, Objective-C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control ...
directives and its macro expansion system. It implements .
[ Mark J. Kilgard]
''Cg in Two Pages''
2003.
HLSL features
* Namespace
* Annotation
Environment
Compilation targets
Cg programs are built for different ''shader profiles'' that stand for GPUs with different capabilities.
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.
[
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. Cg supports some of the newer profiles up to Shader Model 5.0 as well as translation to glsl or hlsl.][
*PS 1.0 — Unreleased ]3dfx
3dfx Interactive was an American technology company headquartered in San Jose, California, founded in 1994, that specialized in the manufacturing of 3D graphics processing units, and later, video cards. It was a pioneer in the field from the l ...
Rampage, DirectX 8.
*PS 1.1 — GeForce 3
The GeForce 3 series (NV20) is the third generation of Nvidia's GeForce graphics processing units (GPUs). Introduced in February 2001, it advanced the GeForce architecture by adding programmable pixel and vertex shaders, multisample anti-aliasi ...
, DirectX 8.
*PS 1.2 — 3Dlabs
3Dlabs was a fabless semiconductor company. It was founded in 1994 with headquarters in San Jose, California. It originally developed the GLINT and PERMEDIA high-end graphics chip technology, that was used on many of the world's leading computer g ...
Wildcat VP, DirectX 8.0a.
*PS 1.3 — GeForce 4 Ti, DirectX 8.0a.
*PS 1.4 — Radeon 8500-9250, Matrox Parhelia
Matrox Parhelia-512 is a graphics processing unit (GPU) with full support for DirectX 8.1 and incorporating several DirectX 9.0 features. Released in 2002, it was best known for its ability to drive three monitors ("Surround Gaming") and its ''Co ...
, DirectX 8.1.
*Shader Model 2.0 — Radeon 9500-9800/X300-X600, DirectX 9.
*Shader Model 2.0a — GeForce FX/PCX-optimized model, DirectX 9.0a.
*Shader Model 2.0b — Radeon X700-X850 shader model, DirectX 9.0b.
*Shader Model 3.0 — Radeon X1000
The R520 (codenamed Fudo) is a graphics processing unit (GPU) developed by ATI Technologies and produced by TSMC. It was the first GPU produced using a 90 nm photolithography process.
The R520 is the foundation for a line of DirectX 9.0c a ...
and GeForce 6
The GeForce 6 series (codename NV40) is Nvidia's sixth generation of GeForce graphic processing units. Launched on April 14, 2004, the GeForce 6 family introduced PureVideo post-processing for video, '' SLI'' technology, and ''Shader Model 3.0 ...
, DirectX 9.0c.
*Shader Model 4.0 — Radeon HD 2000
The graphics processing unit (GPU) codenamed Radeon R600 is the foundation of the Radeon HD 2000 series and the FireGL 2007 series video cards developed by ATI Technologies. The HD 2000 cards competed with nVidia's GeForce 8 series.
Architect ...
and GeForce 8, DirectX 10.
*Shader Model 4.1 — Radeon HD 3000 and GeForce 200, DirectX 10.1.
*Shader Model 5.0 — Radeon HD 5000 and GeForce 400
Serving as the introduction of Fermi (microarchitecture), Fermi, the GeForce 400 series is a series of graphics processing units developed by Nvidia. Its release was originally slated in November 2009; however, after delays, it was released on M ...
, DirectX 11.
*Shader Model 5.1 — GCN 1+, Fermi+, DirectX 12 (11_0+) with WDDM 2.0.
*Shader Model 6.0 — GCN 1+, Kepler+, DirectX 12 (11_0+) with WDDM 2.1.
*Shader Model 6.1 — GCN 1+, Kepler+, DirectX 12 (11_0+) with WDDM 2.3.
*Shader Model 6.2 — GCN 1+, Kepler+, DirectX 12 (11_0+) with WDDM 2.4.
*Shader Model 6.3 — GCN 1+, Kepler+, DirectX 12 (11_0+) with WDDM 2.5.
*Shader Model 6.4 — GCN 1+, Kepler+, Skylake+, DirectX 12 (11_0+) with WDDM 2.6.
*Shader Model 6.5 — GCN 1+, Kepler+, Skylake+, DirectX 12 (11_0+) with WDDM 2.7.
"32 + 64" for ''Executed Instructions'' means "32 texture instructions and 64 arithmetic instructions."
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
Texture mapping is a method for mapping a texture on a computer-generated graphic. Texture here can be high frequency detail, surface texture, or color.
History
The original technique was pioneered by Edwin Catmull in 1974.
Texture mapping ...
functions tex1D and tex2D.
The Cg runtime library
Cg 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 API
An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software Interface (computing), interface, offering a service to other pieces of software. A document or standa ...
s: OpenGL
OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardwa ...
or DirectX
Microsoft DirectX is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. Originally, the names of these APIs all began with "Direct", ...
. 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.
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
LLVM is a set of compiler and toolchain technologies that can be used to develop a front end for any programming language and a back end for any instruction set architecture. LLVM is designed around a language-independent intermediate represen ...
-based DXC (DirectXShaderCompiler) with support for newer HLSL features. Both compilers generate bytecode: while the older FXC used DXBC, DXC now uses DXIL. DXC can also emit SPIR-V
Standard Portable Intermediate Representation (SPIR) is an intermediate language for parallel compute and graphics by Khronos Group. It is used in multiple execution environments, including the Vulkan graphics API and the OpenCL compute API, to re ...
bytecode.
The Khronos Group
The Khronos Group, Inc. is an open, non-profit, member-driven consortium of 170 organizations developing, publishing and maintaining royalty-free interoperability standards for 3D graphics, virtual reality, augmented reality, parallel computation ...
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. This task was previously performed by source-level converters like HLSL2GLSL, but the resulting code is often bloated.
Derived languages
The PlayStation Shading Language is based on Cg/HLSL.
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.
Applications and games that use Cg or HLSL
* '' 3DVIA Virtools''
* Adobe Photoshop
Adobe Photoshop is a raster graphics editor developed and published by Adobe Inc. for Microsoft Windows, Windows and macOS. It was originally created in 1988 by Thomas Knoll, Thomas and John Knoll. Since then, the software has become the indu ...
* Maya
Maya may refer to:
Civilizations
* Maya peoples, of southern Mexico and northern Central America
** Maya civilization, the historical civilization of the Maya peoples
** Maya language, the languages of the Maya peoples
* Maya (Ethiopia), a populat ...
* ''Battlefield 2
''Battlefield 2'' is a first-person shooter video game, developed by DICE (company), DICE and published by Electronic Arts for Microsoft Windows in June 2005 as the third game in the Battlefield (video game series), ''Battlefield'' franchise.
P ...
''
* '' Cafu Engine''
* ''Crystal Space
Crystal Space is a framework for developing 3D applications written in C++ by Jorrit Tyberghein and others. The first public release was on August 26, 1997. It is typically used as a game engine but the framework is more general and can be used ...
''
* '' Dolphinity Racer''
* '' Earth's Special Forces'' - A Half-Life Mod
* '' Enemy Territory: Quake Wars''
* ''Doom 3 BFG Edition
''Doom 3: BFG Edition'' is a Video game remaster, remastered version of ''Doom 3'', released worldwide in October 2012 for Microsoft Windows, PlayStation 3, and Xbox 360. The ''BFG (weapon), BFG Edition'' features enhanced graphics, better audio ...
''
* ''EON Professional™/Ultra™'' of EON Reality
Eon or Eons may refer to: Time
* Aeon, an indefinite long period of time
* Eon (geology), a division of the geologic time scale
Arts and entertainment
Fictional characters
* Eon, in the 2007 film '' Ben 10: Race Against Time''
* Eon, in th ...
* ''eyeon Fusion
Blackmagic Fusion (formerly eyeon Fusion and briefly Maya Fusion, a version produced for Alias-Wavefront) is post-production image compositing developed by Blackmagic Design and originally authored by eyeon Software. It is typically used to crea ...
''
* ''Far Cry
''Far Cry'' is an anthology franchise of first-person shooter games, all of which have been published by Ubisoft. The first game, ''Far Cry'', was developed by Crytek to premiere their CryEngine software, and released in March 2004. Subsequent ...
''
* '' Garshasp: The Monster Slayer''
* '' GLScene''
* ''Gun Metal
Gun metal, also known as red brass in the United States, is a type of bronze; an alloy of copper, tin and zinc. Proportions vary but 88% copper, 8–10% tin, and 2–4% zinc is an approximation. Originally used chiefly for making guns, it has la ...
''
* '' Hitman: Blood Money''
* ''Irrlicht Engine
Irrlicht (pronounced in German) is an open-source game engine written in C++. It is cross-platform, officially running on Windows, macOS, Linux and Windows CE and due to its open nature ports to other systems are available, including FreeBSD, Xb ...
''
* ''League of Legends
''League of Legends'' (''LoL''), commonly referred to as ''League'', is a 2009 multiplayer online battle arena video game developed and published by Riot Games. Inspired by ''Defense of the Ancients'', a Mod (video games), custom map for War ...
''
* '' Lightfeather 3D Engine''
* LightWave
LightWave 3D is a 3D computer graphics program developed by NewTek. It has been used in films, television, motion graphics, digital matte painting, visual effects, video game development, product design, architectural visualizations, virtual ...
11.6
* ''muvee Reveal
muvee Reveal is proprietary video editing software program for Microsoft Windows created by Singapore-based muvee Technologies. Reveal creates video slideshows from input videos, photos, and music. Muvee Reveal 7 was first released in 2007 and ...
''
* ''OGRE
An ogre ( feminine: ogress) is a legendary monster depicted as a large, hideous, man-like being that eats ordinary human beings, especially infants and children. Ogres frequently feature in mythology, folklore, and fiction throughout the wor ...
''
* ''OpenEmu
OpenEmu is an open-source software, open-source multi-system video game emulator designed for macOS. It provides a plugin interface to emulate numerous Video game console, consoles' hardware, such as the Nintendo Entertainment System, Sega Genesis ...
''
* ''Panda3D
Panda3D is a game engine that includes graphics, audio, I/O, collision detection, and other abilities relevant to the creation of 3D games. Panda3D is free, open-source software under the revised BSD license.
Panda3D's intended game-development ...
''
* PCSX2
PCSX2 is a free and open-source PlayStation 2 emulator for Windows, Linux, and macOS that supports a wide range of PlayStation 2 video games with a high level of compatibility and functionality. Although PCSX2 can closely mirror the original gam ...
* PlayStation 3
The PlayStation 3 (PS3) is a home video game console developed by Sony Interactive Entertainment, Sony Computer Entertainment. The successor to the PlayStation 2, it is part of the PlayStation brand of consoles. It was first released on Novemb ...
* ''RetroArch
RetroArch is a free and open-source, cross-platform frontend for emulators, game engines, video games, media players and other applications. It is the reference implementation of the libretro API, designed to be fast, lightweight, portable and w ...
''
* ''R.U.S.E.
''R.U.S.E.'' is a real-time strategy video game developed by Eugen Systems and published by Ubisoft which was released for Microsoft Windows, PlayStation 3, and Xbox 360, in September 2010. The game was subsequently released for macOS through Ste ...
''
* '' Snes9x''
* ''Unity game engine
Unity is a cross-platform game engine developed by Unity Technologies, first announced and released in June 2005 at Apple Worldwide Developers Conference as a Mac OS X game engine. The engine has since been gradually extended to support a var ...
''
* ''Unreal Engine
Unreal Engine (UE) is a 3D computer graphics game engine developed by Epic Games, first showcased in the 1998 first-person shooter game ''Unreal''. Initially developed for PC first-person shooters, it has since been used in a variety of genres ...
''
See also
* Computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
* Computer graphics
Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great de ...
* Vertex and pixel shaders
In computer graphics, a shader is a computer program that calculates the appropriate levels of light, darkness, and color during the rendering of a 3D scene - a process known as ''shading''. Shaders have evolved to perform a variety of spec ...
* High level shader language
The High-Level Shader Language or High-Level Shading Language (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 f ...
* OpenGL shading language
OpenGL Shading Language (GLSL) is a high-level shading language with a syntax based on the C programming language. It was created by the OpenGL ARB (OpenGL Architecture Review Board) to give developers more direct control of the graphics pipelin ...
* Shader Model
The High-Level Shader Language or High-Level Shading Language (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 f ...
* OpenGL
OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardwa ...
* DirectX
Microsoft DirectX is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. Originally, the names of these APIs all began with "Direct", ...
References
Further reading
Randima Fernando
Mark J. Kilgard,
The Cg Tutorial
The Definitive Guide to Programmable Real-Time Graphics'', Addison-Wesley Professional,
Randima Fernando
''GPU Gems: Programming Techniques, Tips, and Tricks for Real-Time Graphics'', Addison-Wesley Professional,
* William R. Mark, R. Steven Glanville, Kurt Akeley
Kurt Akeley (born June 8, 1958) is an American computer graphics engineer.
Akeley was elected into the National Academy of Engineering in 2005 for contributions to the architecture of 3-D graphics systems and the definition of Open GL, now the in ...
, Mark J. Kilgard, ''Cg: A System for Programming Graphics Hardware in a C-like Language'', Proceedings of SIGGRAPH
SIGGRAPH (Special Interest Group on Computer Graphics and Interactive Techniques) is an annual conference on computer graphics (CG) organized by the ACM SIGGRAPH, starting in 1974. The main conference is held in North America; SIGGRAPH Asia ...
2003,
External links
cgc-opensrc
- Mirror for nvidia's open source compiler, 2002
* Some essential materials (e.g. the design paper) are gathered i
these course notes from Siggraph 2005
* Nvidia-hosted materials:
*
Cg FAQ
*
Cg Toolkit
** C
Language Reference and Documentation
NeHe Cg vertex shader tutorial
Documentation for the Cg Shader standard used by emulators
* Aras Pranckevičius,