Setting Matrices for Shader doesn't work

Started by
11 comments, last by m4gnus 18 years ago
Hi, recently i discovered that was loading an effect-file with a vertexshader but not even used it. I still set the matrices with device->SetTransform(D3DTS_WORLD/VIEW/PROJ.,&matrix); so i just changed that to effect->SetMatrix("matWorld"/../..,&matrix) but now my vertices are not transformed at all :( . Here's what my fx file looks like:


string Position_Pass_0_Model : ModelData = "C:/Program Files/ATI Research Inc/RenderMonkey 1.6/Examples/Media/Models/Sphere.3ds";

float4x4 matView : ViewProjection;
float4x4 matProjection : ViewProjection;
float4x4 matWorld : World;

struct VS_INPUT 
{
   float4 Position : POSITION0;
   float3 Normal   : NORMAL0;
   
};

struct VS_OUTPUT 
{
   float4 Position : POSITION0;
   float3 normal : TEXCOORD0;
   
};

VS_OUTPUT Position_Pass_0_Vertex_Shader_vs_main( VS_INPUT Input )
{
   VS_OUTPUT Output;
   float4x4 matViewProjection=mul(matView,matProjection);
   float4 pos = mul( matWorld, Input.Position );
   Output.Position = mul( matViewProjection, pos );
   float4 n=mul( matWorld, float4(Input.Normal, 1.0f) );
   //float4 n2=mul(matViewProjection,n);
   Output.normal=float3(n.x,n.y,n.z);
   
   return( Output );
   
}




float4 Position_Pass_0_Pixel_Shader_ps_main(float3 n : TEXCOORD0) : COLOR0
{   
   float3 l=float3(0.0f, 1.0f, 0.0f);
   return( float4(1.0f,0.0f,0.0f,1.0f)*dot(n,l));
   
}




//--------------------------------------------------------------//
// Technique Section for Position
//--------------------------------------------------------------//
technique Position
{
   pass Pass_0
   {
      PixelShader = compile ps_2_0 Position_Pass_0_Pixel_Shader_ps_main();
   }

}





float4 Position_Pass_0_Pixel_Shader_ps_main(float3 n : TEXCOORD0) : COLOR0
{   
   float3 l=float3(0.0f, 1.0f, 0.0f);
   return( float4(1.0f,0.0f,0.0f,1.0f)*dot(n,l));
   
}




//--------------------------------------------------------------//
// Technique Section for Position
//--------------------------------------------------------------//
technique Position
{
   pass Pass_0
   {
      PixelShader = compile ps_2_0 Position_Pass_0_Pixel_Shader_ps_main();
   }

}



and my rendering loop looks like this:

void ngRenderer::render()
{


	//DO CULLING

	//RENDER BATCHES:
	//Turn on the Z-Test
	ngEngine &engine=ngEngine::getInstance();
	engine.g_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);

	int size=renderPools.size();
	for(vector<ngRenderPool *>::iterator it=renderPools.begin();it!=renderPools.end();++it)
	{
		ngMaterial *mat=(**it).getMaterial();

		unsigned int numPasses=mat->begin();
	/*engine.g_pD3DDevice->SetTransform(D3DTS_WORLD,(**it).getWorldMatrix());
		engine.g_pD3DDevice->SetTransform(D3DTS_VIEW,&engine.getViewMatrix());
		engine.g_pD3DDevice->SetTransform(D3DTS_PROJECTION,&engine.getProjMatrix());*/
		
		effect->SetMatrix("matWorld",(**it).getWorldMatrix());
		effect->SetMatrix("matView",&engine.getViewMatrix());
		effect->SetMatrix("matProjection",&engine.getProjMatrix());


		for(unsigned int uiPass=0;uiPass < numPasses;uiPass++)
		{
			mat->beginPass(uiPass);

			int meshCount;
			CBaseMesh **mesh=(**it).getMeshPointer(&meshCount);
			for(int i=0;i<meshCount;i++)
			{
			ngVertexBuffer *pVBuffer=(*mesh).pVBuff;
			ngIndexBuffer *pIBuffer=(*mesh).pIBuff;
			Draw(pVBuffer ,pIBuffer);
			}
			mat->endPass();
		}
		
		mat->end();
	}


}


