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

typedef struct
{
    int   direction;
    float easeInOut;
} fxVars;

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

static const float moveAmount = 1.0;

static const vec4 deltaTransTable[9] =
{
    vec4(-moveAmount,  moveAmount, 0.0, 0.0),
    vec4( 0.0,         moveAmount, 0.0, 0.0),
    vec4( moveAmount,  moveAmount, 0.0, 0.0),
    vec4(-moveAmount,  0.0,        0.0, 0.0),
    vec4( 0.0,         0.0,        0.0, 0.0),
    vec4( moveAmount,  0.0,        0.0, 0.0),
    vec4(-moveAmount, -moveAmount, 0.0, 0.0),
    vec4( 0.0,        -moveAmount, 0.0, 0.0),
    vec4( moveAmount, -moveAmount, 0.0, 0.0)
};

vertexOut vertexShader(txShaderVerts input)
{
    vertexOut output;

    output.normPos       = input.position.xy;

    vec2 onePixel = 2.0 / u.windowSize;
    vec2 offset   = vec2(u.srcBounds.z - u.srcBounds.x + onePixel.x,
                         u.srcBounds.w - u.srcBounds.y + onePixel.y) * 2.0;

    vec4 deltaTrans  = deltaTransTable[vars.direction] * vec4(offset.x, offset.y, 0.0, 0.0);
    vec4 transOffset = deltaTransTable[vars.direction] * vec4(offset.x, offset.y, 0.0, 0.0);

    deltaTrans = mul(u.worldViewProj, deltaTrans);
    transOffset = mul(u.worldViewProj, transOffset);

    float4x4 translation = float4x4(
        1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0
    );

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

    if (u.transitionState <= 1)
    {
        transOffset -= deltaTrans * segmentStart[vars.direction];

        translation[3][0] += transOffset.x;
        translation[3][1] += transOffset.y;
        translation[3][2] += transOffset.z;
    }
    else if (u.transitionState == 2)
    {
        vec4 deltaOffset = -deltaTrans * (1.0 - segmentStart[vars.direction]);

        translation[3][0] += deltaOffset.x;
        translation[3][1] += deltaOffset.y;
        translation[3][2] += deltaOffset.z;
    }

    output.position = mul(tmpPos, translation);

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

    return output;
}

float4 pixelShader(vertexOut input) : SV_TARGET
{
    vec4 outColor = getColor(inputTex0, input.tex1);
    float alpha = u.baseAlpha;

#ifdef IS_MEDIA_FILL
    alpha *= outColor.a;
    outColor = inputTex1.Sample(linearSampler, input.tex2);
#endif

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

    multiplyOpacity(outColor, alpha);

#ifdef SHOW_SEGMENT_OUTLINE
    showSegmentOutline(outColor);
#endif

    return outColor;
}
