Wednesday, January 7, 2009

Getting useful errors from Cg

Since Cg programs are compiled at the program's runtime, you must catch Cg compile and runtime errors from within your program. Here's how:

First, you will need a callback. Cg will call this function whenver there is an error. Example:

void MyErrorCallback(void) {
CGerror error = cgGetError();
const char* errorString = cgGetErrorString(cgGetError());
printf("Cg error: %s\n", errorString);

// check for compiler errors
if (error == CG_COMPILER_ERROR)
printf("%s\n", cgGetLastListing(context));
}


Next, you need to set the callback:
cgSetErrorCallback(MyErrorCallback);

Now you should get more useful output when Cg has problems.

No comments: