//========= Copyright © 1996-2006, Valve Corporation, All rights reserved. ============//

// STATIC: "BUMPMAP"					"0..1"

// DYNAMIC: "COMPRESSED_VERTS"			"0..1"
// DYNAMIC: "SKINNING"					"0..1"
// DYNAMIC: "MORPHING"					"0..1" [vs30]

// Includes
#include "common_vs_fxc.h"

// Globals
static const bool g_bSkinning		= SKINNING ? true : false;
const float4 cBaseTexCoordTransform[2]	:  register( SHADER_SPECIFIC_CONST_0 );

#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

// Structs
struct VS_INPUT
{
	float4 vPos					: POSITION;		// Position
	float4 vNormal				: NORMAL;		// Normal
	float4 vBoneWeights			: BLENDWEIGHT;	// Skin weights
	float4 vBoneIndices			: BLENDINDICES;	// Skin indices
	float4 vTexCoord0			: TEXCOORD0;	// Base texture coordinates

	#if BUMPMAP
		float4 vTangent				: TANGENT;
	#endif

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

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

struct VS_OUTPUT
{
    float4 vProjPosition		: POSITION;		// Projection-space position
	float3 vWorldNormal			: TEXCOORD0;	// World-space normal
	float3 vProjPosForRefract	: TEXCOORD1;
	float3 vWorldViewVector		: TEXCOORD2;

	#if BUMPMAP
		float3x3 mTangentSpaceTranspose : TEXCOORD3;
		//	     second row				: TEXCOORD4;
		//	     third row				: TEXCOORD5;
		float2 vTexCoord0				: TEXCOORD6;
	#endif
};

// Main
VS_OUTPUT main( const VS_INPUT i )
{
	VS_OUTPUT o;

	// Flexes coming in from a separate stream (contribution masked by cFlexScale.x)
	float4 vObjPosition = i.vPos;
	float3 vObjNormal;

	#if BUMPMAP
		float4 vObjTangent;
		DecompressVertex_NormalTangent( i.vNormal, i.vTangent, vObjNormal, vObjTangent );

		#if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
			ApplyMorph( i.vPosFlex, i.vNormalFlex, vObjPosition.xyz, vObjNormal, vObjTangent.xyz );
		#else
			ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, i.vVertexID, float3( 0, 0, 0 ), vObjPosition.xyz, vObjNormal, vObjTangent.xyz );
		#endif
	#else // !BUMPMAP
		DecompressVertex_Normal( i.vNormal, vObjNormal );

		#if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
			ApplyMorph( i.vPosFlex, i.vNormalFlex, vObjPosition.xyz, vObjNormal );
		#else
			ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, i.vVertexID, float3( 0, 0, 0 ), 
				vObjPosition.xyz, vObjNormal );
		#endif
	#endif

	// Transform the position
	float3 vWorldPosition = { 0.0f, 0.0f, 0.0f };
	float3 vWorldNormal = { 0.0f, 0.0f, 0.0f };
	#if BUMPMAP
		float3 vWorldTangent = { 0.0f, 0.0f, 0.0f };
		float3 vWorldBinormal = { 0.0f, 0.0f, 0.0f };
		SkinPositionNormalAndTangentSpace( g_bSkinning, vObjPosition, vObjNormal.xyz, vObjTangent.xyzw, i.vBoneWeights, i.vBoneIndices, vWorldPosition, vWorldNormal, vWorldTangent, vWorldBinormal );
	#else
		SkinPositionAndNormal( g_bSkinning, vObjPosition, vObjNormal.xyz, i.vBoneWeights, i.vBoneIndices, vWorldPosition, vWorldNormal );
	#endif
	o.vWorldNormal.xyz = normalize( vWorldNormal.xyz );

	// Transform into projection space
	float4 vProjPosition = mul( float4( vWorldPosition, 1.0f ), cViewProj );
	o.vProjPosition = vProjPosition;

	// Map projected position to the refraction texture
	float2 vRefractPos;
	vRefractPos.x = vProjPosition.x;
	vRefractPos.y = -vProjPosition.y; // Invert Y
	vRefractPos = (vRefractPos + vProjPosition.w) * 0.5f;
	o.vProjPosForRefract.xyz = float3(vRefractPos.x, vRefractPos.y, vProjPosition.w);

	// View vector
	float3 vWorldViewVector = normalize (vWorldPosition.xyz - cEyePos.xyz);
	o.vWorldViewVector.xyz = vWorldViewVector.xyz;

	// Tangent space transform
	#if BUMPMAP
		o.mTangentSpaceTranspose[0] = float3( vWorldTangent.x, vWorldBinormal.x, vWorldNormal.x );
		o.mTangentSpaceTranspose[1] = float3( vWorldTangent.y, vWorldBinormal.y, vWorldNormal.y );
		o.mTangentSpaceTranspose[2] = float3( vWorldTangent.z, vWorldBinormal.z, vWorldNormal.z );
	#endif

	// Texture coordinates
	#if BUMPMAP
		o.vTexCoord0.x = dot( i.vTexCoord0.xy, cBaseTexCoordTransform[0] );
		o.vTexCoord0.y = dot( i.vTexCoord0.xy, cBaseTexCoordTransform[1] );
	#endif

	return o;
}
