Pass Variable to Shader in HLSL (like fTime from Docs)

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
yv
Posts: 34
Joined: Sun Aug 20, 2006 2:52 pm

Pass Variable to Shader in HLSL (like fTime from Docs)

Post by yv »

yv wrote:I would like to pass a few variable to the vertex and pixel shaders within Irrlicht. When I use GLSL shaders, I can pass variables just fine.

When I use HLSL shaders the simplest example from docs fails to compile.

Can someone shed some light on what is going on?

See me trying to pass fTime to the shader below.

Code: Select all


	virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
	{
		video::IVideoDriver* driver = services->getVideoDriver();

		// set inverted world matrix
		// if we are using highlevel shaders (the user can select this when
		// starting the program), we must set the constants by name.

		core::matrix4 invWorld = driver->getTransform(video::ETS_WORLD);
		invWorld.makeInverse();

		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mInvWorld", &invWorld.M[0], 16);

		// set clip matrix

		core::matrix4 worldViewProj;
		worldViewProj = driver->getTransform(video::ETS_PROJECTION);			
		worldViewProj *= driver->getTransform(video::ETS_VIEW);
		worldViewProj *= driver->getTransform(video::ETS_WORLD);

		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mWorldViewProj", &worldViewProj.M[0], 16);

		// set camera position
		core::vector3df pos = device->getSceneManager()->getActiveCamera()->getAbsolutePosition();
		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mLightPos", reinterpret_cast<f32*>(&pos), 3);
	
		// set light color 
		video::SColorf col(1.0f,0.0f,0.0f,0.0f);
		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mLightColor", reinterpret_cast<f32*>(&col), 4);

		// set transposed world matrix
		core::matrix4 world = driver->getTransform(video::ETS_WORLD);
		world = world.getTransposed();
		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mTransWorld", &world.M[0], 16);
		if (UseHighLevelShaders)
		{
			f32 time = (f32)device->getTimer()->getTime()/100000.0f;
			services->setVertexShaderConstant("fTime", &time, 1);
		}   


Here is the declaration of the variable inside the hlsl file:

Code: Select all

...
float fTime;
...


Here is the repeating error I get on the console:

HLSL Variable to set not found: 'fTime'. Available variables are:
'mInvWorld' Registers:[begin:8, count:3]
'mLightColor' Registers:[begin:12, count:1]
'mLightPos' Registers:[begin:11, count:1]
'mTransWorld' Registers:[begin:4, count:4]
'mWorldViewProj' Registers:[begin:0, count:4]


Please help!
Kalda wrote:Yes. It's true... I have same problem. Please move this post to Bug Reports please.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

This is not a bug. The error comes up if the variable ftime is not used at all in the shader file. (EVEN IF IT IS DEFINED!)

Can you post your shader file for inspection?
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Last edited by Spintz on Mon Dec 10, 2007 2:00 pm, edited 2 times in total.
yv
Posts: 34
Joined: Sun Aug 20, 2006 2:52 pm

Variables are not only initialized but used in an assignment

Post by yv »

Here is my code including OnSetConstants, Vertex and Pixel Shader file:

Code: Select all


virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
	{
		video::IVideoDriver* driver = services->getVideoDriver();

		// set inverted world matrix
		// if we are using highlevel shaders (the user can select this when
		// starting the program), we must set the constants by name.

		core::matrix4 invWorld = driver->getTransform(video::ETS_WORLD);
		invWorld.makeInverse();

		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mInvWorld", &invWorld.M[0], 16);

		// set clip matrix

		core::matrix4 worldViewProj;
		worldViewProj = driver->getTransform(video::ETS_PROJECTION);			
		worldViewProj *= driver->getTransform(video::ETS_VIEW);
		worldViewProj *= driver->getTransform(video::ETS_WORLD);

		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mWorldViewProj", &worldViewProj.M[0], 16);

		// set camera position
		core::vector3df pos = device->getSceneManager()->getActiveCamera()->getAbsolutePosition();
		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mLightPos", reinterpret_cast<f32*>(&pos), 3);
	
		// set light color 
		video::SColorf col(1.0f,0.0f,0.0f,0.0f);
		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mLightColor", reinterpret_cast<f32*>(&col), 4);

		// set transposed world matrix
		core::matrix4 world = driver->getTransform(video::ETS_WORLD);
		world = world.getTransposed();
		if (UseHighLevelShaders)
			services->setVertexShaderConstant("mTransWorld", &world.M[0], 16);
		if (UseHighLevelShaders)
		{
			f32 time = (f32)device->getTimer()->getTime()/100000.0f;
			services->setPixelShaderConstant("fTime", &time, 1);


			f32 mSlices = ViewAlignedCubeSlices;
			services->setPixelShaderConstant("mSlices", &mSlices, 1);
			//services->setPixelShaderConstant("fTime", &time, 1);

			f32 noiseScale = 0.2;
			services->setVertexShaderConstant("noiseScale", &noiseScale, 1);


		}   
Vertex/Pixel shader file where noiseScale, fTime, mSlices are used:

Code: Select all



//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
float4x4 mInvWorld;       // Inverted world matrix
float4x4 mWorldViewProj;  // World * View * Projection transformation
float4x4 mTransWorld;     // Transposed world matrix
float3 mLightPos;         // Light position
float4 mLightColor;       // Light color
float noiseScale;

// Vertex shader output structure
struct VS_OUTPUT
{
	float4 Position   : POSITION;   // vertex position 
	float4 Diffuse    : COLOR0;     // vertex diffuse color
	float2 TexCoord0  : TEXCOORD0;  // tex coords
	float2 TexCoord1  : TEXCOORD1;  // tex coords

};


VS_OUTPUT vertexMain( in float4 vPosition : POSITION,
                      in float3 vNormal   : NORMAL,
                      in float2 texCoord0    : TEXCOORD0,
		      in float2 texCoord1    : TEXCOORD1
		    )
{
	VS_OUTPUT Output;

	// transform position to clip space 
	Output.Position = mul(vPosition, mWorldViewProj) * noiseScale;
	
	// transform normal 
	float3 normal = mul(vNormal, mInvWorld);
	
	// renormalize normal 
	normal = normalize(normal);
	
	// position in world coodinates
	float3 worldpos = mul(mTransWorld, vPosition);
	
	// calculate light vector, vtxpos - lightpos
	float3 lightVector = worldpos - mLightPos;
	
	// normalize light vector 
	lightVector = normalize(lightVector);
	
	// calculate light color 
	float3 tmp = dot(-lightVector, normal);
	tmp = lit(tmp.x, tmp.y, 1.0);
	tmp = mLightColor * tmp.y;

	Output.Diffuse = float4(tmp.x, tmp.y, tmp.z, 0);

	//Output.Diffuse = float4(0, 0, 0, 0);
	Output.TexCoord0 = texCoord0;
	Output.TexCoord1 = texCoord1;
	
	return Output;
}


sampler2D tex0;
float fTime;
float mSlices;


// Pixel shader output structure
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};



	
PS_OUTPUT pixelMain( float4 Position : POSITION,
                     float4 Diffuse  : COLOR0,
		     float2 TexCoord0 : TEXCOORD0,
                     float2 TexCoord1 : TEXCOORD1
		   ) 
{ 
	PS_OUTPUT Output;

        float2 xy = float2(TexCoord0);
	float z = float(TexCoord1.x);	  
        float s = float(sqrt(xy.x*xy.x+z*z));
	float2 texture_xy = float2(s,xy.y);
 

	float4 col = tex2D( tex0, texture_xy );

	float slices = 5.0 - mSlices/15.0+(0.000001*fTime);
        Output.RGBColor = float4(col.rgb*slices,col.a);

	return Output;
}
                                  
and here is the errors from the console:

HLSL Variable to set not found: 'noiseScale'. Available variables are:
'mInvWorld' Registers:[begin:8, count:3]
'mLightColor' Registers:[begin:12, count:1]
'mLightPos' Registers:[begin:11, count:1]
'mTransWorld' Registers:[begin:4, count:4]
'mWorldViewProj' Registers:[begin:0, count:4]
HLSL Variable to set not found: 'fTime'. Available variables are:
'tex0' Registers:[begin:0, count:1]
HLSL Variable to set not found: 'mSlices'. Available variables are:
'tex0' Registers:[begin:0, count:1]
yv
Posts: 34
Joined: Sun Aug 20, 2006 2:52 pm

Variables are USED in shader code, but still getting errors

Post by yv »

Provided shader code for inspection, please take a look.
yv
Posts: 34
Joined: Sun Aug 20, 2006 2:52 pm

Issue is solved!

Post by yv »

I have been meaning to upgrade for awhile and today got around to upgrading to 1.31. This issue has gone away! Thank you, team!
Post Reply