#include "../ColorPalette.hls"
#include "../DeathScreen.hls"
#include "../MipLevels.hls"
#include "../FogOfWar/FogOfWar_PS.hls"
#include "SoftParticle.hls"
#include "ColorRemap.hls"
#include "AlphaErode.hls"
#include "navmeshmask_ps.hls"

sampler TEXTURE                 : register(s0);
sampler PARTICLE_COLOR_TEXTURE  : register(s1);
#ifdef REFLECTIVE
    samplerCUBE REFLECTION_MAP  : register(s2);
#endif
#ifdef  MULT_PASS
sampler TEXTUREMULT             : register(s3);
#endif

float2 COLOR_LOOKUP_UV          : register(c0);
float4 FRESNEL                  : register(c30);


#if defined(COLORPALETTE_COLORBLIND)
   float4 APPLY_TEAM_COLOR_CORRECTION : register(c11);
#endif

struct PS_IN
{
    float4  color0          : TEXCOORD0;    // N.B. colors can be > 1 (e.g. mod2x), so use a full interpolator and not COLOR0 which saturates on ps_2_0

    float4  uv0_fog_W       : TEXCOORD1;
    // xy = uv0
    // z  = 0.0 or PS fog when FOG_PIXELSHADER_BLEND defined
    // w  = projection W, compensates for perspective correction in screen space uv

#ifndef DISABLE_FOW
    float4  uvFOW_fadeFOW   : TEXCOORD2;    // FOW: xy = uv, zw = height based fade (out,in)
#endif

    // define TEXCOORD3 according to shader variant to minimize interpolators
#if defined( SEPARATE_ALPHA_UV ) && defined( MULT_PASS )
    float4  uvMult_uvAlpha  : TEXCOORD3;
#elif defined( SEPARATE_ALPHA_UV )
    float2  uvAlpha         : TEXCOORD3;
#elif defined( MULT_PASS )
    float2  uvMult          : TEXCOORD3;
#endif

#ifdef REFLECTIVE
    float4  reflectRay      : TEXCOORD4;
    float3  reflTint        : COLOR1;
#endif
    float4  fresnelColor    : TEXCOORD6;    // fresnel based rim effect available even w/o reflection map
#ifdef SOFT_PARTICLES
    float4  projPosition    : TEXCOORD7;
#endif

#ifdef MASKED
    float2   uvMap : TEXCOORD5;
#endif
};

void main( in PS_IN input, out float4 out_color0:COLOR0 )
{
    // extract inputs from interpolators (variant dependent)
    float2 uv0      = input.uv0_fog_W.xy;
#if defined( SEPARATE_ALPHA_UV ) && defined( MULT_PASS )
    float2 uvMult   = input.uvMult_uvAlpha.xy;
    float2 uvAlpha  = input.uvMult_uvAlpha.zw;
#elif defined( SEPARATE_ALPHA_UV )
    float2 uvAlpha  = input.uvAlpha.xy;
#elif defined( MULT_PASS )
    float2 uvMult   = input.uvMult.xy;
#endif
 
#ifdef SCREEN_SPACE_UV
    // correct texture coordinates
    float projW = input.uv0_fog_W.w;
    float correction = 1/projW; // @todo supply inverse to interpolator instead
    uv0 *= correction;
    #ifdef SEPARATE_ALPHA_UV
        uvAlpha *= correction;
    #endif
    // mult texture is anchored to the model, so it isn't corrected here
#endif

    float4 texel = tex2D(TEXTURE, uv0);
#ifdef SEPARATE_ALPHA_UV    
    texel.w = tex2D(TEXTURE, uvAlpha).w;    
#endif

#ifdef PALETTIZE_TEXTURES
    // remap textures according to palettization settings
    texel.rgb = ColorizeMainTexture(texel).rgb;
#endif

#ifdef MULT_PASS
    float4 particle_color_texel = tex2D(TEXTUREMULT, uvMult);
    texel *= particle_color_texel;
#elif !defined(ALPHA_EROSION)
    // apply p-rgba colorizing texture (if not using alpha erosion which uses same sampler)
    // @todo investigate discrepancy with quad shader which also applies color texture when using mult
    float4 particle_color_texel = tex2D(PARTICLE_COLOR_TEXTURE, COLOR_LOOKUP_UV);
    texel *= particle_color_texel;
#endif
    out_color0 = input.color0 * texel;

    //**
    // color palette transforms
#ifdef COLORPALETTE_COLORBLIND
    if (APPLY_TEAM_COLOR_CORRECTION.r != 0.0f)
    {
        ApplyColorCorrectionColorblind(out_color0);
    }
#endif 

    //**
    // Reflection
#ifdef REFLECTIVE
    //This reflection vector is used to look up the Reflection color in the cube map.
    //This color is scaled by opacity stored in eyeRay.w (computed in the vertex shader).
    float3 reflectionColor = texCUBE(REFLECTION_MAP, input.reflectRay.xyz).rgb * input.reflectRay.w;

    //Finally the reflection color is modified by the RGB Reflection Tint (computed in the vertex shader)
    //and added to the pixel color (finalColor).
    out_color0.rgb += reflectionColor*input.reflTint;
#endif
    out_color0.rgb += input.fresnelColor.rgb * out_color0.a;    // add fresnel rim effect (available even w/o a reflection map)

    //**
    // Fog of War
#ifndef DISABLE_FOW 
#ifdef SCREEN_SPACE_UV
    float4 fowFactors = GetFogOfWarFactors(input.uvFOW_fadeFOW.xy);
#else
    float fowFadeInFactorByHeight = input.uvFOW_fadeFOW.w;
    float4 fowFactors = GetFogOfWarFactorsWithFadeTowardFogged(input.uvFOW_fadeFOW.xy, fowFadeInFactorByHeight);
#endif

    // Apply FOW to final result
    // NOTE:  This ignores the FowOverlay texture tint and assumes the FoW is black
    out_color0.rgb *= fowFactors.a;
#endif

#ifdef ALPHA_EROSION
    out_color0.a *= CalcAlphaErosionForMesh(uv0);
#endif

    //**
    // soft particles
#ifdef SOFT_PARTICLES
    out_color0 = CalcSoftParticleColor(input.projPosition,out_color0);
#endif

    //**
    // Finalize output color
    out_color0.rgb = saturate(out_color0.rgb); // @todo should we be saturating before these in all shaders?
    out_color0.rgb = ApplyMipColorsVisualization(out_color0.rgb, uv0);
    
#ifdef MASKED
    out_color0 = ApplyNavmeshMask(out_color0, input.uvMap.xy);
#endif
}
