// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

namespace Stride.Rendering.Images
{
    /// <summary>
	/// Screen Space Local Reflections shader for Blur Pass
    /// </summary>
    shader SSLRBlurPass : ImageEffectShader
    {			
		// Options:
		// 3 - 5-tap blur
		// 5 - 9-tap blur
		#define SSR_BLUR_STEPS 3
		
		override stage float4 Shading()
		{
		#if CONVOLVE_VERTICAL
			const float2 offsets[SSR_BLUR_STEPS] = {
			#if SSR_BLUR_STEPS == 3
                {0, 0},
                {1.3846153846, 0},
                {3.2307692308, 0}
			#elif SSR_BLUR_STEPS == 5
                {0, 0},
                {1, 0},
                {2, 0},
                {3, 0},
                {4, 0}
			#endif
			};
		#else
			const float2 offsets[SSR_BLUR_STEPS] = {
			#if SSR_BLUR_STEPS == 3
                {0, 0},
                {0, 1.3846153846},
                {0, 3.2307692308}
			#elif SSR_BLUR_STEPS == 5
                {0, 0},
                {0, 1},
                {0, 2},
                {0, 3},
                {0, 4}
			#endif
			};
		#endif
			const float weights[SSR_BLUR_STEPS] = {
			#if SSR_BLUR_STEPS == 3
                0.2270270270,
                0.3162162162,
                0.0702702703
			#elif SSR_BLUR_STEPS == 5
                0.2270270270,
                0.1945945946,
                0.1216216216,
                0.0540540541,
                0.0162162162
			#endif
			};
			
			float3 color = Texture0.Sample(LinearSampler, streams.TexCoord).rgb * weights[0];
			
			for (int i = 1; i < SSR_BLUR_STEPS; i++)
			{
				float2 texCoordOffset = offsets[i] * Texture0TexelSize;

				color += (Texture0.Sample(LinearSampler, streams.TexCoord + texCoordOffset).rgb
						+ Texture0.Sample(LinearSampler, streams.TexCoord - texCoordOffset).rgb)
						* weights[i];
			}
			
			return float4(color, 1.0f);
		}
    };

    effect SSLRBlurPassEffectH
    {
        mixin macro CONVOLVE_VERTICAL = 0;
        mixin SSLRBlurPass;
    };

    effect SSLRBlurPassEffectV
    {
        mixin macro CONVOLVE_VERTICAL = 1;
        mixin SSLRBlurPass;
    };
}
