#include "metalShaderTypes.h"
#include "alpha.h"
#include "fadeHelper.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 texColorA = getColor(inputTex0, input.tex1);
    vec4 texColorB = getColor(inputTex1, input.tex2);
    vec4 burnColor = vars.burnColor;
#ifndef PRE_MULT
    multiplyAlpha(texColorA);
    multiplyAlpha(texColorB);
#endif
    multiplyAlpha(burnColor);  // burnColor always comes in straight alpha
    vec4 outColor;
    colorBurnAB(outColor, texColorA, texColorB, burnColor, u.transitionProgress);
    float alpha = u.baseAlpha;
#ifdef USE_MASK
    alpha *= float(inputTexMask.sample(linearSampler, input.texMask).a);
#endif
    outColor *= alpha;
#ifndef PRE_MULT
    divideAlpha(outColor);
#endif
    return outColor;
}
