⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transitiontable.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
📖 第 1 页 / 共 4 页
字号:

#ifdef DEBUG_BOARD_STATE
	// This isn't quite true, but it's necessary for other error checking to work
	BoardState().m_AlphaBlendEnable = bEnable;
	BoardState().m_SrcBlend = srcBlend;
	BoardState().m_DestBlend = destBlend;
#endif
}

void ApplySeparateAlphaBlend( const ShadowState_t& state, int stage )
{
	g_pTransitionTable->ApplySeparateAlphaBlend( state.m_SeparateAlphaBlendEnable,
		state.m_SrcBlendAlpha, state.m_DestBlendAlpha );
}

void CTransitionTable::ApplySeparateAlphaBlend( bool bEnable, D3DBLEND srcBlendAlpha, D3DBLEND destBlendAlpha )
{
	if (!bEnable)
	{
		if (m_CurrentState.m_SeparateAlphaBlendEnable)
		{
			SetRenderState( D3DRS_SEPARATEALPHABLENDENABLE, bEnable );
			m_CurrentState.m_SeparateAlphaBlendEnable = bEnable;
		}
	}
	else
	{
		if (!m_CurrentState.m_SeparateAlphaBlendEnable)
		{
			SetRenderState( D3DRS_SEPARATEALPHABLENDENABLE, bEnable );
			m_CurrentState.m_SeparateAlphaBlendEnable = bEnable;
		}

		// Set the blend state here...
		if (m_CurrentState.m_SrcBlendAlpha != srcBlendAlpha)
		{
			SetRenderState( D3DRS_SRCBLENDALPHA, srcBlendAlpha );
			m_CurrentState.m_SrcBlendAlpha = srcBlendAlpha;
		}

		if (m_CurrentState.m_DestBlendAlpha != destBlendAlpha)
		{
			SetRenderState( D3DRS_DESTBLENDALPHA, destBlendAlpha );
			m_CurrentState.m_DestBlendAlpha = destBlendAlpha;
		}
	}

#ifdef DEBUG_BOARD_STATE
	// This isn't quite true, but it's necessary for other error checking to work
	BoardState().m_SeparateAlphaBlendEnable = bEnable;
	BoardState().m_SrcBlendAlpha = srcBlendAlpha;
	BoardState().m_DestBlendAlpha = destBlendAlpha;
#endif
}

//-----------------------------------------------------------------------------
// Applies alpha texture op
//-----------------------------------------------------------------------------
void ApplyColorTextureStage( const ShadowState_t& state, int stage )
{
	g_pTransitionTable->ApplyColorTextureStage( stage, state.m_TextureStage[stage].m_ColorOp,
		state.m_TextureStage[stage].m_ColorArg1, state.m_TextureStage[stage].m_ColorArg2 );
}

void ApplyAlphaTextureStage( const ShadowState_t& state, int stage )
{
	g_pTransitionTable->ApplyAlphaTextureStage( stage, state.m_TextureStage[stage].m_AlphaOp,
		state.m_TextureStage[stage].m_AlphaArg1, state.m_TextureStage[stage].m_AlphaArg2 );
}

void CTransitionTable::ApplyColorTextureStage( int stage, D3DTEXTUREOP op, int arg1, int arg2 )
{
	if (m_CurrentState.m_TextureStage[stage].m_ColorOp != op)
	{
		SetTextureStageState( stage, D3DTSS_COLOROP, op );
		m_CurrentState.m_TextureStage[stage].m_ColorOp = op;
	}

	if (op != D3DTOP_DISABLE)
	{
		if (m_CurrentState.m_TextureStage[stage].m_ColorArg1 != arg1)
		{
			SetTextureStageState( stage, D3DTSS_COLORARG1, arg1 );
			m_CurrentState.m_TextureStage[stage].m_ColorArg1 = arg1;
		}
		if (m_CurrentState.m_TextureStage[stage].m_ColorArg2 != arg2)
		{
			SetTextureStageState( stage, D3DTSS_COLORARG2, arg2 );
			m_CurrentState.m_TextureStage[stage].m_ColorArg2 = arg2;
		}
	}

#ifdef DEBUG_BOARD_STATE
	// This isn't quite true, but it's necessary for other error checking to work
	BoardState().m_TextureStage[stage].m_ColorOp = op;
	BoardState().m_TextureStage[stage].m_ColorArg1 = arg1;
	BoardState().m_TextureStage[stage].m_ColorArg2 = arg2;
#endif
}

