/*===============================================================================*\
|#######################    [PsxFX Shader Suite v2.00]    ########################|
|#########################         By Asmodean          ##########################|
||                                                                               ||
||          This program is free software; you can redistribute it and/or        ||
||          modify it under the terms of the GNU General Public License          ||
||          as published by the Free Software Foundation; either version 2       ||
||          of the License, or (at your option) any later version.               ||
||                                                                               ||
||          This program is distributed in the hope that it will be useful,      ||
||          but WITHOUT ANY WARRANTY; without even the implied warranty of       ||
||          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        ||
||          GNU General Public License for more details. (c)2016                 ||
||                                                                               ||
|#################################################################################|
\*===============================================================================*/
#version 150 compatibility

#extension GL_NV_gpu_shader5 : enable
#extension GL_ARB_gpu_shader5 : enable
#extension GL_ARB_shader_precision : enable
#extension GL_ARB_sample_shading : enable
#extension GL_ARB_texture_rectangle : enable
#extension GL_ARB_shader_storage_buffer_object : enable
#extension GL_ARB_shader_image_load_store : enable
#extension GL_ARB_bindless_texture : enable
#extension GL_ARB_shading_language_420pack : enable


/*------------------------------------------------------------------------------
                            [DEFINITIONS|GLOBALS]
------------------------------------------------------------------------------*/

// NOTE: For intended results;
// Only enable (set to 1) any of the following if you also have it enabled in the fragment/slf shader.
// Only use one of them at a time.

#define XBR_ENABLED             0       //[0 or 1] Change to 1 to enable the xBR-Lv2-3D vertex code path.
#define SFAA_ENABLED            0       //[0 or 1] Change to 1 to enable the SFAA vertex code path.
#define DDT_ENABLED             0       //[0 or 1] Change to 1 to enable the DDT vertex code path.

//Uniforms
uniform vec4 OGL2Param;
uniform vec4 OGL2Size;
uniform vec4 OGL2InvSize;
//FXAA texel offset
uniform float texelStep = 0.5;

//VS --> PS
out vec4 TexCoord;
out vec4 Vertex;
out vec3 eyeVect;
out vec3 Normal;
out vec4 rcpPos;
out vec4 Position;
out vec3 lightPos;
out vec3 lightVect;
out float Distance;

/*------------------------------------------------------------------------------
                        [MAIN() & VERTEX CODE SECTION]
------------------------------------------------------------------------------*/

void main()
{
    // Texcoord, & Position assignment
    TexCoord = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    Position = ftransform();
    
    // Setup V, N, L for passing lighting params to PS
    Vertex = gl_ModelViewMatrix * gl_Vertex;
    Normal = gl_NormalMatrix * gl_Normal;
    
    lightPos = gl_LightSource[0].position.xyz;
    lightVect = lightPos - Position.xyz;
    Distance = length(lightVect);
    eyeVect = Position.xyz;

    // FXAA zw uv0 coordinates /w offset
    rcpPos = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    rcpPos.zw = rcpPos.xy - (OGL2InvSize.xy * texelStep);

    // Assignment to gl built-in variables
    gl_TexCoord[0] = TexCoord; gl_ClipVertex = Vertex; gl_Position = Position;

    gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;
    gl_TexCoord[2] = gl_TextureMatrix[2] * gl_MultiTexCoord2;
    gl_TexCoord[3] = gl_TextureMatrix[3] * gl_MultiTexCoord3;
    gl_TexCoord[4] = gl_TextureMatrix[4] * gl_MultiTexCoord4;
    gl_TexCoord[5] = gl_TextureMatrix[5] * gl_MultiTexCoord5;
    gl_TexCoord[6] = gl_TextureMatrix[6] * gl_MultiTexCoord6;
    gl_TexCoord[7] = gl_TextureMatrix[7] * gl_MultiTexCoord7;

    #if XBR_ENABLED == 1
    //  xBR VS code path
        vec2 OGLInvSize = vec2(1.0 / 1024.0, 1.0 / 512.0);
        
        float dx = OGLInvSize.x;
        float dy = OGLInvSize.y;

        gl_TexCoord[1] = gl_TexCoord[0].xxxy + vec4( -dx, 0, dx,-2.0*dy); // A1 B1 C1
        gl_TexCoord[2] = gl_TexCoord[0].xxxy + vec4( -dx, 0, dx,    -dy); //  A  B  C
        gl_TexCoord[3] = gl_TexCoord[0].xxxy + vec4( -dx, 0, dx,      0); //  D  E  F
        gl_TexCoord[4] = gl_TexCoord[0].xxxy + vec4( -dx, 0, dx,     dy); //  G  H  I
        gl_TexCoord[5] = gl_TexCoord[0].xxxy + vec4( -dx, 0, dx, 2.0*dy); // G5 H5 I5
        gl_TexCoord[6] = gl_TexCoord[0].xyyy + vec4(-2.0*dx,-dy, 0,  dy); // A0 D0 G0
        gl_TexCoord[7] = gl_TexCoord[0].xyyy + vec4( 2.0*dx,-dy, 0,  dy); // C4 F4 I4
        
    #elif DDT_ENABLED == 1
    //  DDT VS code path
        float dx = OGL2InvSize.x;
        float dy = OGL2InvSize.y;

        gl_TexCoord[1].xy = vec2(dx, 0.0); // F
        gl_TexCoord[1].zw = vec2(0.0, dy); // H
        gl_TexCoord[2].xy = gl_TexCoord[0].xy * vec2(dx, dy);

    #elif SFAA_ENABLED == 1
    //  SFAA VS code path
        float x = OGL2InvSize.x;
        float y = OGL2InvSize.y;

        vec2 dg1 = vec2(x, y), dg2 = vec2(-x, y);
        vec2 sd1 = dg1 * 0.5, sd2 = dg2 * 0.5;
        vec2 ddx = vec2(x, 0.0), ddy = vec2(0.0, y);

        gl_TexCoord[1].xy = gl_TexCoord[0].xy - sd1;
        gl_TexCoord[2].xy = gl_TexCoord[0].xy - sd2;
        gl_TexCoord[3].xy = gl_TexCoord[0].xy + sd1;
        gl_TexCoord[4].xy = gl_TexCoord[0].xy + sd2;
        gl_TexCoord[5].xy = gl_TexCoord[0].xy - dg1;
        gl_TexCoord[6].xy = gl_TexCoord[0].xy + dg1;
        gl_TexCoord[5].zw = gl_TexCoord[0].xy - dg2;
        gl_TexCoord[6].zw = gl_TexCoord[0].xy + dg2;
        gl_TexCoord[1].zw = gl_TexCoord[0].xy - ddy;
        gl_TexCoord[2].zw = gl_TexCoord[0].xy + ddx;
        gl_TexCoord[3].zw = gl_TexCoord[0].xy + ddy;
        gl_TexCoord[4].zw = gl_TexCoord[0].xy - ddx;
    #endif
}
