Sign in to follow this  
Alessandro

question about cg shader

Recommended Posts

Hi, i have a very simple lighting shader that works just fine. I decided to do a simple texture lookup but when i compile the shader i get a 'tex2D is not supported by current profile' message. I tried to compile the shader using all of the possible profiles (even if arbfp1 should support tex2D), so i don't understand what's wrong. If you like you can try to compile yourself, here is the shader:
struct vertex
{
    float4 position : POSITION;
    float4 normal   : NORMAL;
    float4 color0   : COLOR0;
    float2 texcoord0 : TEXCOORD0;
};

struct fragment
{
    float4 position : POSITION;
    float4 color0   : COLOR0;
    float2 texcoord0 : TEXCOORD0;
};


fragment main( vertex IN, 
               uniform float4x4 modelViewProjection,
               uniform float4x4 modelViewInverse,
		uniform float4 eyePosition,
               uniform float4 lightVector,
	       uniform sampler2D testTexture: TEXUNIT0
	       )
{
    fragment OUT;

    OUT.position = mul( modelViewProjection, IN.position );

    // Transform normal from model-space to view-space
    float4 normal = normalize( mul( modelViewInverse, IN.normal ).xyzz );

    // Store normalized light vector
    float4 light = normalize( lightVector );

    // Calculate half angle vector
    float4 eye = eyePosition;
    float4 half = normalize( light + eye );
    
    // Calculate diffuse component
    float diffuse = dot( normal, light );

    // Calculate specular component
    float specular = dot( normal, half );
    specular = pow( specular, 32 );

    // A little ambient color
    float4 ambientColor = float4(0,0,0, 1.0 );

    // White specular
    float4 specularMaterial = float4( .9,.9,.9, 1.0 );

    // Combine diffuse and specular contributions and output final vertex color
    OUT.color0 = tex2D(testTexture, IN.texcoord0 );
    //OUT.color0 = diffuse * IN.color0 + specular * specularMaterial + ambientColor ;

    return OUT;
}


Share this post


Link to post
Share on other sites
Sure, this is the shader initialization code:

void initShader_cg(void)
{
if( cgGLIsProfileSupported(CG_PROFILE_ARBFP1) )
g_CGprofile_pixel = CG_PROFILE_ARBFP1;
else if( cgGLIsProfileSupported(CG_PROFILE_FP30) )
g_CGprofile_pixel = CG_PROFILE_FP30;
else if( cgGLIsProfileSupported(CG_PROFILE_FP20) )
g_CGprofile_pixel = CG_PROFILE_FP20;
else
{
MessageBox( NULL,"Failed to initialize pixel shader! Hardware doesn't "
"support any of the pixel shading extensions!",
"ERROR",MB_OK|MB_ICONEXCLAMATION );
return;
}

if( cgGLIsProfileSupported(CG_PROFILE_ARBVP1) )
g_CGprofile2 = CG_PROFILE_ARBVP1;
else if( cgGLIsProfileSupported(CG_PROFILE_VP30) )
g_CGprofile2 = CG_PROFILE_VP30;
else if( cgGLIsProfileSupported(CG_PROFILE_VP20) )
g_CGprofile2 = CG_PROFILE_VP20;
else
{
MessageBox( NULL,"Failed to initialize vertex shader! Hardware doesn't "
"support any of the vertex shading extensions!",
"ERROR",MB_OK|MB_ICONEXCLAMATION );
return;
}

// Create the context...
g_CGcontext2 = cgCreateContext();

//
// Create the vertex shader...
//

g_CGprogram2 = cgCreateProgramFromFile( g_CGcontext2,
CG_SOURCE,
"shaders/ogl_cg_lighting.cg",
g_CGprofile2,
NULL,
NULL );

//
// Load the program using Cg's expanded interface...
//

cgGLLoadProgram( g_CGprogram2 );

//
// Bind some parameters by name so we can set them later...
//

g_CGparam_modelViewProj2 = cgGetNamedParameter( g_CGprogram2, "modelViewProjection" );
g_CGparam_modelViewInverse2 = cgGetNamedParameter( g_CGprogram2, "modelViewInverse" );
g_CGparam_eyePosition2 = cgGetNamedParameter( g_CGprogram2, "eyePosition" );
g_CGparam_lightVector2 = cgGetNamedParameter( g_CGprogram2, "lightVector");
}



In the display function, i have the following code:


// This matrix will be used to tansform the vertices from model-space to clip-space
cgGLSetStateMatrixParameter( g_CGparam_modelViewProj2,
CG_GL_MODELVIEW_PROJECTION_MATRIX,
CG_GL_MATRIX_IDENTITY );

// This matrix will be used to transform the normals from model-space to view-space
cgGLSetStateMatrixParameter( g_CGparam_modelViewInverse2,
CG_GL_MODELVIEW_MATRIX,
CG_GL_MATRIX_INVERSE_TRANSPOSE );

fLightVector[0] = 0;
fLightVector[1] = 1270*cos(camerax_pov*M_PI/180.0f);
fLightVector[2] = 560;

// Normalize light vector
float fLength = sqrtf( fLightVector[0]*fLightVector[0] +
fLightVector[1]*fLightVector[1] +
fLightVector[2]*fLightVector[2] );
fLightVector[0] /= fLength;
fLightVector[1] /= fLength;
fLightVector[2] /= fLength;

cgGLSetParameter4fv( g_CGparam_eyePosition2, fEyePosition );
cgGLSetParameter4fv( g_CGparam_lightVector2, fLightVector );
cgGLBindProgram( g_CGprogram2 );
cgGLEnableProfile( g_CGprofile2 );

RenderObjects();

cgGLDisableProfile( g_CGprofile2 );

Share this post


Link to post
Share on other sites
I think i'm doing something wrong specifying texture coordinates.
I think the error should be at the following line:

OUT.color0 = tex2D(testTexture, IN.texcoord0 );

I'm not certain if texcoord0 is properly initialized and contains texture coordinates ...

Share this post


Link to post
Share on other sites
Hi...

I think the problem is that you are inserting fragment operations in a vertex program.

From you input (struct vertex) i can say this is a vp because you are using the NORMAL semantic. And your code snippet confirms that. g_CGprofile2 is a vp profile.

Doing a tex2D inside a vp is only possible in VP40 profile. If your card support vertex texture fetch (VTF) trying compiling your shader with VP40 profile. But i don't think that this is a good solution. Textures in vertex programs have no filtering applied. So i suggest you move the tex2D to a fragment program and use that with the vp you have.

HellRaiZer

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this