void CTransitionTable::ApplyAlphaTextureStage( int stage, D3DTEXTUREOP op, int arg1, int arg2 )
{
	if (m_CurrentState.m_TextureStage[stage].m_AlphaOp != op)
	{
		SetTextureStageState( stage, D3DTSS_ALPHAOP, op );
		m_CurrentState.m_TextureStage[stage].m_AlphaOp = op;
	}

	if (op != D3DTOP_DISABLE)
	{
		if (m_CurrentState.m_TextureStage[stage].m_AlphaArg1 != arg1)
		{
			SetTextureStageState( stage, D3DTSS_ALPHAARG1, arg1 );
			m_CurrentState.m_TextureStage[stage].m_AlphaArg1 = arg1;
		}
		if (m_CurrentState.m_TextureStage[stage].m_AlphaArg2 != arg2)
		{
			SetTextureStageState( stage, D3DTSS_ALPHAARG2, arg2 );
			m_CurrentState.m_TextureStage[stage].m_AlphaArg2 = arg2;
		}
	}

#ifdef DEBUG_BOARD_STATE
	// This isn't quite true, but it's necessary for other error checking to work
	BoardState().m_TextureStage[stage].m_AlphaOp = op;
	BoardState().m_TextureStage[stage].m_AlphaArg1 = arg1;
	BoardState().m_TextureStage[stage].m_AlphaArg2 = arg2;
#endif
}


//-----------------------------------------------------------------------------
// NOTE: The following state setting methods are used only by state blocks
// instead of some of the transitions listed above
//-----------------------------------------------------------------------------
APPLY_RENDER_STATE_FUNC( D3DRS_ALPHABLENDENABLE,	AlphaBlendEnable )
APPLY_RENDER_STATE_FUNC( D3DRS_SRCBLEND,			SrcBlend )
APPLY_RENDER_STATE_FUNC( D3DRS_DESTBLEND,			DestBlend )

APPLY_TEXTURE_STAGE_STATE_FUNC( D3DTSS_COLOROP,		ColorOp )
APPLY_TEXTURE_STAGE_STATE_FUNC( D3DTSS_COLORARG1,	ColorArg1 )
APPLY_TEXTURE_STAGE_STATE_FUNC( D3DTSS_COLORARG2,	ColorArg2 )
APPLY_TEXTURE_STAGE_STATE_FUNC( D3DTSS_ALPHAOP,		AlphaOp )
APPLY_TEXTURE_STAGE_STATE_FUNC( D3DTSS_ALPHAARG1,	AlphaArg1 )
APPLY_TEXTURE_STAGE_STATE_FUNC( D3DTSS_ALPHAARG2,	AlphaArg2 )


//-----------------------------------------------------------------------------
// All transitions below this point depend on dynamic render state
// FIXME: Eliminate these virtual calls?
//-----------------------------------------------------------------------------
void ApplyZBias( const ShadowState_t& shadowState, int arg )
{
	ShaderAPI()->ApplyZBias( shadowState );

#ifdef DEBUG_BOARD_STATE
	BoardState().m_ZBias = shadowState.m_ZBias;
#endif
}

void ApplyTextureEnable( const ShadowState_t& state, int stage )
{														
	ShaderAPI()->ApplyTextureEnable( state, stage );

#ifdef DEBUG_BOARD_STATE
	BoardState().m_TextureStage[stage].m_TextureEnable = state.m_TextureStage[stage].m_TextureEnable;
#endif
}

