//======= Copyright (c) 1996-2007, Valve Corporation, All rights reserved. ======

//  STATIC: "DECAL"						"0..1" [vs30]
//  STATIC: "USE_STATIC_CONTROL_FLOW"	"0..1" [vs20]

//  DYNAMIC: "COMPRESSED_VERTS"			"0..1"
//	DYNAMIC: "DOWATERFOG"				"0..1"
//	DYNAMIC: "SKINNING"					"0..1"
//  DYNAMIC: "LIGHTING_PREVIEW"			"0..1" [PC]
//  DYNAMIC: "LIGHTING_PREVIEW"			"0..0" [XBOX]
//  DYNAMIC: "MORPHING"					"0..1" [vs30]
//  DYNAMIC: "NUM_LIGHTS"				"0..2" [vs20]

// If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo
//  SKIP: $USE_STATIC_CONTROL_FLOW && ( $NUM_LIGHTS > 0 ) [vs20]

#include "common_vs_fxc.h"

static const bool g_bSkinning		= SKINNING ? true : false;
static const int g_FogType			= DOWATERFOG;

const float4 cBaseTexCoordTransform[2]		: register( SHADER_SPECIFIC_CONST_0 );
const float4 cDetailTexCoordTransform[2]	: register( SHADER_SPECIFIC_CONST_4 );

#ifdef SHADER_MODEL_VS_3_0
// NOTE: cMorphTargetTextureDim.xy = target dimensions,
//		 cMorphTargetTextureDim.z = 4tuples/morph
const float3 cMorphTargetTextureDim			: register( SHADER_SPECIFIC_CONST_6 );
const float4 cMorphSubrect					: register( SHADER_SPECIFIC_CONST_7 );

sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
#endif


//-----------------------------------------------------------------------------
// Input vertex format
//-----------------------------------------------------------------------------
struct VS_INPUT
{
	// This is all of the stuff that we ever use.
	float4 vPos						: POSITION;
	float4 vBoneWeights				: BLENDWEIGHT;
	float4 vBoneIndices				: BLENDINDICES;
	float4 vNormal					: NORMAL;
	float4 vColor					: COLOR0;
	float3 vSpecular				: COLOR1;
	// make these float2's and stick the [n n 0 1] in the dot math.
	float4 vTexCoord0				: TEXCOORD0;
	float4 vTexCoord1				: TEXCOORD1;
	float4 vTexCoord2				: TEXCOORD2;
	float4 vTexCoord3				: TEXCOORD3;
	float3 vTangentS				: TANGENT;
	float3 vTangentT				: BINORMAL;
	float4 vUserData				: TANGENT;

	// Position and normal/tangent deltas
	float4 vPosFlex					: POSITION1;
	float4 vNormalFlex				: NORMAL1;

#ifdef SHADER_MODEL_VS_3_0
	float vVertexID					: POSITION2;
#endif
};

struct VS_OUTPUT
{
	// Stuff that isn't seen by the pixel shader
	float4 projPos					: POSITION;	
#if !defined( _X360 )    
	float  fog						: FOG;
#endif
	// Stuff that is seen by the pixel shader
	float4 baseTexCoord				: TEXCOORD0;			// includes detail tex coord
	float3 lightAtten				: TEXCOORD1;
	float3 worldVertToEyeVector		: TEXCOORD2;
	float3x3 tangentSpaceTranspose	: TEXCOORD3;
	//	     second row				: TEXCOORD4;
	//	     third row				: TEXCOORD5;
	float4 worldPos_atten3			: TEXCOORD6;
	float4 projPos_fWrinkleWeight	: TEXCOORD7;
};

//-----------------------------------------------------------------------------
// Main shader entry point
//-----------------------------------------------------------------------------
VS_OUTPUT main( const VS_INPUT v )
{
	VS_OUTPUT o = ( VS_OUTPUT )0;

	float4 vPosition = v.vPos;
	float3 vNormal;
	float4 vTangent;
	DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vNormal, vTangent );

#if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
	ApplyMorph( v.vPosFlex, v.vNormalFlex, 
		vPosition.xyz, vNormal, vTangent.xyz, o.projPos_fWrinkleWeight.w );
#else
	ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, v.vTexCoord2,
		vPosition.xyz, vNormal, vTangent.xyz, o.projPos_fWrinkleWeight.w );
#endif

	// Perform skinning
	float3 worldNormal, worldPos, worldTangentS, worldTangentT;
	SkinPositionNormalAndTangentSpace( g_bSkinning, vPosition, vNormal, vTangent,
		v.vBoneWeights, v.vBoneIndices, worldPos,
		worldNormal, worldTangentS, worldTangentT );

	// Always normalize since flex path is controlled by runtime
	// constant not a shader combo and will always generate the normalization
	worldNormal   = normalize( worldNormal );
	worldTangentS = normalize( worldTangentS );
	worldTangentT = normalize( worldTangentT );

#if defined( SHADER_MODEL_VS_3_0 ) && MORPHING && DECAL
	// Avoid z precision errors
	worldPos += worldNormal * 0.05f * v.vTexCoord2.z;
#endif

	// Transform into projection space
	float4 vProjPos = mul( float4( worldPos, 1 ), cViewProj );
	o.projPos = vProjPos;
	vProjPos.z = dot( float4( worldPos, 1  ), cViewProjZ );

	o.projPos_fWrinkleWeight.xyz = vProjPos.xyz;

#if !defined( _X360 )
	o.fog = CalcFog( worldPos, vProjPos.xyz, g_FogType );
#endif
	// Needed for water fog alpha and diffuse lighting 
	// FIXME: we shouldn't have to compute this all the time.
	o.worldPos_atten3.xyz = worldPos;

	// Needed for specular
	o.worldVertToEyeVector = VSHADER_VECT_SCALE * (cEyePos - worldPos);

	// Compute bumped lighting
	// FIXME: We shouldn't have to compute this for unlit materials
#if defined ( SHADER_MODEL_VS_2_0 ) && ( !USE_STATIC_CONTROL_FLOW )
	o.lightAtten.xyz = float3(0,0,0);
	o.worldPos_atten3.w = 0.0f;
	#if ( NUM_LIGHTS > 0 )
		o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, false );
	#endif
	#if ( NUM_LIGHTS > 1 )
		o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, false );
	#endif
	#if ( NUM_LIGHTS > 2 )
		o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, false );
	#endif
	#if ( NUM_LIGHTS > 3 )
		o.worldPos_atten3.w = GetVertexAttenForLight( worldPos, 3, false );
	#endif
#else
	o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, true );
	o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, true );
	o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, true );
	o.worldPos_atten3.w = GetVertexAttenForLight( worldPos, 3, true );
#endif

	// Base texture coordinate transform
	o.baseTexCoord.x = dot( v.vTexCoord0, cBaseTexCoordTransform[0] );
	o.baseTexCoord.y = dot( v.vTexCoord0, cBaseTexCoordTransform[1] );
	o.baseTexCoord.z = dot( v.vTexCoord0, cDetailTexCoordTransform[0] );
	o.baseTexCoord.w = dot( v.vTexCoord0, cDetailTexCoordTransform[1] );

	// Tangent space transform
	o.tangentSpaceTranspose[0] = float3( worldTangentS.x, worldTangentT.x, worldNormal.x );
	o.tangentSpaceTranspose[1] = float3( worldTangentS.y, worldTangentT.y, worldNormal.y );
	o.tangentSpaceTranspose[2] = float3( worldTangentS.z, worldTangentT.z, worldNormal.z );

	return o;
}