float4 AmbientColor : register(c0);
sampler2D AnisoMap : register(s4);
sampler2D BaseMap : register(s0);
float3 EyePosition : register(c1);
float3 HairTint : register(c2);
sampler2D LayerMap : register(s5);
float4 LightData[8] : register(c9);
float3 MatAlpha : register(c3);
sampler2D NormalMap : register(s1);
float4 ToggleADTS : register(c5);
float4 ToggleNumLights : register(c6);

float4 TESR_ShadowData : register(c40);

sampler2D TESR_ShadowMapBufferNear : register(s8) = sampler_state { ADDRESSU = CLAMP; ADDRESSV = CLAMP; MAGFILTER = LINEAR; MINFILTER = LINEAR; MIPFILTER = LINEAR; };
sampler2D TESR_ShadowMapBufferFar : register(s9) = sampler_state { ADDRESSU = CLAMP; ADDRESSV = CLAMP; MAGFILTER = LINEAR; MINFILTER = LINEAR; MIPFILTER = LINEAR; };


//
// Registers:
//
//   Name            Reg   Size
//   --------------- ----- ----
//   AmbientColor    const_0       1
//   EyePosition     const_1       1
//   HairTint        const_2       1
//   MatAlpha        const_3       1
//   ToggleADTS      const_5       1
//   ToggleNumLights const_6       1
//   LightData[0]       const_9        1
//   LightData[1]       const_10        1
//   LightData[2]       const_11        1
//   LightData[3]       const_12        1
//   LightData[4]       const_13        1
//   LightData[5]       const_14        1
//   LightData[6]       const_15        1
//   LightData[7]       const_16        1
//   BaseMap         texture_0       1
//   NormalMap       texture_1       1
//   AnisoMap        texture_4       1
//   LayerMap        texture_5       1
//


// Structures:

struct VS_OUTPUT {
    float2 BaseUV : TEXCOORD0;
    float2 color_0 : COLOR0;    
    float4 texcoord_1 : TEXCOORD1;
	  float4 texcoord_2 : TEXCOORD2;
    float3 texcoord_3 : TEXCOORD3_centroid;
    float3 texcoord_4 : TEXCOORD4_centroid;
    float3 texcoord_5 : TEXCOORD5_centroid;
    float3 texcoord_6 : TEXCOORD6_centroid;
    float4 texcoord_7 : TEXCOORD7_centroid;
   
};

struct PS_OUTPUT {
    float4 color_0 : COLOR0;
};

// Code:
#include "..\Includes\Shadow.hlsl"


