#ifndef _CURVE_FXH_
#define _CURVE_FXH_
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//                                                                      
//  1d Curve LUT by kingeric1992        
//                                              update: Nov.16.2017
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include "enbInternals.fxh"
#include "Utilities.fxh"

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Internals
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Function
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


struct CurveStruct {
    bool      State;
    float     Weight;
    Texture2D Tex;
};

//performance on dependent read?
//or using hermite interpolator?
float3 Curve( float3 col, CurveStruct IN)
{
    if(!IN.State) return col;
    float k = 255.0/256.0;
    float o = 0.5 / 256.0;    
    
    col    = saturate(RGB_to_HSL(saturate(col)));
    col.x += IN.Tex.SampleLevel(SamplerLinear, float2(col.x * k + o, 0.5 / 9.0), 0).x; //Hue vs Hue
    col.x  = frac(col.x + 1.0);
    col.y += IN.Tex.SampleLevel(SamplerLinear, float2(col.x * k + o, 1.5 / 9.0), 0).x; //Hue vs Sat
    col.z += IN.Tex.SampleLevel(SamplerLinear, float2(col.x * k + o, 2.5 / 9.0), 0).x; //Hue vs Lum
    col.yz = saturate(col.yz);
    col.y += IN.Tex.SampleLevel(SamplerLinear, float2(col.z * k + o, 3.5 / 9.0), 0).x; //Lum vs Sat
    col.y  = saturate(col.y);
    col.y += IN.Tex.SampleLevel(SamplerLinear, float2(col.y * k + o, 4.5 / 9.0), 0).x; //Sat vs Sat
    col    = saturate(HSL_to_RGB(saturate(col)));
    
    col.x  = IN.Tex.SampleLevel(SamplerLinear, float2(col.x * k + o, 5.5 / 9.0), 0).x; //RGB   vs RGB
    col.y  = IN.Tex.SampleLevel(SamplerLinear, float2(col.y * k + o, 5.5 / 9.0), 0).x; //RGB   vs RGB
    col.z  = IN.Tex.SampleLevel(SamplerLinear, float2(col.z * k + o, 5.5 / 9.0), 0).x; //RGB   vs RGB
    col    = saturate(col * 0.5 + 0.5);
    
    col.x  = IN.Tex.SampleLevel(SamplerLinear, float2(col.x * k + o, 6.5 / 9.0), 0).x; //Red   vs Red
    col.y  = IN.Tex.SampleLevel(SamplerLinear, float2(col.y * k + o, 7.5 / 9.0), 0).x; //Green vs Green
    col.z  = IN.Tex.SampleLevel(SamplerLinear, float2(col.z * k + o, 8.5 / 9.0), 0).x; //Blue  vs Blue
    
    return saturate(col * 0.5 + 0.5);
}

#endif //_CURVE_FXH_