Jump to content
  • Advertisement
Sign in to follow this  
mlt

Cg debugging?

This topic is 3691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!