PS_OUTPUT main(VS_OUTPUT IN) {
    PS_OUTPUT OUT;

#define	expand(v)		(((v) - 0.5) / 0.5)
#define	compress(v)		(((v) * 0.5) + 0.5)
#define	shade(n, l)		max(dot(n, l), 0)
#define	shades(n, l)		saturate(dot(n, l))
#define	weight(v)		dot(v, 1)
#define	sqr(v)			((v) * (v))

    const int4 const_18 = {-1, -2, 0, 2};
    const int4 const_19 = {-2, -3, -4, 0};
    const int4 const_20 = {-4, -5, -6, 0};
    const float4 const_4 = {-0.05, 0, 0.7, 4};
    const float4 const_7 = {-0.5, 0, 1, 0.5};
    const int4 const_8 = {2, -6, -7, 0};

    float3 eye99;
    float1 l10;
    float3 l128;
    float3 l13;
    float3 l14;
    float1 l16;
    float3 l19;
    float3 l20;
    float1 l22;
    float3 l27;
    float1 l356;
    float3 l4;
    float3 l8;
    float3 m106;
    float3 m114;
    float3 m122;
    float3 m129;
    float3 q1;
    float1 q12;
    float3 q15;
    float1 q18;
    float3 q2;
    float3 q21;
    float1 q24;
    float3 q3;
    float3 q43;
    float1 q5;
    float3 q50;
    float3 q56;
    float3 q60;
    float3 q62;
    float3 q7;
    float3 q9;
    float4 r0;
    float4 r1;
    float3 r10;
    float3 r11;
    float3 r12;
    float3 r13;
    float3 r14;
    float3 r15;
    float2 r16;
    float4 r2;
    float4 r3;
    float4 r4;
    float4 r5;
    float3 r6;
    float4 r7;
    float3 r8;
    float3 r9;     
    float1 shadow;
  

#define	TanSpaceProj	float3x3(r12.xyz, r11.xyz, r10.xyz)
#define	TanSpaceProj	float3x3(r12.xyz, r11.xyz, r10.xyz)

    r1.xyzw = tex2D(NormalMap, IN.BaseUV.xy);
    r5.xyz = normalize(expand(r1.xyz));
    r0.xyz = r5.xyz * 0.5;
    q1.xyz = r0.xyz + const_7.yyz;
    r0.w = r0.z + 1;
    r0.xyz = q1.xyz / sqrt(dot(r0.xyw, q1.xyz));
    r0.w = r0.x - 0.05;
    q2.xyz = r0.xyz + const_4.xyy;
    r8.xyz = q2.xyz / sqrt(dot(r0.wyz, q2.xyz));
    r10.xyz = normalize(IN.texcoord_5.xyz);
    r11.xyz = normalize(IN.texcoord_4.xyz);
    r12.xyz = normalize(IN.texcoord_3.xyz);
    r4.xyz = mul(TanSpaceProj, LightData[1].xyz);
    eye99.xyz = mul(TanSpaceProj, EyePosition.xyz - IN.texcoord_6.xyz);
    r9.xyz = normalize(eye99.xyz);
    q3.xyz = normalize(r4.xyz + r9.xyz);
    r2.x = dot(r8.xyz, r4.xyz);
    r2.y = dot(r8.xyz, q3.xyz);
    r2.xyzw = tex2D(AnisoMap, r2.xy);
    r3.x = dot(r0.xyz, r4.xyz);
    r3.y = dot(r0.xyz, q3.xyz);
    r3.xyzw = tex2D(AnisoMap, r3.xy);
    r6.xyz = const_7.xyz;
    r4.w = (ToggleNumLights.x <= 0.0 ? r6.y : r6.z);

    if (0 != r4.w
) {
      r0.w = 1;
      l4.xyz = ((r2.w * (2 * ((IN.color_0.g * (r6.x + HairTint.rgb)) + 0.5))) + (r3.w * 0.7)) * LightData[0].xyz;
      r1.xyz = max(r4.z, 0) * l4.xyz;
      r4.xyz = shade(r5.xyz, r4.xyz) * LightData[0].xyz;
    }
    else {
      r0.w = 0;
      r4.xyz = r0.w;
      r1.xyz = r0.w;
    }

    q5.x = min(ToggleNumLights.y, 4 - ToggleNumLights.x);
    r13.x = 2 * r0.w;
    r13.yz = r13.x + const_18.xy;
    q7.xyz = r13.x + const_18.zxy;
    r14.xyz = (q7.xyz >= 0.0 ? q7.xyz : -r13.xyz);
    l8.xyz = (r14.z <= 0.0 ? LightData[3].xyz : (r14.y <= 0.0 ? LightData[2].xyz : (r14.x <= 0.0 ? LightData[1].xyz : r6.y)));
    r15.xyz = l8.xyz - IN.texcoord_6.xyz;
    m106.xyz = mul(TanSpaceProj, r15.xyz);
    r7.xyz = normalize(m106.xyz);
    q9.xyz = normalize(r9.xyz + r7.xyz);
    r16.x = dot(r8.xyz, r7.xyz);
    r16.y = dot(r8.xyz, q9.xyz);
    r2.xyzw = tex2D(AnisoMap, r16.xy);
    r3.x = dot(r0.xyz, r7.xyz);
    r3.y = dot(r0.xyz, q9.xyz);
    r3.xyzw = tex2D(AnisoMap, r3.xy);
    r4.w = ((q5.x >= 0.0 ? 0 : 1) * (frac(q5.x) <= 0.0 ? 0 : 1)) + (q5.x - frac(q5.x));
    r5.w = (r4.w <= 0.0 ? 0 : 1);

    if (0 != r5.w
) {
      r2.xyz = r13.x + const_18.zxy;
      r5.w = r0.w + 1;
      l13.xyz = (r2.z == 0.0 ? LightData[2].xyz : (r2.y == 0.0 ? LightData[1].xyz : (r2.x == 0.0 ? LightData[0].xyz : r6.y)));
      q43.xyz = (r2.w * ((0.3 * ((IN.color_0.g * (r6.x + HairTint.rgb)) + 0.5)) + 0.2)) + (r3.w * 0.7);
      l10.x = (r14.z <= 0.0 ? LightData[3].w : (r14.y <= 0.0 ? LightData[2].w : (r14.x <= 0.0 ? LightData[1].w : r6.y)));
      q12.x = 1.0 - sqr(saturate(length(r15.xyz) / l10.x));
      r1.xyz = (max(r7.z, 0) * ((q12.x * l13.xyz) * q43.xyz)) + r1.xyz;
      r4.xyz = (max(dot(r5.xyz, r7.xyz) * q12.x, 0) * l13.xyz) + r4.xyz;
    }
    else {
      r5.w = r0.w;
    }

    r7.w = 2 * r5.w;
    r13.xyz = r7.w + const_19.xyz;
    l14.xyz = (r13.z == 0.0 ? LightData[5].xyz : (r13.y == 0.0 ? LightData[4].xyz : (r13.x == 0.0 ? LightData[3].xyz : r6.y)));
    r14.xyz = l14.xyz - IN.texcoord_6.xyz;
    m114.xyz = mul(TanSpaceProj, r14.xyz);
    r7.xyz = normalize(m114.xyz);
    q15.xyz = normalize(r9.xyz + r7.xyz);
    r15.x = dot(r8.xyz, r7.xyz);
    r15.y = dot(r8.xyz, q15.xyz);
    r2.xyzw = tex2D(AnisoMap, r15.xy);
    r3.x = dot(r0.xyz, r7.xyz);
    r3.y = dot(r0.xyz, q15.xyz);
    r3.xyzw = tex2D(AnisoMap, r3.xy);

    if (1 < r4.w
) {
      r2.xyz = r7.w + const_19.xyz;
      r5.w = r5.w + 1;
      l19.xyz = (r2.z == 0.0 ? LightData[4].xyz : (r2.y == 0.0 ? LightData[3].xyz : (r2.x == 0.0 ? LightData[2].xyz : r6.y)));
      q50.xyz = (r2.w * ((0.3 * ((IN.color_0.g * (r6.x + HairTint.rgb)) + 0.5)) + 0.2)) + (r3.w * 0.7);
      l16.x = (r13.z == 0.0 ? LightData[5].w : (r13.y == 0.0 ? LightData[4].w : (r13.x == 0.0 ? LightData[3].w : r6.y)));
      q18.x = 1.0 - sqr(saturate(length(r14.xyz) / l16.x));
      r1.xyz = (max(r7.z, 0) * ((q18.x * l19.xyz) * q50.xyz)) + r1.xyz;
      r4.xyz = (max(dot(r5.xyz, r7.xyz) * q18.x, 0) * l19.xyz) + r4.xyz;
    }

    r7.w = 2 * r5.w;
    r13.xyz = r7.w + const_20.xyz;
    l20.xyz = (r13.z == 0.0 ? LightData[7].xyz : (r13.y == 0.0 ? LightData[6].xyz : (r13.x == 0.0 ? LightData[5].xyz : r6.y)));
    r14.xyz = l20.xyz - IN.texcoord_6.xyz;
    m122.xyz = mul(TanSpaceProj, r14.xyz);
    r7.xyz = normalize(m122.xyz);
    q21.xyz = normalize(r9.xyz + r7.xyz);
    r15.x = dot(r8.xyz, r7.xyz);
    r15.y = dot(r8.xyz, q21.xyz);
    r2.xyzw = tex2D(AnisoMap, r15.xy);
    r3.x = dot(r0.xyz, r7.xyz);
    r3.y = dot(r0.xyz, q21.xyz);
    r3.xyzw = tex2D(AnisoMap, r3.xy);

    if (2 < r4.w
) {
      r2.xyz = r7.w + const_20.xyz;
      r5.w = r5.w + 1;
      l128.xyz = (r2.z == 0.0 ? LightData[6].xyz : (r2.y == 0.0 ? LightData[5].xyz : (r2.x == 0.0 ? LightData[4].xyz : r6.y)));
      q56.xyz = (r2.w * ((0.3 * ((IN.color_0.g * (r6.x + HairTint.rgb)) + 0.5)) + 0.2)) + (r3.w * 0.7);
      l22.x = (r13.z == 0.0 ? LightData[7].w : (r13.y == 0.0 ? LightData[6].w : (r13.x == 0.0 ? LightData[5].w : r6.y)));
      q24.x = 1.0 - sqr(saturate(length(r14.xyz) / l22.x));
      r1.xyz = (max(r7.z, 0) * ((q24.x * l128.xyz) * q56.xyz)) + r1.xyz;
      r4.xyz = (max(dot(r5.xyz, r7.xyz) * q24.x, 0) * l128.xyz) + r4.xyz;
    }

    r7.xyz = LightData[7].xyz - IN.texcoord_6.xyz;
    m129.xyz = mul(TanSpaceProj, r7.xyz);
    r3.xyz = normalize(m129.xyz);
    r9.xyz = r9.xyz + r3.xyz;
    r2.xyz = normalize(r9.xyz);
    r9.x = dot(r8.xyz, r3.xyz);
    r9.y = dot(r8.xyz, r2.xyz);
    r2.y = dot(r0.xyz, r2.xyz);
    r2.x = dot(r0.xyz, r3.xyz);
    r0.xyzw = tex2D(AnisoMap, r9.xy);
    r2.xyzw = tex2D(AnisoMap, r2.xy);

    if (3 < r4.w
) {
      r3.w = dot(r5.xyz, r3.xyz);
      l356.x = 1.0 - sqr(saturate(length(r7.xyz) / LightData[7].w));
      q60.xyz = (r0.w * ((0.3 * ((IN.color_0.g * (r6.x + HairTint.rgb)) + 0.5)) + 0.2)) + (r2.w * 0.7);
      r3.xy = (2 * r5.w) + const_8.yz;
      l27.xyz = (r3.y == 0.0 ? LightData[7].xyz : (r3.x == 0.0 ? LightData[6].xyz : r6.y));
      r1.xyz = (max(r3.z, 0) * ((l356.x * l27.xyz) * q60.xyz)) + r1.xyz;
      r4.xyz = (max(r3.w * l356.x, 0) * l27.xyz) + r4.xyz;
    }
                         shadow = GetLightAmount(IN.texcoord_1, IN.texcoord_2);
         r4 *= shadow;

    r2.xyzw = tex2D(LayerMap, IN.BaseUV.xy);
    r0.xyzw = tex2D(BaseMap, IN.BaseUV.xy);
    r3.xyz = r1.xyz * IN.color_0.g;
    r1.xyz = lerp(r0.xyz, r2.xyz, r2.w);
    r0.xyz = r4.xyz + ((ToggleADTS.x * AmbientColor.rgb) + (r6.z - ToggleADTS.x));
    q62.xyz = (((2 * ((IN.color_0.g * (r6.x + HairTint.rgb)) + 0.5)) * r1.xyz) * r0.xyz) + (r1.w * r3.xyz);
    OUT.color_0.a = r0.w * MatAlpha.x;
    OUT.color_0.rgb = (IN.texcoord_7.w * (IN.texcoord_7.xyz - q62.xyz)) + q62.xyz;

    return OUT;
};

// approximately 291 instruction slots used (13 texture, 278 arithmetic)
