
#include "metalShaderTypes.h"
#include "alpha.h"

typedef struct
{
    float4 burnColor;
} fxVars;

vertex vertexOut vertexFunc(uint vertexID [[ vertex_id ]],
                            const device txShaderVers* in[[ buffer(0) ]],
                            constant txShaderUniforms& uniforms[[ buffer(3) ]],
                            constant fxVars& vars[[ buffer(2)]])
{
    vertexOut out;

    out.position=in[vertexID].pos*uniforms.worldViewProj;

    out.tex1=in[vertexID].tex1;
    out.tex2=in[vertexID].tex2;
    out.texMask=in[vertexID].texMask;
    return out;
}

fragment float4 fragmentFunc(vertexOut input [[stage_in]]
                             ,texture2d<half> inputTex0 [[ texture(0) ]]
                             ,texture2d<half> inputTex1 [[ texture(1) ]]
                             ,texture2d<half> inputTexMask [[ texture(2) ]]
                             ,constant txShaderUniforms& u[[ buffer(2) ]]
                             ,constant subFXVars& subVars[[buffer(3)]]
                             ,constant fxVars& vars[[ buffer(1)]])
{
    vec4 outColor = u.transitionProgressLinear < 0.5
        ? getColor(inputTex1, input.tex2)
        : getColor(inputTex0, input.tex1);
    vec4 burnColor = vec4(0,0,0,0);

#ifndef PRE_MULT
    multiplyAlpha(outColor);
#endif

    outColor = u.transitionProgressLinear < 0.5
        ? mix(outColor, burnColor, u.transitionProgressLinear * 2.0)
        : mix(burnColor, outColor, (u.transitionProgressLinear - 0.5) * 2.0);

#ifdef USE_MASK
    multiplyOpacityPremultiplied(outColor, float(inputTexMask.sample(linearSampler, input.texMask).a));
#endif

#ifndef PRE_MULT
    divideAlpha(outColor);
#endif

    return outColor;
}
