CgFX in 3d Studio Max

CgFX is a dialect of the Cg language (C for Graphics) invented by NVIDIA for programming the so-called hardware shaders. In this article we will take a closer look at this language and will try to reveal its advantages at the example of 3d studio max graphics package.

by Igor Sivakov
08/20/2003 | 01:53 PM

“Hardware shaders”, “interactive rendering”, “real-time cinematic effects”… These terms and things they denote are permeating into the 3D graphics field. It all began when NVIDIA and ATI developed and rolled out the new generation of graphics processors with revolutionary 3D features This process has recently been developing very actively due to the more advanced hardware GPU models coming, as well as due to special software worked out particularly for them.


The Cg language (C for Graphics) invented by NVIDIA is a special-purpose C-like language for programming the so-called hardware shaders. Shaders allow fantastic and spectacular visual effects in real time and using only the graphics card’s hardware. Regrettably, Cg is too narrowly specialized and won't suite an amateur in programming. For example, it won't go for a designer who works with graphics, including interactive one, too.

So, we were waiting for another, more accessible and mass-oriented tool for work with shaders. And it did come out – CgFX is a Cg dialect, compatible with DirectX shader specifications and requiring the user-defined parameters from a shader that change its visual representation. The programmability of shaders allows the designer to process them with traditional graphics tools.

The standards imposed by the developers of system software (operation systems) are changing, too. Today we already talk about the ninth version of DirectX and, accordingly, more flexible and powerful shaders. They now resemble fully-fledged programs with conditional branches, loops and so on. Modern shaders are proficient in implementing complex and sophisticated effects in real time, while interactive technologies themselves are becoming the hot talk of designers, artists, movie directors. It is also quite natural that after the arrival of CgFX the modern 3D graphics packages offered plug-ins to support it. Today there are plug-ins available for Softimage XSI, Maya and 3d studio max.

In this article I will try to show you what CgFX is by the example of 3d studio max.

CgFX and 3d studio max

So, CgFX for max is first of all a tool for visual hardware shader development within the environment and with the 3d studio max tools. NVIDIA guarantees you full WYSIWYG: you will see in the interactive application or game exactly what you see in the 3d studio viewport. The shader code may include any command from the Cg language as well as insertions in Assembler or in any other low-level language.

Overall, the CgFX plug-in uses 3d studio max environment to open shader files, execute them, modify them and display the result in the viewport in real time. Shader files are compatible with the FX format developed for DirectX, with all ensuing consequences.

Every shader program can and even must have user-defined parameters. Thus, two objects produced by the same shader but with different parameters can have quite distinct looks (the shader code itself remains unchanged, of course).

These parameters are exactly where the designer enters the game. Every shader has its own list of parameters depending on the purpose each particular shader was created for. Basic parameters are textures and color. Just imagine: you have a shader library for rendering human skin, hair, eyes. It is ready for immediate use, photo-like, adjustable and interactive! ...R elax, of course, there is sure no such library so far.

Besides the things I have mentioned above, the shader can have several techniques. The technique is a method (code portion) that implements this or that shader function. As the shader is executed by the GPU of your graphics card, there arises the compatibility issue between the code and the capabilities of the graphics card it runs on. Incorporation of several techniques into a shader makes it “heavier” (longer), but allows it to run on both: GeForce 2 and GeForce FX, for example. The plug-in can also open the shader file in the project window so that you could manually edit its code.

Now, I have covered the basic things you should know about shaders and Cg, let's go over to more concrete matters.

CgFX in Practice

CgFX works with the fifth version of max and with DirectX API, only. If your project demands OpenGL, you may stop reading any further: you will find no useful info for yourself. Max doesn't allow this: you will have to export a scene for a standalone CgFX Viewer program and process the shader there. Thus, you won't use the advantages of the max operational environment.

Or you can choose an alternative: NVIDIA has already issued a similar plug-in for Maya, which works with OpenGL only. By the way, developers say that in this case OpenGL instructions are translated into DirectX ones to be performed. And in fact particularly the fifth version of max is required because only in this version, the material editor has a new tab called Viewpoint Shader Manager through which you access the hardware shader functions.

The last requirement deals with the graphic card you use. If you are planning to take full advantage of CgFX, I strongly recommend you to use a graphics card with the last-generation GPU from NVIDIA. Shaders may not run on “older” GPUs at all, or will be emulated, that is, executed by the central processor. It is really slow.  Moreover, no interactivity is to be mentioned in this case.

So, 3d studio max 5 is configured for DirectX API, your workstation has a Quadro FX (or at least GeForce FX) graphics card, and the object is loaded – what comes next? Next you launch the material editor, find the Viewport Manager tab, select CgFX in the list and check “Enabled” on the left.

After that, you gain access to the “Viewport Shader – CgFX” scroll that contains Material Mapping Options and Connection Editor. The first is for bitmap processing, the second is for setting up the user-defined parameters of the hardware shader.

