// 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) readonly buffer bottom_top_blob { sfp bottom_top_blob_data[]; };
layout (binding = 1) writeonly buffer max_workspace { sfp max_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(outw) || gy >= psc(outh) || gz >= psc(outc))
        return;

    int positive_axis = axis < 0 ? psc(dims) + axis : axis;
    afp max_value = afp(-99999999.f);

    if (psc(dims) == 1) // positive_axis == 0
    {
        for (int i = 0; i < psc(w); i++)
        {
            afp v = buffer_ld1(bottom_top_blob_data, i);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, 0, max_value);
        return;
    }

    if (psc(dims) == 2 && positive_axis == 0)
    {
        for (int i = 0; i < psc(h); i++)
        {
            int v_offset = i * psc(w) + gx;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gx, max_value);
        return;
    }

    if (psc(dims) == 2 && positive_axis == 1)
    {
        for (int i = 0; i < psc(w); i++)
        {
            int v_offset = gx * psc(w) + i;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gx, max_value);
        return;
    }

    if (psc(dims) == 3 && positive_axis == 0)
    {
        for (int i = 0; i < psc(c); i++)
        {
            int v_offset = i * psc(cstep) + gy * psc(w) + gx;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gy * psc(w) + gx, max_value);
        return;
    }

    if (psc(dims) == 3 && positive_axis == 1)
    {
        for (int i = 0; i < psc(h); i++)
        {
            int v_offset = gy * psc(cstep) + i * psc(w) + gx;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gy * psc(w) + gx, max_value);
        return;
    }

    if (psc(dims) == 3 && positive_axis == 2)
    {
        for (int i = 0; i < psc(w); i++)
        {
            int v_offset = gy * psc(cstep) + gx * psc(w) + i;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gy * psc(h) + gx, max_value);
        return;
    }

    if (psc(dims) == 4 && positive_axis == 0)
    {
        for (int i = 0; i < psc(c); i++)
        {
            int v_offset = i * psc(cstep) + gz * psc(h) * psc(w) + gy * psc(w) + gx;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gz * psc(outcstep) + gy * psc(w) + gx, max_value);
        return;
    }

    if (psc(dims) == 4 && positive_axis == 1)
    {
        for (int i = 0; i < psc(d); i++)
        {
            int v_offset = gz * psc(cstep) + i * psc(h) * psc(w) + gy * psc(w) + gx;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gz * psc(outcstep) + gy * psc(w) + gx, max_value);
        return;
    }

    if (psc(dims) == 4 && positive_axis == 2)
    {
        for (int i = 0; i < psc(h); i++)
        {
            int v_offset = gz * psc(cstep) + gy * psc(h) * psc(w) + i * psc(w) + gx;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gz * psc(outcstep) + gy * psc(w) + gx, max_value);
        return;
    }

    if (psc(dims) == 4 && positive_axis == 3)
    {
        for (int i = 0; i < psc(w); i++)
        {
            int v_offset = gz * psc(cstep) + gy * psc(h) * psc(w) + gx * psc(w) + i;
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
            max_value = max(max_value, v);
        }
        buffer_st1(max_workspace_data, gz * psc(outcstep) + gy * psc(h) + gx, max_value);
        return;
    }
}
