#include "dxShaderIncludes.h"
#include "alpha.h"
#include "textAnimationHelper.h"

typedef struct
{
    int   direction;
    float blurBias;
    float easeInOut;
    float motionRamp;
    float motionSpeed;
} fxVars;

cbuffer variableBuffer : register(b2)
{
    fxVars vars;
}

Texture2D blur1 : register(t3);
Texture2D blur2 : register(t4);
Texture2D blur3 : register(t5);
Texture2D blur4 : register(t6);
Texture2D blur5 : register(t7);
Texture2D blur6 : register(t8);
Texture2D blur1Alt : register(t9);
Texture2D blur2Alt : register(t10);
Texture2D blur3Alt : register(t11);
Texture2D blur4Alt : register(t12);
Texture2D blur5Alt : register(t13);
Texture2D blur6Alt : register(t14);

vertexOut vertexShader(txShaderVerts input)
{
    vertexOut output;

    output.normPos = input.position.xy;

    float4 tmpPos = mul(u.worldViewProj, float4(input.position, 1.0));

    if (u.transitionState == 0)
    {
        float scale = mix(.6, 1.0, u.transitionProgress);
        output.mixValue.x = u.transitionProgress;
    }
    else if (u.transitionState == 2)
    {
        output.mixValue.x = 1.0 - u.transitionProgress;
    }

    tmpPos = handleContinuedMotion(tmpPos, u.localTime, vars.motionRamp, vars.motionSpeed);

    output.position = tmpPos;

    output.tex1    = input.tex1;
    output.tex2    = input.tex2;
    output.texMask = input.texMask;

    return output;
}

float4 pixelShader(vertexOut input) : SV_TARGET
{
    float4 textColor = inputTex0.Sample(linearSampler, input.tex1);
//    float4 b6 = blur6.Sample(linearSampler, input.tex1);
    float4 b5 = blur5.Sample(linearSampler, input.tex1);
    float4 b4 = blur4.Sample(linearSampler, input.tex1);
    float4 b3 = blur3.Sample(linearSampler, input.tex1);
    float4 b2 = blur2.Sample(linearSampler, input.tex1);
    float4 b1 = blur1.Sample(linearSampler, input.tex1);

#ifdef IS_MEDIA_FILL
    float4 sharpColor = inputTex1.Sample(linearSampler, input.tex2);
//    float4 b6Alt = blur6Alt.Sample(linearSampler, input.tex2);
    float4 b5Alt = blur5Alt.Sample(linearSampler, input.tex2);
    float4 b4Alt = blur4Alt.Sample(linearSampler, input.tex2);
    float4 b3Alt = blur3Alt.Sample(linearSampler, input.tex2);
    float4 b2Alt = blur2Alt.Sample(linearSampler, input.tex2);
    float4 b1Alt = blur1Alt.Sample(linearSampler, input.tex2);
#endif

    float distance = segmentEnd[vars.direction] - segmentStart[vars.direction];
    float progress = segmentStart[vars.direction] + distance * input.normPos.x;
    float alpha = u.baseAlpha * progress;

    float4 outColor = getBlur(1.0 - progress, textColor, b5, b4, b3, b2, b1, vars.blurBias);

#ifdef HAS_BACKGROUND_EFFECT
    outColor = backgroundEffect(outColor, inputTex1, inputTexMask, input.position.xy, u, subVars);
#endif

#ifdef IS_MEDIA_FILL
    alpha *= outColor.a;
    outColor = getBlur(1.0 - progress, sharpColor, b5Alt, b4Alt, b3Alt, b2Alt, b1Alt, vars.blurBias);
#endif

#ifdef USE_MASK
    alpha *= inputTexMask.Sample(linearSampler, input.texMask).a;
#endif

    multiplyOpacity(outColor, alpha);

#ifdef SHOW_SEGMENT_OUTLINE
    showSegmentOutline(outColor);
#endif

    return outColor;
}
