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

#version 450

#define FLT_MAX 3.402823466e+38

layout (constant_id = 0) const int pooling_type = 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 c = 0;
layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;

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

layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };

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

    int outdims;
    int outw;
    int outh;
    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;

    afpvec8 res;

    // calculate adaptive kernel size
    const int sx = psc(w) * gx / psc(outw);
    const int ex = (psc(w) * (gx + 1) + psc(outw) - 1) / psc(outw);
    const int kernel_w = ex - sx;
    const int sy = psc(h) * gy / psc(outh);
    const int ey = (psc(h) * (gy + 1) + psc(outh) - 1) / psc(outh);
    const int kernel_h = ey - sy;

    if (pooling_type == 0)
    {
        res = afpvec8(afpvec4(-FLT_MAX), afpvec4(-FLT_MAX));

        int v_offset = gz * psc(cstep) + sy * psc(w) + sx;

        for (int y = 0; y < kernel_h; y++)
        {
            for (int x = 0; x < kernel_w; x++)
            {
                afpvec8 v = buffer_ld8(bottom_blob_data, v_offset + x);
                res[0] = max(res[0], v[0]);
                res[1] = max(res[1], v[1]);
            }

            v_offset += psc(w);
        }
    }
    else if (pooling_type == 1)
    {
        mat2x4 res_fp32 = mat2x4(vec4(0.f), vec4(0.f));  // force accumulation in fp32

        int v_offset = gz * psc(cstep) + sy * psc(w) + sx;

        for (int y = 0; y < kernel_h; y++)
        {
            for (int x = 0; x < kernel_w; x++)
            {
                afpvec8 v = buffer_ld8(bottom_blob_data, v_offset + x);
                res_fp32[0] += v[0];
                res_fp32[1] += v[1];
            }

            v_offset += psc(w);
        }

        int area = kernel_h * kernel_w;
        res_fp32[0] /= float(area);
        res_fp32[1] /= float(area);
        res = afpvec8(res_fp32);  // cast to fp16 if possible
    }

    const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;

    buffer_st8(top_blob_data, gi, res);
}
