Shader "BlurShader"
{
    Properties
    {
        _MainTex ("Main Tex", 2D) = "white" {}
        //_BlurIntensity("Intensity", Range(0, 20)) = 0.5
        _BlurTex("BlurTex", 2D) = "white" {}
        _Color("Color", color) = (1, 1, 1, 1.0)
    }
    SubShader
    {
        Cull Off ZWrite Off ZTest Always

        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" 

        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"

         struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
                o.vertex = vertexInput.positionCS;
                o.uv = v.uv;
                return o;
            }

            float4 blur(sampler2D tex, float2 uv, float4 dir, float4 tex_TexelSize)
            {
                float4 c = 0;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * .75)) * 126;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * 2.25)) * 84;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * 3.75)) * 36;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * 5.25)) * 9;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * 6.75)) * 1;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * .75)) * 126;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * 2.25)) * 84;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * 3.75)) * 36;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * 5.25)) * 9;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * 6.75)) * 1;
                return c / 512;
            }

#define BLUR_KERNEL 0

#if BLUR_KERNEL == 0

            const static int kTapCount = 3;
            const static float kOffsets[] = {
                -1.33333333,
                 0.00000000,
                 1.33333333
            };
            const static half kCoeffs[] = {
                 0.35294118,
                 0.29411765,
                 0.35294118
            };

#elif BLUR_KERNEL == 1

            const static int kTapCount = 5;
            const static float kOffsets[] = {
                -3.23076923,
                -1.38461538,
                 0.00000000,
                 1.38461538,
                 3.23076923
            };
            const static half kCoeffs[] = {
                 0.07027027,
                 0.31621622,
                 0.22702703,
                 0.31621622,
                 0.07027027
            };
