Tuesday, October 26, 2010

Loading OpenGL cg shader using Object file

Tip - While loading the cg file in OpenGL it is found that loading cg using Object file is faster than source file loading.

Details - In OpenGL a shader is loaded into GPU memory using the function 
cgCreateProgram(context, CG_SOURCE, const char *pchVertexProgramString,CG_PROFILE_ARBVP1, "main", args);

In this the second argument indicates whether the pchVertexProgramString is normal source string or whether it is pre-compiled object file. If we give the second argument as CG_OBJECT, a pre-compiled .asm file (.asm file is the assembly file generated when the cg file is compiled) and the assembly code as the next argument will reduce the time of cg compiling at the run time.

In this case the pre-compiled .asm file should be available before calling the cgCreateProgram API from our cpp files. If we are using the MakeFile for build purpose we can use the .BEFORE statement to compile cg before normal cpp files are compiled. In that case the MakeFile will look like below.

---------------------------------------------------
TARGET_DLL    = MyDll
TUSPDB=TRUE

CPP_SOURCES=\
    Stdafx.cpp \
    Source.cpp \
RC_SOURCES=\
    Resource.rc \

USER_INCLUDES=\
    -I"\Include" \

USER_DLL_LINK_FLAGS=\
    /libpath:"\Cg\Lib" \

LINK_ONLY_LIBS=\
    cg.lib \

.BEFORE:
    cgc.exe -profile fp40 Shader.cg -o Shader.asm
---------------------------------------------------

Posted By : Xavier K Joseph

No comments:

Post a Comment