//====== Copyright © 1996-2007, Valve Corporation, All rights reserved. =======//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
// STATIC: "CONVERT_TO_SRGB"				"0..1"	[ps20b][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
// STATIC: "CONVERT_TO_SRGB"				"0..0"	[= 0] [XBOX]
// STATIC: "DETAILTEXTURE"					"0..1"
// STATIC: "BUMPMAP"						"0..1"
// STATIC: "VERTEXCOLOR"					"0..1"
// STATIC: "SELFILLUM"						"0..1"
// STATIC: "DIFFUSEBUMPMAP"					"0..1"
// STATIC: "DETAIL_ALPHA_MASK_BASE_TEXTURE"	"0..1"
// STATIC: "FLASHLIGHT"						"0..1"
// STATIC: "SEAMLESS"						"0..1"
// STATIC: "FLASHLIGHTDEPTHFILTERMODE"		"0..2"	[ps20b] [PC]
// STATIC: "FLASHLIGHTDEPTHFILTERMODE"		"0..0"	[ps20b] [XBOX]

// DYNAMIC: "WRITEWATERFOGTODESTALPHA"		"0..1"
// DYNAMIC: "PIXELFOGTYPE"					"0..1"
// DYNAMIC: "WRITE_DEPTH_TO_DESTALPHA"		"0..1" [ps20b] [PC]
// DYNAMIC: "WRITE_DEPTH_TO_DESTALPHA"		"0..0" [ps20b] [XBOX]
// DYNAMIC: "FLASHLIGHTSHADOWS"				"0..1" [ps20b]

//	SKIP: $DETAILTEXTURE && ( $BUMPMAP && !$DETAIL_ALPHA_MASK_BASE_TEXTURE )
//  SKIP: !$BUMPMAP && $DIFFUSEBUMPMAP
//	SKIP: $VERTEXCOLOR && $BUMPMAP
//  SKIP: FLASHLIGHT && $SELFILLUM
//  SKIP: FLASHLIGHT && $DETAIL_ALPHA_MASK_BASE_TEXTURE
//  SKIP: FLASHLIGHT && ($BUMPMAP || $DIFFUSEBUMPMAP)

// We don't care about flashlight depth unless the flashlight is on
// SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTSHADOWS == 1 )	[ps20b]

#if defined( SHADER_MODEL_PS_2_0 )
#	define WRITE_DEPTH_TO_DESTALPHA 0
#endif


#define HDRTYPE HDR_TYPE_NONE
#include "common_flashlight_fxc.h"
#include "common_ps_fxc.h"

const HALF4 g_SelfIllumTint						: register( c7 );
static const HALF g_OverbrightFactor = 2.0f;

const HALF3 g_EyePos							: register( c10 );
const HALF4 g_FogParams							: register( c11 );

const HALF3 g_FlashlightPos						: register( c15 );
// flashlightfixme: Move this math into the vertex shader.
const float4x4 g_FlashlightWorldToTexture		: register( c16 );

const float4 g_FlashlightAttenuationFactors		: register( c20 );

sampler BaseTextureSampler			: register( s0 );
sampler LightmapSampler				: register( s1 );
sampler FlashlightSampler			: register( s2 );
sampler DetailSampler				: register( s3 );
sampler BumpmapSampler				: register( s4 );
sampler NormalizeSampler			: register( s6 );

struct PS_INPUT
{
	HALF2 baseTexCoord				: TEXCOORD0;
	HALF4 detailOrBumpTexCoord		: TEXCOORD1;	
	HALF4 lightmapTexCoord1And2		: TEXCOORD2;	// CENTROID: TEXCOORD2	
	HALF2 lightmapTexCoord3			: TEXCOORD3;	// CENTROID: TEXCOORD3
	HALF4 worldPos_projPosZ			: TEXCOORD4;
	HALF3x3 tangentSpaceTranspose	: TEXCOORD5;
	//		tangentSpaceTranspose	: TEXCOORD6;
	//		tangentSpaceTranspose	: TEXCOORD7;
	HALF4 vertexColor				: COLOR;
};