#endif

            float4 blur2(sampler2D tex, float2 uv, float4 tex_TexelSize, float v)
            {
                float4 color = 0;
                for (int i = 0; i < kTapCount; i++)
                {
                    float2 coord = tex_TexelSize.xy * kOffsets[i] * v + uv;
                    half3 col = tex2D(tex, coord).xyz;
                    color.rgb += col * kCoeffs[i];
                }
                //color.rgb / = color.w + 1e-4;
                return color;
            }

            float4 blur3(sampler2D tex, float2 uv, float4 tex_TexelSize, float v)
            {
                const int kCount = 5;
                const float2 kTaps[] = {
                    float2(0.0,  0.0),
                    float2(0.9, -0.4),
                    float2(-0.9,  0.4),
                    float2(0.4,  0.9),
                    float2(-0.4, -0.9)
                };

                float4 color = 0;
                color.a = 1;
                for (int i = 0; i < kCount; i++)
                {
                    float2 coord = tex_TexelSize.xy * kTaps[i] * v + uv;
                    half3 col = tex2D(tex, coord).xyz;
                    color.rgb += col;
                }
                //color.rgb / = color.w + 1e-4;
                color = color * rcp(kCount);
                color.a = 1;
                return color;
            }

            static const half4 GaussWeight[7] =
            {
                half4(0.0205,0.0205,0.0205,0),
                half4(0.0855,0.0855,0.0855,0),
                half4(0.232,0.232,0.232,0),
                half4(0.324,0.324,0.324,1),
                half4(0.232,0.232,0.232,0),
                half4(0.0855,0.0855,0.0855,0),
                half4(0.0205,0.0205,0.0205,0)
            };

            float4 DownSmp(sampler2D tex, float2 uv, float4 tex_TexelSize)
            {
                float4 color = (0, 0, 0, 0);

                color += tex2D(tex, uv + tex_TexelSize.xy * half2(0.5, 0.5));
                color += tex2D(tex, uv + tex_TexelSize.xy * half2(-0.5h, -0.5h));
                color += tex2D(tex, uv + tex_TexelSize.xy * half2(0.5h, -0.5h));
                color += tex2D(tex, uv + tex_TexelSize.xy * half2(-0.5h, 0.5h));
                color = color / 4;
                color.a = 1;
                return color;
            }

            float4 blur5(sampler2D tex, float2 uv, float2 dir, float4 tex_TexelSize)
            {
                float4 color = 0;
                half2 weight = tex_TexelSize.xy * dir;
                half2 offsetUV = uv - weight * 3.0;
                for (int j = 0; j < 7; j++)
                {
                    half4 texCol = tex2D(tex, offsetUV);
                    color += texCol * GaussWeight[j];
                    offsetUV += weight;
                }
           
                color.a = 1.0f;
                return color;
            }


        ENDHLSL

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            //#include "UnityCG.cginc"
            sampler2D _MainTex;
            half4 _MainTex_TexelSize;
            float _BlurIntensity;
            half4 _Color;

            half4 frag(v2f i) : SV_Target
            {
                /*
                float4 oc = blur5(_MainTex, i.uv, float2(_BlurIntensity, 0), _MainTex_TexelSize);
                oc += blur5(_MainTex, i.uv, float2(0, _BlurIntensity), _MainTex_TexelSize);
                oc.rgb *= 0.2f;
                oc.a = 1;
                return oc;
                */
                //return blur3(_MainTex, i.uv, _MainTex_TexelSize, _BlurIntensity);
                //return lerp(oc, blur2(_MainTex, i.uv, _MainTex_TexelSize), _BlurIntensity);
                float4 origCol = tex2D(_MainTex, i.uv);
                float4 col = blur(_MainTex, i.uv, float4(_BlurIntensity, 0, 0, 0), _MainTex_TexelSize);
                col += blur(_MainTex, i.uv, float4(0, _BlurIntensity, 0, 0), _MainTex_TexelSize);
                col += blur(_MainTex, i.uv, float4(_BlurIntensity, _BlurIntensity, 0, 0), _MainTex_TexelSize);
                col += blur(_MainTex, i.uv, float4(_BlurIntensity, -_BlurIntensity, 0, 0), _MainTex_TexelSize);
                col *= 0.25f;
                col.a = 1.0f;
                return col * _Color;
            }
            ENDHLSL
        }


            Pass
            {
                Name "DownSmp"
                HLSLPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                //#include "UnityCG.cginc"
                sampler2D _MainTex;
                half4 _MainTex_TexelSize;
                half4 _Color;

                half4 frag(v2f i) : SV_Target
                {
                    return DownSmp(_MainTex, i.uv, _MainTex_TexelSize) * _Color;
                }
                    ENDHLSL
            }


                Pass
                {
                    Name "Blur Vertical"
					HLSLPROGRAM
					#pragma vertex vert
					#pragma fragment frag

					//#include "UnityCG.cginc"
					sampler2D _MainTex;
					half4 _MainTex_TexelSize;
					half4 _Color;
                    uniform float _BlurIntensity;

					half4 frag(v2f i) : SV_Target
					{
						return blur5(_MainTex, i.uv, float2(0, _BlurIntensity), _MainTex_TexelSize);
					}
                        ENDHLSL
				}

                    Pass
                    {
                        Name "Blur Horizontal"
                        HLSLPROGRAM
                        #pragma vertex vert
                        #pragma fragment frag

                        //#include "UnityCG.cginc"
                        sampler2D _MainTex;
                        half4 _MainTex_TexelSize;
                        half4 _Color;
                        float _BlurIntensity;

                        half4 frag(v2f i) : SV_Target
                        {
                            return blur5(_MainTex, i.uv, float2(_BlurIntensity, 0), _MainTex_TexelSize);
                        }
                            ENDHLSL
                    }
        /*Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            sampler2D _MainTex;
            half4 _MainTex_TexelSize;
            float _BlurIntensity;

            sampler2D _BlurTex;

            half4 frag(v2f i) : SV_Target
            {
                float4 origCol = tex2D(_MainTex, i.uv);
                float4 blurCol = tex2D(_BlurTex, i.uv);

                return origCol + blurCol;
            }
            ENDHLSL
        }*/
    }
}
