float4 AmbientColor : register(c1);
sampler2D BaseMap : register(s0);
float4 EmittanceColor : register(c6);
sampler2D GlowMap : register(s4);
sampler2D NormalMap : register(s1);
float4 PSLightColor[4] : register(c2);
float4 Toggles : register(c7);

float4 TESR_ShadowData : register(c8);
sampler2D TESR_ShadowMapBufferNear : register(s2) = sampler_state { ADDRESSU = CLAMP; ADDRESSV = CLAMP; MAGFILTER = LINEAR; MINFILTER = LINEAR; MIPFILTER = LINEAR; };
sampler2D TESR_ShadowMapBufferFar : register(s3) = sampler_state { ADDRESSU = CLAMP; ADDRESSV = CLAMP; MAGFILTER = LINEAR; MINFILTER = LINEAR; MIPFILTER = LINEAR; };


//
//
// Registers:
//
//   Name           Reg   Size
//   -------------- ----- ----
//   AmbientColor   const_1       1
//   PSLightColor[0]   const_2        1
//   EmittanceColor const_6       1
//   Toggles        const_7       1
//   BaseMap        texture_0       1
//   NormalMap      texture_1       1
//   GlowMap        texture_4       1
//


// Structures:

struct VS_OUTPUT {
    float2 BaseUV : TEXCOORD0;
    float3 texcoord_1 : TEXCOORD1_centroid;
    float3 color_0 : COLOR0;
    float4 color_1 : COLOR1; 
    float4 texcoord_6 : TEXCOORD6;
	  float4 texcoord_7 : TEXCOORD7;
};

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))

    float3 q1;
    float3 q2;
    float3 q3;
    float3 q6;
    float4 r0;
    float4 r1;
    float4 r2;       
       float shadow;
    
     shadow = saturate (GetLightAmount(IN.texcoord_6, IN.texcoord_7));

    r2.xyzw = tex2D(NormalMap, IN.BaseUV.xy);
    r1.xyzw = tex2D(GlowMap, IN.BaseUV.xy);
    r0.xyzw = tex2D(BaseMap, IN.BaseUV.xy);
    r1.xyz = (r1.xyz * EmittanceColor.rgb) + AmbientColor.rgb;
    q6.xyz = shadow * (shades(normalize(expand(r2.xyz)), IN.texcoord_1.xyz) * PSLightColor[0].rgb) + r1.xyz;
    q1.xyz = (Toggles.x <= 0.0 ? r0.xyz : (r0.xyz * IN.color_0.rgb));
    
    q2.xyz = q1.xyz * q6.xyz;
    q3.xyz = (Toggles.y <= 0.0 ? q2.xyz : ((IN.color_1.a * (IN.color_1.rgb - (q1.xyz * q6.xyz))) + q2.xyz));
    OUT.color_0.a = r0.w * AmbientColor.a;
    OUT.color_0.rgb = q3.xyz;   
  // OUT.color_0.rgb = 1;

    return OUT;
};

// approximately 21 instruction slots used (3 texture, 18 arithmetic)