void ApplyCullEnable( const ShadowState_t& state, int arg )
{
	ShaderAPI()->ApplyCullEnable( state.m_CullEnable );

#ifdef DEBUG_BOARD_STATE
	BoardState().m_CullEnable = state.m_CullEnable;
#endif
}

void ApplyVertexBlendEnable( const ShadowState_t& state, int stage )
{
	ShaderAPI()->SetVertexBlendState( state.m_VertexBlendEnable ? -1 : 0 );

#ifdef DEBUG_BOARD_STATE
	BoardState().m_VertexBlendEnable = state.m_VertexBlendEnable;
#endif
}

void ApplyVertexShaderOverbright( const ShadowState_t& state, int stage )
{
	ShaderAPI()->SetVertexShaderConstantC1( state.m_VertexShaderOverbright );

#ifdef DEBUG_BOARD_STATE
	BoardState().m_VertexShaderOverbright = state.m_VertexShaderOverbright;
#endif
}


//-----------------------------------------------------------------------------
// Creates an entry in the state transition table
//-----------------------------------------------------------------------------
inline void CTransitionTable::AddTransition( ApplyStateFunc_t func, int arg )
{
	int elem = m_TransitionOps.AddToTail();
	m_TransitionOps[elem].m_Op = func;
	m_TransitionOps[elem].m_Argument = arg;
}

