/* YUV -> RGB shader for 4:2:2 I420 format (+ rounded rectangles)
 *                                                                            
 * MIT X11 license, Copyright (c) 2007 by:                               
 *                                                                            
 * Authors:                                                                   
 *      Michael Dominic K. <mdk@mdk.am>
 *                                                                            
 * Permission is hereby granted, free of charge, to any person obtaining a   
 *  copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation  
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   
 * and/or sell copies of the Software, and to permit persons to whom the      
 * Software is furnished to do so, subject to the following conditions:       
 *                                                                            
 * The above copyright notice and this permission notice shall be included    
 * in all copies or substantial portions of the Software.                     
 *                                                                            
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN  
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     
 *                                                                            
 */

struct pixel_in {                                                                                                                                                                                      
    float2 texcoord1 : TEXCOORD0;
    float2 texcoord2 : TEXCOORD1;
    float2 texcoord3 : TEXCOORD2;
    uniform samplerRECT texture1 : TEXUNIT0;
    uniform samplerRECT texture2 : TEXUNIT1;
    uniform samplerRECT texture3 : TEXUNIT2;
    float4 color : COLOR0;                                                                                                                                                                          
};

struct pixel_out {                                                                                                                                                                                     
    float4 color : COLOR0;                                                                                                                                                                          
};  

pixel_out
main (pixel_in IN)
{
    pixel_out OUT;

    float3 pre;

    pre.r = texRECT (IN.texture1, IN.texcoord1).x - (16.0 / 256.0);
    pre.g = texRECT (IN.texture2, IN.texcoord2).x - (128.0 / 256.0);
    pre.b = texRECT (IN.texture3, IN.texcoord3).x - (128.0 / 256.0);

    const float3 red = float3 (0.00456621, 0.0, 0.00625893) * 255.0;
    const float3 green = float3 (0.00456621, -0.00153632, -0.00318811) * 255.0;
    const float3 blue = float3 (0.00456621, 0.00791071, 0.0) * 255.0;

    OUT.color.r = dot (red, pre);
    OUT.color.g = dot (green, pre);
    OUT.color.b = dot (blue, pre);

    /* The stuff below is a poorman's-ugly-hacky rounded rectangles and
     * smooth borders implementation. Just replace it with:
       OUT.color.a = IN.color.a;
       if you're interested only in the YUV->RGB methodology */

    float r;

    if (IN.texcoord1.x < 21 && IN.texcoord1.y < 21) 
        r = (IN.texcoord1.x - 21) * (IN.texcoord1.x - 21) + (IN.texcoord1.y - 21) * (IN.texcoord1.y - 21);
    else if (IN.texcoord1.x > 619 && IN.texcoord1.y < 21)
        r = (IN.texcoord1.x - 619) * (IN.texcoord1.x - 619) + (IN.texcoord1.y - 21) * (IN.texcoord1.y - 21);
    else if (IN.texcoord1.x > 619 && IN.texcoord1.y > 459)
        r = (IN.texcoord1.x - 619) * (IN.texcoord1.x - 619) + (IN.texcoord1.y - 459) * (IN.texcoord1.y - 459);
    else if (IN.texcoord1.x < 21 && IN.texcoord1.y > 459)
       r = (IN.texcoord1.x - 21) * (IN.texcoord1.x - 21) + (IN.texcoord1.y - 459) * (IN.texcoord1.y - 459);

    /*
      The smooth borders require too much ALU instructions for the
      intel GMA915 chipset. Arne Caspari 11/30/2007
     */
    /*else if (IN.texcoord1.y < 21)
        r = (IN.texcoord1.y - 21) * (IN.texcoord1.y - 21);
    else if (IN.texcoord1.y > 219)
        r = (IN.texcoord1.y - 219) * (IN.texcoord1.y - 219);
    else if (IN.texcoord1.x < 21)
        r = (IN.texcoord1.x - 21) * (IN.texcoord1.x - 21);
    else if (IN.texcoord1.x > 379)
    r = (IN.texcoord1.x - 379) * (IN.texcoord1.x - 379);*/
    else
        r = 400;

    float v = clamp (r, 400, 440);
    v = (v - 400) / 40.0;

    OUT.color.a = IN.color.a * (1.0 - v);
    
    return OUT;
}
