Cg
By NVIDIA
Cg, short for C for Graphics, is a shading language developed by NVIDIA in collaboration with Microsoft for writing vertex and pixel shaders that could compile to either OpenGL or Direct3D targets. It was one of the earliest widely adopted…
Definition
Cg, short for C for Graphics, is a shading language developed by NVIDIA in collaboration with Microsoft for writing vertex and pixel shaders that could compile to either OpenGL or Direct3D targets. It was one of the earliest widely adopted high-level shading languages, offering a C-like syntax that abstracted over the two competing graphics APIs of its era, though NVIDIA has since deprecated it in favor of native alternatives.
Overview
Cg was introduced by NVIDIA in the early 2000s at a time when GPU-programmable shaders were new and developers otherwise had to write shader code in low-level, GPU-specific assembly that varied between hardware vendors. Its goal was to provide a single, C-like high-level language that could compile down to either OpenGL's shader assembly or Microsoft's Direct3D shader formats, sparing developers from maintaining separate low-level shader code for each competing graphics API. Mechanically, Cg shaders were written as small functions operating on a single vertex or pixel, similar in structure to HLSL, which it closely resembled since NVIDIA developed it jointly with Microsoft around the same period that HLSL was created for Direct3D 9. A separate Cg compiler toolchain translated Cg source into the target API's native shader assembly, letting the same source code support both major graphics APIs of the time from one shared, maintainable codebase rather than two. Cg's closest relative is HLSL, sharing much of its syntax and a joint development history, and both emerged from the same NVIDIA-Microsoft collaboration around Direct3D 9, though HLSL became the API-native language for Direct3D while Cg attempted the broader, cross-API role spanning both major standards. As GLSL matured as OpenGL's own native shading language and Direct3D's HLSL solidified as the standard for that platform, Cg's cross-API positioning became progressively less necessary, and NVIDIA formally deprecated the Cg toolchain in the early 2010s. In practice, Cg saw significant use in game development and early GPU-compute experiments during the 2000s, including in the Unity engine, which used Cg-derived shaders internally for many years before migrating to its own shader language built on HLSL and GLSL translation layers. Some legacy codebases, older Unity shader assets, and historical graphics research from that era still reference or actively use Cg-derived shader syntax in maintained projects. Its main limitation today is that it is a discontinued, unsupported language: NVIDIA no longer updates the Cg toolchain or fixes compatibility issues, and developers starting new projects should use HLSL for Direct3D, GLSL for OpenGL and Vulkan, or WGSL for WebGPU instead. Cg remains relevant mainly for historical context and for maintaining older codebases that have not yet been migrated away from it onto a currently supported shading language. Its brief prominence nonetheless shaped conventions, such as semantic bindings for shader inputs, that persisted into both HLSL and later shading language designs, giving Cg a lasting influence beyond its own active lifespan.
Key Features
- C-like shading language developed jointly by NVIDIA and Microsoft
- Designed to compile to both OpenGL and Direct3D shader targets
- Closely related in syntax and history to Microsoft's HLSL
- Used internally by early versions of the Unity game engine
- One of the first widely adopted high-level shading languages
- Officially deprecated by NVIDIA in favor of native alternatives
- Historically significant in early 2000s GPU-programmable graphics