/*
 * Copyright (c) Contributors to the Open 3D Engine Project.
 * For complete copyright and license terms please see the LICENSE at the root of this distribution.
 *
 * SPDX-License-Identifier: Apache-2.0 OR MIT
 *
 */
#pragma once

#include <Atom/RPI.Public/Image/StreamingImage.h>

#include <PostProcessing/SMAABasePass.h>

namespace AZ
{
    namespace Render
    {
        static const char* const SMAABlendingWeightCalculationPassTemplateName = "SMAABlendingWeightCalculationTemplate";

        //! SMAA blending weight calculation pass implementation. The second pass of the SMAA uses the edge information generated by
        //! the first pass to determine the contribution of corresponding edge pixels to the edge geometry and thus to calculate
        //! their blending weight with neighbour pixels to generate a blending weight texture as output.
        class SMAABlendingWeightCalculationPass final
            : public SMAABasePass
        {
        public:
            AZ_RTTI(SMAABlendingWeightCalculationPass, "{989CD6FD-41E4-4F2A-99D0-40A4BA74FE95}", SMAABasePass);
            AZ_CLASS_ALLOCATOR(SMAABlendingWeightCalculationPass, SystemAllocator, 0);
            virtual ~SMAABlendingWeightCalculationPass() = default;

            //! Creates a SMAABlendingWeightCalculationPass
            static RPI::Ptr<SMAABlendingWeightCalculationPass> Create(const RPI::PassDescriptor& descriptor);

            void SetMaxSearchSteps(int steps);
            void SetMaxSearchStepsDiagonal(int steps);
            void SetCornerRounding(int cornerRounding);
            void SetDiagonalDetectionEnable(bool enable);
            void SetCornerDetectionEnable(bool enable);

        private:
            SMAABlendingWeightCalculationPass(const RPI::PassDescriptor& descriptor);

            // Pass behavior overrides...
            void InitializeInternal() override;

            // SMAABasePass functions...
            void UpdateSRG() override;
            void GetCurrentShaderOption(AZ::RPI::ShaderOptionGroup& shaderOption) const override;

            AZ::Data::Instance<AZ::RPI::StreamingImage> m_areaTexture;
            AZ::Data::Instance<AZ::RPI::StreamingImage> m_searchTexture;

            RHI::ShaderInputNameIndex m_areaTextureShaderInputIndex = "m_areaTexture";
            RHI::ShaderInputNameIndex m_searchTextureShaderInputIndex = "m_searchTexture";
            RHI::ShaderInputNameIndex m_renderTargetMetricsShaderInputIndex = "m_renderTargetMetrics";
            RHI::ShaderInputNameIndex m_maxSearchStepsShaderInputIndex = "m_maxSearchSteps";
            RHI::ShaderInputNameIndex m_maxSearchStepsDiagonalShaderInputIndex = "m_maxSearchStepsDiagonal";
            RHI::ShaderInputNameIndex m_cornerRoundingShaderInputIndex = "m_cornerRounding";

            const AZ::Name m_enableDiagonalDetectionFeatureOptionName;
            const AZ::Name m_enableCornerDetectionFeatureOptionName;

            // How many steps to be used in horizontal/vertical search process. For details please check comment related to SMAA_MAX_SEARCH_STEPS in SMAA.azsli.
            int m_maxSearchSteps = 32;
            // How many steps to be used in diagonal search process. For details please check comment related to SMAA_MAX_SEARCH_STEPS_DIAG in SMAA.azsli.
            int m_maxSearchStepsDiagonal = 16;
            // This is a tweak value for the sharp corner feature. For details please check comment related to SMAA_CORNER_ROUNDING in SMAA.azsli.
            int m_cornerRounding = 25;
            // Diagonal edge detection feature flag. For details please check comment related to SMAA_DISABLE_DIAG_DETECTION in SMAA.azsli.
            bool m_enableDiagonalDetection = true;
            // Corner detection feature flag. For details please check comment related to SMAA_DISABLE_CORNER_DETECTION in SMAA.azsli.
            bool m_enableCornerDetection = true;
        };
    }   // namespace Render
}   // namespace AZ
