//=============================================================================
//// Shader uses position and texture
//=============================================================================
SamplerState samPoint
{
    Filter = MIN_MAG_MIP_POINT;
    AddressU = Mirror;
    AddressV = Mirror;
};

Texture2D gTexture;
int gPixelOffset = 1;

/// Create Depth Stencil State (ENABLE DEPTH WRITING)
DepthStencilState stencilState
{
    DepthEnable = true;
};
/// Create Rasterizer State (Backface culling) 
RasterizerState backCulling
{
    CullMode = BACK;
};

//IN/OUT STRUCTS
//--------------
struct VS_INPUT
{
    float3 Position : POSITION;
    float2 TexCoord : TEXCOORD0;

};

struct PS_INPUT
{
    float4 Position : SV_POSITION;
    float2 TexCoord : TEXCOORD1;
};


//VERTEX SHADER
//-------------
PS_INPUT VS(VS_INPUT input)
{
    PS_INPUT output = (PS_INPUT) 0;
    output.Position = float4(input.Position.xy, 0, 1);
    output.TexCoord = input.TexCoord;
	
    return output;
}


//PIXEL SHADER
//------------
float4 PS(PS_INPUT input) : SV_Target
{
    float2 textureSize;
    gTexture.GetDimensions(textureSize.x, textureSize.y);
    float2 pixelSize = 1.f / textureSize;
    
    float redChannel = gTexture.Sample(samPoint, input.TexCoord - gPixelOffset * float2(pixelSize.x, 0)).r;
    float greenChannel = gTexture.Sample(samPoint, input.TexCoord - gPixelOffset * float2(0, pixelSize.y)).g;
    float blueChannel = gTexture.Sample(samPoint, input.TexCoord + gPixelOffset * float2(pixelSize.x, 0)).b;
    return float4(redChannel, greenChannel, blueChannel, 1);
}


//TECHNIQUE
//---------
technique11 Grayscale
{
    pass P0
    {
        // Set states...
        SetVertexShader(CompileShader(vs_4_0, VS()));
        SetGeometryShader(NULL);
        SetPixelShader(CompileShader(ps_4_0, PS()));

        SetRasterizerState(backCulling);
        SetDepthStencilState(stencilState, 0);
    }
}

