# Cg debugging?

## Recommended Posts

I have a .cg file with a vertex program. But how do I debug the program? I can't seem to use printf statements, so how do I get information about values of variables and execution position?

##### Share on other sites
You could pass them to your pixel shader, where you write them to a (floating point!) texture ... And then you use something like "glReadPixels" to get the values...

You can't debug it directly, the code runs on the GPU not the CPU.

Maybe you can find software that pretends to be a shader but that runs on the cpu.

##### Share on other sites
Ok I have written this vertex program based on the tutorial in the Cg toolkit manual:

struct vertex_input
{
float4 position : POSITION;
float4 normal : NORMAL;
};

struct vertex_output
{
// Homogeneous 'clip-space' coordinates.
float4 position : POSITION;
float4 color : COLOR;
};

vertex_output main(
vertex_input IN
, uniform float4x4 ModelViewProj ///< Concatenation of the modelview and projection matrices
, uniform float4x4 ModelViewIT ///< The inverse transpose of the modelview matrix.
, uniform float4 light_vector ///< Specification of light location.
)
{
vertex_output OUT;

// Transform (multiply) the WCS vertex position into homogeneous clip-space.
OUT.position = mul(ModelViewIT, IN.position);

// Transform normal from model-space to view-space
float3 normalVec = normalize(mul(ModelViewIT, IN.normal).xyz);

float3 lightVec = normalize(light_vector.xyz);

float3 eyeVec = float3(0.0, 0.0, 1.0);
float3 halfVec = normalize(lightVec);

float diffuse = dot(normalVec, lightVec);

float specular = dot(normalVec, halfVec);

float4 lighting = lit(diffuse, specular, 32);

float3 diffuseMaterial = float3(0.0, 0.0, 1.0);

float3 specularMaterial = float3(1.0, 1.0, 1.0);

OUT.Color.rgb = lighting.y * diffuseMaterial + lighting.z * specularMaterial;

OUT.Color.a = 1.0;

return OUT;
}

But when I run the code I just get an error:

Cg runtime Error encountered CG ERROR : The compile returned an error.

But how should I be able to find where the error occurred?

##### Share on other sites
Please use the [ source ] and [ /source ] tags.

And then try to find these functions in the manuals:

cgGetLastErrorString and cgGetLastListing

Put them in a function, and after compiling your shader call that function.

Edit: OUT.Color.something should be OUT.color.something because you declare it like this:
struct vertex_output{// Homogeneous 'clip-space' coordinates. float4 position : POSITION;float4 color : COLOR;};

##### Share on other sites
Ok I have now changed the vertex program to only contain:

struct vertex_input{  float4 position : POSITION;  float4 normal   : NORMAL;};struct vertex_output{  // Homogeneous 'clip-space' coordinates.	  float4 position : POSITION;  float4 color    : COLOR;};vertex_output main(                     vertex_input IN                   , uniform float4x4 ModelViewProj ///< Concatenation of the modelview and projection matrices                   , uniform float4x4 ModelViewIT   ///< The inverse transpose of the modelview matrix.                   , uniform float4   light_vector	///< Specification of light location.             ){  vertex_output OUT;   return OUT;}

But I still get the same error. Any hints?

BTW: There were no hits for cgGetLastErrorString in the 1.4 toolkit. And if I do:

std::cout << cgGetLastListing(m_context) << std::endl;

after:

m_program = cgCreateProgramFromFile(m_context, CG_SOURCE, cstr, m_profile, 0, 0);

the application just crashes when I run it.

##### Share on other sites
An empty shader like that obviously crashes because a vertex shader has to output a position.

Edit: and I just found cgGetLastListing in the documentation.

Edit2: "the application just crashes when I run it." ... No error message?

##### Share on other sites
Well I also found:

cgGetLastListing

in the documentation but its:

cgGetLastErrorString

that I cannot find. There is no error message when I run the application. It just crashes but I get the option to open it with the debugger. But that just opens an assembler file which makes no sense to try and understand.

##### Share on other sites
cgGetLastErrorString is also in the documentation. Are you sure you also checked the reference manual?

About the crash, you do realise that
/*sucks, if getlastlisting returns 0 (which it can), you crash with an access violation.Your compiler should be able to tell you that. Do you use Visual C++ ?*/std::cout << cgGetLastListing(m_context) << std::endl;//decent version:const char* lastListing = cgGetLastListing();std::cout << (lastListing == 0 ? "No error." : lastListing) << std::endl;

[Edited by - Dolf on October 13, 2008 12:53:42 PM]

##### Share on other sites
I use VS 2008 for windows Vista 64.

I have now tried this:

m_program = cgCreateProgramFromFile(m_context, CG_SOURCE, cstr, m_profile, 0, 0);if (m_program == 0){  std::cerr << "CgProgram::load_from_file("<<filename<<"): could not create program" << std::endl;  return false;}const char* lastListing = cgGetLastListing();std::cout << (lastListing == 0 ? "No error." : lastListing) << std::endl;

But the compiler says that cgGetLastListing must have an argument:

error C2660: 'cgGetLastListing' : function does not take 0 arguments

which makes ok sense since that is what the documentation also says. I have then tried to supply an argument to cgGetLastListing:

      m_program = cgCreateProgramFromFile(m_context, CG_SOURCE, cstr, m_profile, 0, 0);      if (m_program == 0)      {        std::cerr << "CgProgram::load_from_file("<<filename<<"): could not create program" << std::endl;        return false;      }			const char* lastListing = cgGetLastListing(m_context);			std::cout << (lastListing == 0 ? "No error." : lastListing) << std::endl;

When I run the program it prints "No error" but (m_program == 0) is still true! Could be nice if it was possible to find the implementation of cgCreateProgramFromFile.

##### Share on other sites
m_program = cgCreateProgramFromFile(m_context, CG_SOURCE, cstr, m_profile, 0, 0);

Why are you passing 0 as entry point?

##### Share on other sites
No clue. The above code is from a file: cg_program that our teacher has given us an we are actually not meant to make any changes to that file (others have used it before without any errors). Its just happens that it is in this file that I keep getting errors.

I don't understand this:

std::cout << (lastListing == 0 ? "No error." : lastListing) << std::endl;

If lastListing == 0 it prints "No error", but earlier you wrote that if lastListing == 0 it was a bad thing (crash with an access violation). I have tried:

std::cout << (lastListing == 0 ? lastListing : "No error.") << std::endl;

but then it crashes as before.

EDIT: I know changed OUT.Color... to OUT.color as you recommended and now I don't get the error anymore. But I still don't see how I could get info about that kind of error.

##### Share on other sites
PM me your msn and i'll have a real-time look if you want.

Or skype.

Preferably skype.

##### Share on other sites
Problem solved, as you can see from the above message.

But I still don't understand how I am able to find this kind of errors in the future (Errors like: OUT.color instead of OUT.Color).

##### Share on other sites
Neither:

(1)
std::cout << (lastListing == 0 ? "No error." : lastListing) << std::endl;

or:

(2)
CGerror error;const char* errorString = cgGetLastErrorString( &error );std::cout << errorString << std::endl;

gives any info. Actually the (2) just makes the program crash when I run it. Seems that its not possible to print a charpointer at runtime.

##### Share on other sites
Just running the Cg compiler on the .cg file gives me all the info I need:

cgc test.cg

So I can't see the need to use the build in routines for error checking.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628345
• Total Posts
2982200

• 9
• 9
• 24
• 10
• 9