//
// Generated by Microsoft (R) D3DX9 Shader Compiler 9.08.299.0000
//
//   psa shaderdump19/STLEAF2000.pso /Fcshaderdump19/STLEAF2000.pso.dis
//
//
// Parameters:
//
sampler2D DiffuseMap : register(s0);
float4 TESR_ShadowData : register(c5);
float4 TESR_SunDirection : register(c222);

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

//
//
// Registers:
//
//   Name         Reg   Size
//   ------------ ----- ----
//   DiffuseMap   texture_0       1
//


// Structures:

struct VS_OUTPUT {
    float2 DiffuseUV : TEXCOORD0;
			// partial precision
    float3 texcoord_1 : TEXCOORD1;
			// partial precision
    float4 texcoord_6 : TEXCOORD6;
    float4 texcoord_7 : TEXCOORD7;
   
};

struct PS_OUTPUT {
    float4 color_0 : COLOR0;
};

// Code:

static const float BIAS = 0.001f;

float LookupLeaves(float4 ShadowPos) {
	float Shadow = tex2D(TESR_ShadowMapBufferNear, ShadowPos.xy).r;
	if (Shadow < ShadowPos.z - BIAS) return min(TESR_SunDirection.w + 0.4f, TESR_ShadowData.y + 0.6f);
	return saturate(TESR_SunDirection.w + 0.4f);
}

float LookupFarLeaves(float4 ShadowPos) {
	float Shadow = tex2D(TESR_ShadowMapBufferFar, ShadowPos.xy).r;
	if (Shadow < ShadowPos.z - BIAS) return min(TESR_SunDirection.w + 0.4f, TESR_ShadowData.y + 0.6f);
	return saturate(TESR_SunDirection.w + 0.4f);
}

float GetLightAmountFarLeaves(float4 ShadowPos) {

	float Shadow = 0.0f;
	float x;
	float y;

	ShadowPos.xyz /= ShadowPos.w;
	ShadowPos.x = ShadowPos.x * 0.5f + 0.5f;
	ShadowPos.y = ShadowPos.y * -0.5f + 0.5f;
	Shadow = LookupFarLeaves(ShadowPos);

	return Shadow;

}


float GetLightAmountLeaves(float4 ShadowPos, float4 ShadowPosFar) {

	float Shadow = 0.0f;
	float x;
	float y;
	float distToExternalLight = 0.0f;

	ShadowPos.xyz /= ShadowPos.w;
	if (ShadowPos.x < -1.0f || ShadowPos.x > 1.0f ||
		ShadowPos.y < -1.0f || ShadowPos.y > 1.0f ||
		ShadowPos.z < 0.0f || ShadowPos.z > 1.0f)
		return GetLightAmountFarLeaves(ShadowPosFar);

	ShadowPos.x = ShadowPos.x * 0.5f + 0.5f;
	ShadowPos.y = ShadowPos.y * -0.5f + 0.5f;
	Shadow = LookupLeaves(ShadowPos);

	
	return saturate(Shadow);

}



PS_OUTPUT main(VS_OUTPUT IN) {
    PS_OUTPUT OUT;

    float4 r0;
    float shadow = GetLightAmountLeaves(IN.texcoord_6, IN.texcoord_7);

    r0.xyzw = tex2D(DiffuseMap, IN.DiffuseUV.xy);
			// partial precision

    r0.xyz *= shadow;

    OUT.color_0.a = r0.w;
			// partial precision
    OUT.color_0.rgb = r0.xyz * IN.texcoord_1.xyz;
			// partial precision

    return OUT;
};

// approximately 3 instruction slots used (1 texture, 2 arithmetic)
