// Software pixel shader generated by Wild Magic.
//
// var float4          kProjectedTCoord $vin.TEXCOORD0
// var float4          kPixelColor      $vout.COLOR
// var float           DepthBias        c[0]
// var sampler2DSHADOW ColorSampler     texunit 0
// var sampler2DSHADOW DepthSampler     texunit 1

#include "Wm4SoftRenderer.h"
#include "Wm4ColorRGBA.h"
#include "Wm4Vector4.h"

namespace Wm4
{

ColorRGBA p_ShadowMap (const float* afRegister, SoftSampler** apkSampler,
    const float* afInPixel)
{
    // Get registers.
    float fDepthBias = afRegister[0];

    // Get samplers.
    SoftSampler& rkColorSampler = *apkSampler[0];
    SoftSampler& rkDepthSampler = *apkSampler[1];

    // Get input values.
    const float* afProjTCoord = *(const Vector4f*)&afInPixel[0];

    // *** program ***

    // The samplers perform the perspective divide in the texture coordinate.
    ColorRGBA kColor = rkColorSampler(afProjTCoord);
    ColorRGBA kDepth = rkDepthSampler(afProjTCoord);

    float fPointDepth = afProjTCoord[2]/afProjTCoord[3];
    float fBiasedDiff = fPointDepth - kDepth[0] - fDepthBias;
    ColorRGBA kPixelColor;
    if (fBiasedDiff >= 0.0f)
    {
        kPixelColor[0] = 0.0f;
        kPixelColor[1] = 0.0f;
        kPixelColor[2] = 0.0f;
        kPixelColor[3] = kColor[3];
    }
    else
    {
        kPixelColor = kColor;
    }

    return kPixelColor;
}

WM4_IMPLEMENT_PPROGRAM(ShadowMap);
WM4_REGISTER_PPROGRAM(ShadowMap);

}
