void CD3DSkelMesh::BeginRender(D3DMATRIX* pD3DTransforms, CD3DRenderStyle* pRenderStyle, uint32 iRenderPass)
{

// [dlj] remove this because DX9 doesn't have this bug

	// DX8 has bug with table fog with blended meshes...
//	PD3DDEVICE->GetRenderState(D3DRS_FOGTABLEMODE, &m_nPrevFogTableMode);
//	PD3DDEVICE->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_NONE);
//	PD3DDEVICE->GetRenderState(D3DRS_FOGVERTEXMODE, &m_nPrevFogVertexMode);
//	PD3DDEVICE->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);

	// Do we need to do software vert processing...
	bool bSoftwareProcessing = m_bSWVertProcessing;

	// Check if we need to do software processing...
	switch (m_eRenderMethod)
	{
	case eD3DRenderDirect			:
		if (m_iMaxBonesPerTri > g_Device.GetDeviceCaps()->MaxVertexBlendMatrices)
		{
			bSoftwareProcessing = true;
		}
		break;
	case eD3DRenderMatrixPalettes	:
		// Note: I am multiplying by two because the spec sais, if you're doing normals as well, it's half of the cap sais...
		if ((m_iMaxBone-m_iMinBone)*2+1 > g_Device.GetDeviceCaps()->MaxVertexBlendMatrixIndex)
		{
			bSoftwareProcessing = true;
		}
		break;
	}

	// If not already software vertex processing then set 
	if (!m_bSWVertProcessing && bSoftwareProcessing)
	{
		m_bSWVertProcessing = true;
		FreeDeviceObjects();
		ReCreateObject();
	}


	// If this pass has a vertex shader, use it.
	RSD3DRenderPass *pPass = pRenderStyle->GetRenderPass_D3DOptions(iRenderPass);
	if (NULL != pPass &&
	    pPass->bUseVertexShader &&
		pPass->VertexShaderID != LTVertexShader::VERTEXSHADER_INVALID)
	{
		if ( m_bSWVSBuffers && !m_bSWVertProcessing )
		{
			m_bSWVertProcessing = true;
			FreeDeviceObjects();
			ReCreateObject();
		}

		// Store the pointer to the actual shader during rendering.
		m_pVertexShader = LTVertexShaderMgr::GetSingleton().GetVertexShader(pPass->VertexShaderID);
		if (m_pVertexShader != NULL)
		{
			// Install the shader.
			if (!LTVertexShaderMgr::GetSingleton().InstallVertexShader(m_pVertexShader))
			{
				m_pVertexShader = NULL;
				return;
			}
		}
	}
	else if (!m_VBController.getVertexFormat(0) || m_bNonFixPipeData)
	{

		RSD3DOptions rsD3DOptions;
		pRenderStyle->GetDirect3D_Options(&rsD3DOptions);
		if(!rsD3DOptions.bUseEffectShader)
		{
			return; // This is a non fixed function pipe VB - bail out...
		}

		//return;				// This is a non fixed function pipe VB - bail out...
	}
	else if (FAILED(g_RenderStateMgr.SetVertexShader(m_VBController.getVertexFormat(0))))
	{
		return;
	}

	// If this pass has a pixel shader, use it.
	if (NULL != pPass &&
	    pPass->bUsePixelShader &&
		pPass->PixelShaderID != LTPixelShader::PIXELSHADER_INVALID)
	{
		// Store the pointer to the actual shader during rendering.
		m_pPixelShader = LTPixelShaderMgr::GetSingleton().GetPixelShader(pPass->PixelShaderID);
		if (m_pPixelShader != NULL)
		{
			// Install the shader.
			if (!LTPixelShaderMgr::GetSingleton().InstallPixelShader(m_pPixelShader))
			{
				m_pPixelShader = NULL;
				return;
			}
		}
	}


	// We need software processing 
	if(m_bSWVertProcessing)
	{
		PD3DDEVICE->SetSoftwareVertexProcessing(TRUE);
	}


	m_VBController.SetStreamSources();

	if(m_eRenderMethod == eD3DRenderMatrixPalettes)
	{
		PD3DDEVICE->SetRenderState(D3DRS_INDEXEDVERTEXBLENDENABLE, TRUE);
	}
}
void CD3DVAMesh::Render(ModelInstance *pInstance, D3DMATRIX& WorldTransform, CD3DRenderStyle* pRenderStyle, uint32 iRenderPass)
{
	RSD3DOptions rsD3DOptions;
	pRenderStyle->GetDirect3D_Options(&rsD3DOptions);

	// If this pass has a vertex shader, use it.
	RSD3DRenderPass *pPass = pRenderStyle->GetRenderPass_D3DOptions(iRenderPass);
	if (NULL != pPass &&
	    pPass->bUseVertexShader &&
		pPass->VertexShaderID != LTVertexShader::VERTEXSHADER_INVALID)
	{
		if ( m_bSWVSBuffers && !m_bSWVertProcessing )
		{
			m_bSWVertProcessing = true;
			FreeDeviceObjects();
			ReCreateObject();
		}

		// Store the pointer to the actual shader during rendering.
		m_pVertexShader = LTVertexShaderMgr::GetSingleton().GetVertexShader(pPass->VertexShaderID);
		if (m_pVertexShader != NULL)
		{
			// Install the shader.
			if (!LTVertexShaderMgr::GetSingleton().InstallVertexShader(m_pVertexShader))
			{
				m_pVertexShader = NULL;
				return;
			}
		}
	}
	else if (!rsD3DOptions.bUseEffectShader)		
	{
		if (!m_VBController.getVertexFormat(0) || m_bNonFixPipeData)
		{
			// This is a non fixed function pipe VB - bail out...
			return;
		}
		else if(FAILED(g_RenderStateMgr.SetVertexShader(m_VBController.getVertexFormat(0))))
		{
			return;
		}
	}

	// If this pass has a pixel shader, use it.
	if (NULL != pPass &&
	    pPass->bUsePixelShader &&
		pPass->PixelShaderID != LTPixelShader::PIXELSHADER_INVALID)
	{
		// Store the pointer to the actual shader during rendering.
		m_pPixelShader = LTPixelShaderMgr::GetSingleton().GetPixelShader(pPass->PixelShaderID);
		if (m_pPixelShader != NULL)
		{
			// Install the shader.
			if (!LTPixelShaderMgr::GetSingleton().InstallPixelShader(m_pPixelShader))
			{
				m_pPixelShader = NULL;
				return;
			}
		}
	}

	// We need software processing 
	if(m_bSWVertProcessing)
	{
		PD3DDEVICE->SetSoftwareVertexProcessing(TRUE);
	}


	g_RenderStateMgr.SetTransform(D3DTS_WORLDMATRIX(0), &WorldTransform);



	m_VBController.SetStreamSources();

	// Set the vertex shader constants.
	if (m_pVertexShader != NULL)
	{
		// Let the client set some constants.
		if (NULL != i_client_shell)
		{
			i_client_shell->OnVertexShaderSetConstants(m_pVertexShader, iRenderPass, pRenderStyle, pInstance,
													   LTShaderDeviceStateImp::GetSingleton());
		}

		// Send the constants to the video card.
		LTVertexShaderMgr::GetSingleton().SetVertexShaderConstants(m_pVertexShader);
	}

	// Set the pixel shader constants.
	if (m_pPixelShader != NULL)
	{
		// Let the client set some constants.
		if (NULL != i_client_shell)
		{
			i_client_shell->OnPixelShaderSetConstants(m_pPixelShader, iRenderPass, pRenderStyle, pInstance,
													  LTShaderDeviceStateImp::GetSingleton());
		}

		// Send the constants to the video card.
		LTPixelShaderMgr::GetSingleton().SetPixelShaderConstants(m_pPixelShader);
	}

	if(rsD3DOptions.bUseEffectShader)
	{
		LTEffectImpl* _pEffect = (LTEffectImpl*)LTEffectShaderMgr::GetSingleton().GetEffectShader(rsD3DOptions.EffectShaderID);
		ID3DXEffect* pEffect = _pEffect->GetEffect();

		if(pEffect)
		{
			i_client_shell->OnEffectShaderSetParams((LTEffectShader*)_pEffect, pRenderStyle, pInstance, LTShaderDeviceStateImp::GetSingleton());
			pEffect->CommitChanges();
		}
	}

	m_VBController.Render(0,0,m_iVertCount,m_iPolyCount);


	if ( m_bSWVertProcessing )
	{
		// If we are running with hardware then turn back on hardware processing
		if ( (g_Device.GetDeviceCaps()->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) )
		{
		
			PD3DDEVICE->SetSoftwareVertexProcessing(FALSE);
		}
	}

	PD3DDEVICE->SetStreamSource(0, 0, 0, 0);
	PD3DDEVICE->SetIndices(0);

	// Uninstall the vertex shader.
	if (NULL != m_pVertexShader)
	{
		LTVertexShaderMgr::GetSingleton().UninstallVertexShader();
		m_pVertexShader = NULL;
	}

	// Uninstall the pixel shader.
	if (NULL != m_pPixelShader)
	{
		LTPixelShaderMgr::GetSingleton().UninstallPixelShader();
		m_pPixelShader = NULL;
	}
}