The name of the file with the shader is displayed above the “Connection Editor” button.

As for the bitmaps, they are analogous to the bitmap slots of the standard part of the materials editor – diffusion color, transparency, light and so on. Checking an option allows the shader to use this attribute at rendering. To do it, you must also load the corresponding bitmap into the properties slot in the standard part of the materials editor.

I'd like to stress the fact that it is the shader program that solely defines what material properties from the list are actually used. You can load all material properties slots with textures, but only those of them will be visible that are processed in the shader code. Conversely, if a bitmap is processed by the shader, but is not loaded in the materials editor slot, the object will be rendered wrongly (or may not be displayed at all).

Below the material properties, you see the “Connection Editor” button. Click it and you will activate the editor of user-defined shader parameters.

The dialog window of this editor is divided in two: the left panel lists the shader's parameters and the right panel lists the values set for these parameters. The upper line under the Properties scroll in the Connection Editor – CgFX Shader – shows the name of the loaded file with the shader. A click at the right part of this line gives you access to additional interface elements – Edit and File (otherwise, they are invisible). These two options allow editing the program text of the loaded shader or load a file with another shader.

The Technique line is next. This is the rendering technique used by the shader (for example, with or without vertex and pixel pipelines). This is a drop-down list: if the graphics card allows, there will be several options available. As we have seen above, different rendering methods should be coded in one shader to ensure compatibility with older graphics cards. When loaded, the shader automatically selects the most optimal technique for the installed card. The list and values of the parameters listed below is specific for each shader.

That's about all. Summing up, we will have the following work algorithm for processing shaders in 3d studio max: 1) load a 3D object, 2) using the standard max tools assign texture coordinates to the surface, 3) activate CgFX and 4) load a necessary hardware shader from a file. Then you are free to be creative: change the shader parameters or edit its code directly and view the result in the viewport. The values you assign to the parameters are local, that is, they are only working in the current scene. The changes you make to the code are global – they change all scenes that use this shader. And don't forget about loading your own textures into the corresponding material properties slots!

On the whole, the plug-in is easy to use. Once again, I'd like to stress that the hardware shader is displayed in the project window. Its result will generally be different from the results you achieve with the rendering module of 3d studio max: the inbuilt 3d studio max rendering capabilities are downright poor in contrast to CgFX.

On the left – the picture is constructed by DiffuseBump.fx shader from the library coming with the plug-in (a screenshot of max’s viewport on a GeForce 2 Pro graphics card).

On the right – the same picture as rendered by max’s scanline-renderer. Today, there are much more perfect graphics cards in the market, which produce much better images. However, the picture we took on GeForce2, which has no pixel or simplest vertex shader support altogether, looks very nice.


CgFX Shader Execution Speed

Just for you to know how fast pixel shaders are, I include the results shown by a GeForce FX 5200 graphics card at executing some pixel shaders in the max viewport.

The testbed was configured as follows:

I used NV_Shaderball.max model and shaders coming with the CgFX plug-in for 3ds max 5. The test method (transformation of the viewport) has been described in our previous articles.

The test scene contains a model of a spherical object made of relatively few polygons (about 4,000), two directional light sources and a camera. The camera window (the scene default setting) was switched to the perspective view window. Then I loaded a shader file in the Connection Editor and measured the performance. The results are listed in good old frames per second (fps).

So, now you will see the test results and screenshots of the viewport (interactivity in numbers).

BumpReflectCg.fx – DirectX 8, assembler, bumpmap, texture:

The second number refers to the viewport zoom. I included it because the zoom speed differs greatly from any other transformation in most tests.

BumpReflectCgDX9.fx – DirectX 9, bumpmap, texture:

This is a DirectX 9 analog of the previous shader. It doesn't use Assembler, but features richer visual effects and parameters and thus is executed slower than its analog.

Goochy.fx – DirectX 8, non-photorealistic style, own spotlight source:


A DirectX 9 analog of the previous shader. It runs slower, but has twice as many user-defined parameters.


Goochy_tex.fx is the same DirectX 8 shader, but with an additional texture for the diffuse color.


It is a DirectX 9 analog of the previous shader.

MetalIP.fx – DirectX 8, metallic surface, 1 spotlight, Phong shading:


This is a DX9 analog of MetalIP.fx with two additional adjustable parameters, different representation and 5-6 times slower speed.


It's the same, but with a texture and reflection.

DiffNoise.fx – DirectX 8, 3D noise texture, 1 light source:

When the object is transformed, its texture appears “noisy” - quite a weird effect.

Dispersion.fx – light dispersion shader, DirectX 8, texture, assembler, fixed functions:

Ghostly2.fx – DirectX 8, transparency, texture, assembler:

So, the test results suggest that DirectX 9 shaders are generally much harder on the graphics cards compared to their DirectX 8 analogs – that's the price you pay for higher settings flexibility, new capabilities and easy programmability.