CG Shader throws exception

Problems building or running the engine, queries about how to use features etc.
Post Reply
delorian0815
Gnoblar
Posts: 3
Joined: Thu Jan 24, 2008 1:52 pm

CG Shader throws exception

Post by delorian0815 »

Hi,
I have a problem with a simple normal mapping shader. It is written in cg and it works fine with OpenGL. But when I start with DirectX the program throws an exception.

Here's the material:

Code: Select all

vertex_program testVP cg
{
	source testVP.cg
	entry_point main_vp
	profiles vp_1_1 arbvp1
	
	default_params
	{
		param_named_auto lightPos light_direction_object_space 0
		param_named_auto ModelViewProj worldviewproj_matrix
	}
}

fragment_program testFP cg
{
	source testFP.cg
    	entry_point main_fp
    	profiles ps_2_0 arbfp1

    	default_params
    	{
		param_named_auto LightDiffuseColor light_diffuse_colour 0
	}
}

material ArrowTexture
{
	technique Default
	{
		pass Main
		{
			lighting on
			vertex_program_ref testVP
			{
			}

			fragment_program_ref testFP
			{
			}
			
			texture_unit
			{
				texture unwrap_helper.jpg
			}
			texture_unit
			{
				texture ArrowNormal.png
			}
		}
	}
}
and here is the vertex and the fragment program:

Code: Select all

void main_vp(	in float4 position : POSITION,
			in float2 texCoords : TEXCOORD0,
			in float3 Tangent : TANGENT0,
			in float3 Normal : NORMAL,

			out float4 positionOUT : POSITION,
			out float2 texCoordsOUT : TEXCOORD0,
			out float2 normalCoordsOUT : TEXCOORD1,
			out float3 LightVector : TEXCOORD2,

			const uniform float4x4 modelViewProjMatrix,
			const uniform float3 LightPosition)
{
	LightVector = -LightPosition;
	float3 Binormal = cross(Tangent, Normal);
	float3x3 TBNMatrix = float3x3(Tangent, Binormal, Normal);
	positionOUT = mul(modelViewProjMatrix, position); // <--- This causes the Exception
	texCoordsOUT = texCoords;
	normalCoordsOUT = texCoords;
	LightVector = normalize(mul(TBNMatrix, LightVector));
}
There seems to be something wrong with writing into POSITION.

Code: Select all

void main_fp(	in float4 colorIN : COLOR0,
			in float2 texCoords : TEXCOORD0,
			in float2 normalCoords : TEXCOORD1,
			in float3 LightVector : TEXCOORD2,

			out float4 colorOUT : COLOR0,
		
			uniform sampler2D baseTexture : TEXUNIT0,
			uniform sampler2D normalTexture : TEXUNIT1,

			uniform float3 LightDiffuseColor)
{

	float3 Normal = 2.0f * (tex2D(normalTexture, normalCoords).rgb - 0.5f);

	float3 temp = LightDiffuseColor * tex2D(baseTexture, texCoords).rgb * saturate(dot(Normal, LightVector));
	colorOUT = float4(temp, 1);
}
and don't forget the exception:

Code: Select all

OGRE EXCEPTION(3:RenderingAPIException): Failed to DrawPrimitive : An undetermined error occurred in D3D9RenderSystem::_render at ..\src\OgreD3D9RenderSystem.cpp (line 2719)

I appreciate all help.
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19265
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

Never seen that one before. What shader profiles are you using? If in doubt, grab the DirectX Debug Runtime and enable it, that will give you more detail in the VC 'output' window.
delorian0815
Gnoblar
Posts: 3
Joined: Thu Jan 24, 2008 1:52 pm

Post by delorian0815 »

I'm using vp_1_1 and ps_2_0.

After turning the DirectX debug information to full (at least I think I did it), all that's shown in addition is that:

Direct3D9: (ERROR) :Stage 1 - Texture coordinate index in the stage must be equal to the stage index when programmable vertex pipeline is used
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: CG Shader throws exception

Post by xavier »

delorian0815 wrote: profiles vp_1_1 arbvp1
Shouldn't that be vs_1_1?
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
delorian0815
Gnoblar
Posts: 3
Joined: Thu Jan 24, 2008 1:52 pm

Post by delorian0815 »

You're right. But I corrected this a while ago. This wasn't the problem.

But I actually solved my problem.
To transfer the LightVector from the vertex-program to the fragment-program I used the textur coordinates.

Code: Select all

out float3 LightVector : TEXCOORD2
Now I use the color value to transfer the LightVector and the exception is gone. Of course I have to limit the values from 0 to 1 and rebuild them inside the fragment program.

Thanks for your help
Delorian
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 42
Contact:

Post by Oogst »

Hmm, I think something else is wrong somewhere. Either you stumbled on a bug in something else, or something by you, but you should be able to use TEXCOORD2 in shader model 1.1. Do note that TEXCOORDs have limited range in shader model 1.1, though.
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19265
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

In SM1 there has to be a 1:1 relationship between bound texture units and texture coordinates, you can't for example use texture coord set 1 to access texture unit 2. It's probably related to this. SM2 introduced mix-and-match to UVs and texture units.
Post Reply