Sunday, 21 March 2010

Compile GLSL shaders from Visual Studio

While I was programming some shaders I found it useful to compile shaders from Visual Studio. Of course it is a much nicer way to do it with programs like Render Monkey, but for some shaders (my particle shader) it can be useful to compile directly.

So, to do that, I needed to have installed Cg (and to have an Nvidia graphics board!).

Here are the steps:

Compile glsl shaders before runtime using cgc:

1. Download and install cg (http://developer.nvidia.com/object/cg_download.html)

2. Open VS. Project->Properties->C++->
add at Additional Include Directories $(CG_INC_PATH)
(CG_INC_PATH should be an environment var after installing cg)

3. (Add the shader as files for the project.)
Right click on the shader -> Configuration Properties ->
Custom build step;

- write at Command line: cgc $(InputPath) -oglsl -profile gp4vp
for vertex shader and
cgc $(InputPath) -oglsl -profile gp4fp
(gp4vp/gp4fp are openGL profiles for Nvidia 8 series;
Here is a list of other profiles (from cg doc):

DirectX profiles
dx8ps, dx8vs, dx9ps2, dxvs2, hlslf, hlslv, ps_1_1, ps_1_2, ps_1_3, ps_2_0, ps_2_x, ps_3_0, vs_1_1, vs_2_0, vs_2_x, vs_3_0

OpenGL profiles
arbfp1, arbvp1, fp20, fp30, fp30unlimited, fp40, fp40unlimited, glslf, glslv, gp4fp, gp4gp, gp4vp, gpu_fp, gpu_gp, gpu_vp, vp20, vp30, vp40

Fragment profiles
arbfp1, dx8ps, dx9ps2, hlslf, fp20, fp30, fp30unlimited, fp40, fp40unlimited, glslf, gp4fp, gpu_fp, ps_1_1, ps_1_2, ps_1_3, ps_2_0, ps_2_x, ps_3_0

Geometry profiles
gp4gp, gpu_gp

Vertex profiles
arbvp1, dx8vs, dxvs2, glslv, gp4vp, gpu_vp, hlslv, vp20, vp30, vp40, vs_1_1, vs_2_0, vs_2_x, vs_3_0

Geforce 3/4 profiles
fp20, vp20

Geforce 5 profiles
fp30, vp30

Geforce 6/7 profiles
fp40, vp40

Geforce 8 profiles
gp4fp, gp4gp, gp4vp, gpu_fp, gpu_gp, gpu_vp
)

-write at outputs (in the same window) $(InputName).vp for vertex program
and $(InputName).fp for fragment program



//--------------------------------------------------------------------------


If you wish to have the shader code highlighted, here is a solution:


1. Copy usertype.dat (included on the Cg CD) to the Visual Studio bin directory
(Microsoft Visual Studio 8\Common7\IDE)
2. Start regedit (Start -> Run -> regedit) and go to
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language services\C\C++
(older ver: HKEY_CURRENT_USER\Software\Microsoft\DevStudio\6.0\Text Editor\Tabs/Language Settings\C/C++)

3. Add .vert;.frag; (.cg;) to the end of the FileExtensions key
(each extension in the list should be separated with a semicolon)
4. Open Visual Studio, tools->text editor->file extension->
add .vert and .frag (.cg), selecting C++ editor
5. Restart Visual Studio

Your shaders should now have syntax highlighting.

No comments:

Post a Comment