// Copyright 2019 Tencent
// SPDX-License-Identifier: BSD-3-Clause

#version 450

layout (constant_id = 0) const int axis = 0;

#define shape_constant_id_offset 1
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
layout (constant_id = shape_constant_id_offset + 3) const int d = 0;
layout (constant_id = shape_constant_id_offset + 4) const int c = 0;
layout (constant_id = shape_constant_id_offset + 5) const int cstep = 0;

layout (constant_id = shape_constant_id_offset + 6) const int outdims = 0;
layout (constant_id = shape_constant_id_offset + 7) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 8) const int outh = 0;
layout (constant_id = shape_constant_id_offset + 9) const int outd = 0;
layout (constant_id = shape_constant_id_offset + 10) const int outc = 0;
layout (constant_id = shape_constant_id_offset + 11) const int outcstep = 0;

layout (binding = 0) buffer bottom_top_blob { sfpvec8 bottom_top_blob_data[]; };
layout (binding = 1) readonly buffer sum_workspace { sfpvec8 sum_workspace_data[]; };

layout (push_constant) uniform parameter
{
    int dims;
    int w;
    int h;
    int d;
    int c;
    int cstep;

    int outdims;
    int outw;
    int outh;
    int outd;
    int outc;
    int outcstep;
} p;

void main()
{
    int gx = int(gl_GlobalInvocationID.x);
    int gy = int(gl_GlobalInvocationID.y);
    int gz = int(gl_GlobalInvocationID.z);

    if (gx >= psc(w) || gy >= psc(h) * psc(d) || gz >= psc(c))
        return;

    int positive_axis = axis < 0 ? psc(dims) + axis : axis;

    int gi = gz * psc(cstep) + gy * psc(w) + gx;

    afpvec8 v = buffer_ld8(bottom_top_blob_data, gi);

    afpvec8 sum;

    if (psc(dims) == 1) // positive_axis == 0
    {
        sum = buffer_ld8(sum_workspace_data, 0);
    }
    else if (psc(dims) == 2 && positive_axis == 0)
    {
        sum = buffer_ld8(sum_workspace_data, gx);
    }
    else if (psc(dims) == 2 && positive_axis == 1)
    {
        sum = buffer_ld8(sum_workspace_data, gy);
    }
    else if (psc(dims) == 3 && positive_axis == 0)
    {
        sum = buffer_ld8(sum_workspace_data, gy * psc(w) + gx);
    }
    else if (psc(dims) == 3 && positive_axis == 1)
    {
        sum = buffer_ld8(sum_workspace_data, gz * psc(w) + gx);
    }
    else if (psc(dims) == 3 && positive_axis == 2)
    {
        sum = buffer_ld8(sum_workspace_data, gz * psc(h) + gy);
    }
    else // if (psc(dims) == 4)
    {
        int yd = gy / psc(h);
        int yh = gy % psc(h);

        gi = gz * psc(cstep) + yd * psc(h) * psc(w) + yh * psc(w) + gx;

        if (positive_axis == 0)
        {
            sum = buffer_ld8(sum_workspace_data, yd * psc(outcstep) + yh * psc(w) + gx);
        }
        if (positive_axis == 1)
        {
            sum = buffer_ld8(sum_workspace_data, gz * psc(outcstep) + yh * psc(w) + gx);
        }
        if (positive_axis == 2)
        {
            sum = buffer_ld8(sum_workspace_data, gz * psc(outcstep) + yd * psc(w) + gx);
        }
        if (positive_axis == 3)
        {
            sum = buffer_ld8(sum_workspace_data, gz * psc(outcstep) + yd * psc(h) + yh);
        }
    }

#if NCNN_fp16_packed || NCNN_fp16_storage
    // NOTE reduce max may produce (X, undef, X, undef) on nvidia fp16p/fp16s
    // TODO only enable this workaround for some nvidia driver
    if (positive_axis == 0)
    {
        sum = afpvec8(afpvec4(sum[0].r), afpvec4(sum[0].r));
    }
#endif

    v[0] /= sum[0];
    v[1] /= sum[1];

    buffer_st8(bottom_top_blob_data, gi, v);
}