Where's the problem with this? Edit: Ok i found the solution for that problem: i forgot to compile the shader in the technique... But now after adding the compilation i only get 1 black quad(of a cube) that deforms funny when i move the camera... regards, m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
Advertisement
Hey bud,

Does the D3D Runtime spit out anything useful? I have found that all my shader errors have been solvable with this information.

Dave
no sadly the debug output says nothing but ignoring redundant renderstate. btw noticed that matView and matProjection had the same semantic and fixed that. the error must be in the shader(because the matrices are right) but i don't know what could be wrong there.

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
that shader doesnt compile for me. you define the same pixelshader twice:
Position_Pass_0_Pixel_Shader_ps_main()

i removed one and it works in fx composer. lit red mesh.
oops i think i did something worng with pasting..mom i'll post my new shader(fixed semantics):

//**************************************************************////  Effect File exported by RenderMonkey 1.6////  - Although many improvements were made to RenderMonkey FX  //    file export, there are still situations that may cause   //    compilation problems once the file is exported, such as  //    occasional naming conflicts for methods, since FX format //    does not support any notions of name spaces. You need to //    try to create workspaces in such a way as to minimize    //    potential naming conflicts on export.                    //    //  - Note that to minimize resulting name collisions in the FX //    file, RenderMonkey will mangle names for passes, shaders  //    and function names as necessary to reduce name conflicts. //**************************************************************////--------------------------------------------------------------//// Position//--------------------------------------------------------------////--------------------------------------------------------------//// Pass 0//--------------------------------------------------------------//string Position_Pass_0_Model : ModelData = "C:/Program Files/ATI Research Inc/RenderMonkey 1.6/Examples/Media/Models/Sphere.3ds";float4x4 matView : matView;float4x4 matProjection : matProjection;float4x4 matWorld : matWorld;struct VS_INPUT {   float4 Position : POSITION0;   float3 Normal   : NORMAL0;   };struct VS_OUTPUT {   float4 Position : POSITION0;   float3 normal : TEXCOORD0;   };VS_OUTPUT Position_Pass_0_Vertex_Shader_vs_main( VS_INPUT Input ){   VS_OUTPUT Output;   float4x4 matViewProjection=mul(matView,matProjection);   float4 pos = mul( matWorld, Input.Position );   Output.Position = mul( matViewProjection, pos );   float4 n=mul( matWorld, float4(Input.Normal, 1.0f) );   //float4 n2=mul(matViewProjection,n);   Output.normal=float3(n.x,n.y,n.z);      return( Output );   }float4 Position_Pass_0_Pixel_Shader_ps_main(float3 n : TEXCOORD0) : COLOR0{      float3 l=float3(0.0f, 1.0f, 0.0f);   return( float4(1.0f,0.0f,0.0f,1.0f)*dot(n,l));   }//--------------------------------------------------------------//// Technique Section for Position//--------------------------------------------------------------//technique Position{   pass Pass_0   {      PixelShader = compile ps_2_0 Position_Pass_0_Pixel_Shader_ps_main();      VertexShader = compile vs_2_0 Position_Pass_0_Vertex_Shader_vs_main();   }}


Hmm if the shader works ok for you then something with setting the matrices is wrong. but i don't see any problem there.

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
do your meshes vertices have the same format at VS_INPUT?
struct VS_INPUT {   float4 Position : POSITION0;   float3 Normal   : NORMAL0;   };


