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

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
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 »

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
Posts: 47
Joined: Wed Aug 23, 2006 1:38 pm
Location: Prostejov, Czech Republic
Contact:

Post by Kalda »

Yes. It's true... I have same problem. Please move this post to Bug Reports please.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

You might be sending it to the wrong shader. Or, it might be that your shader code doesn't use fTime, or the use of fTime doesn't affect the output, so fTime is removed in the optimization stage of the shader compilation, and thus there is no longer any variable called fTime.
If you don't have anything nice to say, don't say anything at all.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Yes what luben said. If the shader does not actually use fTime in any equations e.g "OUT.Position = sin(fTime);" then it will not include it as part of the shader even if it is defined and will create that error. Also I have noticed if you have console enabled that error comes up every frame and slows it down, so if you dont need fTime remove it from the shader callback.
Post Reply