# [CgFX] cgSetStateCallbacks() messes up my textures

## Recommended Posts

Greetings everyone! First of all, I have already posted this on the nvidia forums, but people seem to not want to answer / cannot answer etc. So I want to try my luck here.. :) --- I am creating a state manager for CgFX. I am caching (registering) the states in an std::map like so:
	// cache states
CGstate state = cgGetFirstState(mContext);
while(state)
{
// get state name
String stateName = cgGetStateName(state);

// create effect state -- this is a custom struct that wraps some data
EffectState* effectState = new EffectState();
effectState->CRC = 0;
effectState->Name = stateName;
effectState->Type = cgGetStateType(state);

// save state
mStates[state] = effectState;

// setup callback functions -- CALLING THIS FUNCTION MESSES UP MY TEXTURES!
cgSetStateCallbacks(state, Effect::stateSet, Effect::stateReset, Effect::stateValidate);

// next!
state = cgGetNextState(state);
}

Note how I call cgSetStateCallbacks(). If I don't do that, everything works fine. If I do that, however, my textures don't get set anymore. This doesn't work anymore:
		cgD3D9SetTextureParameter(aParameter, d3d9Texture);
cgSetSamplerState(aParameter);

And this is the texture/sampler variable in the shader:
uniform sampler2D gDiffuseMap : DIFFUSEMAP = sampler_state
{
WrapS 		= Repeat;
WrapT 		= Repeat;
};

So in conclusion: I call cgSetStateCallbacks() turns my objects black. Why is this?

##### Share on other sites
shameless bamp :( cgfx make programmer cry

##### Share on other sites
Doesn't anyone here know how to properly use cgSetStateCallbacks() ?

##### Share on other sites
can you show us the source of your callback function that sets the texture?

##### Share on other sites
Of course. What I do is I check the value from the state assignment with the cached value. If they don't match, I set the state assignment value to the cache and then return true, if they match, I return false.

CGbool Effect::stateSet( CGstateassignment aState ){	EffectState* effectState = mStates[cgGetStateAssignmentState(aState)];	ASSERTFATAL(!effectState, "Trying to set an unregistered state");	switch(effectState->Type)	{	case CG_PROGRAM_TYPE:		{			CGprogram program = cgGetProgramStateAssignmentValue(aState);			CRChash crc = (CRChash)program;			if(crc != effectState->CRC)			{				effectState->CRC = crc;				return CG_TRUE;			}			else			{				return CG_FALSE;			}		}		break;	case CG_BOOL:		{			int nvals;			CGbool val = *cgGetBoolStateAssignmentValues(aState, &nvals);			if(val != effectState->CRC)			{				effectState->CRC = val;				return CG_TRUE;			}			else			{				return CG_FALSE;			}		}		break;	default:		{			ASSERTFATAL(true, "Invalid state type");		}		break;	}}

##### Share on other sites
I did some further checking, and I found that the following states are causing a huge slowdown: FragmentProgram and VertexProgram. If I create states for everything but those two, everything slows down to 3 fps in debug mode. If I create state callbacks for those two states alone, everything goes up to 28 fps in debug.

Now, the thing is, that its so fast because the states aren't being set properly. Adding state callbacks for boolean values, results in the boolean values not being set properly... no wonder things speed up when not setting them properly.

I tried calling the cgSetProgramStateAssignmentValue() function, but that doesn't quite work either... :(

So what is the proper way of handling state assignments?

[Edited by - wiegje85 on April 30, 2010 5:49:40 PM]

##### Share on other sites
Another, desperate, shameless bamp at the discrete interval of roughly 24 hours.