edit: actually, position is usually a float3.
my FVF is:D3DFVF_XYZ|D3DFVF_TEX1|D3DFVF_NORMAL
i changed the shader to:
//**************************************************************////  Effect File exported by RenderMonkey 1.6////  - Although many improvements were made to RenderMonkey FX  //    file export, there are still situations that may cause   //    compilation problems once the file is exported, such as  //    occasional naming conflicts for methods, since FX format //    does not support any notions of name spaces. You need to //    try to create workspaces in such a way as to minimize    //    potential naming conflicts on export.                    //    //  - Note that to minimize resulting name collisions in the FX //    file, RenderMonkey will mangle names for passes, shaders  //    and function names as necessary to reduce name conflicts. //**************************************************************////--------------------------------------------------------------//// Position//--------------------------------------------------------------////--------------------------------------------------------------//// Pass 0//--------------------------------------------------------------//string Position_Pass_0_Model : ModelData = "C:/Program Files/ATI Research Inc/RenderMonkey 1.6/Examples/Media/Models/Sphere.3ds";float4x4 matView : matView;float4x4 matProjection : matProjection;float4x4 matWorld : matWorld;struct VS_INPUT {   float4 Position : POSITION0;   float2 tex1     : TEXCOORD0;   float3 Normal   : NORMAL0;   };struct VS_OUTPUT {   float4 Position : POSITION0;   float3 normal : TEXCOORD1;   };VS_OUTPUT Position_Pass_0_Vertex_Shader_vs_main( VS_INPUT Input ){   VS_OUTPUT Output;   float4x4 matViewProjection=mul(matView,matProjection);   float4 pos = mul( matWorld, Input.Position );   Output.Position = mul( matViewProjection, pos );   float4 n=mul( matWorld, float4(Input.Normal, 1.0f) );   //float4 n2=mul(matViewProjection,n);   Output.normal=float3(n.x,n.y,n.z);      return( Output );   }float4 Position_Pass_0_Pixel_Shader_ps_main(float3 n : TEXCOORD1) : COLOR0{      float3 l=float3(0.0f, 1.0f, 0.0f);   return( float4(1.0f,0.0f,0.0f,1.0f)*dot(n,l));   }//--------------------------------------------------------------//// Technique Section for Position//--------------------------------------------------------------//technique Position{   pass Pass_0   {      PixelShader = compile ps_2_0 Position_Pass_0_Pixel_Shader_ps_main();      VertexShader = compile vs_2_0 Position_Pass_0_Vertex_Shader_vs_main();   }}


but i still get the same result

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
that no longer works in fx composer.
id guess the worldviewproj is calculated incorrectly. but i dont have time to play with it now.
you should calc that in your app anyway, and then pass it in. that way youre only calculating it once per mesh instead of once per vertex.
Quote:
that no longer works in fx composer.


i think that's because i changed the semantics to matView, matProjection, matWorld instead of just View,Projection,..

Quote:
id guess the worldviewproj is calculated incorrectly. but i dont have time to play with it now.
you should calc that in your app anyway, and then pass it in. that way youre only calculating it once per mesh instead of once per vertex.

i also thought about that but i thought maybe one of my shaders needs them seperated. Should i just pass one combined matrix(matViewProjection or even matWorldViewProjection) AND 2(3) seperated matrices(matView,matProjection,matWorld)?

Edit: sending one combined matrix(world*view*proj) doesn't work. I still results in a strange looking quad.


regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
Hi,

First, try to stick to the standar semantics so your code is more compatible with more effects. So please return the original names.

Second. Create a D3DXHandle object and use:
D3DXHANDLE hWorld = effect->GetParameterBySemantic(NULL, 'WORLD');
Be careful its case sensitive.
The idea behind this is to get a handle which is way faster to use than the parameter name.

Finally. Use your handle:
effect->SetMatrix(hWorld, yourMatrix);

Just make sure you call the GetParameterBySemantic only when you load the effect once and save the handle in a global variable or a class attribute for later use. Calling this function each frame will do nothing.
Also, remember that in DX9c you should call CommitChanges if the change is inside the pass. As it says in the docs:

"If the application changes any effect state using any of the Effect::Setx methods inside of a BeginPass/EndPass matching pair, the application must call CommitChanges to set the update the device with the state changes. If no state changes occur within a BeginPass and EndPass matching pair, it is not necessary to call CommitChanges."


Luck!
Guimo


This topic is closed to new replies.

Advertisement