float4 main( PS_INPUT i ) : COLOR
{
	bool bDetailTexture = DETAILTEXTURE ? true : false;
	bool bBumpmap = BUMPMAP ? true : false;
	bool bDiffuseBumpmap = DIFFUSEBUMPMAP ? true : false;
	bool bVertexColor = VERTEXCOLOR ? true : false;
	bool bSelfIllum = SELFILLUM ? true : false;
	bool bDetailAlphaMaskBaseTexture = DETAIL_ALPHA_MASK_BASE_TEXTURE ? true : false;
	bool bFlashlight = FLASHLIGHT ? true : false;

	HALF3 lightmapColor1 = HALF3( 1.0f, 1.0f, 1.0f );
	HALF3 lightmapColor2 = HALF3( 1.0f, 1.0f, 1.0f );
	HALF3 lightmapColor3 = HALF3( 1.0f, 1.0f, 1.0f );
	if( bBumpmap && bDiffuseBumpmap )
	{
		HALF2 bumpCoord1;
		HALF2 bumpCoord2;
		HALF2 bumpCoord3;
		ComputeBumpedLightmapCoordinates( i.lightmapTexCoord1And2, i.lightmapTexCoord3.xy,
			bumpCoord1, bumpCoord2, bumpCoord3 );
		
		HALF4 lightmapSample1 = tex2D( LightmapSampler, bumpCoord1 );
		lightmapColor1 = lightmapSample1.rgb;
		lightmapColor2 = tex2D( LightmapSampler, bumpCoord2 );
		lightmapColor3 = tex2D( LightmapSampler, bumpCoord3 );
	}
	else
	{
		if( !bFlashlight )
		{
			HALF2 bumpCoord1 = ComputeLightmapCoordinates( i.lightmapTexCoord1And2, i.lightmapTexCoord3.xy );
			HALF4 lightmapSample1 = tex2D( LightmapSampler, bumpCoord1 );
			lightmapColor1 = lightmapSample1.rgb;
		}
	}

	HALF4 detailColor = HALF4( 1.0f, 1.0f, 1.0f, 1.0f );
	if( bDetailTexture )
	{
		detailColor = tex2D( DetailSampler, i.detailOrBumpTexCoord.xy );
	}

	HALF4 baseColor = HALF4( 1.0f, 1.0f, 1.0f, 1.0f );
	baseColor = tex2D( BaseTextureSampler, i.baseTexCoord );
	if ( bDetailAlphaMaskBaseTexture )
	{
		// This is what WorldTwoTextureBlend_DX6 does.
		baseColor.rgb = saturate( saturate( baseColor * 2 ) * detailColor.a + (1 - detailColor.a) );
		baseColor.rgb *= detailColor;
	}
	else
	{
		baseColor.rgb = lerp( baseColor, detailColor, detailColor.a );
	}
	
	HALF3 normal = HALF3( 0.0f, 0.0f, 1.0f );
	if( bBumpmap )
	{
		HALF3 normalTexel;
		normalTexel = tex2D( BumpmapSampler, i.detailOrBumpTexCoord.xy );
		normal = 2.0 * normalTexel - 1.0;
	}

	HALF3 albedo = HALF3( 1.0f, 1.0f, 1.0f );
	HALF alpha = 1.0f;
	albedo *= baseColor;
	if( !bSelfIllum )
	{
		alpha *= baseColor.a;
	}

	// The vertex color contains the modulation color + vertex color combined
	albedo *= i.vertexColor;
	alpha *= i.vertexColor.a; // not sure about this one

	HALF3 diffuseLighting;
	if( bFlashlight )
	{
		float3 worldSpaceNormal;
		// Make the unbumped version not so fucking stupid and not need tangentSpaceTranspose you knob.
		worldSpaceNormal = mul( normal, i.tangentSpaceTranspose );

		int nShadowSampleLevel = 0;
		bool bDoShadows = false;
// On ps_2_b, we can do shadow mapping
#if ( FLASHLIGHTSHADOWS && (defined(SHADER_MODEL_PS_2_B) || defined(SHADER_MODEL_PS_3_0) ) )
		nShadowSampleLevel = FLASHLIGHTDEPTHFILTERMODE;
		bDoShadows = true;
#endif
		float4 flashlightSpacePosition = mul( float4( i.worldPos_projPosZ.xyz, 1.0f ), g_FlashlightWorldToTexture );

		diffuseLighting = DoFlashlight( g_FlashlightPos, i.worldPos_projPosZ.xyz, flashlightSpacePosition,
			worldSpaceNormal, g_FlashlightAttenuationFactors.xyz, 
			g_FlashlightAttenuationFactors.w, FlashlightSampler, FlashlightSampler, NormalizeSampler,
			nShadowSampleLevel,	bDoShadows, false, float2(0, 0), false );
	}
	else
	{
		if( bBumpmap && bDiffuseBumpmap )
		{
			float dot1 = saturate( dot( normal, bumpBasis[0] ) );
			float dot2 = saturate( dot( normal, bumpBasis[1] ) );
			float dot3 = saturate( dot( normal, bumpBasis[2] ) );

			float sum = dot1 + dot2 + dot3;
			diffuseLighting = dot1 * lightmapColor1 +
							  dot2 * lightmapColor2 +
							  dot3 * lightmapColor3;
			diffuseLighting *= 1.0f / sum;
		}
		else
		{
			diffuseLighting = lightmapColor1;
		}

		// Only scale here since the flashlight will already be scaled properly
		diffuseLighting *= g_OverbrightFactor;
	}

	HALF3 diffuseComponent = albedo * diffuseLighting;

	if( bSelfIllum )
	{
		HALF3 selfIllumComponent = g_SelfIllumTint * albedo;
		diffuseComponent = lerp( diffuseComponent, selfIllumComponent, baseColor.a );
	}

	HALF3 specularLighting = HALF3( 0.0f, 0.0f, 0.0f );
	HALF3 result = diffuseComponent + specularLighting;

	float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );

#if WRITEWATERFOGTODESTALPHA && (PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT)
	alpha = fogFactor;
#endif

	return FinalOutput( float4( result, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR, (WRITE_DEPTH_TO_DESTALPHA != 0), i.worldPos_projPosZ.w );
}