#define ADD_RENDER_STATE_TRANSITION( _state )				\
	if (bForce || (toState.m_ ## _state != fromState.m_ ## _state))	\
	{														\
		AddTransition(::Apply ## _state, 0 );				\
		++numOps;											\
	}

#define ADD_TEXTURE_STAGE_STATE_TRANSITION( _stage, _state )\
	if (bForce || (toState.m_TextureStage[_stage].m_ ## _state != fromState.m_TextureStage[_stage].m_ ## _state))	\
	{														\
		Assert( _stage < NUM_TEXTURE_STAGES );				\
		AddTransition(::Apply ## _state, _stage );			\
		++numOps;											\
	}

int CTransitionTable::CreateStateBlockTransitions( const ShadowState_t& fromState, const ShadowState_t& toState, bool bForce )
{
	unsigned short numOps = 0;

	ADD_RENDER_STATE_TRANSITION( AlphaBlendEnable )
	ADD_RENDER_STATE_TRANSITION( SrcBlend )
	ADD_RENDER_STATE_TRANSITION( DestBlend )

	int nStageCount = HardwareConfig()->GetNumTextureStages();
	int i;
	for ( i = 0; i < nStageCount; ++i )
	{
		ADD_TEXTURE_STAGE_STATE_TRANSITION( i, TexCoordIndex );

		ADD_TEXTURE_STAGE_STATE_TRANSITION( i, ColorOp );
		ADD_TEXTURE_STAGE_STATE_TRANSITION( i, ColorArg1 );
		ADD_TEXTURE_STAGE_STATE_TRANSITION( i, ColorArg2 );

		ADD_TEXTURE_STAGE_STATE_TRANSITION( i, AlphaOp );
		ADD_TEXTURE_STAGE_STATE_TRANSITION( i, AlphaArg1 );
		ADD_TEXTURE_STAGE_STATE_TRANSITION( i, AlphaArg2 );
	}

	return numOps;
}

int CTransitionTable::CreateNormalTransitions( const ShadowState_t& fromState, const ShadowState_t& toState, bool bForce )
{
	int numOps = 0;

	// Special case for alpha blending to eliminate extra transitions
	bool blendEnableDifferent = (toState.m_AlphaBlendEnable != fromState.m_AlphaBlendEnable);
	bool srcBlendDifferent = toState.m_AlphaBlendEnable && (toState.m_SrcBlend != fromState.m_SrcBlend);
	bool destBlendDifferent = toState.m_AlphaBlendEnable && (toState.m_DestBlend != fromState.m_DestBlend);
	if (bForce || blendEnableDifferent || srcBlendDifferent || destBlendDifferent)
	{
		AddTransition( ::ApplyAlphaBlend, 0 );
		++numOps;
	}

	bool blendSeparateAlphaEnableDifferent = (toState.m_SeparateAlphaBlendEnable != fromState.m_SeparateAlphaBlendEnable);
	bool srcBlendAlphaDifferent = toState.m_SeparateAlphaBlendEnable && (toState.m_SrcBlendAlpha != fromState.m_SrcBlendAlpha);
	bool destBlendAlphaDifferent = toState.m_SeparateAlphaBlendEnable && (toState.m_DestBlendAlpha != fromState.m_DestBlendAlpha);
	if (bForce || blendSeparateAlphaEnableDifferent || srcBlendAlphaDifferent || destBlendAlphaDifferent)
	{
		AddTransition( ::ApplySeparateAlphaBlend, 0 );
		++numOps;
	}

	int nStageCount = HardwareConfig()->GetNumTextureStages();
	int i;
	for ( i = 0; i < nStageCount; ++i )
	{
		// Special case for texture stage ops to eliminate extra transitions
		const TextureStageShadowState_t& fromTexture = fromState.m_TextureStage[i];
		const TextureStageShadowState_t& toTexture = toState.m_TextureStage[i];

		bool fromEnabled = (fromTexture.m_ColorOp != D3DTOP_DISABLE);
		bool toEnabled = (toTexture.m_ColorOp != D3DTOP_DISABLE);
		if (fromEnabled || toEnabled || bForce)
		{
			bool opDifferent = (toTexture.m_ColorOp != fromTexture.m_ColorOp);
			bool arg1Different = (toTexture.m_ColorArg1 != fromTexture.m_ColorArg1);
			bool arg2Different = (toTexture.m_ColorArg2 != fromTexture.m_ColorArg2);
			if (opDifferent || arg1Different || arg2Different || bForce)
			{
				AddTransition( ::ApplyColorTextureStage, i );
				++numOps;
			}
		}

		fromEnabled = (fromTexture.m_AlphaOp != D3DTOP_DISABLE);
		toEnabled = (toTexture.m_AlphaOp != D3DTOP_DISABLE);
		if (fromEnabled || toEnabled || bForce)
		{
			bool opDifferent = (toTexture.m_AlphaOp != fromTexture.m_AlphaOp);
			bool arg1Different = (toTexture.m_AlphaArg1 != fromTexture.m_AlphaArg1);
			bool arg2Different = (toTexture.m_AlphaArg2 != fromTexture.m_AlphaArg2);
			if (opDifferent || arg1Different || arg2Different || bForce)
			{
				AddTransition( ::ApplyAlphaTextureStage, i );
				++numOps;
			}
		}

		ADD_TEXTURE_STAGE_STATE_TRANSITION( i, TexCoordIndex );
	}

	return numOps;
}

void CTransitionTable::CreateTransitionTableEntry( int to, int from )
{
	// If from < 0, that means add *all* transitions into it.

	unsigned short firstElem = m_TransitionOps.Count();
	unsigned short numOps = 0;

	const ShadowState_t& toState = m_ShadowStateList[to];
	const ShadowState_t& fromState = (from >= 0) ? m_ShadowStateList[from] : m_ShadowStateList[to];
	bool bForce = (from < 0);

	ADD_RENDER_STATE_TRANSITION( ZFunc )
	ADD_RENDER_STATE_TRANSITION( ZWriteEnable )
	ADD_RENDER_STATE_TRANSITION( ZEnable )
	ADD_RENDER_STATE_TRANSITION( ColorWriteEnable )
	ADD_RENDER_STATE_TRANSITION( AlphaTestEnable )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -