//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//  enbeffectpostpass template for FO4 by kingeric1992
//                                                  Nov.10.2017
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#define ENB_NAME "Film Workshop"

//enable advanced UI options (comment out to disable)
#define UI_FG_ADV
#define UI_VG_ADV
#define UI_BB_ADV
#define UI_CW_ADV
#define UI_LD_ADV

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// include functions
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define  ENBINTERNALS_POSTPASS
#include "Shaders/internals/enbInternals.fxh"
#include "Shaders/internals/Utilities.fxh"

//function name == file name,
#include "Shaders/enbeffectpostpass/PerlinNoise.fxh"
#include "Shaders/enbeffectpostpass/HSLShift.fxh"
#include "Shaders/enbeffectpostpass/Curve.fxh"
#include "Shaders/enbeffectpostpass/CLut.fxh"
#include "Shaders/enbeffectpostpass/ColorWheel.fxh"
#include "Shaders/enbeffectpostpass/FilmGrain.fxh"
#include "Shaders/enbeffectpostpass/PostCC.fxh"
#include "Shaders/enbeffectpostpass/Sepia.fxh"
#include "Shaders/enbeffectpostpass/Border.fxh"
#include "Shaders/enbeffectpostpass/Vibrance.fxh"
#include "Shaders/enbeffectpostpass/ToneMap.fxh"  //this is not HDR->LDR tonemapping
#include "Shaders/enbeffectpostpass/Gradient.fxh"     //need to check this
#include "Shaders/enbeffectpostpass/ChromaDistort.fxh"

#ifndef UI_FG_ADV
#define UI_FG_ADV static const
#endif

#ifndef UI_VG_ADV
#define UI_VG_ADV static const
#endif

#ifndef UI_CW_ADV
#define UI_CW_ADV static const
#endif

#ifndef UI_BB_ADV
#define UI_BB_ADV static const
#endif

#ifndef UI_LD_ADV
#define UI_LD_ADV static const
#endif

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  widgets toggle
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          int    Widgets        <string UIName="|- Widgets ---------------------";   float UIMin=0; float UImax=0;> = 0;
          bool   CLut_Widget    <string UIName="|    (Shift + L)  Film Stocks";              > = false;
          bool   CW_Widget      <string UIName="|    (Shift + C)  Color Wheels";       > = false;
          bool   HSL_Widget     <string UIName="|    (Shift + H)  HSL Widget";              > = false;
          bool   Curve_Widget   <string UIName="|    (Shift + U)  Curves Widget";            > = false;
          bool   Scope_Widget   <string UIName="|    (Shift + S)  Scopes";            > = false;
          int    empty0         <string UIName="|";                                  float UIMin=0; float UImax=0;> = 0;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Camera & Lens Params
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          int    TM_Enable      <string UIName="|- Camera ----------------------";   float UIMin=0.0; float UImax=0.0;> = 0;
          int sharp_strength_amt   <string UIName="|    Sharpening";            string UIWidget="Spinner"; float UIMin=0; float UIMax=30; float UIStep=1;> = {6};
          float  TM_Exposure    <string UIName="|    Exposure";                      float UIMin=-2;  float UIMax=2;  > = 0.0;
          float  TM_Gamma       <string UIName="|    Gamma";                         float UIMin=0.1; float UIMax=2;  > = 1.0;
          int    TM_ColTemp     <string UIName="|    Color Temperature";             float UIMin=-30; float UIMax=30; > = 0;
          int    TM_Tint        <string UIName="|    Tint";                          float UIMin=-30; float UIMax=30; > = 0;
          int    VB_Strength    <string UIName="|    Vibrance";             float UIMin=-100;float UIMax=100;> = 0;
          bool   BW_Enable      <string UIName="|    Enable Monochrome (B&W)";   > = false;
          bool   TM_FogFix      <string UIName="|    TFC Gamma Compensation";        > = false;
          int    empty1         <string UIName="| ";                                 float UIMin=0; float UImax=0;    > = 0;
          int   LF_Banner      <string UIName="|- Lens ----------";   float UIMin=0; float UImax=0;> = 0;
          bool ENABLE_DISTORTION <string UIName = "|    Enable Lens Distortion";> = {false};
          int LCAStrengthAmt <string UIName="|    Chromatic Aberration";               string UIWidget="spinner"; float UIMin=0; float UIMax=50; float UIStep=1;> = {15};
          int LCADistAmt <string UIName="|    Lens Distortion Amount";              string UIWidget="spinner"; float UIMin=-150; float UIMax=150; float UIStep=1;> = {50};
          bool   LF_Enable      <string UIName="|    Enable Lens Filter";   > = false;
          int    LF_Density     <string UIName="|    Filter Density";                float UIMin=0;   float UIMax=100;> = 0;
          float3 LF_Color       <string UIName="|    Filter Color";                  string UIWidget="color";         > = {1.0, 1.0, 1.0};

          int    empty95         <string UIName="|                  ";                                 float UIMin=0; float UImax=0;    > = 0;

          #define LCAStrength LCAStrengthAmt * 0.01
          #define fFisheyeDistortionCubic 0.0
          //#define fFisheyeDistortion lerp((LCAStrength * 0.1), lerp((LCADistAmt * 0.003), ((LCADistAmt * -1) * 0.0015), (LCADistMode - 1.0)), ENABLE_DISTORTION)
          //#define fFisheyeZoom lerp(0.5 - (fFisheyeDistortionCubic * 0.75), lerp(0.5 + (LCADistAmt * 0.00076), 0.5 - (LCADistAmt * 0.00019), (LCADistMode - 1.0)), ENABLE_DISTORTION)
          #define fFisheyeDistortion lerp((LCAStrength * 0.1), LCADistAmt * (LCADistAmt > 0.0? 0.003: 0.0015), ENABLE_DISTORTION)
          #define fFisheyeZoom lerp(0.5 - (fFisheyeDistortionCubic * 0.75), 0.5 + LCADistAmt * (LCADistAmt > 0.0? 0.00076 : 0.00019), ENABLE_DISTORTION)
          #define fFisheyeColorshift 0.0
          #define sharp_strength lerp((sharp_strength_amt * 0.1), ((sharp_strength_amt * 0.1) + (abs(LCADistAmt) * 0.01)), ENABLE_DISTORTION)
          #define pattern 2
          #define sharp_clamp 0.028
          #define offset_bias 1.0
          #define CoefLuma float3(0.2126, 0.7152, 0.0722)

          //#define sharp_strength sharp_strength_amt * 0.1
          #include "Shaders/enbeffectpostpass/LensCA.fxh"
          #include "Shaders/enbeffectpostpass/LumaSharpen.fxh"

          #define pattern 2
          #define sharp_clamp 0.028
          #define offset_bias 1.0
          #define CoefLuma float3(0.2126, 0.7152, 0.0722)

          bool   LD_Enable = false;
          float  LD_Ratio = 1.0;
          float  LD_Scale = 0.0;
          float  LD_Feather = 0.5; //edge feather when barral distort
          float  LD_Disperse = 0.0;
          bool   VG_Enable = false;
          float  VG_Ratio = 1.78;
          float  VG_Scale = 5;
          float  VG_Feather = 0.10;
          float3 VG_Color = {0.0, 0.0, 0.0};
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  FilmGrain Params
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          int   FG_Banner      <string UIName="|- Film Grain ------------";   float UIMin=0; float UImax=0;> = 0;
          bool   FG_Enable      <string UIName="|    Enable Film Grain";   > = false;
UI_FG_ADV int    FG_StrengthN   <string UIName="|    Negative Grain Amount (Coarse)";float UIMin=0; float UIMax=100.0;> = 15;
 float   FG_SizeN  = 2.40;
 int    FG_FreqN = 100;
 float  FG_colWeightN = 0.50;
 float  FG_lumWeightN = 0.25;
UI_FG_ADV int    FG_StrengthP   <string UIName="|    Print Grain Amount (Fine)";     float UIMin=0; float UIMax=100.0;> = 30;
 float  FG_SizeP = 1.4;
 int    FG_FreqP = 100;
 float  FG_colWeightP = 0.40;
 float  FG_lumWeightP = 0.80;
          int    empty2         <string UIName="|  ";                                float UIMin=0; float UImax=0;    > = 0;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  ColorGrading Params
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          //int    VB_Enable      <string UIName="|- Vibrance --------------------";   float UIMin=0.0; float UImax=0.0;> = 0;
          int    LV_Enable      <string UIName="|- Level Adjustments -----------";   float UIMin=0.0; float UImax=0.0;> = 0;
          int    LV_inBlack     <string UIName="|    Lift Shadows";                  float UIMin=0;   float UIMax=100;> = 0;
          int    LV_inWhite     <string UIName="|    Drop Heighlights";              float UIMin=185; float UIMax=255;> = 255;
          int    LV_calBlack    <string UIName="|    Crush Shadows (CLIPPING)";      float UIMin=0;   float UIMax=100;> = 0;
          int    LV_calWhite    <string UIName="|    Expand Highlights (CLIPPING)";  float UIMin=0;   float UIMax=60; > = 0; //check this
          int    empty13         <string UIName="|       ";                                 float UIMin=0; float UImax=0;    > = 0;
          int   AGE_Banner     <string UIName="|- Age Filter ---------------";   float UIMin=0; float UImax=0;    > = 0;
          bool   AGE_Enable     <string UIName="|    Enable Filter";   > = false;
          int    AGE_Mode       <string UIName="|    Filter Modes (Sepia, Magenta)"; float UIMin=0;   float UIMax=1;  > = 0;
          int    AGE_Strength   <string UIName="|    Age Amount";                 float UIMin=0;   float UIMax=100;> = 0;
          int    empty17         <string UIName="|                       ";                                 float UIMin=0; float UImax=0;    > = 0;
          int   BB_Banner      <string UIName="|- Image Format ---------------";   float UIMin=0.0; float UImax=0.0;> = 0;
          bool   BB_Enable      <string UIName="|    Enable Letterbox/Pillarbox";   > = true;
          float  BB_Ratio       <string UIName="|    Aspect Ratio";                  float UIMin=0.0; float UIMax=10; > = 2.35;
          float  BB_Feather = 0.0;
          float3 BB_Color = {0.0, 0.0, 0.0};
          int    empty7         <string UIName="|              ";                           float UIMin=0.0; float UImax=0.0;> = 0;
          int    UI_Internals   <string UIName="|- Plugin Internals DO NOT CHANGE ------------";   float UIMin=0.0; float UImax=0.0;> = 0;
          float  CW_lowrange = 0.3333;
          float  CW_highrange = 0.5555;
          float  CW_overlap = 0.25;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Plugin Internals (these are used to passing data, not to operate by user)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

float3 CW_Lift      <string UIName="CW Lift";     string UIWidget="vector";> = {0.0, 0.0, 0.0};
float3 CW_Gamma     <string UIName="CW Gamma";    string UIWidget="vector";> = {0.0, 0.0, 0.0};
float3 CW_Gain      <string UIName="CW Gain";     string UIWidget="vector";> = {0.0, 0.0, 0.0};
float3 CW_Offset    <string UIName="CW Offset";   string UIWidget="vector";> = {0.0, 0.0, 0.0};
float3 CW_Highlight <string UIName="CW Highlight";string UIWidget="vector";> = {0.0, 0.0, 0.0};
float3 CW_Midtone   <string UIName="CW Midtone";  string UIWidget="vector";> = {0.0, 0.0, 0.0};
float3 CW_Shadow    <string UIName="CW Shadow";   string UIWidget="vector";> = {0.0, 0.0, 0.0};

float3 HSL_Red      <string UIName="HSL Red";     string UIWidget="color"; > = {0.75, 0.25, 0.25};
float3 HSL_Orange   <string UIName="HSL Orange";  string UIWidget="color"; > = {0.75, 0.50, 0.25};
float3 HSL_Yellow   <string UIName="HSL Yellow";  string UIWidget="color"; > = {0.75, 0.75, 0.25};
float3 HSL_Green    <string UIName="HSL Green";   string UIWidget="color"; > = {0.25, 0.75, 0.25};
float3 HSL_Cyan     <string UIName="HSL Cyan";    string UIWidget="color"; > = {0.25, 0.75, 0.75};
float3 HSL_Blue     <string UIName="HSL Blue";    string UIWidget="color"; > = {0.25, 0.25, 0.75};
float3 HSL_Purple   <string UIName="HSL Purple";  string UIWidget="color"; > = {0.50, 0.25, 0.75};
float3 HSL_Magenta  <string UIName="HSL Magenta"; string UIWidget="color"; > = {0.75, 0.25, 0.75};

int4      CLut_ParamN   <string UIName="CLut ParamN";> = 0; // { ID, format, preview ID, preview format }
int4      CLut_ParamP   <string UIName="CLut ParamP";> = 0; // { ID, format, preview ID, preview format }
int4      CLut_ParamC   <string UIName="CLut ParamC";> = 0; // { ID, format, preview ID, preview format }
Texture2D CLut_TexN     <string UIName="CLut TexN";     string ResourceName="Dummy.bmp";>;
Texture2D CLut_TexP     <string UIName="CLut TexP";     string ResourceName="Dummy.bmp";>;
Texture2D CLut_TexC     <string UIName="CLut TexC";     string ResourceName="Dummy.bmp";>;
Texture2D CLut_TexN_tmp <string UIName="CLut TexN tmp"; string ResourceName="Dummy.bmp";>;
Texture2D CLut_TexP_tmp <string UIName="CLut TexP tmp"; string ResourceName="Dummy.bmp";>;
Texture2D CLut_TexC_tmp <string UIName="CLut TexC tmp"; string ResourceName="Dummy.bmp";>;

bool   HSL_Enable     <string UIName="HSL Enable";   > = true;
bool   Curve_Enable   <string UIName="Curves Enable";   > = true;
bool   CW_Enable      <string UIName="Wheels Enable";   > = true;

Texture2D Curve_Tex   <string UIName="Curve Tex"; string ResourceName="Dummy.bmp";>; //use cubic

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Input Items
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  These are here for quick adjustments

static CLutStruct CLutData_N     = { step(1, CLut_ParamN.x),-step(0, CLut_ParamN.z), CLut_ParamN.y, CLut_TexN };
static CLutStruct CLutData_P     = { step(1, CLut_ParamP.x),-step(0, CLut_ParamP.z), CLut_ParamP.y, CLut_TexP };
static CLutStruct CLutData_C     = { step(1, CLut_ParamC.x),-step(0, CLut_ParamC.z), CLut_ParamC.y, CLut_TexC };

static CLutStruct CLutData_N_tmp = { step(1, CLut_ParamN.z), step(0, CLut_ParamN.z), CLut_ParamN.w, CLut_TexN_tmp };
static CLutStruct CLutData_P_tmp = { step(1, CLut_ParamP.z), step(0, CLut_ParamP.z), CLut_ParamP.w, CLut_TexP_tmp };
static CLutStruct CLutData_C_tmp = { step(1, CLut_ParamC.z), step(0, CLut_ParamC.z), CLut_ParamC.w, CLut_TexC_tmp };

static FilmGrainStruct FilmGrainData_N = {
    FG_Enable, 1.0,
    FG_SizeN, FG_FreqN, FG_colWeightN, FG_lumWeightN, FG_StrengthN * 0.0005
};

static FilmGrainStruct FilmGrainData_P = {
    FG_Enable, 1.0,
    FG_SizeP, FG_FreqP, FG_colWeightP, FG_lumWeightP, FG_StrengthP * 0.0005
};

static CurveStruct CurveData = {
    Curve_Enable, 1.0, Curve_Tex
};

static HSLShiftStruct HSLShiftDataA = {
    HSL_Enable * BW_Enable, 1.0, {
        float4(HSL_Red,     0),float4(HSL_Orange,  30),float4(HSL_Yellow, 60),
        float4(HSL_Green, 120),float4(HSL_Cyan,   180),float4(HSL_Blue,  240),
        float4(HSL_Purple,270),float4(HSL_Magenta,300),float4(HSL_Red,   360)
    }
};

static HSLShiftStruct HSLShiftDataB = {
    HSL_Enable * !BW_Enable, 1.0, {
        float4(HSL_Red,     0),float4(HSL_Orange,  30),float4(HSL_Yellow, 60),
        float4(HSL_Green, 120),float4(HSL_Cyan,   180),float4(HSL_Blue,  240),
        float4(HSL_Purple,270),float4(HSL_Magenta,300),float4(HSL_Red,   360)
    }
};

static ColorWheelStruct ColorWheelData = {
    CW_Enable,   1.0,
    CW_lowrange, CW_highrange, CW_overlap,
    CW_Shadow,   CW_Midtone,   CW_Highlight,
    CW_Lift,     CW_Gamma,     CW_Gain,      CW_Offset
};

static PostCCStruct MonochromeData = {
    BW_Enable, 1.0,
    F4_ONE,
    F3_ZERO, F3_ONE, F3_ZERO, F3_ONE
};

//double check this
static PostCCStruct LevelData = {
    true, 1.0,
    F4_ZERO,
    -(LV_inBlack.xxx + 12.0)/255.0,
    1.0 + (235.0 - LV_inWhite.xxx)/255.0,
    F3_ZERO, F3_ONE
};

//double check this
static PostCCStruct Level2Data = {
    true, 1.0,
    F4_ZERO,
    (LV_calBlack.xxx + 7.0)/255.0,
    1.0 - LV_calWhite.xxx/255.0, F3_ZERO, F3_ONE
};

//mode == 0: sepia, mode == 1 magenta
static SepiaStruct AgeData = {
    AGE_Enable, 1.0,
    AGE_Strength * 0.0255, AGE_Strength * 0.0255 * -0.09,
    AGE_Mode > 0 ? float3(1.000, 0.920, 0.945): float3(1.000, 0.945, 0.900)
};

//[0, 0.5]
static float2 BB_Offset(){
    return float2(min(1.0, ScreenSize.w * BB_Ratio),
                  min(1.0, ScreenSize.z / BB_Ratio))*0.5;
}

//BB for Black Bar
static BorderStruct BorderData = {
    BB_Enable, 1.0,
    0.5-BB_Offset(), 0.5+BB_Offset(),
    BB_Feather * 100.0,
    BB_Color
};

static float2 VG_Rcoord( float2 uv )
{
    uv  -= 0.5;
    uv  *= max(float2(ScreenSize.z / VG_Ratio, ScreenSize.w * VG_Ratio), 1.0) * 2.0;
    uv.x = length(uv) / VG_Scale + 0.5;
    uv.y = 0.5;
    return uv;
}

static BorderStruct VignetteData = {
    VG_Enable, 1.0,
    {0.0, 0.0}, {1.0, 1.0},
    VG_Feather * 100.0,
    VG_Color
};

static ToneMapStruct ToneMapData = {
    true, 1.0,
    TM_Exposure + (TM_ColTemp + TM_Tint) * 0.005,
    lerp( TM_Gamma + 0.05, TM_Gamma + 0.22, TM_FogFix),
    0.0,
    0.0,
    2.55,
    TM_ColTemp * 0.001 * float3(0.333, 0.666, 1.0  ) +
    TM_Tint    * 0.001 * float3(1.0,   0.333, 0.666),
};

static ToneMapStruct LensFilterData = {
    LF_Enable, 1.0,
    1.0 - (LF_Density * 0.005),
    1.0,
    0.0,
    0.0,
    LF_Density * -0.001, LF_Color
};

static VibranceStruct VibranceData = {
    true, 1.0,
    1.0,
    float(VB_Strength * 0.01).xxx
};

static BorderStruct DistortBorderData = {
    LD_Enable, 1.0,
    {0.0, 0.0}, {1.0, 1.0},
    LD_Feather * 100.0 * saturate(LD_Scale),
    F3_ZERO
};

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
    Check these effect Later:
    float4(LumaSharpenPass(IN.uv.xy), 1.0);
    res.rgb = msDither(res.rgb,  IN.uv.xy);
*/

struct PostProcessIO {
    float4 pos : SV_POSITION;
    float4 uv  : TEXCOORD0;
};

void VS_PostProcess( inout PostProcessIO IO)
{
    IO.pos.w = 1.0;
    IO.uv.zw = IO.uv.xy - 0.5; //centered coord
}


float4 PS_CA( PostProcessIO IN) : SV_Target
{
  float4 res = TextureColor.Sample(SamplerLinear, IN.uv.xy);
  if(ENABLE_DISTORTION == false) return res;
  float2 FishEyeRcoord, FishEyeGcoord, FishEyeBcoord;
  FishEyePass(IN.uv.xy, FishEyeRcoord, FishEyeGcoord, FishEyeBcoord);
  return float4( LensCA(FishEyeRcoord).r, LensCA(FishEyeGcoord).g, LensCA(FishEyeBcoord).b, 1.0); //24 taps
}

//pre-vignette
float4 PS_PostProcess_P0_Alt( PostProcessIO IN) : SV_Target
{
    float4 res     = TextureColor.Sample(SamplerLinear, IN.uv.xy);        //rgb10a2
    float2 vg_uv   = VG_Rcoord(IN.uv.xy);
           res.rgb = lerp(res.rgb, Border(vg_uv, res.rgb, VignetteData),   VignetteData.Weight);
    return res;
}

