// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.


#ifndef INCLUDED_OCIO_GRADINGBSPLINECURVE_H
#define INCLUDED_OCIO_GRADINGBSPLINECURVE_H

#include <array>
#include <vector>

#include <OpenColorIO/OpenColorIO.h>

namespace OCIO_NAMESPACE
{
class GpuShaderText;

class GradingBSplineCurveImpl : public GradingBSplineCurve
{
public:
    explicit GradingBSplineCurveImpl(size_t size);
    explicit GradingBSplineCurveImpl(size_t size, BSplineType splineType);
    GradingBSplineCurveImpl(const std::vector<GradingControlPoint> & controlPoints);
    GradingBSplineCurveImpl(const std::vector<GradingControlPoint> & controlPoints, BSplineType splineType);
    ~GradingBSplineCurveImpl() = default;

    GradingBSplineCurveRcPtr createEditableCopy() const override;
    BSplineType getSplineType() const override;
    void setSplineType(BSplineType splineType) override;
    size_t getNumControlPoints() const noexcept override;
    void setNumControlPoints(size_t size) override;
    const GradingControlPoint & getControlPoint(size_t index) const override;
    GradingControlPoint & getControlPoint(size_t index) override;
    float getSlope(size_t index) const override;
    void setSlope(size_t index, float slope) override;
    bool slopesAreDefault() const override;
    void validate() const override;

    bool isIdentity() const;

    // The KnotsCoefs struct is used when evaluating the curves.  Unlike the GradingBSplineCurve
    // class, which is for a single curve, the KnotsCoefs struct is something used by the
    // DynamicPropertyGradingRGBCurve to hold the parameters needed to evaluate _all_ the curves.
    //
    // For optimization and reusability purposes, the renderers will expect all of the curve data
    // to be packed in arrays. The curve data will be packed in the order of the curve Type enum.
    //
    // The RGBCurve will pack the curves in RGB_RED, RGB_GREEN, RGB_BLUE, RGB_MASTER order.
    //
    // note: DynamicProperties
    //
    // The curves use dynamic values for all of its data. Since the GPU code only finalizes the
    // GLSL shader once, we must associate each Uniform objects of the GpuShaderCreator used by
    // the renderer to an address in memory that will contain the dynamic value. This is because
    // when dynamic values change, no one is notified of the change (not the GPU renderer nor the
    // GpuShaderCreator). The Uniform object simply uses the function pointer it holds to update
    // the uniform in the shader everytime shader is used.
    //
    // The dynamic property keeps an instance of KnotsCoefs, and exposes its content with a set
    // of accessors. See DynamicPropertyGradingRGBCurveImpl.
    struct KnotsCoefs
    {
        KnotsCoefs() = delete;

        explicit KnotsCoefs(size_t numCurves);

        // Pre-processing scalar values.

        // Do not apply the op if all curves are identity.
        bool m_localBypass;

        // Pre-processing arrays of length NumCurves*2. Contains offest and count for each curve.
        // When curve is identity, offset is -1 and count is 0.
        std::vector<int> m_knotsOffsetsArray; // Contains offsets info for ALL curves.
        std::vector<int> m_coefsOffsetsArray; // Contains offsets info for ALL curves.

        // The max number of control points must be kept to a minimum, otherwise we may reach the
        // max number of allowed uniforms (1024), resulting in linking errors.
        //
        // On older hardware, even if it links, the responsiveness may become extremely slow if 
        // the number is too large.  It's not clear how to estimate what that limit is but
        // 200 knots & 600 coefs is too many.
        //
        // An alternative would be to use a dynamic size for the uniform arrays, but this would
        // require a finalization of the transform each time the size changes.
        //
        // There are three coefs needed for each polynomial segment, which is the number of knots
        // -1.  The number of knots is chosen dynamically based on what is needed to fit the
        // control points but the number of knots may be, at most, the number of control
        // points * 2 - 1.
        // 
        // There are four spline curves (R, G, B, M) in an RGBCurve and eight in a HueCurve.
        // We want to keep the total for two instances well below the 200 knot, 600 coef limit.
        //
        // A value of 60 knots would allow about 30 control points spread across the 4 or 6 curves.
        // Note that the default RGB curves use 3 control points each and the hue curves may use as
        // many as 6 even for the default.  However, there is an optimization below that does not
        // add knots for curves that are simply identity.
        //
        // UPDATE: Hardware has improved since this was introduced. For OCIO 2.5, double the
        // allowed knots/coefs from 60/180 to 120/360.
        //
        // Maximum size of the knots array (for ALL curves).
        static constexpr int MAX_NUM_KNOTS = 120;
        // Maximum size of the coefs array (for ALL curves).
        static constexpr int MAX_NUM_COEFS = 360;

        // Pre-processing arrays of length MAX_NUM_KNOTS and MAX_NUM_COEFS.
        std::vector<float> m_coefsArray;  // Contains packed coefs of ALL curves.
        std::vector<float> m_knotsArray;  // Contains packed knots of ALL curves.

        int m_numCoefs = 0;
        int m_numKnots = 0;

        // Forward evaluation of any spline type.
        float evalCurve(int curveIdx, float x, float identity_x) const;
        // Reverse evaluation of B_SPLINE or DIAGONAL_B_SPLINE.
        float evalCurveRev(int curveIdx, float y) const;
        // Reverse evaluation of HUE_HUE_B_SPLINE or HUE_FX curves using PERIODIC_0_B_SPLINE.
        float evalCurveRevHue(int c, float y) const;
    };

    // Compute knots and coefs for a curve and add result to knotsCoefs. It has to be called for
    // each curve using a given curve order.
    void computeKnotsAndCoefs(KnotsCoefs & knotsCoefs, int curveIdx, bool drawCurveOnly) const;

    // Forward evaluation of any spline type.
    static void AddShaderEvalFwd(GpuShaderText & st,
                                 const std::string & knotsOffsets, const std::string & coefsOffsets,
                                 const std::string & knots, const std::string & coefs);
    // Reverse evaluation of B_SPLINE or DIAGONAL_B_SPLINE.
    static void AddShaderEvalRev(GpuShaderText & st,
                                 const std::string & knotsOffsets, const std::string & coefsOffsets,
                                 const std::string & knots, const std::string & coefs);
    // Reverse evaluation of HUE_HUE_B_SPLINE or HUE_FX curves using PERIODIC_0_B_SPLINE.
    static void AddShaderEvalRevHue(GpuShaderText & st,
                                    const std::string & knotsOffsets, const std::string & coefsOffsets,
                                    const std::string & knots, const std::string & coefs);
private:
    void computeKnotsAndCoefsForRGBCurve(KnotsCoefs & knotsCoefs, int curveIdx) const;
    void computeKnotsAndCoefsForHueCurve(KnotsCoefs & knotsCoefs, int curveIdx, bool drawCurveOnly) const;

    void validateIndex(size_t index) const;

    std::vector<GradingControlPoint> m_controlPoints;
    std::vector<float> m_slopesArray;  // Optional slope values for the control points.

    BSplineType m_splineType = BSplineType::B_SPLINE;
};

bool IsGradingCurveIdentity(const ConstGradingBSplineCurveRcPtr & curve);

} // namespace OCIO_NAMESPACE

#endif // INCLUDED_OCIO_GRADINGBSPLINECURVE_H