//test "if" for performance difference
float4 PS_PostProcess_P1( PostProcessIO IN ) : SV_Target
{
    if(CLutData_C.State) discard;
    if(CLutData_C_tmp.State && (IN.uv.x >= 0.5)) discard; //discard half the screen

    float2 uv    = IN.uv.xy;
    float4 res   = TextureColor.Sample(SamplerLinear, uv); //rgb10a2
    float3 col   = res.rgb;
    float2 vg_uv = VG_Rcoord(uv);

    float splitScreen = step(0.5, IN.uv.x);

    col 		= float4(LumaSharpenPass(IN.uv.xy), 1.0);
    col = lerp(col, Border(vg_uv, col, VignetteData),   VignetteData.Weight);
    col = lerp(col, ToneMap(      col, LensFilterData), LensFilterData.Weight);
    col = lerp(col, ToneMap(      col, ToneMapData),    ToneMapData.Weight);
    col = lerp(col, Vibrance(     col, VibranceData),   VibranceData.Weight);
    col = lerp(col, CLut(         col, CLutData_N),     CLutData_N.Weight     * splitScreen + 1.0);
    col = lerp(col, CLut(         col, CLutData_N_tmp), CLutData_N_tmp.Weight * splitScreen); //preview
    col = lerp(col, FilmGrain(uv, col, FilmGrainData_N),FilmGrainData_N.Weight);

//Rec709 grading started
    col = pow(Log2Lin(col), 0.45);
    col = lerp(col, Curve(        col, CurveData),      CurveData.Weight);
    col = lerp(col, HSLShift(     col, HSLShiftDataA),  HSLShiftDataA.Weight);
    col = lerp(col, PostCC(       col, MonochromeData), MonochromeData.Weight);
    col = lerp(col, ColorWheel(   col, ColorWheelData), ColorWheelData.Weight);
    col = lerp(col, HSLShift(     col, HSLShiftDataB),  HSLShiftDataB.Weight);
//    col = lerp(col, Gradient(     col, GradientData),   GradientData.Weight);  //WIP
    col = Lin2Log(pow(col, 2.2));
//Rec709 grading ended

    col = lerp(col, PostCC(       col, LevelData),      LevelData.Weight);
    col = lerp(col, FilmGrain(uv, col, FilmGrainData_P),FilmGrainData_P.Weight);
    col = lerp(col, CLut(         col, CLutData_P),     CLutData_P.Weight     * splitScreen + 1.0);
    col = lerp(col, CLut(         col, CLutData_P_tmp), CLutData_P_tmp.Weight * splitScreen); //preview
//Linear
    col = lerp(col, PostCC(       col, Level2Data),     Level2Data.Weight);
    col = lerp(col, Sepia(        col, AgeData),        AgeData.Weight);
    col = lerp(col, Border(   uv, col, BorderData),     BorderData.Weight);

    res.rgb = col;
    return res;
}

float4 PS_PostProcess_P1_Alt(PostProcessIO IN) : SV_Target
{
    if(!CLutData_C.State && !CLutData_C_tmp.State) discard; //if both false
    if(!CLutData_C.State && (IN.uv.x < 0.5)) discard;       //discard leftside

    float2 uv  = IN.uv.xy;
    float4 res = TextureColor.Sample(SamplerLinear, IN.uv.xy);
    float3 col = res.rgb;

    float splitScreen = step(0.5, IN.uv.x);

    col = lerp(col, Border(   uv, col, VignetteData),   VignetteData.Weight);
    col = lerp(col, FilmGrain(uv, col, FilmGrainData_P),FilmGrainData_P.Weight); //this might be too heavy?
    col = lerp(col, CLut(         col, CLutData_C),     CLutData_C.Weight     * splitScreen + 1.0);
    col = lerp(col, CLut(         col, CLutData_C_tmp), CLutData_C_tmp.Weight * splitScreen); //preview
    col = lerp(col, Border(   uv, col, BorderData),     BorderData.Weight);

    res.rgb = col;
	return res;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// TECHNIQUES
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

technique11 postpass < string UIName = ENB_NAME; >
{
  pass p0  {
    SetVertexShader(CompileShader(vs_5_0, VS_PostProcess()));
    SetPixelShader(CompileShader(ps_5_0, PS_CA()));
  }
}

technique11 postpass1
{
    pass p0  {
        SetVertexShader(CompileShader(vs_5_0, VS_PostProcess()));
        SetPixelShader(CompileShader(ps_5_0, PS_PostProcess_P1()));
    }

    pass p1  {
        SetVertexShader(CompileShader(vs_5_0, VS_PostProcess()));
        SetPixelShader(CompileShader(ps_5_0, PS_PostProcess_P1_Alt()));
    }
}
